Intro. to Programming : Fall 2017 -Online

# Csci 1020 September 24-October 1, 2017 Program Assignment 6

Write a GUI which demonstrates some sort of game or situation.

You should first get the sample p6.py program working, and study the code. You must import the correct sound library and the correct code for your machine: mac, windows, or linux. Make sure that you copy the 3 cat files and the boing sound .wav file, and put these with p6.py sample code in a directory.

Three cats are moved around the canvas using a random number generator. When any two images get too close (within 50 pixel units), the sound plays, and the cats go back to their corners and restart.

Here are some things you could do.

1. Show a ball moving around a hole, a goal, a catcher's mitt, whatever. Play the sound then reset when the ball gets close enough to count as a score.
2. Show people walking around, and they say "pardon me" when they get too close, then they jump to a position a proper distance apart.
3. Play freeze tag, or some modification of freeze tag..

That list above is not a limitation, just some thoughts. Your program must do this:

• Your GUI should have at least 3 buttons including the Quit button.
• Your GUI should have at least 1 sound. Keep sounds brief!
• Your gui should make at least 1 image move.
• You gui should have at least 2 images.
• As usual, you may enhance the GUI as you wish. Some padding around widgets (buttons, labels, i.e. everything placed on the window W using the .grid placement method) might make things look better. An image somewhere could be attractive. You might play some uplifting tune right after the GUI is opened. Adding directions somewhere above or below the buttons and other items is a nice touch.
• Email your program with all necessary files attached ( the .py program file, images, whatever) to me: ted@fdltcc.edu

I can't check it out without these files. Snapshot images of the code and running program are a nice extra, and thanks, but I MUST have the source code and necessary files in order to run the program.
• Enter a remark in the D2L discussion under p6.

Here are the links to the sample code and needed files:

Someone noted that the .gif files above didn't work properly in their Macs, and this is puzzling because they worked in Mac, Windows, and Linux python 2.7 tkinter on campus machines. They also displayed properly otherwise, just improper for tkinter. For that case, try using these .ppm files as an alternative.

To use these, you need to rename the files accordingly with .ppm extensions. However, this should not stop you from using .gif images because they all should work. I probably did something strange to make those .gif cat files above, and I've not yet figured out what that is.

The sample p6 program introduces several new things. One is a class. We will study classes separately and later, but it is something to talk about now with canvas because you have been using them already.

class HeadClass:
pass


This above creates a new class HeadClass, and it will hold all the information about each cat object: x, y, dx, dy, and image.

head = []
ncats = 3


This above creates an empty list head which will hold cat objects, and there will be ncats of them in the list (3).

x = [30,Cwidth-30, 30]
y = [30,Cheight-30, Cheight-30]
dx = [1,-1, 1]
dy = [1,-1,-1]

cat1 = PhotoImage(file='cat1.gif')
cat2 = PhotoImage(file='cat2.gif')
cat3 = PhotoImage(file='cat3.gif')

cats = [cat1,cat2,cat3]


Above the information for each cat is put in a list, 3 values for x, y, dx, dy, and cats.

for i in range(ncats):
h = HeadClass()   # create an object
h.x = x[i]        # x position
h.y = y[i]        # y position
h.dx = dx[i]      # x motion
h.dy = dy[i]      # y motion
h.cim = C.create_image(x[i],y[i],image=cats[i]) # place the head on Canvas


Above the lists are used to initialize h which is type HeadClass, and after each h is initialized, it is loaded into the list head. After this loop runs, head contains the information about each cat: head[0], head[1], and head[2]. Note that the indices start at 0 and go up to 2 for ncats=3.

The for loop loops 3 times. range(ncats) creates a list which is [0,1,2] in this case because ncats=3, then for each element of the list, i is set to that value and the instructions in the for loop are executed. Thus, this section loops three times with i=0, i=1, and i=2, and i corresponds to the position in x, y, dx, dy, and cats lists.

def runit():
global running,x,y,dx,dy
running = True # set True on entry
while running==True:
for k in range(ncats):
for j in range(ncats):
if(j==k): continue # skip
play_sound("boing_spring.wav")
for i in range(ncats):
W.update()
continue
W.update()
sleep(.005)


Above is the loop which runs when the Run button is pressed. The variable "running" is set to be true, and while running==True, the while loop keeps running, over and over endlessly.

There is another for loop whithin the while loop, for k in range(ncats). This checks each cat object. The for j in range(ncats) loop, also checks each other cat, and this is examined below.

         for j in range(ncats):
if(j==k): continue # skip
play_sound("boing_spring.wav")
for i in range(ncats):
W.update()
continue


Above, the distance between cats is calculated using the Pythagorean theorem, and if the distance is less than or equal to 50, a sound is played, then each cat position is reset to its original x, y position, then each cat image is moved to its original x, y position. The W.update() comand redraws the Canvas, and this should be done after a move or an object is placed on the Canvas so that it shows immediately. The continue command exits the for loop.

         head[k].dx += random.choice([-1,1])