-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdino.py
308 lines (263 loc) · 10.6 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
import sys
import os
from random import choice, randint
import pygame
from pygame.locals import *
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
FOES = ['brontosaurus32.png',
'pterodactyl32.png',
'stegosaurus32.png',
'triceratops32.png',
'tyrannosaurus_rex32.png']
class Projectiles(pygame.sprite.Group):
def __init__(self):
super(Projectiles, self).__init__()
class FoeProjectiles(pygame.sprite.Group):
def __init__(self):
super(FoeProjectiles, self).__init__()
class Foes(pygame.sprite.Group):
def __init__(self):
super(Foes, self).__init__()
class FoeProjectile(pygame.sprite.Sprite):
def __init__(self):
super(FoeProjectile, self).__init__()
self.fire_sound = pygame.mixer.Sound('sounds/roar.ogg')
self.image = pygame.image.load('images/invader_bullet.png')
self.rect = self.image.get_rect()
def fire(self, screen, foe_rect):
self.fire_sound.play()
self.rect = self.image.get_rect(center=(foe_rect.centerx ,
foe_rect.bottom + 10))
screen.blit(self.image, self.rect)
def move(self, screen):
new_rect = self.rect.move(0, 2)
if screen.get_rect().contains(new_rect):
self.rect = new_rect
screen.blit(self.image, self.rect)
else:
self.kill()
class Projectile(pygame.sprite.Sprite):
def __init__(self):
super(Projectile, self).__init__()
self.fire_sound = pygame.mixer.Sound('sounds/roar.ogg')
self.image = pygame.image.load('images/burn.png')
self.rect = self.image.get_rect()
def fire(self, screen, dino_rect):
self.fire_sound.play()
self.rect = self.image.get_rect(center=(dino_rect.centerx - 16, dino_rect.top - 10))
screen.blit(self.image, self.rect)
def move(self, screen):
new_rect = self.rect.move(0, -10)
if screen.get_rect().contains(new_rect):
self.rect = new_rect
screen.blit(self.image, self.rect)
else:
self.kill()
class Foe(pygame.sprite.Sprite):
foe_projectiles = FoeProjectiles()
fire_power = 5
def __init__(self, path=None, center=None, speed=0.2, direction=1, level=1):
super(Foe, self).__init__()
if path is None:
fn = choice(FOES)
path = os.path.join('images', choice(FOES))
self.type = fn
self.image = pygame.image.load(path).convert_alpha()
if center is None:
center = (randint(32, SCREEN_WIDTH - 24), randint(16, SCREEN_HEIGHT - 80))
else:
self.rect = self.image.get_rect(center=center)
self.speed = speed
self.direction = direction
def move(self, screen):
new_rect = self.rect.move(self.direction * 1 * self.speed, 0)
if screen.get_rect().contains(new_rect):
self.rect = new_rect
else:
self.direction *= -1
self.rect = self.rect.move(0, 32)
def fire(self, screen):
if len(Foe.foe_projectiles) < Foe.fire_power and randint(1,100) == 100:
projectile = FoeProjectile()
projectile.add(Foe.foe_projectiles)
projectile.fire(screen, self.rect)
class Level(object):
def __init__(self, number):
self.foes = Foes()
#if number == 1:
x_multiplier = SCREEN_WIDTH / (number * 3)
foe_image_path = 'images/pterodactyl32.png'
for n in range(1, number * 3):
foe = Foe(foe_image_path, (n * x_multiplier, number * 56), 2, level=number)
foe.add(self.foes)
foe = Foe(foe_image_path, (n * x_multiplier, 78), 2, -1, level=number)
foe.add(self.foes)
foe_image_path = 'images/brontosaurus32.png'
for n in range(1, number * 3):
foe = Foe(foe_image_path, (n * x_multiplier, 96), 1, level=number)
foe.add(self.foes)
foe = Foe(foe_image_path, (n * x_multiplier, 128), 1, -1, level=number)
foe.add(self.foes)
class Game(object):
def __init__(self):
self.score = 0
self.level = Level(1)
self.num_level = 1
self.max_levels = 10
self.dino = Dino()
self._init_graphics()
self._init_sounds()
def draw_background(self, screen):
screen.blit(self.background, (0, 0))
def shutdown(self, screen):
screen.blit(self.game_font.render("Shutdown", 0, ((176, 0, 0))),
(250, SCREEN_HEIGHT / 2 - 36))
pygame.display.flip()
pygame.time.wait(200)
sys.exit()
def draw_score(self, screen):
screen.blit(self.game_font.render(str(self.score), 0, (255, 255,
255)), (20, 20))
def draw_lives(self, screen):
screen.blit(self.game_font.render("Lives: {0}".format(self.dino.num_lives_left), 0, (255, 255,
255)), (20, 50))
def next_level(self, screen):
self.num_level += 1
Foe.fire_power += 2
if self.num_level <= self.max_levels:
self.level = Level(self.num_level)
else:
self.display_in_center("Last level completed!\nGame over", screen)
pygame.time.wait(2000)
sys.exit()
self.display_in_center("Level {0}".format(self.num_level), screen)
pygame.time.wait(2000)
def display_in_center(self, message, screen):
screen.blit(self.game_font.render(message, 0, ((176, 0, 0))),
(250, SCREEN_HEIGHT / 2 - 36))
pygame.display.flip()
def over(self, screen):
screen.blit(self.game_font.render("Game Over", 0, ((176, 0, 0))),
(250, SCREEN_HEIGHT / 2 - 36))
pygame.display.flip()
pygame.time.wait(200)
sys.exit()
def _init_sounds(self):
self.game_sound = pygame.mixer.Sound('sounds/foresta_nera.ogg')
self.over_sound = pygame.mixer.Sound('sounds/over.wav')
self.wound_sound1 = pygame.mixer.Sound('sounds/wound.wav')
self.wound_sound2 = pygame.mixer.Sound('sounds/wound.ogg')
self.boom_sound = pygame.mixer.Sound('sounds/boom.ogg')
self.drop1 = pygame.mixer.Sound('sounds/drop1.wav')
self.drop2 = pygame.mixer.Sound('sounds/drop2.wav')
self.move_sound = pygame.mixer.Sound('sounds/fiu.ogg')
self.game_sound.set_volume(0.1)
self.game_sound.play(loops=-1)
def _init_graphics(self):
self.game_font = pygame.font.Font( os.path.join("fonts", "04b_25__.ttf"), 24)
self.background = pygame.image.load('images/background_rex.png').convert()
class Dino(pygame.sprite.Sprite):
def __init__(self):
self.dinoR = pygame.image.load('images/tyrannosaurus_rex64.png').convert_alpha()
self.dinoL = pygame.image.load('images/tyrannosaurus_rex64_m.png').convert_alpha()
self.dino = self.dinoR
self.num_lives_left = 2
# Set dino starting position to the center of the bottom of the screen
self.dino_rect = self.dino.get_rect(center=(
SCREEN_WIDTH / 2,
SCREEN_HEIGHT - (self.dino.get_height() / 2)))
self.speed = 2
self.fire_power = 3
self.can_fire = True
self.projectiles = Projectiles()
def draw_dino(self, screen):
screen.blit(self.dino, self.dino_rect)
def move(self, screen, direction):
if direction == 'left':
self.dino = self.dinoR
multiplier = -1
else:
self.dino = self.dinoL
multiplier = 1
new_rect = self.dino_rect.move(self.speed * 5 * multiplier, 0)
# assign new position if dino is still inside the screen after moving
if screen.get_rect().contains(new_rect):
self.dino_rect = new_rect
def fire(self, screen):
if len(self.projectiles) < self.fire_power:
projectile = Projectile()
projectile.add(self.projectiles)
projectile.fire(screen, self.dino_rect)
def toggle_lock(self):
self.can_fire = not self.can_fire
def main_loop():
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("Dinos")
pygame.key.set_repeat(20, 30)
game = Game()
while True:
pygame.time.wait(20)
# Input handlers
keys_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if keys_pressed[pygame.K_ESCAPE] or keys_pressed[pygame.K_q]:
game.shutdown(screen)
if keys_pressed[pygame.K_RIGHT]:
game.dino.move(screen, 'right')
game.drop2.play()
if keys_pressed[pygame.K_LEFT]:
game.dino.move(screen, 'left')
game.drop1.play()
if keys_pressed[pygame.K_SPACE]:
if game.dino.can_fire:
game.dino.fire(screen)
game.dino.toggle_lock()
if not keys_pressed[pygame.K_SPACE]:
game.dino.toggle_lock()
# Loop operations
game.draw_background(screen)
game.draw_score(screen)
game.draw_lives(screen)
game.dino.draw_dino(screen)
if len(game.level.foes) == 0:
pygame.display.update()
Foe.foe_projectiles.empty()
game.dino.projectiles.empty()
pygame.time.wait(1000)
game.next_level(screen)
game.dino.num_lives_left += 1
for foe in game.level.foes:
foe.move(screen)
foe.fire(screen)
if foe.rect.colliderect(game.dino.dino_rect):
game.over_sound.play()
game.over(screen)
game.level.foes.draw(screen)
for projectile in game.dino.projectiles:
projectile.move(screen)
for foe in game.level.foes:
if foe.rect.colliderect(projectile.rect):
game.boom_sound.play()
game.score += 10
foe.kill()
projectile.kill()
shrinked_rect = game.dino.dino_rect.inflate(-40, -40)
for projectile in Foe.foe_projectiles:
projectile.move(screen)
if shrinked_rect.colliderect(projectile.rect):
game.boom_sound.play()
projectile.kill()
if game.dino.num_lives_left > 0:
game.dino.num_lives_left-= 1
game.wound_sound1.play()
game.wound_sound2.play()
pygame.time.wait(300)
else:
game.over_sound.play()
pygame.time.wait(1000)
game.over(screen)
pygame.display.update()
if __name__ == '__main__':
main_loop()