-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
322 lines (263 loc) · 9.75 KB
/
Copy pathgame.py
File metadata and controls
322 lines (263 loc) · 9.75 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import pygame
import pygame.draw
from math import floor, modf, pi
from random import randint
import time
f = open("scoreboard.txt", "a+")
f.seek(0)
scores = []
for r in f.readlines():
nm, tm, ers, tot = r.split()
scores.append({
"name": nm,
"time": int(tm),
"errors": int(ers),
"total": int(tot)
})
scores.sort(key=lambda x: x["total"], reverse=True)
pygame.init()
colors = [(0, 0, 255),
(255, 0, 0),
(0, 255, 0),
(100, 160, 40),
(160, 70, 20),
(160, 160, 40),
(50, 100, 50),
(60, 80, 40),
(80, 100, 150),
(70, 250, 220),
(50, 100, 95),
(30, 120, 200),
(220, 150, 25),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
(randint(0,255),randint(0,255),randint(0,255)),
]
CARD_WIDTH = 50
CARD_HEIGHT = 80
PADDING_TOP = 50
PADDING_LEFT = 50
class Card:
x = 0
y = 0
alpha = 255
color = 0
faceUp = True
done = False
def __init__(self, x, y, color):
self.x = x
self.y = y
self.alpha = 255
self.color = color
def Draw(self):
r, g, b = colors[self.color]
if not self.done:
pygame.draw.rect(screen, (r, g, b, self.alpha) if self.faceUp else (
42, 42, 42, self.alpha), [self.x, self.y, CARD_WIDTH, CARD_HEIGHT], 0)
def TestCollisionSelfPoint(self, x, y):
return x > self.x and y > self.y and x < self.x + CARD_WIDTH and y < self.y + CARD_HEIGHT
COLOR_INACTIVE = (151, 175, 171)
COLOR_ACTIVE = (27, 246, 246)
class InputBox:
def __init__(self, x, y, w, h, doneCallback, text=''):
self.rect = pygame.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = font.render(text, True, self.color)
self.active = False
self.doneCallback = doneCallback
def Handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.active = not self.active
self.text = ""
else:
self.active = False
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
self.doneCallback(self.text)
self.text = ""
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
self.txt_surface = font.render(self.text, True, self.color)
def Update(self):
width = max(200, self.txt_surface.get_width()+10)
self.rect.w = width
def Draw(self, screen):
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
pygame.draw.rect(screen, self.color, self.rect, 2)
Cards = []
CARDS_ROW = 4
CARD_COLUMNS = 7
COLORS_TOTAL = floor(CARDS_ROW * CARD_COLUMNS / 2)
colorsOut = {}
def pullUniqueColor():
i = randint(0, COLORS_TOTAL - 1)
v = colorsOut.get(i)
while v == 2:
i = randint(0, COLORS_TOTAL - 1)
v = colorsOut.get(i)
if v == 1:
colorsOut[i] = 2
else:
colorsOut[i] = 1
return i
def findPressedCard(x, y):
for card in Cards:
if card.TestCollisionSelfPoint(x, y):
return card
return None
i = 0
for y in range(0, CARDS_ROW):
for x in range(0, CARD_COLUMNS):
Cards.append(Card(x * 3 / 2 * CARD_WIDTH + PADDING_LEFT, y *
3 / 2 * CARD_HEIGHT + PADDING_TOP, pullUniqueColor()))
i += 0.5
SCREEN_WIDTH = floor(CARD_COLUMNS * 3 / 2 * CARD_WIDTH + PADDING_LEFT * 2)
SCREEN_HEIGHT = floor(12 * CARD_HEIGHT + PADDING_TOP)
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("MemoryGame")
font = pygame.font.SysFont('Comic Sans MS', 30)
TIME_TILL_TURN = 20
done = False
clock = pygame.time.Clock()
startTime = 0
gameStarted = False
CARD_PUT_DOWN_TIME = 3
card1up = None
card2up = None
cardsSolved = 0
cardPutDownTime = CARD_PUT_DOWN_TIME
lastCard = None
puttingDown = False
errors = 0
endTime = 0
writeHighscore = False
def SaveHighScore(name):
global writeHighscore
f.write("{0} {1} {2} {3} \n".format(name, floor(endTime - startTime),
errors, 1000 - floor(endTime - startTime) - errors * 10))
scores.append({
"name": name,
"time": floor(endTime - startTime),
"errors": errors,
"total": 1000 - floor(endTime - startTime) - errors * 10
})
scores.sort(key=lambda x: x["total"], reverse=True)
writeHighscore = True
highscoreBox = InputBox(SCREEN_WIDTH / 2 - 100,
400, 200, 32, SaveHighScore, "Username")
while not done:
if TIME_TILL_TURN > 0:
clock.tick(1)
TIME_TILL_TURN -= 1
elif not gameStarted:
clock.tick(30)
gameStarted = True
for card in Cards:
card.faceUp = False
startTime = time.time()
else:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if gameStarted and cardsSolved == COLORS_TOTAL:
highscoreBox.Handle_event(event)
screen.fill((255, 255, 255))
x, y = pygame.mouse.get_pos()
pressed, _, _ = pygame.mouse.get_pressed()
if gameStarted:
if cardsSolved == COLORS_TOTAL:
if endTime == 0:
endTime = time.time()
d = floor(endTime - startTime)
timeFromStart = font.render(str(d), False, (0, 0, 0))
screen.blit(timeFromStart, (SCREEN_WIDTH /
2 - 50, 200))
errorsSprite = font.render(str(errors), False, (230, 0, 0))
screen.blit(errorsSprite, (SCREEN_WIDTH /
2 + 50, 200))
screen.blit(font.render(str(errors), False, (230, 0, 0)), (SCREEN_WIDTH /
2 + 50, 200))
if not writeHighscore:
highscoreBox.Update()
highscoreBox.Draw(screen)
count = 0
for s in scores:
screen.blit(font.render(s["name"], False, (0, 0, 0)),
(SCREEN_WIDTH / 2 - 250, 500 + count * 25))
screen.blit(font.render(str(s["time"]), False, (120, 120, 120)),
(SCREEN_WIDTH / 2 - 50, 500 + count * 25))
screen.blit(font.render(str(s["errors"]), False, (255, 0, 0)),
(SCREEN_WIDTH / 2 + 50, 500 + count * 25))
screen.blit(font.render(str(s["total"]), False, (0, 0, 0)),
(SCREEN_WIDTH / 2 + 150, 500 + count * 25))
count += 1
else:
d = floor((time.time()) - startTime)
timeFromStart = font.render(str(d), False, (0, 0, 0))
screen.blit(timeFromStart, (SCREEN_WIDTH / 2 - 50, 0))
errorsSprite = font.render(str(errors), False, (230, 0, 0))
screen.blit(errorsSprite, (SCREEN_WIDTH / 2 + 50, 0))
if time.time() > cardPutDownTime and puttingDown:
if card1up != None:
card1up.faceUp = False
if card2up != None:
card2up.faceUp = False
card1up = None
card2up = None
puttingDown = False
if pressed:
card = findPressedCard(x, y)
if card != None and card != lastCard:
card.faceUp = True
if card1up == None:
card1up = card
elif card2up == None:
card2up = card
if card1up.color == card2up.color:
card1up.done = True
card2up.done = True
cardsSolved += 1
card1up = None
card2up = None
else:
cardPutDownTime = CARD_PUT_DOWN_TIME + time.time()
puttingDown = True
errors += 1
else:
puttingDown = False
card1up.faceUp = False
card2up.faceUp = False
card1up = card
card2up = None
lastCard = card
else:
timeTillStart = font.render(str(TIME_TILL_TURN), False, (0, 0, 0))
screen.blit(timeTillStart, (0, 0))
for card in Cards:
card.Draw()
pygame.display.flip()
pygame.quit()
f.close()