Added player's animations

This commit is contained in:
theo@manjaro 2021-11-17 20:56:33 +01:00
parent 9d4c8b000e
commit 07c79e8f7a
11 changed files with 44 additions and 5 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 849 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 895 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 815 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 850 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 841 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 865 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 841 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 837 B

View File

@ -9,7 +9,19 @@ class Player(Movable):
spawnpoint = game.lib.choice(self.tileset.spawns)
self.rect.move_ip(spawnpoint.center)
self.sprite = game.sprite_lib["player/0.png"]
self.sprites = game.getSpriteDir("player/walking/") # Walking animation
self.spritestill = game.sprite_lib["player/still.png"] # Standing still
self.spritejumping = game.sprite_lib["player/jumping.png"] # Ascending
self.spritefalling = game.sprite_lib["player/falling.png"] # Descending
self.spritehurt = game.sprite_lib["player/hurt.png"] # Being hurted
self.spritelanding = game.sprite_lib["player/landing.png"]
self.landingtimer = 0
self.maxlandtime = 0.2
self.spriteindex = 0
self.flipx = False
self.animationspeed = 2.5
self.sprite = self.spritestill
self.rect[2] = self.sprite.get_width()
self.rect[3] = self.sprite.get_height()
@ -23,7 +35,6 @@ class Player(Movable):
self.dustparticles = game.getSpriteDir("particles/dust/")
self.speed = 200
self.controlled = True
self.playerid = 0
self.teamid = self.playerid
@ -86,8 +97,23 @@ class Player(Movable):
canmove = (abs(self.verkb)+1)*(abs(self.horkb)+1)<2
keys = self.game.inputs["keys"]
self.horspd = 0
if self.controlled and canmove:
if canmove:
self.horspd=(keys["right"]["pressed"]-keys["left"]["pressed"])*self.game.dt*self.speed
if self.landingtimer<=0:
self.sprite = self.spritestill
else:
self.landingtimer-=self.game.dt
self.spriteindex = 0
if self.horspd != 0:
if self.landingtimer<=0:
self.spriteindex+=self.horspd*self.animationspeed*self.game.dt
self.sprite = self.sprites[int(self.spriteindex)%len(self.sprites)]
self.flipx = self.horspd<0
else:
# Beeing hurt
self.sprite = self.spritehurt
if self.attackstate:
self.attackstate(self) # Si je suis sur le sol
self.onground = False
@ -100,6 +126,9 @@ class Player(Movable):
self.fastfall = False
# Spawns dust
self.game.addParticle(self.dustparticles,self.rect.center[0],self.rect.center[1]+self.rect[3]/2*self.gravityway,fps=25)
if self.verspd!=0:
self.sprite=self.spritelanding
self.landingtimer = self.maxlandtime
if self.gravityway > 0:
self.verspd=min(0,self.verspd)
else:
@ -117,6 +146,15 @@ class Player(Movable):
else:
self.verspd = min(0,self.verspd)
# Jumping and falling sprites
if canmove and not self.onground:
verdir = self.verspd>0
gravdir = self.gravityway>0
if verdir != gravdir:
self.sprite=self.spritejumping
else:
self.sprite=self.spritefalling
# Adding knockback
self.horspd+=self.horkb
self.verspd+=self.verkb
@ -175,4 +213,5 @@ class Player(Movable):
self.game.globals["cameray"] = self.rect.center[1]-self.game.globals["camerah"]/2
def draw(self):
self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])
sprite = self.game.pygame.transform.flip(self.sprite,self.flipx,False)
self.game.window.blit(sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])