-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_items.py
executable file
·337 lines (249 loc) · 9.52 KB
/
level_items.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
import math
import simplegui
from typing import TYPE_CHECKING, Tuple
from constants import PLAYER_SIZE, PLAYER_VELOCITY, PLAYER_DEATH_VELOCITY, PLAYER_RESPAWN_X_OFFSET, \
ACCEL_GRAVITY, BLOCK_SIZE, GRID_SIZE, WINDOW_SIZE, Key, PLAYER_POTATO
from sprite import Sprite
from util import Color
from geom import Vector, BoundingBox
from window import Renderable
# Work around cyclic imports.
if TYPE_CHECKING:
from world import World
__all__ = ['LevelItem', 'Rect', 'Trap', 'Finish', 'Platform', 'Player']
class LevelItem(Renderable):
def __init__(self, world: 'World'):
super().__init__(world.window)
self.world = world
def get_bounds(self) -> BoundingBox:
raise NotImplementedError
def on_collide(self, player: 'Player'):
pass
def collides_with(self, box: BoundingBox) -> bool:
return self.get_bounds().collides(box)
class Rect(LevelItem):
"""
A generic rectangle to be drawn on the canvas.
*Should be extended, not created directly.*
"""
def __init__(self, world: 'World'):
super().__init__(world)
def get_pos(self) -> Tuple[int, int]:
raise NotImplementedError
def get_size(self) -> Tuple[int, int]:
return 1, 1
def get_border_width(self) -> int:
"""
Returns the border width.
"""
return 1
def get_border_color(self) -> Color:
"""
Returns the border color of the rectangle.
"""
return Color(200, 200, 200)
def get_fill_color(self) -> Color:
"""
Returns the fill color of the rectangle.
"""
return Color(0, 0, 0)
def get_bounds(self) -> BoundingBox:
pos = self.get_pos()
size = self.get_size()
pos = Vector(
pos[0] * BLOCK_SIZE,
(GRID_SIZE[1] - pos[1] - 1) * BLOCK_SIZE,
)
size = Vector(
size[0] * BLOCK_SIZE,
size[1] * BLOCK_SIZE,
)
pos = pos - self.world.level.offset
size = size
return BoundingBox(pos, pos + size)
def get_render_bounds(self) -> BoundingBox:
bounds = self.get_bounds()
dpi_factor = self.window.hidpi_factor
return BoundingBox(bounds.min * dpi_factor, bounds.max * dpi_factor)
def render(self, canvas: simplegui.Canvas):
rbounds = self.get_render_bounds()
point_list = rbounds.into_point_list()
border_width = self.get_border_width()
border_color = str(self.get_border_color())
fill_color = str(self.get_fill_color())
canvas.draw_polygon(point_list, border_width, border_color, fill_color)
class Trap(Rect):
def __init__(self, world: 'World', pos: Tuple[int, int], size: Tuple[int, int]):
super().__init__(world)
self.pos = pos
self.size = size
self.color = Color(200, 80, 80)
def get_pos(self) -> Tuple[int, int]:
return self.pos
def get_size(self) -> Tuple[int, int]:
return self.size
def get_border_color(self) -> Color:
return self.color
def get_fill_color(self) -> Color:
return self.color
def on_collide(self, player: 'Player'):
# Perform a "hopping off" animation and disable collision
player.on_ground = False
player.is_dying = True
player.vel = Vector(math.copysign(PLAYER_DEATH_VELOCITY[0], player.vel.x),
-PLAYER_DEATH_VELOCITY[1])
class Finish(Rect):
def __init__(self, world: 'World', pos: Tuple[int, int], size: Tuple[int, int]):
super().__init__(world)
self.pos = pos
self.size = size
self.color = Color(0, 102, 255)
def get_pos(self) -> Tuple[int, int]:
return self.pos
def get_size(self) -> Tuple[int, int]:
return self.size
def get_border_color(self) -> Color:
return self.color
def get_fill_color(self) -> Color:
return self.color
def on_collide(self, player: 'Player'):
self.world.level.finish()
class Platform(Rect):
def __init__(self, world: 'World', pos: Tuple[int, int], size: Tuple[int, int]):
super().__init__(world)
self.pos = pos
self.size = size
self.color = Color(80, 80, 80)
def get_pos(self) -> Tuple[int, int]:
return self.pos
def get_size(self) -> Tuple[int, int]:
return self.size
def get_border_color(self) -> Color:
return self.color
def get_fill_color(self) -> Color:
return self.color
def on_collide(self, player: 'Player'):
bounds = self.get_bounds()
pbounds = player.get_bounds()
if bounds.min.y < pbounds.max.y or bounds.max.y < pbounds.min.y:
if pbounds.min.x <= bounds.min.x <= pbounds.max.x <= bounds.max.x:
# left
player.pos.x = bounds.min.x - player.size.x
elif bounds.min.x <= pbounds.min.x <= bounds.max.x <= pbounds.max.x:
# right
player.pos.x = bounds.max.x
pbounds = player.get_bounds()
if bounds.min.x < pbounds.max.x or bounds.max.x < pbounds.min.x:
if pbounds.min.y <= bounds.min.y <= pbounds.max.y <= bounds.max.y:
# top
player.pos.y = bounds.min.y - player.size.y
player.on_ground = True
player.desired_platform = self
elif bounds.min.y <= pbounds.min.y <= bounds.max.y <= pbounds.max.y:
# bottom
player.pos.y = bounds.max.y
class Player(Renderable):
def __init__(self, world: 'World'):
super().__init__(world.window)
self.world = world
self.size = Vector(*PLAYER_SIZE)
self.pos = world.level.start_pos
self.last_pos = self.pos.copy()
self.last_x = 0
self.vel = Vector(0, 0)
self.accel = Vector(0, 0)
self.on_ground = False
self.jumping = False
self.moving_x = False
self.is_dying = False
self.desired_platform = None
self.score = 0
self.lives = 3
self.sprite_cols = 8
self.sprite = Sprite('assets/player.png', self.sprite_cols, 1)
self.roll = 0
def jump(self):
self.on_ground = False
self.vel.y = -PLAYER_VELOCITY[1]
self.accel.y = -ACCEL_GRAVITY
def get_bounds(self) -> BoundingBox:
pos = self.pos
size = self.size
return BoundingBox(pos, pos + size)
def get_render_bounds(self) -> BoundingBox:
bounds = self.get_bounds()
dpi_factor = self.window.hidpi_factor
return BoundingBox(bounds.min * dpi_factor, bounds.max * dpi_factor)
def on_key_down(self, key: int):
if not self.is_dying:
if key == Key.SPACE:
self.jumping = True # Allow holding jump button.
if self.on_ground:
self.jump()
elif key == Key.KEY_A:
self.vel.x = -PLAYER_VELOCITY[0]
elif key == Key.KEY_D:
self.vel.x = PLAYER_VELOCITY[0]
def on_key_up(self, key: int):
if not self.is_dying:
if key == Key.SPACE:
self.jumping = False
elif key == Key.KEY_A:
if self.vel.x < 0:
self.vel.x = 0
elif key == Key.KEY_D:
if self.vel.x > 0:
self.vel.x = 0
def on_death(self):
self.lives -= 1
if self.lives == 0:
self.world.window.handler = self.world.source
else:
self.vel.x = 0
self.vel.y = 0
if self.desired_platform is None:
self.pos = Vector(PLAYER_RESPAWN_X_OFFSET, -self.size.y)
else:
bounds = self.desired_platform.get_bounds()
if bounds.max.x <= 0:
self.pos = Vector(PLAYER_RESPAWN_X_OFFSET, -self.size.y)
else:
# Place player on platform they last touched
self.pos = Vector(bounds.max.x - self.size.x - PLAYER_RESPAWN_X_OFFSET,
bounds.min.y - self.size.y)
self.is_dying = False
def render(self, canvas: simplegui.Canvas):
bounds = self.get_bounds()
dpi_factor = self.window.hidpi_factor
# Draw player.
if PLAYER_POTATO:
dest_center = self.pos + self.size / 2
index = (self.roll // self.sprite_cols) % self.sprite_cols
self.sprite.draw(canvas, dest_center * dpi_factor, self.size * dpi_factor, (index, 0))
else:
point_list = [p.multiply(dpi_factor).into_tuple() for p in bounds]
color = Color(120, 120, 200)
canvas.draw_polygon(point_list, 1, str(color), str(color))
# Update position.
self.last_pos = self.pos.copy()
self.pos.add(self.vel)
self.roll += self.vel.x * 2 / PLAYER_VELOCITY[0]
self.roll += 1
if abs(self.vel.x) > PLAYER_VELOCITY[0]:
self.vel.x = math.copysign(PLAYER_VELOCITY[0], self.vel.x)
# Check collisions position.
self.on_ground = False
bounds = self.get_bounds()
if not self.is_dying:
for item in self.world.level.items:
if item.collides_with(bounds):
item.on_collide(self)
self.vel.add(self.accel)
# Do gravity and platform collision.
if self.on_ground:
self.vel.y = 0
self.accel.y = 0
else:
self.accel.y = -ACCEL_GRAVITY
if bounds.max.y >= WINDOW_SIZE[1] or bounds.min.x <= 0:
self.on_death()