SHARE
TWEET

MEMO GAME WITH PYCODESTYLE

Mary_99 Feb 1st, 2019 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import random
  3. import pygame.mixer
  4. from itertools import product
  5. # copys all names in pygame.locals into your current namespace.
  6. from pygame.locals import *
  7. from pygame.color import Color
  8.  
  9.  
  10. # main window
  11. FPS = 60  # control speed and game smoothness
  12. SCREEN_WIDTH = 800
  13. SCREEN_HEIGHT = 800
  14. CARD_SIZE = 80
  15. CARD_GAP = 10
  16. BOARD_WIDTH = 8
  17. BOARD_HEIGHT = 4
  18. X_MARGIN = (SCREEN_WIDTH - (BOARD_WIDTH * (CARD_SIZE + CARD_GAP))) // 2
  19. Y_MARGIN = (SCREEN_HEIGHT - (BOARD_HEIGHT * (CARD_SIZE + CARD_GAP))) // 2
  20.  
  21.  
  22. # the board size must be even (pirs)
  23. # expression which is supposed to be always true "assert"
  24. assert (BOARD_HEIGHT * BOARD_WIDTH) % 2 == 0, 'The board size must be even'
  25.  
  26.  
  27. # memo cards shapes
  28. DIAMOND = 'diamond'
  29. SQUARE = 'square'
  30. TRIANGLE = 'triangle'
  31. CIRCLE = 'circle'
  32.  
  33. # background color
  34. BGCOLOR = Color('pink')
  35.  
  36.  
  37. def game_won(revealed):
  38.     """ Returns all cards are found"""
  39.  
  40.     return all(all(x) for x in revealed)
  41.  
  42.  
  43. def game_won_animation(board, revealed):
  44.     """ Change background colors the user won"""
  45.  
  46.     color1 = Color('cyan')
  47.     color2 = BGCOLOR
  48.     for i in range(10):
  49.         color1, color2 = color2, color1
  50.         screen.fill(color1)
  51.         draw_board(board, revealed)
  52.         text_name_end()
  53.         pygame.display.update()
  54.         pygame.time.wait(300)
  55.  
  56.  
  57. def text_name():
  58.     """the welocme text diplaying on main streen"""
  59.     myfont = pygame.font.Font('freesansbold.ttf', 50)
  60.     msg = myfont.render("    MEMO GAME !!   ", True, Color('black'), (50, 50))
  61.     screen.blit(msg, (30, 60))
  62.  
  63.  
  64. def text_name_end():
  65.     """the end text diplaying on main streen"""
  66.     myfont = pygame.font.Font('freesansbold.ttf', 50)
  67.     msg = myfont.render(" CONGRATULATION !! ", True, Color('black'), (50, 50))
  68.     screen.blit(msg, (30, 60))
  69.  
  70.  
  71. def start_window_animation(board):
  72.     """Game show 5 random squares"""
  73.  
  74.     coordinates = list(product(range(BOARD_HEIGHT), range(BOARD_WIDTH)))
  75.     random.shuffle(coordinates)
  76.  
  77.     revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)]
  78.  
  79.     screen.fill(BGCOLOR)
  80.     draw_board(board, revealed)
  81.     text_name()
  82.     pygame.display.update()
  83.     pygame.time.wait(500)
  84.  
  85.     for sz in range(0, BOARD_HEIGHT * BOARD_WIDTH, 5):
  86.         l = coordinates[sz: sz + 5]
  87.         for x in l:
  88.             revealed[x[0]][x[1]] = True
  89.             draw_square(board, revealed, *x)
  90.         pygame.time.wait(500)
  91.         for x in l:
  92.             revealed[x[0]][x[1]] = False
  93.             draw_square(board, revealed, *x)
  94.  
  95.  
  96. def get_random_board(shape, colors):
  97.     """ Generates the board by random shuffling"""
  98.  
  99.     icons = list(product(shape, colors))
  100.     num_icons = BOARD_HEIGHT * BOARD_WIDTH // 2
  101.     icons = icons[:num_icons] * 2
  102.  
  103.     random.shuffle(icons)
  104.     board = [icons[i:i + BOARD_WIDTH]
  105.              for i in range(0, BOARD_HEIGHT * BOARD_WIDTH, BOARD_WIDTH)]
  106.     return board
  107.  
  108.  
  109. def get_coord(x, y):
  110.     """ Gets the (x,y) card."""
  111.  
  112.     top = X_MARGIN + y * (CARD_SIZE + CARD_GAP)
  113.     left = Y_MARGIN + x * (CARD_SIZE + CARD_GAP)
  114.     return top, left
  115.  
  116.  
  117. def draw_icon(icon, x, y):
  118.     """Draws the icon of (x, y) square"""
  119.     px, py = get_coord(x, y)
  120.     if icon[0] == DIAMOND:
  121.         pygame.draw.polygon(screen, icon[1],
  122.                             ((px + CARD_SIZE // 2, py + 5),
  123.                             (px + CARD_SIZE - 5, py + CARD_SIZE // 2),
  124.                              (px + CARD_SIZE // 2, py + CARD_SIZE - 5),
  125.                              (px + 5, py + CARD_SIZE // 2)))
  126.     elif icon[0] == SQUARE:
  127.         pygame.draw.rect(screen, icon[1],
  128.                          (px + 5, py + 5, CARD_SIZE - 10, CARD_SIZE - 10))
  129.     elif icon[0] == TRIANGLE:
  130.         pygame.draw.polygon(screen, icon[1],
  131.                             ((px + CARD_SIZE // 2, py + 5),
  132.                             (px + 5, py + CARD_SIZE - 5),
  133.                             (px + CARD_SIZE - 5, py + CARD_SIZE - 5)))
  134.     elif icon[0] == CIRCLE:
  135.         pygame.draw.circle(screen, icon[1],
  136.                            (px + CARD_SIZE // 2, py + CARD_SIZE // 2),
  137.                            CARD_SIZE // 2 - 5)
  138.  
  139.  
  140. def get_pos(cx, cy):
  141.     """(x,y) coordinates.The squares height and width """
  142.  
  143.     if cx < X_MARGIN or cy < Y_MARGIN:
  144.         return None, None
  145.  
  146.     x = (cy - Y_MARGIN) // (CARD_SIZE + CARD_GAP)
  147.     y = (cx - X_MARGIN) // (CARD_SIZE + CARD_GAP)
  148.  
  149.     if x >= BOARD_HEIGHT or y >= BOARD_WIDTH or (cx - X_MARGIN) % (CARD_SIZE + CARD_GAP) > CARD_SIZE or (cy - Y_MARGIN) % (CARD_SIZE + CARD_GAP) > CARD_SIZE:
  150.         return None, None
  151.     else:
  152.         return x, y
  153.  
  154.  
  155. def draw_square(board, revealed, x, y):
  156.     """Draw square card"""
  157.  
  158.     coords = get_coord(x, y)
  159.     square_rect = (*coords, CARD_SIZE, CARD_SIZE)
  160.     pygame.draw.rect(screen, BGCOLOR, square_rect)
  161.     if revealed[x][y]:
  162.         draw_icon(board[x][y], x, y)
  163.     else:
  164.         pygame.draw.rect(screen, Color('black'), square_rect)
  165.     pygame.display.update(square_rect)
  166.  
  167.  
  168. def draw_board(board, revealed):
  169.     """Draws the entire board"""
  170.  
  171.     for x in range(BOARD_HEIGHT):
  172.         for y in range(BOARD_WIDTH):
  173.             draw_square(board, revealed, x, y)
  174.  
  175.  
  176. def draw_select_box(x, y):
  177.     """Draws the blue squere around the card """
  178.  
  179.     px, py = get_coord(x, y)
  180.     pygame.draw.rect(screen, Color('white'), (px - 5, py - 5,
  181.                                               CARD_SIZE + 10,
  182.                                               CARD_SIZE + 10), 5)
  183.  
  184.  
  185. def main():
  186.     '''main funtion'''
  187.     global screen, clock
  188.     pygame.init()
  189.     screen = pygame.display.set_mode((SCREEN_HEIGHT, SCREEN_WIDTH))
  190.     clock = pygame.time.Clock()
  191.     shape = (DIAMOND, SQUARE, TRIANGLE, CIRCLE)
  192.     colors = (Color('red'), Color('green'), Color('yellow'), Color('white'))
  193.     # Enough symbols
  194.     assert len(shape) * len(colors) >= BOARD_HEIGHT * BOARD_WIDTH // 2, 'Not enought icons'
  195.     board = get_random_board(shape, colors)
  196.     # Generate in random shapes and color the pairs
  197.     revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)]
  198.     # visibility
  199.  
  200.     mouse_x = None
  201.     mouse_y = None
  202.     mouse_clicked = False
  203.     first_selection = None
  204.  
  205.     running = True
  206.     start_window_animation(board)
  207.     # Welocme screen
  208.  
  209.     while running:
  210.         screen.fill(BGCOLOR)
  211.         draw_board(board, revealed)
  212.         for event in pygame.event.get():
  213.             if event.type == QUIT:
  214.                 running = False
  215.             elif event.type == MOUSEMOTION:
  216.                 mouse_x, mouse_y = pygame.mouse.get_pos()
  217.             elif event.type == MOUSEBUTTONDOWN:
  218.                 mouse_x, mouse_y = pygame.mouse.get_pos()
  219.                 mouse_clicked = True
  220.  
  221.         x, y = get_pos(mouse_x, mouse_y)
  222.  
  223.         if x is not None and y is not None:
  224.             if not revealed[x][y]:
  225.                 if mouse_clicked:
  226.                     revealed[x][y] = True
  227.                     draw_square(board, revealed, x, y)
  228.  
  229.                     if first_selection is None:
  230.                         first_selection = (x, y)
  231.                     else:
  232.                         pygame.time.wait(1000)
  233.                         if board[x][y] != board[first_selection[0]][first_selection[1]]:
  234.                             revealed[x][y] = False
  235.                             revealed[first_selection[0]][first_selection[1]] = False
  236.                         first_selection = None
  237.  
  238.                     if game_won(revealed):
  239.  
  240.                         game_won_animation(board, revealed)
  241.  
  242.                         board = get_random_board(shape, colors)
  243.                         revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)]
  244.                         start_game_animation(board)
  245.  
  246.                 else:
  247.                     draw_select_box(x, y)
  248.  
  249.         mouse_clicked = False
  250.         pygame.display.update()
  251.  
  252. # stores the module name
  253. if __name__ == '__main__':
  254.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top