-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·112 lines (86 loc) · 3.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pygame
import game_config as gc
from animal import Animal
from time import sleep
from gui import displayGui
from pygame import display, event, image
def find_index(x, y):
row = y // gc.IMAGE_SIZE
col = x // gc.IMAGE_SIZE
index = row * gc.NUM_TILES_SIDE + col
return index
pygame.init()
display.set_caption("Matchers")
name = displayGui()
cnt = 0
# Return a screen which is a surface object of the desired size
screen = display.set_mode((512, 512))
matched = image.load('other_assets/matched.png')
"""
# Blit method displays an image on another surface
screen.blit(matched, (0, 0))
# Displayes an updated screen after the match
display.flip()
"""
running = True
tiles = [Animal(i) for i in range(0, gc.NUM_TILES_TOTAL)]
current_images = []
"""
event module let us get a list of all the keyboard
and mouse events with the help of get function and all the events
fetched from the events queue from this function will be removed
from the queue and returned as a list
"""
while running:
current_events = event.get()
for e in current_events:
# When we close the window QUIT variable is generated
# which is defined in the pygame module
if e.type == pygame.QUIT:
running = False
# If the escape character is pressed then quit
# keydown = keyboard character
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE:
running = False
# handling mouse character events
if e.type == pygame.MOUSEBUTTONDOWN:
cnt += 1
# get the position of the mouse cursor
mouse_x, mouse_y = pygame.mouse.get_pos()
index = find_index(mouse_x, mouse_y)
if index not in current_images:
current_images.append(index)
if len(current_images) > 2:
current_images = current_images[1:]
screen.fill((255, 255, 255))
total_skipped = 0
for _, tile in enumerate(tiles):
image_i = tile.image if tile.index in current_images else tile.box
if not tile.skip:
screen.blit(image_i, (tile.col * gc.IMAGE_SIZE + gc.MARGIN, tile.row * gc.IMAGE_SIZE + gc.MARGIN))
else:
total_skipped += 1
display.flip()
if len(current_images) == 2:
index1, index2 = current_images
if tiles[index1].name == tiles[index2].name:
tiles[index2].skip = True
tiles[index1].skip = True
sleep(0.4)
screen.blit(matched, (0, 0))
display.flip()
sleep(0.4)
current_images = []
if total_skipped == len(tiles):
win_img = image.load('other_assets/win.jpg')
screen.blit(win_img, (0, 0))
display.flip()
sleep(0.4)
running = False
score_file = open('score_file.txt', 'a')
score_file.write("{}: {}".format(name, cnt))
score_file.write("\n")
score_file.close()
print("{} your total moves are {}".format(name, cnt))
print("Goodbye")