ALGORITHMS
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
update game objects
       # update Game objects to new position in next frame
        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
import time, pygame, random
from uagame import Window
from pygame.locals import *
# 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 __init__(self, window):
    # Initialize a Game.
    # - self is the Game to initialize
    # - window is the uagame window object
     # create all of the game attributes
     self.window = window
     self.pause_time = 0.04 # smaller is faster game
     self.close_clicked = False
     self.continue_game = True
     self.index = 0
     self.image_list = []
     self.import_image()
     self.grid = [ ]
     self.create_grid()
  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"))
     self.image_list = self.image_list + self.image_list
     random.shuffle(self.image_list)
  def create_grid(self):
    # creates our 4x4 grid of tiles by creating three
    # separate rows, each one containing 3 tiles
    # - self: the TTT game to create a grid for
     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 = [ ]
     # calculate the dimensions that tiles need to be
     tile_width = 400 / 4
     tile_height = 400 / 4
     # calculate the y-coordinates of tiles in this row.
     # To do so, we need to know what row number we are
     # creating tiles for. We can do that by taking the
     # current len of the grid, e.g., len 0 = first row
     row_index = len(self.grid)
     tile_y = tile_height * row_index
      # create each tile in the row. Tile x-coordinates are offset
      # by tile’s index (times tile width) in the row.
      for i in range(4):
         tile_x = tile_width * i
         index = 4*row_index + i
         one_row.append(Tile(100, 4, 'Black',self.window, tile_x, tile_y, tile_width,
tile_height,self.image_list[index]))
     # add the newly created row to our grid of tiles
     self.grid.append(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.
     while not self.close_clicked: # until player clicks close box
       # play frame
       self.handle_event()
       self.draw()
       if self.continue_game:
           self.update()
           self.decide_continue()
       time.sleep(self.pause_time) # set game velocity by pausing
  def handle_event(self):
    # Handle each user event by changing the game state
    # appropriately.
    # - self is the Game whose events will be handled.
     event = pygame.event.poll()
     # close the game if someone has clicked on the close button
     if event.type == QUIT:
         self.close_clicked = True
     # obtain position of mouse: event.pos
     # go over each tile and see if it overlaps with the position
     # if tile overlaps with event.pos, update tile content
     if event.type == MOUSEBUTTONUP and self.continue_game == True:
         for row in self.grid:
            for tile in row:
               tile.select(event.pos)
  def draw(self):
    # Draw all game objects.
    # - self is the Game to draw
    self.window.clear()
     # draw each of our tiles
     for row in self.grid:
        for tile in row:
           tile.draw()
     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
  def __init__(self, content_size, border_width, fg_color, window, x, y, width, height,
image):
     self.image = image
     # attributes related to drawing our rectangle
     self.rect = pygame.Rect(x, y, width, height)
     self.border_width = border_width
     # attributes related to drawing our tile content
     self.content_size = content_size
     self.content = ''
     # palette choices
     self.fg_color = fg_color
     # the window to draw our tile to
     self.window = window
  def draw(self):
    # draws our tile contents and borders to the screen
    # - self: the tile to draw
      # draw our rectangle with borders
      surface = self.window.get_surface()
      rect_color = pygame.Color(self.fg_color)
      surface.blit(self.image,(self.rect.x,self.rect.y))
      pygame.draw.rect(self.window.get_surface(), rect_color, self.rect,
self.border_width)
main()