-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
369 lines (332 loc) · 12.2 KB
/
main.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
import pyxel
import time
from enum import Enum
import random
presents = []
enemys = []
hearts = []
class Direction(Enum):
NORTH = 1
NORTH_EAST = 2
EAST = 3
SOUTH_EAST = 4
SOUTH = 5
SOUTH_WEST = 6
WEST = 7
NORTH_WEST = 8
class PhysicsObject:
def __init__(self, x, y, width=8, height=8) -> None:
self.x = x
self.y = y
self.width = width
self.height = height
self.move_speed_x = 1
self.move_speed_y = 1
self.velocity_x = 0
self.velocity_y = 0
def draw(self) -> None:
pass
def update(self) -> None:
pass
def collides_with_walls(self):
if self.x >= pyxel.width - self.width:
self.x -= 1
elif self.x <= 0:
self.x += 1
elif self.y >= pyxel.height - self.height:
self.y -= 1
elif self.y <= 0:
self.y += 1
else:
return False
return True
class Arrow(PhysicsObject):
def __init__(self, x, y, direction, width=8, height=8) -> None:
super().__init__(x, y, width, height)
self.direction = direction
if direction == Direction.NORTH:
self.image_x = 0
self.image_y = 16
self.y -= self.height
elif direction == Direction.NORTH_EAST:
self.image_x = 16
self.image_y = 16
self.x += self.width
self.y -= self.height
elif direction == Direction.EAST:
self.image_x = 8
self.image_y = 16
self.x += self.width
elif direction == Direction.SOUTH_EAST:
self.image_x = 24
self.image_y = 16
self.x += self.width
self.y += self.height
elif direction == Direction.SOUTH:
self.image_x = 0
self.image_y = 24
self.y += self.height
elif direction == Direction.SOUTH_WEST:
self.image_x = 16
self.image_y = 24
self.x -= self.width
self.y += self.height
elif direction == Direction.WEST:
self.image_x = 8
self.image_y = 24
self.x -= self.width
elif direction == Direction.NORTH_WEST:
self.image_x = 24
self.image_y = 24
self.x -= self.width
self.y -= self.height
def draw(self) -> None:
pyxel.blt(self.x, self.y, 0, self.image_x,
self.image_y, self.width, self.height)
class Heart(PhysicsObject):
def __init__(self, x, y, width=8, height=8) -> None:
super().__init__(x, y, width, height)
self.image_x = 32
self.image_y = 16
def draw(self) -> None:
pyxel.blt(self.x, self.y, 0, self.image_x,
self.image_y, self.width, self.height)
class Santa(PhysicsObject):
def __init__(self, x, y, width=6, height=8) -> None:
global presents
super().__init__(x, y, width, height)
self.image_x = 8
self.image_y = 8
self.direction = Direction.EAST
# presents setup
self.presents = presents
self.cooldown = 1
self.present_speed = 2
self.time = time.time()
self.time_delta = 0
def draw(self) -> None:
Arrow(self.x, self.y, self.direction).draw()
pyxel.blt(self.x, self.y, 0, self.image_x,
self.image_y, self.width, self.height)
def move(self):
if (
(pyxel.btn(pyxel.KEY_UP) and pyxel.btn(pyxel.KEY_RIGHT))
or (pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_UP) and pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_RIGHT))):
self.y -= self.move_speed_y
self.x += self.move_speed_x
self.direction = Direction.NORTH_EAST
elif (
(pyxel.btn(pyxel.KEY_DOWN) and pyxel.btn(pyxel.KEY_RIGHT)
or (pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_DOWN) and pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_RIGHT)))):
self.y += self.move_speed_y
self.x += self.move_speed_x
self.direction = Direction.SOUTH_EAST
elif (
(pyxel.btn(pyxel.KEY_UP) and pyxel.btn(pyxel.KEY_LEFT))
or (pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_UP) and pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_LEFT))):
self.y -= self.move_speed_y
self.x -= self.move_speed_x
self.direction = Direction.NORTH_WEST
elif (
(pyxel.btn(pyxel.KEY_DOWN) and pyxel.btn(pyxel.KEY_LEFT))
or (pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_DOWN) and pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_LEFT))):
self.y += self.move_speed_y
self.x -= self.move_speed_x
self.direction = Direction.SOUTH_WEST
elif pyxel.btn(pyxel.KEY_UP) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_UP):
self.y -= self.move_speed_y
self.direction = Direction.NORTH
elif pyxel.btn(pyxel.KEY_DOWN) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_DOWN):
self.y += self.move_speed_y
self.direction = Direction.SOUTH
elif pyxel.btn(pyxel.KEY_LEFT) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_LEFT):
self.x -= self.move_speed_x
self.width = -abs(self.width)
self.direction = Direction.WEST
elif pyxel.btn(pyxel.KEY_RIGHT) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_RIGHT):
self.x += self.move_speed_x
self.width = abs(self.width)
self.direction = Direction.EAST
if self.direction == Direction.NORTH_EAST or self.direction == Direction.SOUTH_EAST:
self.width = abs(self.width)
elif self.direction == Direction.NORTH_WEST or self.direction == Direction.SOUTH_WEST:
self.width = -abs(self.width)
def spawn_present(self):
present = Present(self.x, self.y)
if self.direction == Direction.NORTH:
present.velocity_y = -self.present_speed
elif self.direction == Direction.NORTH_EAST:
present.velocity_y = -self.present_speed
present.velocity_x = self.present_speed
elif self.direction == Direction.EAST:
present.velocity_x = self.present_speed
elif self.direction == Direction.SOUTH_EAST:
present.velocity_y = self.present_speed
present.velocity_x = self.present_speed
elif self.direction == Direction.SOUTH:
present.velocity_y = self.present_speed
elif self.direction == Direction.SOUTH_WEST:
present.velocity_y = self.present_speed
present.velocity_x = -self.present_speed
elif self.direction == Direction.WEST:
present.velocity_x = -self.present_speed
elif self.direction == Direction.NORTH_WEST:
present.velocity_y = -self.present_speed
present.velocity_x = -self.present_speed
self.presents.append(present)
def update(self) -> None:
# update time
old_time = self.time
self.time = time.time()
self.time_delta += self.time - old_time
# check collisions
if self.collides_with_walls():
return
self.move()
if self.time_delta > self.cooldown-0.8:
self.image_x = 8
self.image_y = 8
if pyxel.btn(pyxel.KEY_SPACE) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_A):
if self.time_delta > self.cooldown:
self.image_x = 0
self.image_y = 8
self.spawn_present()
self.time_delta = 0
class Present(PhysicsObject):
def __init__(self, x, y, width=8, height=8) -> None:
global presents
self.presents = presents
self.image_x = 24
self.image_y = 8
super().__init__(x, y, width, height)
def draw(self) -> None:
# (x, y, image, x in image, y in image, height, width)
pyxel.blt(self.x, self.y, 0, self.image_x,
self.image_y, self.width, self.height)
def update(self) -> None:
if self.collides_with_walls():
self.presents.remove(self)
return
self.x += self.velocity_x
self.y += self.velocity_y
class Enemy(PhysicsObject):
def __init__(self, x, y, velocity_x, width=8, height=8) -> None:
global presents, enemys, hearts
self.presents = presents
self.enemys = enemys
self.hearts = hearts
self.image_x = 32
self.image_y = 8
self.dying = False
super().__init__(x, y, width, height)
self.velocity_x = velocity_x
self.time = None
self.time_delta = 0
def draw(self) -> None:
if self.dying:
if self.time is None:
self.velocity_x = 0
self.time = time.time()
self.image_x += 8
old_time = self.time
self.time = time.time()
self.time_delta += self.time - old_time
if self.time_delta > 0.2:
self.image_x -= 8
self.image_y -= 8
if self.time_delta > 0.5:
self.image_x += 8
if self.time_delta > 0.8:
print("remove")
self.enemys.remove(self)
return
pyxel.blt(self.x, self.y, 0, self.image_x,
self.image_y, self.width, self.height)
def collides_with_present(self):
for present in self.presents:
if (self.x < present.x + present.width
and self.x + self.width > present.x
and self.y < present.y + present.height
and self.y + self.height > present.y):
return True
return False
def reached_end(self):
if self.x <= 0:
return True
return False
def update(self) -> None:
if self.reached_end():
del self.hearts[-1]
self.enemys.remove(self)
if self.collides_with_present():
self.dying = True
pyxel.play(0, 1)
return
self.x += self.velocity_x
self.y += self.velocity_y
class App:
def __init__(self) -> None:
global presents, enemys, hearts
pyxel.init(128, 128, fps=60) # , display_scale=8)
pyxel.load("assets/my_resource.pyxres")
pyxel.pal(0, 9)
self.santa = Santa(32, 32)
self.presents = presents
self.enemys = enemys
self.hearts = hearts
# game speed
self.game_speed = 1 / 1000
self.time = time.time()
self.time_delta = 0
self.level = 0
self.create_hearts()
pyxel.run(self.update, self.draw)
def update(self) -> None:
old_time = self.time
self.time = time.time()
self.time_delta += self.time - old_time
if len(self.enemys) == 0:
self.level += 1
self.presents.clear()
self.create_enemys()
if self.time_delta > self.game_speed:
self.santa.update()
present: Present
for present in self.presents:
present.update()
enemy: Enemy
for enemy in self.enemys:
enemy.update()
self.time_delta = 0
def create_enemys(self):
for _ in range(0, self.level):
random_x = random.randrange(int(pyxel.width*0.6), pyxel.width, 8)
random_y = random.randrange(0, pyxel.height, 8)
self.enemys.append(Enemy(random_x, random_y, -0.3))
def create_hearts(self):
position_y = pyxel.height - 9
position_x = 1
for _ in range(0, 3):
self.hearts.append(Heart(position_x, position_y))
position_x += 9
def draw(self) -> None:
if len(self.hearts) == 0:
pyxel.play(0, 0)
pyxel.cls(0)
pyxel.text(pyxel.width//2.75, pyxel.height//2.5, "GAME OVER", 7)
pyxel.text(pyxel.width//2.5, pyxel.height//2, f"LEVEL {self.level}", 7)
pyxel.show()
pyxel.cls(0)
pyxel.bltm(0, 0, 0, 0, 0, pyxel.width, pyxel.height)
self.santa.draw()
pyxel.text(1, 1, f"Level {self.level}", 7)
present: Present
for present in self.presents:
present.draw()
enemy: Enemy
for enemy in self.enemys:
enemy.draw()
heart: Heart
for heart in self.hearts:
heart.draw()
app = App()