Repository navigation
Expand file tree
/
Copy pathSimon Game
More file actions
295 lines (222 loc) · 10.1 KB
/
Copy pathSimon Game
File metadata and controls
295 lines (222 loc) · 10.1 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
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame, sys
from pygame.locals import QUIT
import random
import faulthandler; faulthandler.enable()
pygame.init()
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 300
infoObject = pygame.display.Info()
surface = pygame.display.set_mode((infoObject.current_w, infoObject.current_h), pygame.FULLSCREEN)
w, h = pygame.display.get_surface().get_size()
get_width = surface.get_width() / 2
get_height = surface.get_height() / 2
FPS_CLOCK = pygame.time.Clock()
square_length = 150
square_height = 150
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
light_green = (158, 255, 158)
blue = (0, 0, 255)
light_blue = (173, 216, 230)
red = (255, 0, 0)
light_red = (255, 204, 203)
yellow = (255, 255, 0)
light_yellow = (255, 255, 237)
gameScore = 0
highScores = [0]
def game():
with open("score.txt", "w") as f:
f.write(str(highScores[0]))
random_color = ["red", "green", "blue", "yellow"]
random_number = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
counter = 0
global gameScore
random_color_choice = random.choice(random_color)
random_number_choice = random.choice(random_number)
surface.fill("black")
rect1 = pygame.Rect(get_width - square_length - 5, get_height - square_height - 5,square_length, square_height)
pygame.draw.rect(surface, red, rect1, 0, 20)
rect2 = pygame.Rect(get_width + 5, get_height + 5, square_length, square_height)
pygame.draw.rect(surface, green, rect2, 0,20)
rect3 = pygame.Rect(get_width - 5 - square_length, get_height + 5, square_length, square_height)
pygame.draw.rect(surface, blue, rect3,0,20)
rect4 = pygame.Rect(get_width + 5, get_height - square_height - 5, square_length, square_height)
pygame.draw.rect(surface, yellow, rect4,0,20)
pygame.display.set_caption('Color Game')
font = pygame.font.Font('freesansbold.ttf', 32)
title = font.render(f"click {random_color_choice} {random_number_choice} times", True, white, black)
titleRect = title.get_rect()
titleRect.center = (get_width, get_height - 250)
surface.blit(title, titleRect)
restart_rect = pygame.Rect(get_width - 160, get_height + 175, square_length , square_height / 1.5)
font = pygame.font.Font('freesansbold.ttf', 35)
res_text = font.render("restart", True, white)
resRect = res_text.get_rect()
resRect.center = (get_width - 85, get_height + 225)
quit_game = pygame.Rect(get_width + 10, get_height + 175, square_length , square_height / 1.5)
font = pygame.font.Font('freesansbold.ttf', 35)
quit_text = font.render("quit", True, white)
quitRect = quit_text.get_rect()
quitRect.center = (get_width + 80, get_height + 225)
def win_func():
win_rect = pygame.Rect(get_height - square_length * 2, get_height - square_height * 2, w ,h)
pygame.draw.rect(surface,green, win_rect)
font = pygame.font.Font('freesansbold.ttf', 60)
winText = font.render("you win!", True, white, green)
winRect = winText.get_rect()
winRect.center = (get_width, get_height)
surface.blit(winText, winRect)
surface.blit(res_text, resRect)
surface.blit(quit_text, quitRect)
pygame.draw.rect(surface, white, restart_rect, 4)
pygame.draw.rect(surface, white, quit_game, 4)
def scoreFunc():
global scoreRect
global score
pygame.display.set_caption('Color Game')
font = pygame.font.Font('freesansbold.ttf', 32)
score = font.render(f"score: {gameScore}", True, white, black)
scoreRect = score.get_rect()
scoreRect.center = (get_width, get_height - 200)
def rectangle_color_change(rect, color):
pygame.draw.rect(surface, color, rect, 0, 20)
def lose_func():
global value
lose_rect = pygame.Rect(w - w, h - h,w, h)
pygame.draw.rect(surface,red, lose_rect)
font = pygame.font.Font('freesansbold.ttf', 60)
lose = font.render("you lose", True, white, red)
loseRect = lose.get_rect()
loseRect.center = (get_width, get_height - 130)
surface.blit(lose, loseRect)
surface.blit(res_text, resRect)
surface.blit(quit_text, quitRect)
pygame.draw.rect(surface, white, restart_rect, 4)
pygame.draw.rect(surface, white, quit_game, 4)
if gameScore > highScores[0]:
highScores.remove(highScores[0])
highScores.append(*[gameScore])
with open("score.txt", "w") as f:
f.write(str(highScores))
value = gameScore
scFont = pygame.font.Font('freesansbold.ttf', 32)
renderSc = scFont.render(f"score: {gameScore}", True, white, red)
scRect = renderSc.get_rect()
scRect.center = (get_width - 20, get_height - 60)
surface.blit(renderSc, scRect)
hiFont = pygame.font.Font('freesansbold.ttf', 32)
renderHi = hiFont.render(f"High Score: {highScores[0]}", True, white, red)
highRect = renderHi.get_rect()
highRect.center = (get_width - 20, get_height - 20)
surface.blit(renderHi, highRect)
scoreFunc()
surface.blit(score, scoreRect)
pygame.display.flip()
lose = False
squares = True
game_state = True
colorChange = True
while game_state:
FPS_CLOCK.tick(60)
pygame.init()
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
global mouse_presses
mouse_presses = pygame.mouse.get_pressed()
if mouse_presses[0]:
mouse_location = pygame.mouse.get_pos()
mouse_coords = (mouse_location[0], mouse_location[1])
restart_check = mouse_coords[0] >= restart_rect.bottomleft[0] and mouse_coords[0] <=restart_rect.bottomright[0] and mouse_coords[1] <= restart_rect.bottomleft[1] and mouse_coords[1] >= restart_rect.topleft[1]
quit_check = mouse_coords[0] >= quit_game.bottomleft[0] and mouse_coords[0] <= quit_game.bottomright[0] and mouse_coords[1] <= quit_game.bottomleft[1] and mouse_coords[1] >= quit_game.topleft[1]
rect1_check = mouse_coords[0] >= rect1.bottomleft[0] and mouse_coords[0] <= rect1.bottomright[0] and mouse_coords[1] <= rect1.bottomleft[1] and mouse_coords[1] >= rect1.topleft[1]
rect2_check = mouse_coords[0] >= rect2.bottomleft[0] and mouse_coords[0] <= rect2.bottomright[0] and mouse_coords[1] <= rect2.bottomleft[1] and mouse_coords[1] >= rect2.topleft[1]
rect3_check = mouse_coords[0] >= rect3.bottomleft[0] and mouse_coords[0] <= rect3.bottomright[0] and mouse_coords[1] <= rect3.bottomleft[1] and mouse_coords[1] >= rect3.topleft[1]
rect4_check = mouse_coords[0] >= rect4.bottomleft[0] and mouse_coords[0] <= rect4.bottomright[0] and mouse_coords[1] <= rect4.bottomleft[1] and mouse_coords[1] >= rect4.topleft[1]
if random_color_choice == "red":
if rect1_check and squares == True:
rectangle_color_change(rect1, light_red)
color = red
counter += 1
scoreFunc()
if counter == random_number_choice:
gameScore += 1
game()
if rect2_check or rect3_check or rect4_check:
lose = True
lose_func()
squares = False
colorChange = False
if restart_check and lose == True and squares == False and colorChange == False:
gameScore =0
game()
if quit_check and lose == True and squares == False and colorChange == False:
pygame.quit()
exit()
if random_color_choice == "green":
if rect2_check and squares == True:
rectangle_color_change(rect2, light_green)
color = green
counter += 1
scoreFunc()
if counter == random_number_choice:
gameScore += 1
game()
if rect1_check or rect3_check or rect4_check:
lose = True
lose_func()
squares = False
colorChange = False
if restart_check and lose == True and squares == False and colorChange == False:
gameScore =0
game()
if quit_check and lose == True and squares == False and colorChange == False:
pygame.quit()
exit()
if random_color_choice == "yellow":
if rect4_check and squares == True:
rectangle_color_change(rect4, light_yellow)
color = yellow
counter += 1
scoreFunc()
if counter == random_number_choice:
gameScore += 1
game()
if rect1_check or rect2_check or rect3_check:
lose = True
lose_func()
squares = False
colorChange = False
if restart_check and lose == True and squares == False and colorChange == False:
gameScore =0
game()
if quit_check and lose == True and squares == False and colorChange == False:
pygame.quit()
exit()
if random_color_choice == "blue":
if rect3_check and squares == True:
counter += 1
rectangle_color_change(rect3, light_blue)
color = blue
scoreFunc()
if counter == random_number_choice:
gameScore += 1
game()
if rect1_check or rect2_check or rect4_check:
lose = True
lose_func()
squares = False
colorChange = False
if restart_check and lose == True and squares == False and colorChange == False:
gameScore =0
game()
if quit_check and lose == True and squares == False and colorChange == False:
pygame.quit()
exit()
game()