Advertisement
Mary_99

memo

Jan 28th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.73 KB | None | 0 0
  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. ppp = pygame.mixer.Sound("ppp.wav")
  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), (px + 5, py + CARD_SIZE - 5),
  132.                              (px + CARD_SIZE - 5, py + CARD_SIZE - 5)))
  133.     elif icon[0] == CIRCLE:
  134.         pygame.draw.circle(screen, icon[1],
  135.                            (px + CARD_SIZE // 2, py + CARD_SIZE // 2), CARD_SIZE // 2 - 5)
  136.  
  137.  
  138. def get_pos(cx, cy):
  139.     """(x,y) coordinates.The squares height and width """
  140.  
  141.     if cx < X_MARGIN or cy < Y_MARGIN:
  142.         return None, None
  143.  
  144.     x = (cy - Y_MARGIN) // (CARD_SIZE + CARD_GAP)
  145.     y = (cx - X_MARGIN) // (CARD_SIZE + CARD_GAP)
  146.  
  147.     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:
  148.         return None, None
  149.     else:
  150.         return x, y
  151.  
  152.  
  153. def draw_square(board, revealed, x, y):
  154.     """Draw square card"""
  155.  
  156.     coords = get_coord(x, y)
  157.     square_rect = (*coords, CARD_SIZE, CARD_SIZE)
  158.     pygame.draw.rect(screen, BGCOLOR, square_rect)
  159.     if revealed[x][y]:
  160.         draw_icon(board[x][y], x, y)
  161.     else:
  162.         pygame.draw.rect(screen, Color('black'), square_rect)
  163.     pygame.display.update(square_rect)
  164.  
  165.  
  166. def draw_board(board, revealed):
  167.     """Draws the entire board"""
  168.  
  169.     for x in range(BOARD_HEIGHT):
  170.         for y in range(BOARD_WIDTH):
  171.             draw_square(board, revealed, x, y)
  172.  
  173.  
  174. def draw_select_box(x, y):
  175.     """Draws the blue squere around the card """
  176.  
  177.     px, py = get_coord(x, y)
  178.     pygame.draw.rect(screen, Color('white'), (px - 5, py - 5, CARD_SIZE + 10, CARD_SIZE + 10), 5)
  179.  
  180.  
  181. def main():
  182.     '''main funtion'''
  183.     ppp.play()
  184.     global screen, clock
  185.     pygame.init()
  186.     screen = pygame.display.set_mode((SCREEN_HEIGHT, SCREEN_WIDTH))
  187.     clock = pygame.time.Clock()
  188.     shape = (DIAMOND, SQUARE, TRIANGLE, CIRCLE)
  189.     colors = (Color('red'), Color('green'), Color('yellow'), Color('white'))
  190.     #Enough symbols
  191.     assert len(shape) * len(colors) >= BOARD_HEIGHT * BOARD_WIDTH // 2, 'Not enought icons'
  192.     board = get_random_board(shape, colors) # Generate in random shapes and color the pairs
  193.     revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)]  # visibility
  194.    
  195.     mouse_x = None
  196.     mouse_y = None
  197.     mouse_clicked = False
  198.     first_selection = None
  199.  
  200.     running = True
  201.     start_window_animation(board) # Welocme screen
  202.  
  203.     while running:
  204.         screen.fill(BGCOLOR)
  205.         draw_board(board, revealed)
  206.         for event in pygame.event.get():
  207.             if event.type == QUIT:
  208.                 running = False
  209.             elif event.type == MOUSEMOTION:
  210.                 mouse_x, mouse_y = pygame.mouse.get_pos()
  211.             elif event.type == MOUSEBUTTONDOWN:
  212.                 mouse_x, mouse_y = pygame.mouse.get_pos()
  213.                 mouse_clicked = True
  214.  
  215.         x, y = get_pos(mouse_x, mouse_y)
  216.  
  217.         if x is not None and y is not None:
  218.             if not revealed[x][y]:
  219.                 if mouse_clicked:
  220.                     revealed[x][y] = True
  221.                     draw_square(board, revealed, x, y)
  222.  
  223.                     if first_selection is None:
  224.                         first_selection = (x, y)
  225.                     else:
  226.                         pygame.time.wait(1000)
  227.                         if board[x][y] != board[first_selection[0]][first_selection[1]]:
  228.                             revealed[x][y] = False
  229.                             revealed[first_selection[0]][first_selection[1]] = False
  230.                         first_selection = None
  231.  
  232.                     if game_won(revealed):
  233.  
  234.                         game_won_animation(board, revealed)
  235.  
  236.                         board = get_random_board(shape, colors)
  237.                         revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)]
  238.                         start_game_animation(board)
  239.  
  240.                 else:
  241.                     draw_select_box(x, y)
  242.  
  243.         mouse_clicked = False
  244.         pygame.display.update()
  245.  
  246. #stores the module name
  247. if __name__ == '__main__':
  248.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement