-
Notifications
You must be signed in to change notification settings - Fork 1
/
walkcycle.py
46 lines (40 loc) · 1.44 KB
/
walkcycle.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
# the Player() class is a subclass of Actor()
class Player(Actor):
def __init__(self, **kwargs):
super().__init__(pos = (200,100), image='stand1', **kwargs)
self.state = 'stand'
self.animationdelay = 10
self.animationtimer = 0
# a list of image for each player state
self.images = { 'stand' : ['stand1'],
'walkleft' : ['walkleft1','walkleft2'],
'walkright' : ['walkright1','walkright2']
}
# the index of the current image in the image list
self.animationindex = 0
def update(self):
# update position and state based on keyboard input
if keyboard.left:
self.x -= 1
self.state = 'walkleft'
elif keyboard.right:
self.x += 1
self.state = 'walkright'
else:
self.state = 'stand'
# update animation by incrementing timer
# and updating sprite image if timer limit reached
self.animationtimer += 1
if self.animationtimer >= self.animationdelay:
self.animationtimer = 0
self.animationindex += 1
if self.animationindex > len(self.images[self.state]) - 1:
self.animationindex = 0
self.image = self.images[self.state][self.animationindex]
# create a new player
p = Player()
def update():
p.update()
def draw():
screen.clear()
p.draw()