Skip to content

Commit 4f74efe

Browse files
authored
Update and rename sliding_game.py to simulate_color_game.py
1 parent f68d569 commit 4f74efe

File tree

2 files changed

+374
-525
lines changed

2 files changed

+374
-525
lines changed
+374
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
# Import Required
2+
3+
import random, sys, time, pygame
4+
from pygame.locals import *
5+
6+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
7+
8+
FPS = 30
9+
10+
WINDOWWIDTH = 640
11+
12+
WINDOWHEIGHT = 480
13+
14+
FLASHSPEED = 500 # in milliseconds
15+
16+
FLASHDELAY = 200 # in milliseconds
17+
18+
BUTTONSIZE = 200
19+
20+
BUTTONGAPSIZE = 20
21+
22+
TIMEOUT = 4 # seconds before game over if no button is pushed.
23+
24+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
25+
26+
# R G B
27+
28+
WHITE = (255, 255, 255)
29+
30+
BLACK = ( 0, 0, 0)
31+
32+
BRIGHTRED = (255, 0, 0)
33+
34+
RED = (155, 0, 0)
35+
36+
BRIGHTGREEN = ( 0, 255, 0)
37+
38+
GREEN = ( 0, 155, 0)
39+
40+
BRIGHTBLUE = ( 0, 0, 255)
41+
42+
BLUE = ( 0, 0, 155)
43+
44+
BRIGHTYELLOW = (255, 255, 0)
45+
46+
YELLOW = (155, 155, 0)
47+
48+
DARKGRAY = ( 40, 40, 40)
49+
50+
bgColor = BLACK
51+
52+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
53+
54+
XMARGIN = int((WINDOWWIDTH - (2 * BUTTONSIZE) - BUTTONGAPSIZE) / 2)
55+
56+
YMARGIN = int((WINDOWHEIGHT - (2 * BUTTONSIZE) - BUTTONGAPSIZE) / 2)
57+
58+
# Rect objects for each of the four buttons
59+
60+
YELLOWRECT = pygame.Rect(XMARGIN, YMARGIN, BUTTONSIZE, BUTTONSIZE)
61+
62+
BLUERECT = pygame.Rect(XMARGIN + BUTTONSIZE + BUTTONGAPSIZE, YMARGIN, BUTTONSIZE, BUTTONSIZE)
63+
64+
REDRECT = pygame.Rect(XMARGIN, YMARGIN + BUTTONSIZE + BUTTONGAPSIZE, BUTTONSIZE, BUTTONSIZE)
65+
66+
GREENRECT = pygame.Rect(XMARGIN + BUTTONSIZE + BUTTONGAPSIZE, YMARGIN + BUTTONSIZE + BUTTONGAPSIZE, BUTTONSIZE, BUTTONSIZE)
67+
68+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
69+
70+
def main():
71+
72+
global FPSCLOCK, DISPLAYSURF, BASICFONT, BEEP1, BEEP2, BEEP3, BEEP4
73+
74+
75+
76+
pygame.init()
77+
FPSCLOCK = pygame.time.Clock()
78+
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
79+
pygame.display.set_caption('Simulate')
80+
81+
82+
83+
BASICFONT = pygame.font.Font('freesansbold.ttf', 16)
84+
infoSurf = BASICFONT.render('Match the pattern by clicking on the button or using the Q, W, A, S keys.', 1, DARKGRAY)
85+
infoRect = infoSurf.get_rect()
86+
infoRect.topleft = (10, WINDOWHEIGHT - 25)
87+
88+
89+
90+
# load the sound files
91+
92+
BEEP1 = pygame.mixer.Sound('beep1.ogg')
93+
BEEP2 = pygame.mixer.Sound('beep2.ogg')
94+
BEEP3 = pygame.mixer.Sound('beep3.ogg')
95+
BEEP4 = pygame.mixer.Sound('beep4.ogg')
96+
97+
# Initialize some variables for a new game
98+
99+
pattern = [] # stores the pattern of colors
100+
currentStep = 0 # the color the player must push next
101+
lastClickTime = 0 # timestamp of the player's last button push
102+
score = 0
103+
104+
# when False, the pattern is playing. when True, waiting for the player to click a colored button:
105+
106+
waitingForInput = False
107+
108+
109+
110+
while True: # main game loop
111+
112+
clickedButton = None # button that was clicked (set to YELLOW, RED, GREEN, or BLUE)
113+
DISPLAYSURF.fill(bgColor)
114+
drawButtons()
115+
116+
117+
scoreSurf = BASICFONT.render('Score: ' + str(score), 1, WHITE)
118+
scoreRect = scoreSurf.get_rect()
119+
scoreRect.topleft = (WINDOWWIDTH - 100, 10)
120+
121+
DISPLAYSURF.blit(scoreSurf, scoreRect)
122+
DISPLAYSURF.blit(infoSurf, infoRect)
123+
124+
checkForQuit()
125+
126+
for event in pygame.event.get(): # event handling loop
127+
128+
if event.type == MOUSEBUTTONUP:
129+
130+
mousex, mousey = event.pos
131+
clickedButton = getButtonClicked(mousex, mousey)
132+
133+
elif event.type == KEYDOWN:
134+
135+
if event.key == K_q:
136+
clickedButton = YELLOW
137+
138+
elif event.key == K_w:
139+
clickedButton = BLUE
140+
141+
elif event.key == K_a:
142+
clickedButton = RED
143+
144+
elif event.key == K_s:
145+
clickedButton = GREEN
146+
147+
148+
if not waitingForInput:
149+
150+
# play the pattern
151+
152+
pygame.display.update()
153+
pygame.time.wait(1000)
154+
pattern.append(random.choice((YELLOW, BLUE, RED, GREEN)))
155+
156+
for button in pattern:
157+
158+
flashButtonAnimation(button)
159+
pygame.time.wait(FLASHDELAY)
160+
161+
waitingForInput = True
162+
163+
else:
164+
165+
# wait for the player to enter buttons
166+
167+
if clickedButton and clickedButton == pattern[currentStep]:
168+
169+
# pushed the correct button
170+
171+
flashButtonAnimation(clickedButton)
172+
currentStep += 1
173+
lastClickTime = time.time()
174+
175+
if currentStep == len(pattern):
176+
177+
# pushed the last button in the pattern
178+
179+
changeBackgroundAnimation()
180+
score += 1
181+
waitingForInput = False
182+
currentStep = 0 # reset back to first step
183+
184+
elif (clickedButton and clickedButton != pattern[currentStep]) or (currentStep != 0 and time.time() - TIMEOUT > lastClickTime):
185+
186+
# pushed the incorrect button, or has timed out
187+
188+
gameOverAnimation()
189+
190+
# reset the variables for a new game:
191+
192+
pattern = []
193+
currentStep = 0
194+
waitingForInput = False
195+
score = 0
196+
pygame.time.wait(1000)
197+
changeBackgroundAnimation()
198+
199+
200+
pygame.display.update()
201+
FPSCLOCK.tick(FPS)
202+
203+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
204+
205+
def terminate():
206+
207+
pygame.quit()
208+
sys.exit()
209+
210+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
211+
212+
def checkForQuit():
213+
214+
for event in pygame.event.get(QUIT): # get all the QUIT events
215+
216+
terminate() # terminate if any QUIT events are present
217+
218+
for event in pygame.event.get(KEYUP): # get all the KEYUP events
219+
220+
if event.key == K_ESCAPE:
221+
222+
terminate() # terminate if the KEYUP event was for the Esc key
223+
224+
pygame.event.post(event) # put the other KEYUP event objects back
225+
226+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
227+
228+
def flashButtonAnimation(color, animationSpeed=50):
229+
230+
if color == YELLOW:
231+
232+
sound = BEEP1
233+
flashColor = BRIGHTYELLOW
234+
rectangle = YELLOWRECT
235+
236+
elif color == BLUE:
237+
238+
sound = BEEP2
239+
flashColor = BRIGHTBLUE
240+
rectangle = BLUERECT
241+
242+
elif color == RED:
243+
244+
sound = BEEP3
245+
flashColor = BRIGHTRED
246+
rectangle = REDRECT
247+
248+
elif color == GREEN:
249+
250+
sound = BEEP4
251+
flashColor = BRIGHTGREEN
252+
rectangle = GREENRECT
253+
254+
origSurf = DISPLAYSURF.copy()
255+
flashSurf = pygame.Surface((BUTTONSIZE, BUTTONSIZE))
256+
flashSurf = flashSurf.convert_alpha()
257+
258+
r, g, b = flashColor
259+
260+
sound.play()
261+
262+
for start, end, step in ((0, 255, 1), (255, 0, -1)): # animation loop
263+
264+
for alpha in range(start, end, animationSpeed * step):
265+
266+
checkForQuit()
267+
DISPLAYSURF.blit(origSurf, (0, 0))
268+
flashSurf.fill((r, g, b, alpha))
269+
DISPLAYSURF.blit(flashSurf, rectangle.topleft)
270+
pygame.display.update()
271+
FPSCLOCK.tick(FPS)
272+
273+
DISPLAYSURF.blit(origSurf, (0, 0))
274+
275+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
276+
277+
def drawButtons():
278+
279+
pygame.draw.rect(DISPLAYSURF, YELLOW, YELLOWRECT)
280+
pygame.draw.rect(DISPLAYSURF, RED, REDRECT)
281+
pygame.draw.rect(DISPLAYSURF, BLUE, BLUERECT)
282+
pygame.draw.rect(DISPLAYSURF, GREEN, GREENRECT)
283+
284+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
285+
286+
def changeBackgroundAnimation(animationSpeed=40):
287+
288+
global bgColor
289+
290+
newBgColor = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
291+
newBgSurf = pygame.Surface((WINDOWWIDTH, WINDOWHEIGHT))
292+
newBgSurf = newBgSurf.convert_alpha()
293+
294+
r, g, b = newBgColor
295+
296+
for alpha in range(0, 255, animationSpeed): # animation loop
297+
298+
checkForQuit()
299+
DISPLAYSURF.fill(bgColor)
300+
newBgSurf.fill((r, g, b, alpha))
301+
DISPLAYSURF.blit(newBgSurf, (0, 0))
302+
drawButtons() # redraw the buttons on top of the tint
303+
pygame.display.update()
304+
FPSCLOCK.tick(FPS)
305+
306+
bgColor = newBgColor
307+
308+
309+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
310+
311+
312+
def gameOverAnimation(color=WHITE, animationSpeed=50):
313+
314+
# play all beeps at once, then flash the background
315+
316+
origSurf = DISPLAYSURF.copy()
317+
318+
flashSurf = pygame.Surface(DISPLAYSURF.get_size())
319+
flashSurf = flashSurf.convert_alpha()
320+
321+
BEEP1.play() # play all four beeps at the same time, roughly.
322+
BEEP2.play()
323+
BEEP3.play()
324+
BEEP4.play()
325+
326+
r, g, b = color
327+
328+
for i in range(3): # do the flash 3 times
329+
330+
for start, end, step in ((0, 255, 1), (255, 0, -1)):
331+
332+
# The first iteration in this loop sets the following for loop to go from 0 to 255, the second from 255 to 0.
333+
334+
for alpha in range(start, end, animationSpeed * step): # animation loop
335+
336+
# alpha means transparency. 255 is opaque, 0 is invisible
337+
338+
checkForQuit()
339+
flashSurf.fill((r, g, b, alpha))
340+
341+
DISPLAYSURF.blit(origSurf, (0, 0))
342+
DISPLAYSURF.blit(flashSurf, (0, 0))
343+
344+
drawButtons()
345+
pygame.display.update()
346+
FPSCLOCK.tick(FPS)
347+
348+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
349+
350+
def getButtonClicked(x, y):
351+
352+
if YELLOWRECT.collidepoint( (x, y) ):
353+
354+
return YELLOW
355+
356+
elif BLUERECT.collidepoint( (x, y) ):
357+
358+
return BLUE
359+
360+
elif REDRECT.collidepoint( (x, y) ):
361+
362+
return RED
363+
364+
elif GREENRECT.collidepoint( (x, y) ):
365+
366+
return GREEN
367+
368+
return None
369+
370+
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
371+
372+
if __name__ == '__main__':
373+
374+
main()

0 commit comments

Comments
 (0)