-
Notifications
You must be signed in to change notification settings - Fork 0
/
elisa_7_sprite_animation.py
195 lines (154 loc) · 4.97 KB
/
elisa_7_sprite_animation.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
# auth: christian bitter
# name: elisa_7_sprite_animation.py
# desc: based on the previous elisa, we will now look into better definition of animation.
from pygame.locals import QUIT
import os
from elisa.sprite import SpriteSheet, SpriteAnimation
from elisa.arch.sm import State, StateMachine, Transition
import pygame
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")
# init pygame - create the main window, and a background surface
pygame.init()
elisa_sm = define_state_machine(pygame.key.get_pressed)
S_WIDTH = 640
S_HEIGHT = 480
S_TITLE = "Elisa - Statemachine and GFX"
C_WHITE = (250, 250, 250)
screen_buffer = pygame.display.set_mode(size=(S_WIDTH, S_HEIGHT))
pygame.display.set_caption(S_TITLE)
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"],
)
back_buffer: pygame.Surface = pygame.Surface(screen_buffer.get_size())
back_buffer = back_buffer.convert()
back_buffer.fill(C_WHITE)
# FPS watcher
fps_watcher = pygame.time.Clock()
is_done = False
sprite_x, sprite_y = 100, 100
current_anim = idle_anim
while not is_done:
_elapsed_ms = fps_watcher.tick(24)
for event in pygame.event.get():
if event.type == QUIT:
is_done = True
break
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")
back_buffer.fill(C_WHITE)
back_buffer.blit(
current_anim.update(_elapsed_ms).as_pygame_sprite, (sprite_x, sprite_y)
)
screen_buffer.blit(back_buffer, (0, 0))
pygame.display.flip()
if __name__ == "__main__":
main()