Skip to content

Commit 2107fd3

Browse files
Merge pull request #1983 from Vyshnavi21504/Vyshnavi
commit msg #1955
2 parents 6da65d2 + a3df9c3 commit 2107fd3

File tree

12 files changed

+126
-0
lines changed

12 files changed

+126
-0
lines changed

Memory-game/animal.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Sep 22 11:40:09 2020
4+
5+
@author: Mansi
6+
"""
7+
import random
8+
import os
9+
import game_config as gc
10+
11+
from pygame import image, transform
12+
13+
animals_count = dict((a, 0) for a in gc.ASSET_FILES)
14+
15+
def available_animals():
16+
return [animal for animal, count in animals_count.items() if count < 2]
17+
18+
class Animal:
19+
def __init__(self, index):
20+
self.index = index
21+
self.name = random.choice(available_animals())
22+
self.image_path = os.path.join(gc.ASSET_DIR, self.name)
23+
self.row = index // gc.NUM_TILES_SIDE
24+
self.col = index % gc.NUM_TILES_SIDE
25+
self.skip = False
26+
self.image = image.load(self.image_path)
27+
self.image = transform.scale(self.image, (gc.IMAGE_SIZE - 2 * gc.MARGIN, gc.IMAGE_SIZE - 2 * gc.MARGIN))
28+
self.box = self.image.copy()
29+
self.box.fill((200, 200, 200))
30+
31+
animals_count[self.name] += 1

Memory-game/app.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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!')

Memory-game/assets/chicken.png

2.95 KB
Loading

Memory-game/assets/cow.png

3.33 KB
Loading

Memory-game/assets/dog.png

3.13 KB
Loading

Memory-game/assets/duck.png

2.62 KB
Loading

Memory-game/assets/elephant.png

4.41 KB
Loading

Memory-game/assets/giraffe.png

3.91 KB
Loading

Memory-game/assets/hippo.png

2.5 KB
Loading

Memory-game/assets/panda.png

3.32 KB
Loading

0 commit comments

Comments
 (0)