abbarnes

Memory V2 with classes [codeskulptor]

Dec 12th, 2017
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. # Build clickable version of Memory using the Tile class
  2.  
  3. #################################################
  4. # Student adds code where appropriate    
  5. #http://www.codeskulptor.org/#user41_PVXLfjf7kOdDozJ.py
  6.  
  7. import simplegui
  8. import random
  9.  
  10. # define globals
  11. TILE_WIDTH = 50
  12. TILE_HEIGHT = 100
  13. DISTINCT_TILES = 8
  14.  
  15. # helper function to initialize globals
  16. def new_game():
  17.     global my_tiles, state, turns, selected_pair
  18.  
  19.     tile_numbers = range(DISTINCT_TILES) * 2
  20.     random.shuffle(tile_numbers)
  21.     my_tiles = [Tile(tile_numbers[i], False, [TILE_WIDTH * i, TILE_HEIGHT]) for i in range(2 * DISTINCT_TILES)]
  22.    
  23.     selected_pair = []
  24.     state = 0
  25.     turns = 0
  26.     label.set_text("Turns = "+str(turns))  
  27.  
  28. # definition of a Tile class
  29. class Tile:
  30.    
  31.     # definition of intializer
  32.     def __init__(self, num, exp, loc):
  33.         self.number = num
  34.         self.exposed = exp
  35.         self.location = loc
  36.        
  37.     # definition of getter for number
  38.     def get_number(self):
  39.         return self.number
  40.    
  41.     # check whether tile is exposed
  42.     def is_exposed(self):
  43.         return self.exposed
  44.    
  45.     # expose the tile
  46.     def expose_tile(self):
  47.         self.exposed = True
  48.        
  49.     # hide the tile      
  50.     def hide_tile(self):
  51.         self.exposed = False
  52.        
  53.     # string method for tiles    
  54.     def __str__(self):
  55.         return "Number is " + str(self.number) + ", exposed is " + str(self.exposed)    
  56.  
  57.     # draw method for tiles
  58.     def draw_tile(self, canvas):
  59.         loc = self.location
  60.         if self.exposed:
  61.             text_location = [loc[0] + 0.2 * TILE_WIDTH, loc[1] - 0.3 * TILE_HEIGHT]
  62.             canvas.draw_text(str(self.number), text_location, TILE_WIDTH, "White")
  63.         else:
  64.             tile_corners = (loc, [loc[0] + TILE_WIDTH, loc[1]], [loc[0] + TILE_WIDTH, loc[1] - TILE_HEIGHT], [loc[0], loc[1] - TILE_HEIGHT])
  65.             canvas.draw_polygon(tile_corners, 1, "Red", "Green")
  66.  
  67.     # selection method for tiles
  68.     def is_selected(self, pos):
  69.         inside_hor = self.location[0] <= pos[0] < self.location[0] + TILE_WIDTH
  70.         inside_vert = self.location[1] - TILE_HEIGHT <= pos[1] <= self.location[1]
  71.         return  inside_hor and inside_vert    
  72.        
  73.  
  74. # define event handlers
  75. def mouseclick(pos):
  76.     global state, selected_pair, turns
  77.    
  78.     for tile in my_tiles:
  79.         if tile.is_selected(pos) and not tile.is_exposed():
  80.             tile.expose_tile()
  81.            
  82.             if state < 2:
  83.                 selected_pair.append(tile)
  84.                 state += 1
  85.             else:
  86.                 if not selected_pair[0].get_number() == selected_pair[1].get_number():
  87.                     [card.hide_tile() for card in selected_pair]
  88.                 selected_pair = [tile]
  89.                 state = 1
  90.                 turns += 1
  91.    
  92.                        
  93.        
  94.  
  95.            
  96.        
  97.            
  98. # draw handler
  99. def draw(canvas):
  100.     for tile in my_tiles:
  101.         tile.draw_tile(canvas)
  102.    
  103. # create frame and add a button and labels
  104. frame = simplegui.create_frame("Memory", 2 * DISTINCT_TILES * TILE_WIDTH, TILE_HEIGHT)
  105. frame.add_button("Restart", new_game)
  106. label = frame.add_label("Turns = 0")
  107. frame.set_draw_handler(draw)
  108. frame.set_mouseclick_handler(mouseclick)
  109.  
  110.  
  111. # get things rolling
  112. new_game()
  113. frame.start()
  114.    
  115.    
  116. ###################################################
  117. # Clicking on tile should flip them once
Advertisement
Add Comment
Please, Sign In to add comment