Memory PDF

Title Memory
Course Introduction to the Foundations of Computation I
Institution University of Alberta
Pages 5
File Size 83.9 KB
File Type PDF
Total Downloads 31
Total Views 178

Summary

assignment...


Description

#it is a game in which we check we have to match our tiles in minimum time import pygame, random, time # User-defined functions def main(): # initialize all pygame modules (some need initialization) pygame.init() # create a pygame display window pygame.display.set_mode((500, 400)) # set the title of the display window pygame.display.set_caption('Memory game') # get the display surface w_surface = pygame.display.get_surface() # create a game object game = Game(w_surface) # start the main game loop by calling the play method on the game object game.play() # quit pygame and clean up the pygame window pygame.quit() # User-defined classes class Game: # An object in this class represents a complete game. def __init__(self, surface): # Initialize a Game. # - self is the Game to initialize # - surface is the display window surface object # === objects that are part of every game that we will discuss self.surface = surface self.bg_color = pygame.Color('black') self.FPS = 60 self.game_Clock = pygame.time.Clock() self.close_clicked = False self.continue_game = True # === game specific objects self.score = 0 self.board_size = 4 self.board = [] self.create_board() self.first_tile = None self.second_tile = None self.clicked = 0 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: # play frame self.handle_events() self.draw() if self.continue_game:

# until player clicks close box

self.update() self.decide_continue() self.game_Clock.tick(self.FPS)

# run at most with FPS Frames

Per Second def create_board(self): self.images = [] new_image = ['image1.bmp', 'image2.bmp', 'image3.bmp', 'image4.bmp', 'image5.bmp', 'image6.bmp', 'image7.bmp', 'image8.bmp'] new_image = new_image + new_image random.shuffle(new_image) for image in new_image: # image = pygame.image.load(image) self.images.append(image) cover = pygame.image.load('image0.bmp') width = self.surface.get_width() // 5 height = self.surface.get_height() // 4 # for each row index for row_index in range(0, self.board_size): # create row as an empty list row = [] # for each column index for col_index in range(0, self.board_size): # create tile using row index and column index x = col_index * width y = row_index * height imageindex = row_index * self.board_size + col_index self.image = self.images[imageindex] # width = self.image.get_width() # height = self.image.get_height() self.tile = Tile(x, y, self.image, cover, self.surface) # append tile to row row.append(self.tile) # append row to board self.board.append(row) def handle_events(self): # Handle each user event by changing the game state appropriately. # - self is the Game whose events will be handled events = pygame.event.get() for event in events: if event.type == pygame.QUIT: self.close_clicked = True if event.type == pygame.MOUSEBUTTONUP: self.exposing_logic(event.pos) def exposing_logic(self, position): #gets the indices oftiles which we want to compare for row in self.board: for tile in row: if tile.exposing_tile(position): if self.first_tile == None: tile.set_is_exposed(True) #makes a list which contains both row and column index of a tile1

self.first_tile = [self.board.index(row), row.index(tile)] else: tile.set_is_exposed(True) # makes a list which contains both row and column index of a tile2 self.second_tile = [self.board.index(row), row.index(tile)] def hide_if_not_match(self): #if tiles do not match it hides them after 1 sec of pause if self.first_tile != None and self.second_tile != None: if not self.board[self.first_tile[0]] [self.first_tile[1]].check_matching_tiles( self.board[self.second_tile[0]][self.second_tile[1]]): time.sleep(1) self.board[self.first_tile[0]] [self.first_tile[1]].set_is_exposed(False) self.board[self.second_tile[0]] [self.second_tile[1]].set_is_exposed(False) self.first_tile = None self.second_tile = None def draw(self): # Draw all game objects. # - self is the Game to draw self.surface.fill(self.bg_color) # clear the display surface first for row in self.board: for tile in row: tile.draw() self.draw_score() pygame.display.update() # make the updated surface appear on the display def draw_score(self): # 1. Set the color size = self.surface.get_width() fg_color = pygame.Color('white') # 2.create the font object font = pygame.font.SysFont('', 70) # 3 Create a text box by rendering the font text_string = '' + str(self.score) text_box = font.render(text_string, True, fg_color, self.bg_color) surface_height = self.surface.get_width() text_box_height = text_box.get_width() location = (surface_height - text_box_height, 0) # 4 Compute the location of the text box # location = (430, 0) # 5 Blit or pin the text box on the surface self.surface.blit(text_box, location) def update(self): # Update the game objects for the next frame. # - self is the Game to update self.score = pygame.time.get_ticks() // 1000 self.hide_if_not_match()

def decide_continue(self): # Check and remember if the game should continue # - self is the Game to check tiles_exposed = 0 for row in self.board: for self.tile in row: if self.tile.tile_exposed == True: tiles_exposed += 1 if tiles_exposed == 16: self.continue_game = False class Tile: # an object of this class represents a Rectangular Tile def __init__(self, x, y, image, cover, surface): # Initialize a tile to contain a ' ' # - x is the int x coord of the upper left corner # - y is the int y coord of the upper left corner # - width is the int width of the tile # - height is the int height of the tile # - surface is pygame.Surface object on which a Tile object is drawn on self.left = x self.top = y self.surface = surface self.image = image self.cover = cover self.tile_exposed = False #initial tile state

def draw(self): # draws a Tile object # -self is the Tile object to draw color = pygame.Color('black') border_width = 5 image = pygame.image.load(self.image) self.rect = pygame.Rect(self.left, self.top, image.get_width(), image.get_height()) pygame.draw.rect(self.surface, color, self.rect, border_width,) self.surface.blit(self.cover, self.rect) if self.tile_exposed == True: self.surface.blit(image, self.rect) else: self.surface.blit(self.cover, self.rect) def exposing_tile(self, position): #checks if tile is to be exposed or not if self.rect.collidepoint(position): if not self.tile_exposed: return True else: return False def check_matching_tiles(self, other_tile): #checks if tiles match if self.image == other_tile.get_image():

return True def set_is_exposed(self, is_exposed): #determines if tile is exposed or not based on parameter input self.tile_exposed = is_exposed def get_image(self): #returns the image of tile return self.image main()...


Similar Free PDFs