@@ -35,9 +35,10 @@ def has_collision(self, obj):
35
35
36
36
class Explosion (Obj ):
37
37
38
- def __init__ (self , cords ):
38
+ def __init__ (self , cords , score = None ):
39
39
super ().__init__ (cords )
40
- self .time = 5
40
+ self .time = 15
41
+ self .score = score
41
42
42
43
def update (self ):
43
44
self .time -= 1
@@ -47,8 +48,12 @@ def is_alive(self):
47
48
48
49
def draw (self ):
49
50
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 )
52
57
53
58
54
59
class Bullet (Obj ):
@@ -88,10 +93,11 @@ class Ship(Obj):
88
93
'r' : 3 ,
89
94
}
90
95
91
- def __init__ (self , cords , rotate , img_index ):
96
+ def __init__ (self , cords , rotate , speed , img_index ):
92
97
super ().__init__ (cords )
93
98
self .rotate = rotate
94
99
self .img_index = img_index
100
+ self .speed = speed
95
101
96
102
def draw (self ):
97
103
w = self .WIDTH_SPRITE
@@ -103,33 +109,34 @@ def draw(self):
103
109
pyxel .blt (x , y , 0 , u , v , w , h , 11 )
104
110
105
111
def move_up (self ):
106
- self .cords [1 ] -= 5
112
+ self .cords [1 ] -= self . speed
107
113
self .rotate = 'u'
108
114
109
115
def move_down (self ):
110
- self .cords [1 ] += 5
116
+ self .cords [1 ] += self . speed
111
117
self .rotate = 'd'
112
118
113
119
def move_left (self ):
114
- self .cords [0 ] -= 5
120
+ self .cords [0 ] -= self . speed
115
121
self .rotate = 'l'
116
122
117
123
def move_right (self ):
118
- self .cords [0 ] += 5
124
+ self .cords [0 ] += self . speed
119
125
self .rotate = 'r'
120
126
121
127
122
128
class Player (Ship ):
123
129
124
130
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 )
126
132
self .score = 0
127
133
128
134
129
135
class EnemyShip (Ship ):
130
136
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 )
133
140
134
141
def update (self ):
135
142
self .move_up ()
@@ -163,6 +170,7 @@ def setup(self):
163
170
self .setup_world ()
164
171
165
172
def setup_world (self ):
173
+ self .time = 0
166
174
self .game_over = False
167
175
self .player = Player ([128 , 128 ])
168
176
self .bullets = []
@@ -171,6 +179,8 @@ def setup_world(self):
171
179
pyxel .play (0 , 3 )
172
180
173
181
def update (self ):
182
+ if not self .game_over and pyxel .frame_count % 10 == 0 :
183
+ self .time += 1
174
184
self .keyboard ()
175
185
self .bullets = self .update_objs (self .bullets )
176
186
self .enemies = self .update_objs (self .enemies )
@@ -193,8 +203,9 @@ def update_collision(self):
193
203
if bullet .has_collision (enemy ) or enemy .has_collision (bullet ):
194
204
bullet .is_destroyed = True
195
205
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 ))
198
209
pyxel .play (0 , 1 )
199
210
200
211
if self .game_over :
@@ -210,10 +221,12 @@ def update_collision(self):
210
221
211
222
212
223
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 :
214
226
x = randint (20 , 200 )
215
227
y = 250
216
228
enemy_type = randint (0 , 4 )
229
+ speed_mult = 1 + min (self .time / 50 , 3 )
217
230
enemy = EnemyShip ([x , y ], enemy_type )
218
231
self .enemies .append (enemy )
219
232
@@ -252,6 +265,7 @@ def draw(self):
252
265
pyxel .text (80 , 70 , "Press R / START to restart" , 8 )
253
266
else :
254
267
pyxel .text (0 , 0 , "Score: %d" % self .player .score , 7 )
268
+ pyxel .text (0 , 8 , "Time: %d" % self .time , 7 )
255
269
self .player .draw ()
256
270
257
271
self .draw_objs (self .bullets )
0 commit comments