-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatformer_example.py
61 lines (44 loc) · 2.36 KB
/
platformer_example.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
from ursina import *
app = Ursina()
from ursina.prefabs.platformer_controller_2d import PlatformerController2d
player = PlatformerController2d(y=1, z=.01, scale_y=1, max_jumps=2)
ground = Entity(model='quad', scale_x=10, collider='box', color=color.black)
quad = load_model('quad', use_deepcopy=True)
level_parent = Entity(model=Mesh(vertices=[], uvs=[]), texture='white_cube')
def make_level(texture):
# destroy every child of the level parent.
# This doesn't do anything the first time the level is generated, but if we want to update it several times
# this will ensure it doesn't just create a bunch of overlapping entities.
[destroy(c) for c in level_parent.children]
for y in range(texture.height):
collider = None
for x in range(texture.width):
col = texture.get_pixel(x,y)
# If it's black, it's solid, so we'll place a tile there.
if col == color.black:
level_parent.model.vertices += [Vec3(*e) + Vec3(x+.5,y+.5,0) for e in quad.generated_vertices] # copy the quad model, but offset it with Vec3(x+.5,y+.5,0)
level_parent.model.uvs += quad.uvs
# Entity(parent=level_parent, position=(x,y), model='cube', origin=(-.5,-.5), color=color.gray, texture='white_cube', visible=True)
if not collider:
collider = Entity(parent=level_parent, position=(x,y), model='quad', origin=(-.5,-.5), collider='box', visible=False)
else:
# instead of creating a new collider per tile, stretch the previous collider right.
collider.scale_x += 1
else:
collider = None
# If it's green, we'll place the player there. Store this in player.start_position so we can reset the plater position later.
if col == color.green:
player.start_position = (x, y)
player.position = player.start_position
level_parent.model.generate()
make_level(load_texture('platformer_tutorial_level')) # generate the level
camera.orthographic = True
camera.position = (30/2,8)
camera.fov = 16
player.traverse_target = level_parent
enemy = Entity(model='cube', collider='box', color=color.red, position=(16,5,-.1))
def update():
if player.intersects(enemy).hit:
print('die')
player.position = player.start_position
app.run()