Commits

César Bustíos committed b031ad7

Compacted board. Half FPS. Customizable box images

  • Participants
  • Parent commits 4240112
  • Tags 0.1

Comments (0)

Files changed (19)

data/images/box1.png

Added
New image

data/images/box1_thumb.png

Added
New image

data/images/box2.png

Added
New image

data/images/box2_thumb.png

Added
New image

data/images/box3.png

Added
New image

data/images/box3_thumb.png

Added
New image

data/images/box4.png

Added
New image

data/images/box4_thumb.png

Added
New image

data/images/buttons/new_game.PNG

Removed
Old image

data/images/buttons/new_game.png

Added
New image

data/images/buttons/quit.PNG

Removed
Old image

data/images/buttons/quit.png

Added
New image

data/images/tux-20.png

Removed
Old image

data/images/tux-30.png

Removed
Old image

data/images/tux-40.png

Removed
Old image

data/images/unrevealed_logo1.png

Removed
Old image

data/images/unrevealed_logo2.png

Removed
Old image

data/images/unrevealed_logo3.png

Removed
Old image
 IMAGES_PATH = join(PROJECT_PATH, '../data/images')
 FONTS_PATH = join(PROJECT_PATH, '../data/fonts')
 SOUNDS_PATH = join(PROJECT_PATH, '../data/sounds')
-UNREVEALED_BOX_IMG_PATH = join(IMAGES_PATH, 'unrevealed_logo1.png')
 BACKGROUND_IMG_PATH = join(IMAGES_PATH, 'memory_background.jpg')
 BACKGROUND_MUSIC_PATH = join(SOUNDS_PATH, 'memory_music.mp3')
 
 WINDOW_HEIGHT = 480
 HALF_WIDTH = WINDOW_WIDTH / 2
 HALF_HEIGHT = WINDOW_HEIGHT / 2
-FPS = 30
+FPS = 15
 BOX_SIZE = 40
-BOX_GAP = 20
+BOX_GAP = 10
 BOARD_COLS = 9
 BOARD_ROWS = 6
 BOARD_WIDTH = (BOARD_COLS * BOX_SIZE) + ((BOARD_COLS - 1) * BOX_GAP)
 BOARD_HEIGHT = (BOARD_ROWS * BOX_SIZE) + ((BOARD_ROWS - 1) * BOX_GAP)
 IMAGES_NEEDED = (BOARD_COLS * BOARD_ROWS) / 2
 HIGHLIGHT_WIDTH = 3
-X_MARGIN = ((WINDOW_WIDTH - BOARD_WIDTH) / 3)
-Y_MARGIN = ((WINDOW_HEIGHT - BOARD_HEIGHT) / 3)
+X_MARGIN = 40
+Y_MARGIN = 40
 
 # Colors
 WHITE = (255, 255, 255)
 HIGHLIGHT_COLOR = BLUE
 
 # Images
-UNREVEALED_BOX_IMG = pygame.image.load(UNREVEALED_BOX_IMG_PATH)
+BOX_IMG = pygame.image.load(join(IMAGES_PATH, 'box1.png'))
 BACKGROUND_IMG = pygame.image.load(BACKGROUND_IMG_PATH)
 
 pygame.init()
 
     board = generate_board(False)
     new_game = False
+    box_img = BOX_IMG
 
     # Number of moves
     score = 0
         SCREEN.blit(BACKGROUND_IMG, (0, 0))
         draw_table(WHITE, BLACK)
 
-        draw_board(board)
+        draw_board(board, box_img)
         clicked = False
 
         # Events handling
                     x, y = event.pos
                     clicked = True
 
-        # New game, Quit, Sound, etc.
-        rect1, rect2 = display_game_options_and_score(str(score))
+        # Display options: New game, Quit, Sound, etc.
+        options = display_game_options_and_score(str(score))
+
         if clicked:
             # New game?
-            if rect1.collidepoint(x, y):
+            if options['new_game'].collidepoint(x, y):
                 new_game = True
                 score = 0
             # Quit!!!???
-            elif rect2.collidepoint(x, y):
+            elif options['quit'].collidepoint(x, y):
                 pygame.quit()
                 sys.exit()
+            elif options['blue_box'].collidepoint(x, y):
+                box_img = pygame.image.load(join(IMAGES_PATH, 'box1.png'))
+            elif options['green_box'].collidepoint(x, y):
+                box_img = pygame.image.load(join(IMAGES_PATH, 'box2.png'))
+            elif options['purple_box'].collidepoint(x, y):
+                box_img = pygame.image.load(join(IMAGES_PATH, 'box3.png'))
+            elif options['tux_box'].collidepoint(x, y):
+                box_img = pygame.image.load(join(IMAGES_PATH, 'box4.png'))
 
+        # Get box at pixel (x,y)
         row, col = get_box(board, x, y)
+
         if row != None and col != None:
             # Mouse is over a box
             if not board['revealed'][row][col]:
 
             # Box is clicked
             if not board['revealed'][row][col] and clicked:
-                reveal_box(board, row, col)
+                reveal_box(board, row, col, box_img)
                 if first_choice == None:
                     first_choice = (row, col)
                 else:
         CLOCK.tick(FPS)
 
 
-def animate_unrevealed(rect):
+def animate_unrevealed(rect, box_img):
     """
     Scales the unrevealed image when clicked
     """
-    img = UNREVEALED_BOX_IMG
-    height = img.get_height()
-    width = img.get_width()
+    height = box_img.get_height()
+    width = box_img.get_width()
     x = rect.x
     y = rect.y
 
         SCREEN.blit(tmp, (rect.x, rect.y))
 
         # Scale and blit the image
-        img = pygame.transform.scale(img, (width, height))
-        SCREEN.blit(img, (x, y))
+        box_img = pygame.transform.scale(box_img, (width, height))
+        SCREEN.blit(box_img, (x, y))
         pygame.time.delay(25)
         pygame.display.update()
         height -= 5
 
 def display_game_options_and_score(score):
     """
-    Display options and score. Returns options rects for colliding process
+    Display options and score. Returns rects for colliding process
     """
-
     # Display current score (moves)
     display_score(score)
 
-    x_margin = 75
-    y_margin = 30
+    x = SCREEN_RESOLUTION[0] - 150
+    y = 95
 
     # Buttons
-    new_game_b = pygame.image.load(join(IMAGES_PATH, 'buttons/new_game.png'))
-    quit_b = pygame.image.load(join(IMAGES_PATH, 'buttons/quit.png'))
+    new_game_surface = pygame.image.load(join(IMAGES_PATH, 'buttons/new_game.png'))
+    quit_surface = pygame.image.load(join(IMAGES_PATH, 'buttons/quit.png'))
 
     # Rects
-    new_game_r = new_game_b.get_rect()
-    quit_r = quit_b.get_rect()
-    new_game_r.center = (SCREEN_RESOLUTION[0] - 100, 95)
-    quit_r.center = (SCREEN_RESOLUTION[0] - 100, 155)
+    new_game_rect = new_game_surface.get_rect()
+    quit_rect = quit_surface.get_rect()
+    new_game_rect.center = (x, y)
+    quit_rect.center = (x, y + 60)
 
     # Show the options
-    SCREEN.blit(new_game_b, new_game_r)
-    SCREEN.blit(quit_b, quit_r)
+    SCREEN.blit(new_game_surface, new_game_rect)
+    SCREEN.blit(quit_surface, quit_rect)
 
-    return new_game_r, quit_r
+    option_rects = {
+        'new_game': new_game_rect,
+        'quit': quit_rect,
+    }
 
+    # Change the ugly box image? Tux maybe?
+    box_rects = display_box_options('pixel.ttf')
 
-def reveal_box(board, row, col):
+    option_rects.update(box_rects)
+
+    return option_rects
+
+
+def display_score(score):
+    """
+    Display the current score (moves)
+    """
+    score_font = pygame.font.Font(join(FONTS_PATH, 'pixel.ttf'), 20)
+    score_text = 'Moves: %s' % str(score.zfill(3))
+    score_surface = score_font.render(score_text, True, GOLD)
+
+    # Rect
+    score_rect = score_surface.get_rect()
+    score_rect.center = (SCREEN_RESOLUTION[0] - 150, 35)
+
+    # Show it
+    SCREEN.blit(score_surface, score_rect)
+
+
+def display_box_options(font_style):
+    """
+    For box image customization.
+    """
+    x = SCREEN_RESOLUTION[0] - 150
+    y = 280
+
+    # Text
+    option_font = pygame.font.Font(join(FONTS_PATH, font_style), 20)
+    option_surface = option_font.render('Box style', True, GOLD)
+    option_rect = option_surface.get_rect()
+    option_rect.center = (x, y)
+
+    SCREEN.blit(option_surface, option_rect)
+
+    # Surfaces
+    box1_surface = pygame.image.load(join(IMAGES_PATH, 'box1_thumb.png'))
+    box2_surface = pygame.image.load(join(IMAGES_PATH, 'box2_thumb.png'))
+    box3_surface = pygame.image.load(join(IMAGES_PATH, 'box3_thumb.png'))
+    box4_surface = pygame.image.load(join(IMAGES_PATH, 'box4_thumb.png'))
+
+    # Rects
+    box1_rect = box1_surface.get_rect()
+    box2_rect = box1_surface.get_rect()
+    box3_rect = box1_surface.get_rect()
+    box4_rect = box1_surface.get_rect()
+
+    # Positions
+    box1_rect.center = (x - 60, y + 40)
+    box2_rect.center = (x -20, y + 40)
+    box3_rect.center = (x + 20, y + 40)
+    box4_rect.center = (x + 60, y + 40)
+
+    # Show them
+    SCREEN.blit(box1_surface, box1_rect)
+    SCREEN.blit(box2_surface, box2_rect)
+    SCREEN.blit(box3_surface, box3_rect)
+    SCREEN.blit(box4_surface, box4_rect)
+
+    box_rects = {
+        'blue_box': box1_rect,
+        'green_box': box2_rect,
+        'purple_box': box3_rect,
+        'tux_box': box4_rect,
+    }
+
+    return box_rects
+
+
+def reveal_box(board, row, col, box_img):
     """
     Revealed a box when it is clicked
     """
     sound = pygame.mixer.Sound(sound_file)
     sound.play()
 
-    animate_unrevealed(rect)
+    animate_unrevealed(rect, box_img)
 
     # Mark box as revealed
     board['revealed'][row][col] = True
     return {'logos': logos, 'rects': rects, 'revealed': revealed}
 
 
-def draw_board(board):
+def draw_board(board, box_img):
     """
     Draw the board according to the current revealed state
     """
             x = rect.x
             y = rect.y
             if not board['revealed'][row][col]:
-                SCREEN.blit(UNREVEALED_BOX_IMG, (x, y))
+                SCREEN.blit(box_img, (x, y))
             else:
                 SCREEN.blit(board['logos'][row][col], (x, y))
 
     pygame.draw.polygon(SCREEN, BLACK, point_list, width)
 
 
-def display_score(score):
-    """
-    Display the current score (moves)
-    """
-    score_font = pygame.font.Font(join(FONTS_PATH, 'pixel.ttf'), 20)
-    score_text = 'Moves: %s' % str(score.zfill(3))
-    score_surface = score_font.render(score_text, True, GOLD)
-
-    # Rect
-    score_rect = score_surface.get_rect()
-    score_rect.center = (SCREEN_RESOLUTION[0] - 100, 35)
-
-    # Show it
-    SCREEN.blit(score_surface, score_rect)
-
-
 if __name__ == '__main__':
     main()