-
Notifications
You must be signed in to change notification settings - Fork 3
/
dino.py
397 lines (330 loc) · 12 KB
/
dino.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import pygame, sys, random
from pygame.locals import *
WINDOWWIDTH = 600
WINDOWHEIGHT = 150
FPS = 60
GRAVITY = 0.75
SPEED_GROUND = 6
IMG_GROUND = pygame.image.load('./img/ground.png')
SPEED_SKY = 1
IMG_SKY = pygame.image.load('./img/sky.png')
IMG_TREX = pygame.image.load('./img/tRex.png')
TIME_CHANGE_TREX = 6
Y_TREX = 105
X_TREX = 50
HIGH_MIN = 90
SPEED_TREX = -12.5
IMG_CATUS = pygame.image.load('./img/cactus.png')
Y_CATUS = 100
BIRD_IMG = pygame.image.load('./img/bird.png')
TIME_CHANGE_BIRD = 10
Y_BIRD_1 = 110
Y_BIRD_2 = 80
Y_BIRD_3 = 50
DISTANCE_MIN = 400
DISTANCE_MAX = 600
pygame.init()
pygame.display.set_caption('T-REX')
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
class T_Rex():
def __init__(self, option = 3):
self.x = X_TREX
self.y = Y_TREX
self.speed = 0
self.img = IMG_TREX
self.option = option
self.surface = pygame.Surface((55, 43), pygame.SRCALPHA)
self.surface.blit(self.img, (0, 0), (80, 0, 40, 43))
self.timeChange = 0
self.jumping = False
self.lowering = False
def update(self, up, down):
self.surface.fill((0, 0, 0, 0))
if not self.jumping:
if up:
self.jumping = True
self.speed = SPEED_TREX
elif down:
self.lowering = True
if self.timeChange <= TIME_CHANGE_TREX:
self.option = 4
else:
self.option = 5
self.timeChange += 1
if self.timeChange > TIME_CHANGE_TREX * 2:
self.timeChange = 0
else:
if self.timeChange <= TIME_CHANGE_TREX:
self.option = 0
else:
self.option = 1
self.timeChange += 1
if self.timeChange > TIME_CHANGE_TREX * 2:
self.timeChange = 0
elif self.jumping:
self.option = 2
if self.y <= Y_TREX - HIGH_MIN and self.speed < 0 and (not up):
self.speed = 0
self.y += int(self.speed + GRAVITY/2)
self.speed += GRAVITY
if self.y >= Y_TREX:
self.jumping = False
self.y = Y_TREX
if self.option == 0:
self.surface.blit(self.img, (0, 0), (0, 0, 40, 43))
elif self.option == 1:
self.surface.blit(self.img, (0, 0), (40, 0, 40, 43))
elif self.option == 2:
self.surface.blit(self.img, (0, 0), (80, 0, 40, 43))
elif self.option == 3:
self.surface.blit(self.img, (0, 0), (120, 0, 40, 43))
elif self.option == 4:
self.surface.blit(self.img, (0, 0), (160, 0, 55, 43))
elif self.option == 5:
self.surface.blit(self.img, (0, 0), (215, 0, 55, 43))
def draw(self):
DISPLAYSURF.blit(self.surface, (self.x, self.y))
class Catus():
def __init__(self, x, y, option):
self.x = x
self.y = y
self.option = option
self.img = IMG_CATUS
self.rect = [0, 0, 0, 0]
if option == 0:
self.rect = [0, 0, 23, 46]
elif option == 1:
self.rect = [0, 0, 47, 46]
elif option == 2:
self.rect = [100, 0, 49, 46]
elif option == 3:
self.rect = [100, 0, 49, 46]
elif option == 4:
self.rect = [25, 0, 73, 46]
self.surface = pygame.Surface((self.rect[2], self.rect[3]), pygame.SRCALPHA)
self.surface.blit(self.img, (0, 0), self.rect)
def update(self, speed):
self.x -= int(speed)
def draw(self):
DISPLAYSURF.blit(self.surface, (self.x, self.y))
class Bird():
def __init__(self, x, y, option = 0):
self.x = x
self.y = y
self.option = option
self.surface = pygame.Surface((42, 36), pygame.SRCALPHA)
self.timeChange = 0
self.img = pygame.image.load('./img/bird.png')
def update(self, speed):
self.surface.fill((0, 0, 0, 0))
self.x -= int(speed)
if self.timeChange <= TIME_CHANGE_BIRD:
self.option = 0
else:
self.option = 1
self.timeChange += 1
if self.timeChange > TIME_CHANGE_BIRD * 2:
self.timeChange = 0
if self.option == 0:
self.surface.blit(self.img, (0, 0), (0, 0, 42, 36))
elif self.option == 1:
self.surface.blit(self.img, (0, 0), (42, 0, 42, 36))
def draw(self):
DISPLAYSURF.blit(self.surface, (self.x, self.y))
class ListCatusAndBirds():
def __init__(self):
self.list = []
for i in range(0, 3):
self.list.append(Catus(500 + WINDOWWIDTH + random.randint(DISTANCE_MIN, DISTANCE_MAX)*i, Y_CATUS, random.randint(0, 3)))
self.speed = SPEED_GROUND
def update(self, score):
self.speed = SPEED_GROUND * (1 + score/500)
if self.speed > SPEED_GROUND * 2:
self.speed = SPEED_GROUND * 2
for i in range(len(self.list)):
self.list[i].update(self.speed)
if self.list[0].x < -132:
self.list.pop(0)
if self.speed > SPEED_GROUND * 1.5:
rand = random.randint(0, 5)
if rand == 5:
self.list.append(Bird(self.list[1].x + random.randint(DISTANCE_MIN + 200, DISTANCE_MAX + 100), random.choice((Y_BIRD_1, Y_BIRD_2, Y_BIRD_3))))
else:
self.list.append(Catus(self.list[1].x + random.randint(DISTANCE_MIN + 100, DISTANCE_MAX + 100), Y_CATUS, random.randint(0, 4)))
else:
self.list.append(Catus(self.list[1].x + random.randint(DISTANCE_MIN, DISTANCE_MAX), Y_CATUS, random.randint(0, 3)))
def draw(self):
for i in range(len(self.list)):
self.list[i].draw()
class Ground():
def __init__(self):
self.x = 0
self.y = 138
self.img = IMG_GROUND
self.speed = SPEED_GROUND
def update(self, score):
self.speed = SPEED_GROUND * (1 + score/500)
if self.speed > SPEED_GROUND * 2:
self.speed = SPEED_GROUND * 2
self.x -= int(self.speed)
if self.x <= -600:
self.x += 600
def draw(self):
DISPLAYSURF.blit(self.img, (self.x, self.y))
class Sky():
def __init__(self):
self.x = 0
self.y = 0
self.speed = SPEED_SKY
self.img = IMG_SKY
def update(self, score):
self.speed = SPEED_SKY * (1 + score/500)
if self.speed > SPEED_SKY * 2:
self.speed = SPEED_SKY * 2
self.x -= int(self.speed)
if self.x <= -600:
self.x += 600
def draw(self):
DISPLAYSURF.blit(self.img, (self.x, self.y))
class Score():
def __init__(self):
self.score = 0
self.highScore = 0
self.textScore = ""
self.textHighScore = ""
self.size = 15
def update(self):
self.score += 0.15
if self.score > self.highScore:
self.highScore = int(self.score)
self.textScore = str(int(self.score))
for i in range(5 - len(self.textScore)):
self.textScore = '0' + self.textScore
self.textHighScore = str(int(self.highScore))
for i in range(5 - len(self.textHighScore)):
self.textHighScore = '0' + self.textHighScore
self.textHighScore = "HI: " + self.textHighScore
def draw(self):
fontObj = pygame.font.SysFont('consolas', self.size)
textSurfaceScore = fontObj.render(self.textScore, True, (0, 0, 0))
DISPLAYSURF.blit(textSurfaceScore, (550, 10))
textSurfaceHighScore = fontObj.render(self.textHighScore, True, (60, 60, 60))
DISPLAYSURF.blit(textSurfaceHighScore, (450, 10))
class BlinkText():
def __init__(self, text):
self.text = text
self.timeChange = 0
self.size = 14
fontObj = pygame.font.SysFont('consolas', self.size)
textSurface = fontObj.render(self.text, False, (0, 0, 0))
self.surface = pygame.Surface(textSurface.get_size())
self.surface.fill((255, 255, 255))
self.surface.blit(textSurface, (0, 0))
self.surface.set_colorkey((255, 255, 255))
self.alpha = 255
def update(self):
self.alpha = abs(int(255 - self.timeChange))
if self.timeChange > 255*2:
self.timeChange = 0
self.timeChange += 5
def draw(self):
self.surface.set_alpha(self.alpha)
DISPLAYSURF.blit(self.surface, (int(WINDOWWIDTH/2 - self.surface.get_width()/2), 100))
def isCollision(tRex, ls):
tRexMask = pygame.mask.from_surface(tRex.surface)
for catusOrBird in ls.list:
catusOrBird_mask = pygame.mask.from_surface(catusOrBird.surface)
result = tRexMask.overlap(catusOrBird_mask, (catusOrBird.x - tRex.x, catusOrBird.y - tRex.y))
if result:
return True
return False
def main():
print("""/* =====Dino Jump===== */
// Space to Play, Esc to Exit
// Up to jump, Down to bow down""")
sky = Sky()
ground = Ground()
tRex = T_Rex(0)
up = False
down = False
ls = ListCatusAndBirds()
score = Score()
blinkText = BlinkText("Space to Play, Esc to Exit")
# haven't start yet
while True:
isStart = False
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
isStart = True
if isStart:
break
sky.draw()
ground.draw()
tRex.draw()
score.draw()
blinkText.update()
blinkText.draw()
pygame.display.update()
FPSCLOCK.tick(FPS)
# started and control the game
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_UP:
up = True
elif event.key == K_DOWN:
down = True
if event.type == KEYUP:
if event.key == K_UP:
up = False
elif event.key == K_DOWN:
down = False
sky.update(score.score)
sky.draw()
ground.update(score.score)
ground.draw()
tRex.update(up, down)
tRex.draw()
ls.update(score.score)
ls.draw()
score.update()
score.draw()
# game over and play again
if isCollision(tRex, ls):
tRex.surface.fill((0, 0, 0, 0))
tRex.surface.blit(tRex.img, (0, 0), (120, 0, 40, 43))
gameOverFontObj = pygame.font.SysFont('consolas', 30, bold=1)
gameOverTextSurface = gameOverFontObj.render("GAME OVER", True, (0, 0, 0))
while True:
isStart = False
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
isStart = True
if isStart:
break
sky.draw()
ground.draw()
tRex.draw()
ls.draw()
score.draw()
DISPLAYSURF.blit(gameOverTextSurface, (int(WINDOWWIDTH/2 - gameOverTextSurface.get_width()/2), 50))
blinkText.update()
blinkText.draw()
pygame.display.update()
FPSCLOCK.tick(FPS)
score.score = 0
ls = ListCatusAndBirds()
pygame.display.update()
FPSCLOCK.tick(FPS)
if __name__ == '__main__':
main()