Skip to content

Commit c171d49

Browse files
committed
ADDED CODE FOR MEMORY_PUZZLE_GAME (ISSUE-231)
1 parent ec940f7 commit c171d49

18 files changed

+147
-0
lines changed

Diff for: PyGamesScripts/Memory Puzzle Game/Images/chicken.png

2.95 KB
Loading

Diff for: PyGamesScripts/Memory Puzzle Game/Images/cow.png

3.33 KB
Loading

Diff for: PyGamesScripts/Memory Puzzle Game/Images/dog.png

3.13 KB
Loading

Diff for: PyGamesScripts/Memory Puzzle Game/Images/duck.png

2.62 KB
Loading
4.41 KB
Loading

Diff for: PyGamesScripts/Memory Puzzle Game/Images/giraffe.png

3.91 KB
Loading

Diff for: PyGamesScripts/Memory Puzzle Game/Images/hippo.png

2.5 KB
Loading

Diff for: PyGamesScripts/Memory Puzzle Game/Images/matched.png

3.97 KB
Loading
17.7 KB
Loading
47.2 KB
Loading
62.1 KB
Loading
37.7 KB
Loading

Diff for: PyGamesScripts/Memory Puzzle Game/Images/panda.png

3.32 KB
Loading

Diff for: PyGamesScripts/Memory Puzzle Game/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Package/Script Name
2+
### **MEMORY_PUZZLE_GAME**
3+
## Short description of package/script
4+
5+
<p>The program runs a memory game that allows you to turn over cards by clicking the mouse.</p> </br>
6+
7+
# Setup
8+
Copy the project to your local enviroment and that's all this code can be run on any python IDE.
9+
10+
## Detailed explanation of script, if needed
11+
12+
#### _**OUR GAME RULES**_
13+
><ul>
14+
><li>The program runs a memory game that allows you to turn over cards by clicking the mouse. </li>
15+
><li> Each grey box represents the image of an animal hidden by default. </li>
16+
><li> If there is a match, the two cards will disappear, leaving a blank white space and giving a message "MATCHED".</li>
17+
><li> If not, they will flip back over.
18+
!</li>
19+
</ul>
20+
21+
22+
23+
## Output
24+
25+
![](https://github.com/HitainKakkar/Awesome_Python_Scripts/blob/main/PyGamesScripts/Memory%20Puzzle%20Game/Images/output_1.jpg)
26+
![](https://github.com/HitainKakkar/Awesome_Python_Scripts/blob/main/PyGamesScripts/Memory%20Puzzle%20Game/Images/output_2.jpg)
27+
![](https://github.com/HitainKakkar/Awesome_Python_Scripts/blob/main/PyGamesScripts/Memory%20Puzzle%20Game/Images/output_3.jpg)
28+
![](https://github.com/HitainKakkar/Awesome_Python_Scripts/blob/main/PyGamesScripts/Memory%20Puzzle%20Game/Images/output_4.jpg)
29+
30+
31+
## Author(s)
32+
##### HitainKakkar

Diff for: PyGamesScripts/Memory Puzzle Game/animal.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#importing required libraries
2+
import random
3+
import os
4+
import game_config as gc
5+
6+
from pygame import image, transform
7+
8+
animals_count = dict((a, 0) for a in gc.ASSET_FILES)
9+
10+
def available_animals(): #function to count number of animals
11+
return [animal for animal, count in animals_count.items() if count < 2]
12+
13+
class Animal: #animal class
14+
def __init__(self, index):
15+
self.index = index
16+
self.name = random.choice(available_animals())
17+
self.image_path = os.path.join(gc.ASSET_DIR, self.name)
18+
self.row = index // gc.NUM_TILES_SIDE
19+
self.col = index % gc.NUM_TILES_SIDE
20+
self.skip = False
21+
self.image = image.load(self.image_path)
22+
self.image = transform.scale(self.image, (gc.IMAGE_SIZE - 2 * gc.MARGIN, gc.IMAGE_SIZE - 2 * gc.MARGIN))
23+
self.box = self.image.copy()
24+
self.box.fill((200, 200, 200))
25+
26+
animals_count[self.name] += 1

Diff for: PyGamesScripts/Memory Puzzle Game/app.py

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

Diff for: PyGamesScripts/Memory Puzzle Game/game_config.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#importing required libraries
2+
import os
3+
4+
IMAGE_SIZE = 128 #size of image
5+
SCREEN_SIZE = 512 #size of screen
6+
NUM_TILES_SIDE = 4 #number of tiles sides
7+
NUM_TILES_TOTAL = 16 #number of tiles in toltal
8+
MARGIN = 8 #margin
9+
10+
ASSET_DIR = 'assets'
11+
ASSET_FILES = [x for x in os.listdir(ASSET_DIR) if x[-3:].lower() == 'png']
12+
assert len(ASSET_FILES) == 8

Diff for: PyGamesScripts/Memory Puzzle Game/requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
random
2+
os
3+
game_config //self defined
4+
pygame

0 commit comments

Comments
 (0)