-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
192 lines (163 loc) · 5.86 KB
/
engine.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
import pygame as pg
from pygame.math import Vector2 as vect2d
from laylib import DefaultEngine
from random import randint
from settings import *
class FlappyBird(DefaultEngine):
"""main engine example """
def __init__(self):
super().__init__()
self.all_sprites = pg.sprite.Group()
self.pipe = []
self.ground = []
self.bird = None
self.score = 0
def new_demo(self):
""" remove old sprites if necessary """
if self.all_sprites:
self.all_sprites.empty()
# create two pipes: 2 x (top+bottom)
self.pipe.append(self.new_pipes(950))
self.pipe.append(self.new_pipes(1400))
self.bird = Bird(self.img[0:3], self.snd[FX_WING])
for i in range(2):
self.ground.append(Ground(self.img[GROUND_IMG], GRND_POS[i]))
# set the sprites
self.all_sprites.add(Background(self.img[BACKGRND_IMG]))
self.all_sprites.add(self.pipe)
self.all_sprites.add(self.ground)
self.all_sprites.add(self.bird)
# play music
pg.mixer.music.play()
self.playing = True
def new_pipes(self, pos_x):
pipe = []
pipe.append(Pipe(self.img[PIPE_DOWN_IMG]))
pipe.append(Pipe(self.img[PIPE_UP_IMG]))
self.reset_pipe(pipe, pos_x)
return pipe
def draw(self):
self.all_sprites.draw(self.screen)
self.res.fnt.render(self.fnt[SKETCH], str(self.score), FONT_POS)
pg.display.flip()
def update(self):
if not self.playing and self.running:
self.new_demo()
# recycle the pipes
if self.pipe[0][DOWN].x < -50:
self.reset_pipe(self.pipe[0], PIPE_X)
if self.pipe[1][DOWN].x < -50:
self.reset_pipe(self.pipe[1], PIPE_X)
# see if the bird hurt any pipe
if self.collide(self.bird, self.pipe[0]):
self.game_over()
# when the bird hit the ground
if self.collide(self.bird, self.ground):
self.bird.vel = vect2d(0.0, 0.0)
self.bird.acc = vect2d(0.0, 0.0)
self.game_over()
self.check_score(self.bird, self.pipe)
self.all_sprites.update(self.dt)
def game_over(self):
pg.mixer.music.stop()
if self.bird.alive:
self.snd[FX_HIT].play()
self.bird.alive = False
# stop the scrolling
for i in range(2):
self.ground[i].vel = vect2d(0.0, 0.0)
self.pipe[i][0].vel = vect2d(0.0, 0.0)
self.pipe[i][1].vel = vect2d(0.0, 0.0)
def check_score(self, bird, pipe):
for i in range(2):
if bird.rect.left >= pipe[i][0].rect.left \
and bird.rect.right <= pipe[i][0].rect.right:
if bird.alive and pipe[i][0].new_pipe:
self.score += 1
self.snd[FX_WIN].play()
pipe[i][0].new_pipe = False
@staticmethod
def reset_pipe(pipe, pos_x):
pipe[0].pos = vect2d(pos_x, randint(*PIPE_POS))
pipe[1].pos = vect2d(pos_x, pipe[0].y + PIPES_DIST)
pipe[0].new_pipe = True
pipe[1].new_pipe = True
@staticmethod
def collide(body1, body2, k1=False, k2=False):
""" test groupe sprites collision"""
return pg.sprite.spritecollide(body1, body2, k1, k2)
class BasicBody(pg.sprite.Sprite):
def __init__(self, image, pos=(0.0, 0.0)):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.pos = vect2d(pos)
self.rect.center = vect2d(pos)
class Background(BasicBody):
def __init__(self, image, pos=vect2d(BKGRND_POS)):
super().__init__(image, pos)
class Ground(BasicBody):
def __init__(self, image, pos):
super().__init__(image, pos)
self.vel = vect2d(GRND_VEL, 0.0)
def update(self, dt):
if self.pos.x < 0:
self.pos.x = GRND_START_POS
self.pos.x -= self.vel.x * dt
self.rect.center = self.pos
class Pipe(BasicBody):
def __init__(self, image, pos=(0.0, 0.0)):
super().__init__(image, pos)
self.vel = vect2d(PIPE_VEL, 0.0)
self.new_pipe = False
def update(self, dt):
self.pos.x -= self.vel.x * dt
self.rect.center = self.pos
@property
def x(self):
return self.pos.x
@property
def y(self):
return self.pos.y
@x.setter
def x(self, value):
self.rect.x = value
@y.setter
def y(self, value):
self.rect.y = value
class Bird(pg.sprite.Sprite):
def __init__(self, image, fx):
super().__init__()
self.frame = 0
self.image_group = image
self.image = image[0]
self.pos = vect2d(WIDTH / 2.0, HEIGHT / 2.0)
self.rect = self.image.get_rect()
self.rect.center = vect2d(self.pos)
self.vel = vect2d(0.0, 0.0)
self.acc = vect2d(0.0, BIRD_GRAVITY)
self.last_update = pg.time.get_ticks()
self.frame_rate = 90
self.alive = True
self.wing = fx
def update(self, dt):
# fly and fall
self.vel += self.acc
self.pos += (self.vel + 0.5 * self.acc) * dt
self.rect.center = self.pos
# anim sprites
if self.alive:
self.image = self.image_group[self.frame]
now = pg.time.get_ticks()
if now - self.last_update > self.frame_rate:
self.last_update = now
self.frame += 1
self.frame %= 3
if self.frame == 2:
self.wing.play()
# keys
keys = pg.key.get_pressed()
if keys[pg.K_SPACE]:
self.fly()
def fly(self):
self.vel.y = FLY_VEL