Skip to content

Commit 5575529

Browse files
committed
improving gameplay
1 parent a47d997 commit 5575529

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

src/game/game.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ def has_collision(self, obj):
3535

3636
class Explosion(Obj):
3737

38-
def __init__(self, cords):
38+
def __init__(self, cords, score=None):
3939
super().__init__(cords)
40-
self.time = 5
40+
self.time = 15
41+
self.score = score
4142

4243
def update(self):
4344
self.time -= 1
@@ -47,8 +48,12 @@ def is_alive(self):
4748

4849
def draw(self):
4950
x, y = self.cords
50-
r = self.time / 5 * 20
51-
pyxel.circ(x, y, r, 7)
51+
if self.score:
52+
pyxel.text(x, y, str(self.score), 10)
53+
54+
r = (self.time - 10) / 5 * 20
55+
if r > 0:
56+
pyxel.circ(x, y, r, 7)
5257

5358

5459
class Bullet(Obj):
@@ -88,10 +93,11 @@ class Ship(Obj):
8893
'r': 3,
8994
}
9095

91-
def __init__(self, cords, rotate, img_index):
96+
def __init__(self, cords, rotate, speed, img_index):
9297
super().__init__(cords)
9398
self.rotate = rotate
9499
self.img_index = img_index
100+
self.speed = speed
95101

96102
def draw(self):
97103
w = self.WIDTH_SPRITE
@@ -103,33 +109,34 @@ def draw(self):
103109
pyxel.blt(x, y, 0, u, v, w, h, 11)
104110

105111
def move_up(self):
106-
self.cords[1] -= 5
112+
self.cords[1] -= self.speed
107113
self.rotate = 'u'
108114

109115
def move_down(self):
110-
self.cords[1] += 5
116+
self.cords[1] += self.speed
111117
self.rotate = 'd'
112118

113119
def move_left(self):
114-
self.cords[0] -= 5
120+
self.cords[0] -= self.speed
115121
self.rotate = 'l'
116122

117123
def move_right(self):
118-
self.cords[0] += 5
124+
self.cords[0] += self.speed
119125
self.rotate = 'r'
120126

121127

122128
class Player(Ship):
123129

124130
def __init__(self, cords):
125-
super().__init__(cords=cords, rotate='u', img_index=0)
131+
super().__init__(cords=cords, rotate='u', speed=5, img_index=0)
126132
self.score = 0
127133

128134

129135
class EnemyShip(Ship):
130136

131-
def __init__(self, cords, enemy_type=0):
132-
super().__init__(cords=cords, rotate='u', img_index=3 + enemy_type)
137+
def __init__(self, cords, enemy_type=0, speed_mult=1):
138+
speed = randint(3, 6) * speed_mult
139+
super().__init__(cords=cords, rotate='u', speed=speed, img_index=3 + enemy_type)
133140

134141
def update(self):
135142
self.move_up()
@@ -163,6 +170,7 @@ def setup(self):
163170
self.setup_world()
164171

165172
def setup_world(self):
173+
self.time = 0
166174
self.game_over = False
167175
self.player = Player([128, 128])
168176
self.bullets = []
@@ -171,6 +179,8 @@ def setup_world(self):
171179
pyxel.play(0, 3)
172180

173181
def update(self):
182+
if not self.game_over and pyxel.frame_count % 10 == 0:
183+
self.time += 1
174184
self.keyboard()
175185
self.bullets = self.update_objs(self.bullets)
176186
self.enemies = self.update_objs(self.enemies)
@@ -193,8 +203,9 @@ def update_collision(self):
193203
if bullet.has_collision(enemy) or enemy.has_collision(bullet):
194204
bullet.is_destroyed = True
195205
enemy.is_destroyed = True
196-
self.player.score += 100
197-
self.animations.append(Explosion(enemy.cords))
206+
score = 100 + self.time * enemy.speed // 50 + enemy.cords[1]
207+
self.player.score += score
208+
self.animations.append(Explosion(enemy.cords, score))
198209
pyxel.play(0, 1)
199210

200211
if self.game_over:
@@ -210,10 +221,12 @@ def update_collision(self):
210221

211222

212223
def spawn_enemies(self):
213-
if pyxel.frame_count % 50 == 20:
224+
spawn_rate = max(50 - self.time // 3, 10)
225+
if pyxel.frame_count % spawn_rate == 5:
214226
x = randint(20, 200)
215227
y = 250
216228
enemy_type = randint(0, 4)
229+
speed_mult = 1 + min(self.time / 50, 3)
217230
enemy = EnemyShip([x, y], enemy_type)
218231
self.enemies.append(enemy)
219232

@@ -252,6 +265,7 @@ def draw(self):
252265
pyxel.text(80, 70, "Press R / START to restart", 8)
253266
else:
254267
pyxel.text(0, 0, "Score: %d" % self.player.score, 7)
268+
pyxel.text(0, 8, "Time: %d" % self.time, 7)
255269
self.player.draw()
256270

257271
self.draw_objs(self.bullets)

0 commit comments

Comments
 (0)