-
Notifications
You must be signed in to change notification settings - Fork 0
/
elisa_15_parallax_mapping.py
313 lines (256 loc) · 9 KB
/
elisa_15_parallax_mapping.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
# auth: christian bitter
# name: elisa_15_parallax_mapping
# desc: a simple parallax mapping example ...
# we load the layers of a parallax mapped background separately
# the arrow keys allow going left and right
# vers: 0.1
import pygame
import os
import sys
from elisa.sprite import load_png, Sprite, SpriteAnimation, SpriteSheet
from elisa.arch.sm import State, StateMachine, Transition
def load_images(
target_width: int = 640, target_height: int = 480, verbose: bool = False
):
img_container = {}
img_base_path = "asset/gfx/raventale_parallax_backgound_scaled"
# all images have the same size - 2048x1546 - we will scale them back
for img_name in os.listdir(img_base_path):
if not img_name.endswith(".png"):
continue
image_fp = os.path.join(img_base_path, img_name)
img = load_png(image_fp, image_only=True)
img = pygame.transform.scale(img, (target_width, target_height))
img_container[img_name] = img
if verbose:
print("Loaded {} images ...".format(len(img_container)))
img_order = [
"_11_background.png",
"_10_distant_clouds.png",
"_09_distant_clouds1.png",
"_08_clouds.png",
"_07_huge_clouds.png",
"_05_hill1.png",
"_06_hill2.png",
"_04_bushes.png",
"_03_distant_trees.png",
"_02_trees and bushes.png",
"_01_ground.png",
]
img_layer_speed = {
"_11_background.png": 3,
"_10_distant_clouds.png": 4,
"_09_distant_clouds1.png": 5,
"_08_clouds.png": 6,
"_07_huge_clouds.png": 8,
"_05_hill1.png": 10,
"_06_hill2.png": 11,
"_04_bushes.png": 12,
"_03_distant_trees.png": 13,
"_02_trees and bushes.png": 14,
"_01_ground.png": 20,
}
return img_container, img_order, img_layer_speed
# we simply copy the state machine from elisa7
def trigger_idle_wl(key_func):
def _trigger():
return key_func()[pygame.K_LEFT]
return _trigger
def trigger_idle_wr(key_func):
def _trigger():
return key_func()[pygame.K_RIGHT]
return _trigger
def trigger_wl_idle(key_func):
def _trigger():
return not key_func()[pygame.K_LEFT]
return _trigger
def trigger_wr_idle(key_func):
def _trigger():
return not key_func()[pygame.K_RIGHT]
return _trigger
def idle_to_walk_fire(s_from, s_to):
print("Now Walking")
def walk_to_idle_fire(s_from, s_to):
print("Now idling")
def idle_to_final(s_from, s_to):
print("In final")
def define_state_machine(key_func):
# define states, transitions and build the state machine
s_idle = State("Idle", "Elisa is in the idle state")
s_walk_left = State("Walk_Left", "Elisa walks left")
s_walk_right = State("Walk_Right", "Elisa walks right")
s_final = State("Final", "The final state")
t_idle_wl = Transition(
s_idle,
s_walk_left,
trigger_idle_wl(key_func),
idle_to_walk_fire,
"T_Idle2WalkLeft",
"transititioning from idle to walk left",
)
t_idle_wr = Transition(
s_idle,
s_walk_right,
trigger_idle_wr(key_func),
idle_to_walk_fire,
"T_Idle2WalkRight",
"transititioning from idle to walk right",
)
t_wl_idle = Transition(
s_walk_left,
s_idle,
trigger_wl_idle(key_func),
walk_to_idle_fire,
"T_WalkLeft2Idle",
"transitioning from walking left to idle",
)
t_wr_idle = Transition(
s_walk_right,
s_idle,
trigger_wr_idle(key_func),
walk_to_idle_fire,
"T_WalkLeft2Idle",
"transitioning from walking left to idle",
)
# final should not be reached
t_idle_final = Transition(
s_idle,
s_final,
lambda x=None: False,
idle_to_final,
"T_Idle_Final",
"Moving from idle to final",
)
sm = StateMachine(
[s_idle, s_walk_left, s_walk_right, s_final],
[t_idle_wl, t_idle_wr, t_wl_idle, t_wr_idle, t_idle_final],
s_idle,
s_final,
)
val_result, val_reason = sm.validate()
if not val_result:
raise ValueError("Validated State Machine: {}".format(val_reason))
return sm
def main():
if not pygame.font:
print("Pygame - fonts not loaded")
if not pygame.mixer:
print("Pygame - audio not loaded")
elisa_sm = define_state_machine(pygame.key.get_pressed)
pygame.init()
w, h, t = 640, 480, "Elisa - 15 Parallax Mapping"
c_black = (0, 0, 0, 255)
screen_buffer = pygame.display.set_mode(size=(w, h))
pygame.display.set_caption(t)
pygame.mouse.set_visible(True)
anim_sheet = SpriteSheet("asset/elise_character/[email protected]")
anim_sheet.initialize(verbose=False)
idle_anim = SpriteAnimation(
name="Elisa_idle", sprite_sheet=anim_sheet, fps=24, repeats=True
)
idle_anim = (
idle_anim.add_frame("idle_1")
.add_frame("idle_2")
.add_frame("idle_3")
.add_frame("idle_4")
)
walk_anim = SpriteAnimation(
name="Elisa_walk",
sprite_sheet=anim_sheet,
fps=24,
repeats=True,
frames=["walk_1", "walk_2", "walk_3", "walk_4", "walk_5", "walk_6"],
)
# we make the target width slightly larger
img_width, img_height = 2 * w, h
layers, layer_ordering, layer_speed = load_images(
target_width=img_width, target_height=img_height
)
# for each of the images .. we define a parallax order
# we assume that the first layer is the farthest away.
back_buffer: pygame.Surface = pygame.Surface(
screen_buffer.get_size(), pygame.SRCALPHA
)
# back_buffer = back_buffer.convert()
back_buffer.fill(c_black)
fps_watcher = pygame.time.Clock()
is_done = False
w2 = w // 2
x_dir = 0
# initially all pictures are aligned to the same x-pos, but over time we make them shift gradually
player_x = 320
x_pos = [320] * len(layers)
sprite_x, sprite_y = player_x, 350
current_anim = idle_anim
while not is_done:
elapsed_millis = fps_watcher.tick(60)
x_dir = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_done = True
break
else:
key_map = pygame.key.get_pressed()
previous = elisa_sm.current
elisa_sm.update()
if elisa_sm.current != previous:
# before we update the animation, we reset
current_anim.reset()
if elisa_sm.current.name == "Idle":
current_anim = idle_anim
elif (
elisa_sm.current.name == "Walk_Left"
or elisa_sm.current.name == "Walk_Right"
):
current_anim = walk_anim
else:
raise ValueError("Unknow state transitioned into")
if key_map[pygame.K_LEFT]:
x_dir = -1
if key_map[pygame.K_RIGHT]:
# shift the area
x_dir = 1
# we calculate the viewing window, as the half to the left and half to the right
# if we go the the outside of the image, we need to add images back at the end
# since every layer can move at different speeds, we will have different viewing windows at every layer
# for now we fix the speed upfront
for i, lo in enumerate(layer_ordering):
lspeed = layer_speed[lo]
x_pos[i] = x_pos[i] + x_dir * lspeed
back_buffer.fill(c_black)
for i, lo in enumerate(layer_ordering):
layer = layers[lo]
l_xpos = x_pos[i]
# if our window is outside of the blitting surface, we stich it together
xmin, xmax = (l_xpos - w2), (l_xpos + w2)
if 0 <= xmin <= img_width and 0 <= xmax <= img_width:
back_buffer.blit(layer, (0, 0), area=[xmin, 0, xmax, h])
elif xmin >= img_width:
x_pos[i] = w2
xmin = 0
xmax = w
back_buffer.blit(layer, (0, 0), area=[xmin, 0, xmax, h])
elif xmin < 0 and xmax <= 0:
x_pos[i] = w2
xmin = 0
xmax = w
back_buffer.blit(layer, (0, 0), area=[xmin, 0, xmax, h])
elif xmin < 0:
# partial blitting
abs_xmin = abs(xmin)
back_buffer.blit(layer, (0, 0), [img_width - abs_xmin, 0, abs_xmin, h])
back_buffer.blit(layer, (abs_xmin, 0), [0, 0, xmax, h])
else:
# partial blitting
back_buffer.blit(layer, (0, 0), [xmin, 0, img_width, h])
back_buffer.blit(
layer, (img_width - xmin, 0), [0, 0, xmax - img_width, h]
)
back_buffer.blit(
current_anim.update(elapsed_millis).as_pygame_sprite, (sprite_x, sprite_y)
)
if not is_done:
screen_buffer.blit(back_buffer, (0, 0))
pygame.display.flip()
if __name__ == "__main__":
main()