-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathpyglet_example.py
116 lines (95 loc) · 4.27 KB
/
pyglet_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
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
import pyglet
import esper
FPS = 60
RESOLUTION = 720, 480
##################################
# Define some Components:
##################################
class Velocity:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
class Renderable:
def __init__(self, sprite):
self.sprite = sprite
self.w = sprite.width
self.h = sprite.height
################################
# Define some Processors:
################################
class MovementProcessor:
def __init__(self, minx, maxx, miny, maxy):
super().__init__()
self.minx = minx
self.miny = miny
self.maxx = maxx
self.maxy = maxy
def process(self, dt):
# This will iterate over every Entity that has BOTH of these components:
for ent, (vel, rend) in esper.get_components(Velocity, Renderable):
# Update the Renderable Component's position by its Velocity:
# An example of keeping the sprite inside screen boundaries. Basically,
# adjust the position back inside screen boundaries if it is outside:
new_x = max(self.minx, rend.sprite.x + vel.x)
new_y = max(self.miny, rend.sprite.y + vel.y)
new_x = min(self.maxx - rend.w, new_x)
new_y = min(self.maxy - rend.h, new_y)
rend.sprite.position = new_x, new_y, rend.sprite.z
###############################################
# Initialize pyglet window and graphics batch:
###############################################
window = pyglet.window.Window(width=RESOLUTION[0],
height=RESOLUTION[1],
caption="Esper pyglet example")
batch = pyglet.graphics.Batch()
# Initialize Esper world, and create a "player" Entity with a few Components:
player = esper.create_entity()
esper.add_component(player, Velocity(x=0, y=0))
player_image = pyglet.resource.image("redsquare.png")
esper.add_component(player, Renderable(sprite=pyglet.sprite.Sprite(img=player_image,
x=100,
y=100,
batch=batch)))
# Another motionless Entity:
enemy = esper.create_entity()
enemy_image = pyglet.resource.image("bluesquare.png")
esper.add_component(enemy, Renderable(sprite=pyglet.sprite.Sprite(img=enemy_image,
x=400,
y=250,
batch=batch)))
# Create some Processor instances, and asign them to the World to be processed:
movement_processor = MovementProcessor(minx=0, miny=0, maxx=RESOLUTION[0], maxy=RESOLUTION[1])
################################################
# Set up pyglet events for input and rendering:
################################################
@window.event
def on_key_press(key, mod):
if key == pyglet.window.key.RIGHT:
esper.component_for_entity(player, Velocity).x = 3
if key == pyglet.window.key.LEFT:
esper.component_for_entity(player, Velocity).x = -3
if key == pyglet.window.key.UP:
esper.component_for_entity(player, Velocity).y = 3
if key == pyglet.window.key.DOWN:
esper.component_for_entity(player, Velocity).y = -3
@window.event
def on_key_release(key, mod):
if key in (pyglet.window.key.RIGHT, pyglet.window.key.LEFT):
esper.component_for_entity(player, Velocity).x = 0
if key in (pyglet.window.key.UP, pyglet.window.key.DOWN):
esper.component_for_entity(player, Velocity).y = 0
@window.event
def on_draw():
# Clear the window:
window.clear()
# Draw the batch of Renderables:
batch.draw()
####################################################
# Schedule a World update and start the pyglet app:
####################################################
if __name__ == "__main__":
# NOTE! schedule_interval will automatically pass a "delta time" argument
# to esper.process, so you must make sure that your Processor classes
# account for this. See the example Processors above.
pyglet.clock.schedule_interval(movement_processor.process, interval=1.0/FPS)
pyglet.app.run()