|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +
|
| 4 | +
|
| 5 | +@author: |
| 6 | +""" |
| 7 | +import pygame |
| 8 | +import game_config as gc |
| 9 | + |
| 10 | +from pygame import display, event, image |
| 11 | +from time import sleep |
| 12 | +from animal import Animal |
| 13 | + |
| 14 | +def find_index_from_xy(x, y): |
| 15 | + row = y // gc.IMAGE_SIZE |
| 16 | + col = x // gc.IMAGE_SIZE |
| 17 | + index = row * gc.NUM_TILES_SIDE + col |
| 18 | + return row, col, index |
| 19 | + |
| 20 | +pygame.init() |
| 21 | +display.set_caption('My Game') |
| 22 | +screen = display.set_mode((gc.SCREEN_SIZE, gc.SCREEN_SIZE)) |
| 23 | +matched = image.load('other_assets/matched.png') |
| 24 | +running = True |
| 25 | +tiles = [Animal(i) for i in range(0, gc.NUM_TILES_TOTAL)] |
| 26 | +current_images_displayed = [] |
| 27 | + |
| 28 | +while running: |
| 29 | + current_events = event.get() |
| 30 | + |
| 31 | + for e in current_events: |
| 32 | + if e.type == pygame.QUIT: |
| 33 | + running = False |
| 34 | + |
| 35 | + if e.type == pygame.KEYDOWN: |
| 36 | + if e.key == pygame.K_ESCAPE: |
| 37 | + running = False |
| 38 | + |
| 39 | + if e.type == pygame.MOUSEBUTTONDOWN: |
| 40 | + mouse_x, mouse_y = pygame.mouse.get_pos() |
| 41 | + row, col, index = find_index_from_xy(mouse_x, mouse_y) |
| 42 | + if index not in current_images_displayed: |
| 43 | + if len(current_images_displayed) > 1: |
| 44 | + current_images_displayed = current_images_displayed[1:] + [index] |
| 45 | + else: |
| 46 | + current_images_displayed.append(index) |
| 47 | + |
| 48 | + # Display animals |
| 49 | + screen.fill((255, 255, 255)) |
| 50 | + |
| 51 | + total_skipped = 0 |
| 52 | + |
| 53 | + for i, tile in enumerate(tiles): |
| 54 | + current_image = tile.image if i in current_images_displayed else tile.box |
| 55 | + if not tile.skip: |
| 56 | + screen.blit(current_image, (tile.col * gc.IMAGE_SIZE + gc.MARGIN, tile.row * gc.IMAGE_SIZE + gc.MARGIN)) |
| 57 | + else: |
| 58 | + total_skipped += 1 |
| 59 | + |
| 60 | + display.flip() |
| 61 | + |
| 62 | + # Check for matches |
| 63 | + if len(current_images_displayed) == 2: |
| 64 | + idx1, idx2 = current_images_displayed |
| 65 | + if tiles[idx1].name == tiles[idx2].name: |
| 66 | + tiles[idx1].skip = True |
| 67 | + tiles[idx2].skip = True |
| 68 | + # display matched message |
| 69 | + sleep(0.2) |
| 70 | + screen.blit(matched, (0, 0)) |
| 71 | + display.flip() |
| 72 | + sleep(0.5) |
| 73 | + current_images_displayed = [] |
| 74 | + |
| 75 | + if total_skipped == len(tiles): |
| 76 | + running = False |
| 77 | + |
| 78 | +print('Goodbye!') |
0 commit comments