LKD LF
LKD LF
Class Game
Instance Attributes
window # the window on which to draw
pause_time # pause time between drawing frames
close_clicked # indicates if close button was clicked
continue_game # indicates if game should continue
# add attributes as required
grid
Instance Methods/Blocks
initialize instance
initialize/create all instance attributes
create a 4x4 grid of tiles
Import image
Create a list to import images
Add the list twice
Randomize the list
play game
while not close_clicked
# ‘play’ a single frame
handle next event
draw the current frame
if continue_game:
update all game objects
decide if game should continue
pause before next iteration/frame
handle event
get next event from the event queue
If event type == QUIT
close_clicked = True
# check more events as required
If event type == MOUSEUP
Place the current player’s letter in the tile they clicked
draw frame
erase the window
# draw the Game objects
draw each tile in the grid at its location
update the window
decide if game should continue
# check if game should continue or not
Class Tile
Instance Attributes
rect # a rectangle that specifies the tile boundaries
surface # the game surface the tile is drawn to
content # the thing drawn inside of the tile
content_size # size (pixels) of tile content
fg_color # the color of things in the foreground of tile
bg_color # the color of things in the background of tile
border_width # how wide (pixels) the tile border is
Instance Methods/Blocks
initialize instance
set attributes based on arguments
create tile rectangle from x,y,width,height
draw
# stuff goes here
draw rectangle with appropriate borders
if tile contains contents, draw contents
Code:
# Memory1
#
# 4x4 grid of rectangles are displayed.
# All 8 pairs are exposed
# User-defined functions
def main():
# Creates the game window and game, and runs game
window = Window('Memory', 500, 400)
window.set_auto_update(False)
game = Game(window)
game.play()
window.close()
# User-defined classes
class Game:
# Defines a game of Tic Tac Toe
def import_image(self):
prefix = "image"
suffix = ".bmp"
for i in range(1,9):
self.image_list.append(pygame.image.load("image" + str(i) + ".bmp"))
for i in range(4):
self.create_row()
def create_row(self):
# creates a single row in our grid of tiles. Each
# row contains 3 tiles.
# - self: the TTT game to create a tile row for
one_row = [ ]
def play(self):
# Play the game until the player presses the close box.
# - self is the Game that should be continued or not.
event = pygame.event.poll()
# close the game if someone has clicked on the close button
if event.type == QUIT:
self.close_clicked = True
def draw(self):
# Draw all game objects.
# - self is the Game to draw
self.window.clear()
self.window.update()
def update(self):
# Update the game objects.
# - self is the Game to update
pass
def decide_continue(self):
# Check and remember if the game should continue
# - self is the Game to check
pass
class Tile:
# represents a single on a Tic Tac Toe board
self.image = image
# palette choices
self.fg_color = fg_color
def draw(self):
# draws our tile contents and borders to the screen
# - self: the tile to draw
main()