Added hitbox on player

This commit is contained in:
theo@manjaro 2021-11-17 14:50:52 +01:00
parent 73d9751608
commit df7d544c31
1 changed files with 19 additions and 2 deletions

View File

@ -14,9 +14,13 @@ class Player(Movable):
self.rect[3] = self.sprite.get_height()
self.water = self.game.gameloop.findname("Water")[0]
self.hitrect = self.rect.copy()
self.hitrect[2] = round(self.hitrect[2]*0.7)
self.hitrect[3] = round(self.hitrect[3]*0.7)
self.canhit = False
self.speed = 200
self.controlled = True
self.playerid = 0
@ -56,6 +60,7 @@ class Player(Movable):
if not self.hitpose:
if self.water and self.rect.y>self.water.rect.y: # Reverse gravity underwater
if self.gravityway == 1:
self.gravityway = -1
@ -115,7 +120,16 @@ class Player(Movable):
if self.leaptimer<0 and not self.onground:
self.verspd = -abs(self.verspd) # Small leap
self.jumped = True
super().step()
super().step() # Actually move
# Updated hitrect
self.hitrect[0] = self.rect.center[0]-self.hitrect[2]/2
offset = 0
if self.verspd>0:
offset = 1
elif self.verspd<0:
offset = -1
self.canhit = abs(self.verspd)>2
self.hitrect[1] = self.rect.center[1]-self.hitrect[3]/2+10*offset
# Reducing the knockback
if self.horkb>0:
@ -152,3 +166,6 @@ class Player(Movable):
def draw(self):
self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])
if self.canhit:
self.game.pygame.draw.rect(self.game.window,[255,100,100],[self.hitrect[0]-self.game.globals["camerax"],self.hitrect[1]-self.game.globals["cameray"],self.hitrect[2],self.hitrect[3]])