Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import pygame.mixer
- from itertools import product
- #copys all names in pygame.locals into your current namespace.
- from pygame.locals import *
- from pygame.color import Color
- ppp = pygame.mixer.Sound("ppp.wav")
- # main window
- FPS = 60 #control speed and game smoothness
- SCREEN_WIDTH = 800
- SCREEN_HEIGHT = 800
- CARD_SIZE = 80
- CARD_GAP = 10
- BOARD_WIDTH = 8
- BOARD_HEIGHT = 4
- X_MARGIN = (SCREEN_WIDTH - (BOARD_WIDTH * (CARD_SIZE + CARD_GAP))) // 2
- Y_MARGIN = (SCREEN_HEIGHT - (BOARD_HEIGHT * (CARD_SIZE + CARD_GAP))) // 2
- # the board size must be even (pirs)
- #expression which is supposed to be always true "assert"
- assert (BOARD_HEIGHT * BOARD_WIDTH) % 2 == 0, 'The board size must be even'
- # memo cards shapes
- DIAMOND = 'diamond'
- SQUARE = 'square'
- TRIANGLE = 'triangle'
- CIRCLE = 'circle'
- #background color
- BGCOLOR = Color('pink')
- def game_won(revealed):
- """ Returns all cards are found"""
- return all(all(x) for x in revealed)
- def game_won_animation(board, revealed):
- """ Change background colors the user won"""
- color1 = Color('cyan')
- color2 = BGCOLOR
- for i in range(10):
- color1, color2 = color2, color1
- screen.fill(color1)
- draw_board(board, revealed)
- text_name_end()
- pygame.display.update()
- pygame.time.wait(300)
- def text_name():
- """the welocme text diplaying on main streen"""
- myfont = pygame.font.Font('freesansbold.ttf', 50)
- msg = myfont.render(" MEMO GAME !!! ", True, Color('black'), (50, 50))
- screen.blit(msg, (30, 60))
- def text_name_end():
- """the end text diplaying on main streen"""
- myfont = pygame.font.Font('freesansbold.ttf', 50)
- msg = myfont.render(" CONGRATULATION !!! ", True, Color('black'), (50, 50))
- screen.blit(msg, (30, 60))
- def start_window_animation(board):
- """Game show 5 random squares"""
- coordinates = list(product(range(BOARD_HEIGHT), range(BOARD_WIDTH)))
- random.shuffle(coordinates)
- revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)]
- screen.fill(BGCOLOR)
- draw_board(board, revealed)
- text_name()
- pygame.display.update()
- pygame.time.wait(500)
- for sz in range(0, BOARD_HEIGHT * BOARD_WIDTH, 5):
- l = coordinates[sz: sz + 5]
- for x in l:
- revealed[x[0]][x[1]] = True
- draw_square(board, revealed, *x)
- pygame.time.wait(500)
- for x in l:
- revealed[x[0]][x[1]] = False
- draw_square(board, revealed, *x)
- def get_random_board(shape, colors):
- """ Generates the board by random shuffling"""
- icons = list(product(shape, colors))
- num_icons = BOARD_HEIGHT * BOARD_WIDTH // 2
- icons = icons[:num_icons] * 2
- random.shuffle(icons)
- board = [icons[i:i + BOARD_WIDTH]
- for i in range(0, BOARD_HEIGHT * BOARD_WIDTH, BOARD_WIDTH)]
- return board
- def get_coord(x, y):
- """ Gets the (x,y) card."""
- top = X_MARGIN + y * (CARD_SIZE + CARD_GAP)
- left = Y_MARGIN + x * (CARD_SIZE + CARD_GAP)
- return top, left
- def draw_icon(icon, x, y):
- """Draws the icon of (x, y) square"""
- px, py = get_coord(x, y)
- if icon[0] == DIAMOND:
- pygame.draw.polygon(screen, icon[1],
- ((px + CARD_SIZE // 2, py + 5),
- (px + CARD_SIZE - 5, py + CARD_SIZE // 2),
- (px + CARD_SIZE // 2, py + CARD_SIZE - 5),
- (px + 5, py + CARD_SIZE // 2)))
- elif icon[0] == SQUARE:
- pygame.draw.rect(screen, icon[1],
- (px + 5, py + 5, CARD_SIZE - 10, CARD_SIZE - 10))
- elif icon[0] == TRIANGLE:
- pygame.draw.polygon(screen, icon[1],
- ((px + CARD_SIZE // 2, py + 5), (px + 5, py + CARD_SIZE - 5),
- (px + CARD_SIZE - 5, py + CARD_SIZE - 5)))
- elif icon[0] == CIRCLE:
- pygame.draw.circle(screen, icon[1],
- (px + CARD_SIZE // 2, py + CARD_SIZE // 2), CARD_SIZE // 2 - 5)
- def get_pos(cx, cy):
- """(x,y) coordinates.The squares height and width """
- if cx < X_MARGIN or cy < Y_MARGIN:
- return None, None
- x = (cy - Y_MARGIN) // (CARD_SIZE + CARD_GAP)
- y = (cx - X_MARGIN) // (CARD_SIZE + CARD_GAP)
- 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:
- return None, None
- else:
- return x, y
- def draw_square(board, revealed, x, y):
- """Draw square card"""
- coords = get_coord(x, y)
- square_rect = (*coords, CARD_SIZE, CARD_SIZE)
- pygame.draw.rect(screen, BGCOLOR, square_rect)
- if revealed[x][y]:
- draw_icon(board[x][y], x, y)
- else:
- pygame.draw.rect(screen, Color('black'), square_rect)
- pygame.display.update(square_rect)
- def draw_board(board, revealed):
- """Draws the entire board"""
- for x in range(BOARD_HEIGHT):
- for y in range(BOARD_WIDTH):
- draw_square(board, revealed, x, y)
- def draw_select_box(x, y):
- """Draws the blue squere around the card """
- px, py = get_coord(x, y)
- pygame.draw.rect(screen, Color('white'), (px - 5, py - 5, CARD_SIZE + 10, CARD_SIZE + 10), 5)
- def main():
- '''main funtion'''
- ppp.play()
- global screen, clock
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_HEIGHT, SCREEN_WIDTH))
- clock = pygame.time.Clock()
- shape = (DIAMOND, SQUARE, TRIANGLE, CIRCLE)
- colors = (Color('red'), Color('green'), Color('yellow'), Color('white'))
- #Enough symbols
- assert len(shape) * len(colors) >= BOARD_HEIGHT * BOARD_WIDTH // 2, 'Not enought icons'
- board = get_random_board(shape, colors) # Generate in random shapes and color the pairs
- revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)] # visibility
- mouse_x = None
- mouse_y = None
- mouse_clicked = False
- first_selection = None
- running = True
- start_window_animation(board) # Welocme screen
- while running:
- screen.fill(BGCOLOR)
- draw_board(board, revealed)
- for event in pygame.event.get():
- if event.type == QUIT:
- running = False
- elif event.type == MOUSEMOTION:
- mouse_x, mouse_y = pygame.mouse.get_pos()
- elif event.type == MOUSEBUTTONDOWN:
- mouse_x, mouse_y = pygame.mouse.get_pos()
- mouse_clicked = True
- x, y = get_pos(mouse_x, mouse_y)
- if x is not None and y is not None:
- if not revealed[x][y]:
- if mouse_clicked:
- revealed[x][y] = True
- draw_square(board, revealed, x, y)
- if first_selection is None:
- first_selection = (x, y)
- else:
- pygame.time.wait(1000)
- if board[x][y] != board[first_selection[0]][first_selection[1]]:
- revealed[x][y] = False
- revealed[first_selection[0]][first_selection[1]] = False
- first_selection = None
- if game_won(revealed):
- game_won_animation(board, revealed)
- board = get_random_board(shape, colors)
- revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)]
- start_game_animation(board)
- else:
- draw_select_box(x, y)
- mouse_clicked = False
- pygame.display.update()
- #stores the module name
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement