-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
218 lines (185 loc) · 7.67 KB
/
memory.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import pygame
from time import time
from math import prod
from random import shuffle
from numpy import array, append
import sys, os
def resource_path(relative_path):
""" Get the absolute path to the resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class Card():
"""Class for the card objects
"""
def __init__(self,pos,number,cards_size):
"""Initialize the card by giving its position inside the screen and the
number that appears when it is turned.
Args:
pos (list): x and y position of the top left of the card
number (int): number that is displayed when the card is turned
"""
self.pos = pos
self.cards_size = cards_size
self.color = (255,255,255)
self.border_color = (255,0,0)
self.border_width = 3
self.margin = 2
self.number = number
self.font = pygame.font.SysFont('calibri',70)
self.text = self.font.render(str(self.number), True, self.border_color)
self.text_center_x = int(self.pos[0]+cards_size[0]/2)
self.text_center_y = int(self.pos[1]+cards_size[1]/2)
self.text_rect = self.text.get_rect(center=(self.text_center_x,
self.text_center_y))
def draw_card(self,surface):
"""Draw the card by using pygame commands. The card is a white
rectangle with a red border.
Args:
surface (pygame.Surface): window where the cards are displayed.
"""
pygame.draw.rect(
surface, self.border_color,
(self.pos[0]+self.margin, self.pos[1]+self.margin, \
self.cards_size[0]-2*self.margin,\
self.cards_size[1]-2*self.margin)
)
pygame.draw.rect(
surface, self.color,
(self.pos[0]+self.margin+self.border_width, \
self.pos[1]+self.margin+self.border_width, \
self.cards_size[0]-2*self.margin-2*self.border_width, \
self.cards_size[1]-2*self.margin-2*self.border_width)
)
def turn(self,screen):
"""Function that displays the number of the card, simulating that the
card is turned around
Args:
screen (pygame.Surface): window where the cards are displayed.
"""
screen.blit(self.text, self.text_rect)
class MainScreen():
def __init__(self,screen_size):
self.screen = pygame.display.set_mode(screen_size,pygame.RESIZABLE,0,32)
self.bg_img = pygame.image.load(resource_path('figures/1_2_winningpicture.jpg'))
self.bg_img2 = pygame.image.load(resource_path('figures/1_2_winningpicture2.jpg'))
def initial_setup(self,card_dict):
pygame.display.update()
self.screen.blit(self.bg_img, (0,0))
for c in card_dict.values():
c[0].draw_card(self.screen)
def new_configuration(self):
pygame.display.set_mode([640,616],pygame.RESIZABLE,0,32)
self.text = ''
self.font = pygame.font.SysFont('calibri',48)
self.text_color = (0,0,0)
self.img_text = self.font.render(self.text, True, self.text_color)
self.rect = self.img_text.get_rect()
self.rect.topleft = (20, 550)
self.cursor = pygame.Rect(self.rect.topright, (3, self.rect.height))
def modify_text(self,event):
if event.key == pygame.K_BACKSPACE:
if len(self.text)>0:
self.text = self.text[:-1]
else:
self.text += event.unicode
self.img_text = self.font.render(self.text, True, self.text_color)
self.rect.size=self.img_text.get_size()
self.cursor.topleft = self.rect.topright
def new_setup(self):
self.screen.blit(self.bg_img2, (0,0))
self.screen.blit(self.img_text, self.rect)
if time() % 1 > 0.5:
pygame.draw.rect(self.screen, self.text_color, self.cursor)
pygame.display.update()
class Deck():
def __init__(self,screen_size):
self.cards_distribution = [6,3]
# self.cards_distribution = [2,1]
self.number_of_cards = prod(self.cards_distribution)
self.cards_size = [
int(x/y) for x, y in zip(screen_size,self.cards_distribution)
]
self.create_cards_dictionary()
self.last_card_id = 0
def create_cards_dictionary(self):
"""Function that creates a dictionary of the cards which are identified by
its position and the number that appears on each card is also displayed
Returns:
dictionary: already mentioned dictionary of cards
"""
self.card_dict = {}; i = 0
card_number = array(range(1,int(self.number_of_cards/2)+1))
card_number = append(card_number,card_number)
shuffle(card_number)
for x in range(self.cards_distribution[0]):
for y in range(self.cards_distribution[1]):
pipe_id = str(self.cards_size[0]*x)+'_'+ \
str(self.cards_size[1]*y)
self.card_dict[pipe_id] = [
Card([self.cards_size[0]*x,self.cards_size[1]*y], \
card_number[i],self.cards_size),
card_number[i]
]
i += 1
def identify_card(self,screen):
mx, my = pygame.mouse.get_pos()
x_card = int(mx/self.cards_size[0])*self.cards_size[0]
y_card = int(my/self.cards_size[1])*self.cards_size[1]
pipe_id = str(x_card)+'_'+str(y_card)
if pipe_id in self.card_dict.keys():
self.card_dict[pipe_id][0].turn(screen.screen)
if self.last_card_id != pipe_id and self.last_card_id != 0 and \
self.card_dict[pipe_id][1] == self.card_dict[self.last_card_id][1]:
self.card_dict.pop(pipe_id)
self.card_dict.pop(self.last_card_id)
self.last_card_id = 0
screen.initial_setup(self.card_dict)
else:
self.last_card_id = pipe_id
def main_memory():
"""Main function for the classic memory game, where a series of the cards
are laid face down on a surface and one card is flipped face up over each
turn. The object of the game is to turn over pairs of matching cards.
"""
screen_size = [640,426]
pygame.init()
pygame.display.set_caption('Remove the cards!')
deck_cards = Deck(screen_size)
screen = MainScreen(screen_size)
# while True:
# e = pygame.event.wait()
# if e.type == pygame.MOUSEBUTTONDOWN:
# break
# if e.type == pygame.QUIT:
# quit()
screen.initial_setup(deck_cards.card_dict)
first = True
game = True
while game:
if first:
screen.initial_setup(deck_cards.card_dict)
first = False
e = pygame.event.wait()
if e.type == pygame.QUIT:
quit()
if e.type == pygame.MOUSEBUTTONDOWN:
deck_cards.identify_card(screen)
screen.initial_setup(deck_cards.card_dict)
if len(deck_cards.card_dict) == 0:
game = False
screen.new_configuration()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
screen.modify_text(event)
screen.new_setup()
if screen.text.upper() == 'FERNAN':
running = False
pygame.display.quit()