Implemented carry animations.

This commit is contained in:
Sakse Dalum 2012-08-08 19:11:43 +02:00
parent 4a3c977a92
commit cbb6caea97
1 changed files with 17 additions and 11 deletions

View File

@ -36,7 +36,8 @@ class Player(worldobject.WorldObject):
worldobject.WorldObject.__init__(self, level, x, y, worldobject.WorldObject.__init__(self, level, x, y,
z=z, movable=movable) z=z, movable=movable)
self.anim = 'idle_right' self.anim_root = 'idle'
self.anim = 'right'
self.frame = 0 self.frame = 0
self.anim_speed = 15 self.anim_speed = 15
@ -49,7 +50,11 @@ class Player(worldobject.WorldObject):
[('idle_up', os.path.join('robot_idle', 'up')), [('idle_up', os.path.join('robot_idle', 'up')),
('idle_down', os.path.join('robot_idle', 'down')), ('idle_down', os.path.join('robot_idle', 'down')),
('idle_right', os.path.join('robot_idle', 'right')), ('idle_right', os.path.join('robot_idle', 'right')),
('idle_left', os.path.join('robot_idle', 'right'))] ('idle_left', os.path.join('robot_idle', 'right')),
('carry_up', os.path.join('robot_carry', 'up')),
('carry_down', os.path.join('robot_carry', 'down')),
('carry_right', os.path.join('robot_carry', 'right')),
('carry_left', os.path.join('robot_carry', 'right'))]
): ):
self.imgs[anim] = [] self.imgs[anim] = []
@ -67,12 +72,12 @@ class Player(worldobject.WorldObject):
img = pygame.image.load(f) img = pygame.image.load(f)
# Special treatment: # Special treatment:
if anim == 'idle_left': if anim in ['idle_left', 'carry_left']:
img = pygame.transform.flip(img, 1, 0) img = pygame.transform.flip(img, 1, 0)
self.imgs[anim].append(img) self.imgs[anim].append(img)
self.img = self.imgs[self.anim][int(self.frame)] self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
def touch(self, touch_x, touch_y): def touch(self, touch_x, touch_y):
for obj in self.level.objects: for obj in self.level.objects:
@ -91,33 +96,34 @@ class Player(worldobject.WorldObject):
if keys[pygame.K_UP]: if keys[pygame.K_UP]:
if not self.holding: if not self.holding:
self.direction = (0, -1) self.direction = (0, -1)
self.anim = 'idle_up' self.anim = 'up'
self.move(0, -1) self.move(0, -1)
if keys[pygame.K_DOWN]: if keys[pygame.K_DOWN]:
if not self.holding: if not self.holding:
self.direction = (0, 1) self.direction = (0, 1)
self.anim = 'idle_down' self.anim = 'down'
self.move(0, 1) self.move(0, 1)
if keys[pygame.K_RIGHT]: if keys[pygame.K_RIGHT]:
if not self.holding: if not self.holding:
self.direction = (1, 0) self.direction = (1, 0)
self.anim = 'idle_right' self.anim = 'right'
self.move(1, 0) self.move(1, 0)
if keys[pygame.K_LEFT]: if keys[pygame.K_LEFT]:
if not self.holding: if not self.holding:
self.direction = (-1, 0) self.direction = (-1, 0)
self.anim = 'idle_left' self.anim = 'left'
self.move(-1, 0) self.move(-1, 0)
# Update the animation # Update the animation
self.frame = ((self.frame + self.anim_speed * dt) % self.frame = ((self.frame + self.anim_speed * dt) %
len(self.imgs[self.anim])) len(self.imgs[self.anim_root + '_' + self.anim]))
worldobject.WorldObject.update(self, e, t, dt) worldobject.WorldObject.update(self, e, t, dt)
def draw(self, window): def draw(self, window):
self.img = self.imgs[self.anim][int(self.frame)] self.anim_root = 'carry' if self.holding else 'idle'
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
self.img.set_alpha(128) self.img.set_alpha(128)
window.blit(self.img, (self.x - 32 - self.level.camera_x, window.blit(self.img, (self.x - 32 - self.level.camera_x,
self.y - self.img.get_size()[1] + 24 self.y - self.img.get_size()[1] + 24