Skip to content

Commit

Permalink
Move character using keyboard, change animation accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
malloblenne committed Sep 30, 2017
1 parent 9709114 commit 2ceb8f3
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 35 deletions.
11 changes: 11 additions & 0 deletions test/create_template_yml_spritesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@ def main():

dict_yml = {'image' : 'spritesheet.png'}
dict_actions = dict()
# animations
for i, act in enumerate(list_action_dir):
list_imgs = []
for j in range(list_img_per_action_dir[i]):
list_imgs.append([j * WIDTH_CHAR, i * HEIGHT_CHAR, WIDTH_CHAR, HEIGHT_CHAR])
dict_actions[act] = list_imgs

# static images (first of each animation e.g.)
list_action_dir_still = [act + '_still' for act in list_action_dir]

for i, act in enumerate(list_action_dir_still):
list_single = []
list_single.append(dict_actions[list_action_dir[i]][0])
dict_actions[act] = list_single

# save all dict under 'action' and dump the file
dict_yml['actions'] = dict_actions
str_yml = yaml.dump(dict_yml)

Expand Down
14 changes: 11 additions & 3 deletions test/loadSpritesheetAsAnimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ def load_animation(list_coord, sprite_img):
image_part = sprite_img.get_region(
x=x, y=bottom_y, width=width, height=height)
regions.append(image_part)
if len(regions) > 1 :
# create an animation from the list of selected regions
animation = pyglet.image.Animation.from_image_sequence(
sequence=regions, period=0.1, loop=True)
return animation
animation = pyglet.image.Animation.from_image_sequence(
sequence=regions, period=0.1, loop=True)
return animation
elif regions:
# a single image
image = regions[0]
return image
else :
return None



#batch = pyglet.graphics.Batch()
Expand Down
69 changes: 58 additions & 11 deletions test/moveCharacterKeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class Velocity:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
self.speed = 3
self.speed_diagonal = self.speed ** 0.5


class Renderable:
Expand All @@ -123,6 +125,7 @@ def __init__(self, sprite):
class SpriteSheetAnimation:
def __init__(self, dict_yml):
self._dict_yml = dict_yml
self.current_action = 'walk_down'

spritesheet_img = pyglet.image.load('../images/' + dict_yml['image'])
self.animations = {}
Expand Down Expand Up @@ -170,31 +173,75 @@ def process(self, dt):
# This will iterate over every Entity that has BOTH of these components:
for ent, (vel, rend, sprit) in self.world.get_components(Velocity, Renderable, SpriteSheetAnimation):
# Trick to restart the animation
rend.sprite.image.frames[0].duration = rend.sprite.image.frames[1].duration
#rend.sprite.image.frames[0].duration = rend.sprite.image.frames[1].duration
# Choose correct animation
if self.keyboard[key.UP] and not self.up:
rend.sprite.image = sprit.animations['walk_up']
self.up = True
#rend.sprite.image = sprit.animations['walk_up']
#sprit.current_action = 'walk_up'
#self.up = True
vel.y = 1
elif self.keyboard[key.DOWN] and not self.down:
rend.sprite.image = sprit.animations['walk_down']
self.down = True
#rend.sprite.image = sprit.animations['walk_down']
#sprit.current_action = 'walk_down'
#self.down = True
vel.y = - 1
elif self.keyboard[key.LEFT] and not self.left:
rend.sprite.image = sprit.animations['walk_left']
self.Left = True
#rend.sprite.image = sprit.animations['walk_left']
#sprit.current_action = 'walk_left'
#self.Left = True
vel.x = - 1
elif self.keyboard[key.RIGHT] and not self.right:
rend.sprite.image = sprit.animations['walk_right']
self.right = True
#rend.sprite.image = sprit.animations['walk_right']
#sprit.current_action = 'walk_right'
#self.right = True
vel.x = 1

self.up = self.keyboard[key.UP]
self.down = self.keyboard[key.DOWN]
self.left = self.keyboard[key.LEFT]
self.right = self.keyboard[key.RIGHT]

# stop speed in one direction if none of the keys
# for that direction is pressed
if not(self.up or self.down):
vel.y = 0

if not(self.left or self.right):
vel.x = 0

# when going diagonal, sqrt or /2 the speed
speed_to_apply = vel.speed_diagonal if vel.x and vel.y else vel.speed
if vel.x:
vel.x = abs(vel.x) / vel.x * speed_to_apply
if vel.y:
vel.y = abs(vel.y) / vel.y * speed_to_apply

# Stop animation if no key pressed (e.g., by pausing one of the frame)
if not(self.up or self.down or self.left or self.right):
rend.sprite.image.frames[0].duration = None
if not('_still' in sprit.current_action):
sprit.current_action = sprit.current_action + '_still'
rend.sprite.image = sprit.animations[sprit.current_action]
else:
rend.sprite.image.frames[0].duration = rend.sprite.image.frames[1].duration
# Animations
if vel.y > 0 and not vel.x:
if sprit.current_action != 'walk_up':
rend.sprite.image = sprit.animations['walk_up']
sprit.current_action = 'walk_up'
elif vel.y < 0 and not vel.x:
if sprit.current_action != 'walk_down':
rend.sprite.image = sprit.animations['walk_down']
sprit.current_action = 'walk_down'
elif vel.x < 0 and not vel.y:
if sprit.current_action != 'walk_left':
rend.sprite.image = sprit.animations['walk_left']
sprit.current_action = 'walk_left'
elif vel.x > 0 and not vel.y:
if sprit.current_action != 'walk_right':
rend.sprite.image = sprit.animations['walk_right']
sprit.current_action = 'walk_right'





def main(file_yml):
Expand Down
84 changes: 63 additions & 21 deletions test/spritesheet_test.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
actions:
hurt_down:
- [0, 1280, 64, 64]
- &id001 [0, 1280, 64, 64]
- [64, 1280, 64, 64]
- [128, 1280, 64, 64]
- [192, 1280, 64, 64]
- [256, 1280, 64, 64]
- [320, 1280, 64, 64]
hurt_down_still:
- *id001
shoot_down:
- [0, 1152, 64, 64]
- &id002 [0, 1152, 64, 64]
- [64, 1152, 64, 64]
- [128, 1152, 64, 64]
- [192, 1152, 64, 64]
Expand All @@ -20,8 +22,10 @@ actions:
- [640, 1152, 64, 64]
- [704, 1152, 64, 64]
- [768, 1152, 64, 64]
shoot_down_still:
- *id002
shoot_left:
- [0, 1088, 64, 64]
- &id003 [0, 1088, 64, 64]
- [64, 1088, 64, 64]
- [128, 1088, 64, 64]
- [192, 1088, 64, 64]
Expand All @@ -34,8 +38,10 @@ actions:
- [640, 1088, 64, 64]
- [704, 1088, 64, 64]
- [768, 1088, 64, 64]
shoot_left_still:
- *id003
shoot_right:
- [0, 1216, 64, 64]
- &id004 [0, 1216, 64, 64]
- [64, 1216, 64, 64]
- [128, 1216, 64, 64]
- [192, 1216, 64, 64]
Expand All @@ -48,8 +54,10 @@ actions:
- [640, 1216, 64, 64]
- [704, 1216, 64, 64]
- [768, 1216, 64, 64]
shoot_right_still:
- *id004
shoot_up:
- [0, 1024, 64, 64]
- &id005 [0, 1024, 64, 64]
- [64, 1024, 64, 64]
- [128, 1024, 64, 64]
- [192, 1024, 64, 64]
Expand All @@ -62,104 +70,130 @@ actions:
- [640, 1024, 64, 64]
- [704, 1024, 64, 64]
- [768, 1024, 64, 64]
shoot_up_still:
- *id005
slash_down:
- [0, 896, 64, 64]
- &id006 [0, 896, 64, 64]
- [64, 896, 64, 64]
- [128, 896, 64, 64]
- [192, 896, 64, 64]
- [256, 896, 64, 64]
- [320, 896, 64, 64]
slash_down_still:
- *id006
slash_left:
- [0, 832, 64, 64]
- &id007 [0, 832, 64, 64]
- [64, 832, 64, 64]
- [128, 832, 64, 64]
- [192, 832, 64, 64]
- [256, 832, 64, 64]
- [320, 832, 64, 64]
slash_left_still:
- *id007
slash_right:
- [0, 960, 64, 64]
- &id008 [0, 960, 64, 64]
- [64, 960, 64, 64]
- [128, 960, 64, 64]
- [192, 960, 64, 64]
- [256, 960, 64, 64]
- [320, 960, 64, 64]
slash_right_still:
- *id008
slash_up:
- [0, 768, 64, 64]
- &id009 [0, 768, 64, 64]
- [64, 768, 64, 64]
- [128, 768, 64, 64]
- [192, 768, 64, 64]
- [256, 768, 64, 64]
- [320, 768, 64, 64]
slash_up_still:
- *id009
spellcast_down:
- [0, 128, 64, 64]
- &id010 [0, 128, 64, 64]
- [64, 128, 64, 64]
- [128, 128, 64, 64]
- [192, 128, 64, 64]
- [256, 128, 64, 64]
- [320, 128, 64, 64]
- [384, 128, 64, 64]
spellcast_down_still:
- *id010
spellcast_left:
- [0, 64, 64, 64]
- &id011 [0, 64, 64, 64]
- [64, 64, 64, 64]
- [128, 64, 64, 64]
- [192, 64, 64, 64]
- [256, 64, 64, 64]
- [320, 64, 64, 64]
- [384, 64, 64, 64]
spellcast_left_still:
- *id011
spellcast_right:
- [0, 192, 64, 64]
- &id012 [0, 192, 64, 64]
- [64, 192, 64, 64]
- [128, 192, 64, 64]
- [192, 192, 64, 64]
- [256, 192, 64, 64]
- [320, 192, 64, 64]
- [384, 192, 64, 64]
spellcast_right_still:
- *id012
spellcast_up:
- [0, 0, 64, 64]
- &id013 [0, 0, 64, 64]
- [64, 0, 64, 64]
- [128, 0, 64, 64]
- [192, 0, 64, 64]
- [256, 0, 64, 64]
- [320, 0, 64, 64]
- [384, 0, 64, 64]
spellcast_up_still:
- *id013
thrust_down:
- [0, 384, 64, 64]
- &id014 [0, 384, 64, 64]
- [64, 384, 64, 64]
- [128, 384, 64, 64]
- [192, 384, 64, 64]
- [256, 384, 64, 64]
- [320, 384, 64, 64]
- [384, 384, 64, 64]
- [448, 384, 64, 64]
thrust_down_still:
- *id014
thrust_left:
- [0, 320, 64, 64]
- &id015 [0, 320, 64, 64]
- [64, 320, 64, 64]
- [128, 320, 64, 64]
- [192, 320, 64, 64]
- [256, 320, 64, 64]
- [320, 320, 64, 64]
- [384, 320, 64, 64]
- [448, 320, 64, 64]
thrust_left_still:
- *id015
thrust_right:
- [0, 448, 64, 64]
- &id016 [0, 448, 64, 64]
- [64, 448, 64, 64]
- [128, 448, 64, 64]
- [192, 448, 64, 64]
- [256, 448, 64, 64]
- [320, 448, 64, 64]
- [384, 448, 64, 64]
- [448, 448, 64, 64]
thrust_right_still:
- *id016
thrust_up:
- [0, 256, 64, 64]
- &id017 [0, 256, 64, 64]
- [64, 256, 64, 64]
- [128, 256, 64, 64]
- [192, 256, 64, 64]
- [256, 256, 64, 64]
- [320, 256, 64, 64]
- [384, 256, 64, 64]
- [448, 256, 64, 64]
thrust_up_still:
- *id017
walk_down:
- [0, 640, 64, 64]
- &id018 [0, 640, 64, 64]
- [64, 640, 64, 64]
- [128, 640, 64, 64]
- [192, 640, 64, 64]
Expand All @@ -168,8 +202,10 @@ actions:
- [384, 640, 64, 64]
- [448, 640, 64, 64]
- [512, 640, 64, 64]
walk_down_still:
- *id018
walk_left:
- [0, 576, 64, 64]
- &id019 [0, 576, 64, 64]
- [64, 576, 64, 64]
- [128, 576, 64, 64]
- [192, 576, 64, 64]
Expand All @@ -178,8 +214,10 @@ actions:
- [384, 576, 64, 64]
- [448, 576, 64, 64]
- [512, 576, 64, 64]
walk_left_still:
- *id019
walk_right:
- [0, 704, 64, 64]
- &id020 [0, 704, 64, 64]
- [64, 704, 64, 64]
- [128, 704, 64, 64]
- [192, 704, 64, 64]
Expand All @@ -188,8 +226,10 @@ actions:
- [384, 704, 64, 64]
- [448, 704, 64, 64]
- [512, 704, 64, 64]
walk_right_still:
- *id020
walk_up:
- [0, 512, 64, 64]
- &id021 [0, 512, 64, 64]
- [64, 512, 64, 64]
- [128, 512, 64, 64]
- [192, 512, 64, 64]
Expand All @@ -198,4 +238,6 @@ actions:
- [384, 512, 64, 64]
- [448, 512, 64, 64]
- [512, 512, 64, 64]
walk_up_still:
- *id021
image: spritesheet.png

0 comments on commit 2ceb8f3

Please sign in to comment.