-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting.py
More file actions
348 lines (316 loc) · 12.1 KB
/
Testing.py
File metadata and controls
348 lines (316 loc) · 12.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# Full game + tweaking because I wanted it to be fun
# start modules
import pgzrun
from random import *
import math
# screen
WIDTH = 800
HEIGHT = 600
xGap = 266+(2/3) # calculation done to make 4 pipes eqidistant
# proj motion
y0 = 300
g = 9.81
t = 0
# wait to play
start = False
# play again
button = Actor("play_again")
button.x = 400
button.y = 475
# highscore
outText = ""
name = ""
keyUp = True
entered = False
# gameplay
score = 0
gameOver = False
# print welcome
print('''The game is about to start!
Click the mouse to "flap" upwards
Dodge the pipes and the floor
Good luck and have fun!''')
# make background
background = Actor("bg")
background.x = 400
background.y = 300
# make bird
bird = Actor("bird")
bird.x = 160
bird.y = 300
print(bird.height)
# pipes class
class Pipes():
def __init__(self, x):
gap = randint(160,260)
y = randint((300-250+(gap//2)),(850-300-(gap//2)))
self.top = Actor("top")
self.top.x = x
self.top.y = y - (300 + (gap // 2))
self.bottom = Actor("bottom")
self.bottom.x = x
self.bottom.y = y + 300 + (gap // 2)
def updatePipes(self, bird): # update pipes each frame
global score, gameOver
self.top.x = self.top.x - 1
self.bottom.x = self.bottom.x - 1
if self.top.x < -43.5:
self.top.x = xGap + (WIDTH-43.5) # calculation done to find where an equidistant pipe would be right as it wraps around
self.bottom.x = xGap + (WIDTH-43.5)
gap = randint(160,260) # generates the gap between the two pipes between 2 "flaps" + bird's height and 4 "flaps" + bird's height
y = randint((300-250+(gap//2)),(850-300-(gap//2))) # generates a point for the middle of the gap ensurinf the top pipe's y is never less than -250 and the bottom pipe's y is never more than 850 so they look significantly on screen at all times
self.top.y = y - (300 + (gap//2)) # finds the top pipe's y value with a calculations based of the middle of the gap and the gaps height and the distance from the gap to the center of the pipe (half it's height)
self.bottom.y = y + 300 + (gap//2) # same as above
score = score + 1
if bird.colliderect(self.top) or bird.colliderect(self.bottom):
print("Game Over!")
print(f"Your score was {score}")
gameOver = True
def drawPipes(self):
self.top.draw()
self.bottom.draw()
# make pipes
pipes1 = Pipes(2*xGap) # start's at 2 x gap to ensure there's time between starting and needing to "flap"
pipes2 = Pipes(3*xGap)
pipes3 = Pipes(4*xGap)
pipes4 = Pipes(5*xGap)
pipes = [pipes1,pipes2,pipes3,pipes4]
# draw everything frame
def draw():
if start == False:
background.draw()
bird.draw()
screen.draw.text("Click the screen to play!", center = (470,300), color = (255,255,255), fontsize = 60, width = 310)
else:
if gameOver == True:
screen.fill((0,0,0))
get_name()
screen.draw.text(f"Game Over!\n{outText}", center = (400,200), fontsize = 60, width = 600)
button.draw()
else:
# draw background
background.draw() # background is an actor as it's easier for beginners of pygame zero to understand instead of "blit"
# draw characters
bird.draw()
for pipe in pipes:
pipe.drawPipes()
screen.draw.text(f"Score: {score}", topleft = (20,20), color = (0,0,0), fontsize = 30)
# update every frame
def update():
global score, gameOver, t
if start == False:
t = t + ((15*math.pi)/60) # what to step by ( 1 frame is one pi/4 th of a second)
# loop time
bird.y = 300 + (math.cos(t/15.0)* 50.0) # bird's y plots a cos line for a smooth "flying" movement
else:
if gameOver == False:
t = t + 0.1 # 1 frame is 1/10 th of a "second"
# update bird
bird.y = y0 + (0.5 * g * (t**2)) # uses projectile motion formula but as the bird is falling, it's initial angle is 0 cancelling a section out
# update pipes
for pipe in pipes:
pipe.updatePipes(bird)
# bird hits bottom of screen
if bird.y > (HEIGHT+30): # bird is full off screen
print("Game Over!")
print(f"Your score was: {score}")
gameOver = True
# mouse clicks
def on_mouse_down(pos):
global start,t,y0
if start == False:
start = True
bird.y = 300
t = 0
else:
if gameOver:
if button.collidepoint(pos):
print("Restarting...")
restart()
else:
bird.y = bird.y - 50
t = 0 # needs to restart as the bird is now re-falling
y0 = bird.y
# restart values
def restart(): # self explanatory
global score, gameOver, y0,t, outText, name, keyUp, entered, pipes
score = 0
gameOver = False
outText = ""
name = ""
keyUp = True
entered = False
bird.y = 300
y0 = 300
t = 0
pipes1 = Pipes(2*xGap)
pipes2 = Pipes(3*xGap)
pipes3 = Pipes(4*xGap)
pipes4 = Pipes(5*xGap)
pipes = [pipes1,pipes2,pipes3,pipes4]
# saving a personal highscore to a file
def scoring():
global score,outText,name
scores={}
won = False
highscore = 0
with open(".\GPN\Flappy_Bird\Highscore.txt","r") as file:
nextLine = file.readline()
while nextLine:
if nextLine != "":
nextLine = nextLine.strip("\n")
hsName,hscore = nextLine.split(": ") # store's the name and score in a dictionary as searching a dictionary is faster/easier than a list of names etc
scores[hsName] = hscore
nextLine = file.readline()
if name in scores:
if score > int(scores[name]):
won = True
scores[name]=str(score) # changes score value to easily add correct info back into file
else:
highscore = scores[name]
else:
won = True
scores[name] = score
with open(".\GPN\Flappy_Bird\Highscore.txt","w") as f:
for person in scores:
f.write(f"{person}: {scores[person]}\n")
f.close()
if won == True:
outText = f"Congratulations you beat your high score!\nYour score was: {score}"
else:
outText = f"Your score was: {score}\nYour current highscore is: {highscore}"
# re-sends the value of name so it updates on screen when the user types a new letter (re-sends every frame_)
def get_name():
global outText
if entered == False:
outText = f"What is your name?\n{name}"
# key logger part 1 (so no held keys are tracked)
def on_key_down():
global keyUp, name, entered
if gameOver and (entered == False) and keyUp:
keyUp = False # once a key is pressed it must be un-pressed to be counted again
if keyboard[keys.RETURN]:
entered = True
scoring() # this means that scoring is only run once
print(f"Your highscore is saved under {name}")
else:
if keyboard[keys.A] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]): # this way is a bit janky but pygame zero doesn't allow for just reading a capital without the use of shift due to some keboard's maufacturing
name+="A"
elif keyboard[keys.B] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="B"
elif keyboard[keys.C] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="C"
elif keyboard[keys.D] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="D"
elif keyboard[keys.E] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="E"
elif keyboard[keys.F] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="F"
elif keyboard[keys.G] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="G"
elif keyboard[keys.H] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="H"
elif keyboard[keys.I] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="I"
elif keyboard[keys.J] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="J"
elif keyboard[keys.K] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="K"
elif keyboard[keys.L] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="L"
elif keyboard[keys.M] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="M"
elif keyboard[keys.N] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="N"
elif keyboard[keys.O] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="O"
elif keyboard[keys.P] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="P"
elif keyboard[keys.Q] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="Q"
elif keyboard[keys.R] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="R"
elif keyboard[keys.S] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="S"
elif keyboard[keys.T] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="T"
elif keyboard[keys.U] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="U"
elif keyboard[keys.V] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="V"
elif keyboard[keys.W] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="W"
elif keyboard[keys.X] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="X"
elif keyboard[keys.Y] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="Y"
elif keyboard[keys.Z] and (keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]):
name+="Z"
elif keyboard[keys.A]:
name+="a"
elif keyboard[keys.B]:
name+="b"
elif keyboard[keys.C]:
name+="c"
elif keyboard[keys.D]:
name+="d"
elif keyboard[keys.E]:
name+="e"
elif keyboard[keys.F]:
name+="f"
elif keyboard[keys.G]:
name+="g"
elif keyboard[keys.H]:
name+="h"
elif keyboard[keys.I]:
name+="i"
elif keyboard[keys.J]:
name+="j"
elif keyboard[keys.K]:
name+="k"
elif keyboard[keys.L]:
name+="l"
elif keyboard[keys.M]:
name+="m"
elif keyboard[keys.N]:
name+="n"
elif keyboard[keys.O]:
name+="o"
elif keyboard[keys.P]:
name+="p"
elif keyboard[keys.Q]:
name+="q"
elif keyboard[keys.R]:
name+="r"
elif keyboard[keys.S]:
name+="s"
elif keyboard[keys.T]:
name+="t"
elif keyboard[keys.U]:
name+="u"
elif keyboard[keys.V]:
name+="v"
elif keyboard[keys.W]:
name+="w"
elif keyboard[keys.X]:
name+="x"
elif keyboard[keys.Y]:
name+="y"
elif keyboard[keys.Z]:
name+="z"
elif keyboard[keys.SPACE]:
name+=" "
elif keyboard[keys.BACKSPACE]:
if len(name)>=1:
name = name[:-1]
elif keyboard[keys.LSHIFT] or keyboard[keys.RSHIFT]:
pass
else:
print("Only type letters and spaces please")
# key logger part 2
def on_key_up():
global keyUp
if gameOver:
keyUp = True
# runs everything
pgzrun.go()