# # CS42 HW9: Spampede! # # Name: # # import Tkinter from SpamMaze import * tk = Tkinter class Spampede(object): ''' A class that implements the game of Spampede using the tkinter library.''' # DEFINE CONSTANTS FOR YOUR PROGRAM HERE TO AVOID MAGIC VALUES! PADDING = 5 # The space between the edge of the window and the board WIDTH = 800 # The width of the gameboard HEIGHT = 600 # The height of the gameboard # Game colors BGCOLOR = "white" def __init__(self, master): ''' Initialize the Spampede application ''' frame = tk.Frame(master) # Create the main frame frame.pack() # Pack it into the master window buttonFrame = tk.Frame(frame) # Create a frame for the buttons buttonFrame.pack(side=tk.TOP) # Put that frame at the top of # the main frame # Create the start button. Bind its command to the self._go # function defined below. That is, Python will automatically # call self._go when the button is pressed. startb = tk.Button(buttonFrame, text="Start", command=self._go) startb.pack(side=tk.LEFT) # Create the other buttons and bind them to their own handlers pauseb = tk.Button(buttonFrame, text="Pause", command=self._pause) pauseb.pack(side=tk.LEFT) resetb = tk.Button(buttonFrame, text="New Game", command=self._reset) resetb.pack(side=tk.LEFT) # Create the canvas object and save it. This is where you will # draw all of the components of the gameboard. self.canvas = tk.Canvas(frame, width=self.WIDTH, height=self.HEIGHT, bg=self.BGCOLOR) self.canvas.pack(fill=tk.BOTH) # Create a message and a label to display the message self.message = "Welcome to Spampede v0.42!" self.messageLabel = tk.Label(frame, text=self.message) self.messageLabel.pack(side=tk.BOTTOM) # Capture all keystroke events. Handle them with the self._keyPressed # function, which you will need to add to. master.bind_all("", self._keyPressed) self.sleepTime = 50 # 50 milliseconds between updates self.cycleNum = 0 # number of update cycles so far self.theMaze = None # the model for our Spampede game self.dir = None # the direction we're moving now... self.currentColor = "blue" # The "current color", FWIW... # You can define other member variables here self.running = False # reset things to get ready to go... self._reset() # Each time you start a new game, you will want to reset the # internal representation of the game. Here's a good place to do it! # Remember, the application will be initialized just once, but you may # play the game many times within that run of the application! def _reset(self): ''' This is the function that will run when the user presses the New Game button. Resets the game.''' #set up the maze here self.theMaze = SpamMaze() #You may also want to initiatlize the centipede's direction here #and to other initiatizations that need to happen each time a new #game is started. self.currentColor = "green" self._drawEnvironment() self._displayMessage() # The _drawEnvironment function, which you'll need to add to. # Right now, the approach that drawEnvironment uses is to clear # all the objects and recreate them every time. This is not # the most efficient way of doing things, but it should work. # If you're getting flicker, alternative would be to maintain a # member variable that # keeps track of the rectangle graphics objects that are part of # the pede and the cans of spam, and only update the ones that # need to go away or appear. It's up to you which you want to # implement, but somehow drawEnvironment will need to accurately # reflect the state of the board, the pede and the spam. def _drawEnvironment(self): ''' Draw the state of the game on the board. Note that (0,0) is in the top left corner of the board and +y is down.''' self._clear() r3 = self.canvas.create_rectangle(40,50,100,100) self.canvas.itemconfig(r3, fill="red") r2 = self.canvas.create_rectangle(250, 100, 350, 200, fill=self.currentColor) if (self.cycleNum+1) % 42 == 0: self.currentColor = "magenta" # You might use this method to move the centipede one square # Also, this method can check if the centipede runs # into a can of spam, a wall, itself, etc. and act appropriately. def _updateCentipede(self): ''' Update the centipede. You don't have to do this every cycle if you want a slower game. ''' return # You might use this method to add/delete spam cans periodically def _updateSpam(self): ''' Possibly add or remove spam. You won't want to do this every cycle. ''' return def _displayMessage(self): ''' Display a text message to the user. ''' self.messageLabel.config(text=self.message) def _clear(self): ''' Clear the display completely (except for the message)''' self.canvas.delete(tk.ALL) self.canvas.config(bg=self.BGCOLOR) def _keyPressed(self, e): ''' Handle a keypress event. i, j, k and l control the direction of the pede. r reverses the pede. a sends the spam into auto-mode.''' if e.char in 'qrs': self.currentColor = 'blue' elif e.char == 'n': self.message = "you pressed " + 'm' else: self.message = "you pressed " + e.char self._displayMessage() def _go(self): ''' This is the function that will run when the user presses the Start button. (Re)Starts the game.''' if not self.running: self.running = True self._cycle() def _cycle(self): ''' This is the main "loop" that controls the gameplay. ''' if self.running: self._updateCentipede() # update the Spampede deque self._updateSpam() # update the Spam deque self._drawEnvironment() # draw things to buffer self._displayMessage() # display messages self.cycleNum += 1 # add a cycle # Wait a little while and then cycle again. self.canvas.after(self.sleepTime, self._cycle) def _pause(self): ''' This is the function that will run when the user presses the Pause button. Pauses the game.''' self.running = False def main(): ''' Start the game. ''' root = tk.Tk() app = Spampede(root) root.mainloop() if __name__ == "__main__" : main()