Better player movement on map

This commit is contained in:
theo@manjaro 2021-11-23 10:22:33 +01:00
parent b0212e98b3
commit f3e0ee8fdc
1 changed files with 32 additions and 11 deletions

View File

@ -14,6 +14,9 @@ class Levels(BaseObject):
self.playerwalking = self.game.getSpriteDir("player/walking/")
self.playerstill = self.game.sprite_lib["player/still.png"]
self.flip = False
self.moving = False
self.spriteindex = 0
self.flagsindex = []
self.names = []
@ -44,6 +47,8 @@ class Levels(BaseObject):
self.animspeed = 7
self.cursor = 0 # Which level is selected
self.playerx = self.nodes[0]["x"]
self.playery = self.nodes[1]["y"]
self.tileset = self.game.gameloop.findname("TilesetRenderer")[0]
self.maxwidth = self.tileset.layers[0]["surface"].get_width()
@ -52,17 +57,29 @@ class Levels(BaseObject):
for i in range(len(self.flagsindex)):
self.flagsindex[i]+=self.animspeed*self.game.dt
if self.game.inputs["keys"]["right"]["timer"]==1:
self.cursor+=1
if self.game.inputs["keys"]["left"]["timer"]==1:
self.cursor-=1
if self.game.inputs["keys"]["up"]["timer"]==1:
# Launch the level
self.game.scenes.ingame(self.game,level="Level "+str(self.cursor+1))
if not self.moving:
if self.game.inputs["keys"]["right"]["timer"]>0:
self.cursor+=1
self.flip = False
if self.game.inputs["keys"]["left"]["timer"]>0:
self.cursor-=1
self.flip = True
if self.game.inputs["keys"]["up"]["timer"]>0:
# Launch the level
self.game.scenes.ingame(self.game,level="Level "+str(self.cursor+1))
self.cursor = min(self.cursor,len(self.nodes)-1)
self.cursor = max(0,self.cursor)
self.playerx = self.nodes[self.cursor]["x"]
speedx = (self.nodes[self.cursor]["x"]-self.playerx)
speedy = (self.nodes[self.cursor]["y"]-self.playery)
self.moving = (abs(speedx)+1)*(abs(speedy)+1)>3
if self.moving:
self.spriteindex += self.animspeed*self.game.dt
else:
self.spriteindex = 0
self.playerx += speedx*4*self.game.dt
self.playery += speedy*4*self.game.dt
self.game.globals["camerax"] = self.playerx-self.game.globals["cameraw"]/2
self.game.globals["camerax"] = max(0,self.game.globals["camerax"])
@ -78,9 +95,13 @@ class Levels(BaseObject):
self.game.window.blit(sprite,[x,y])
# Draw player
sprite = self.playerstill
x = self.nodes[self.cursor]["x"]-self.game.globals["camerax"]-sprite.get_width()/2
y = self.nodes[self.cursor]["y"]-self.game.globals["cameray"]-sprite.get_height()/2
if self.moving:
sprite = self.playerwalking[int(self.spriteindex)%len(self.playerwalking)]
else:
sprite = self.playerstill
sprite = self.game.pygame.transform.flip(sprite,self.flip,False)
x = self.playerx-self.game.globals["camerax"]-sprite.get_width()/2
y = self.playery-self.game.globals["cameray"]-sprite.get_height()/2-6
self.game.window.blit(sprite,[x,y])
# Draw level's number