对不起,英语不好:(
这是我的get hitbox函数:
def hitbox(self):
return (self.x - 50,self.y - 50),(self.x + 50,self.y + 50)
我正在使用它来显示Hitbox:
pygame.draw.rect(screen,(122,122,0),(self.hitbox()[0],self.hitbox()[1]),3)
但是我的精灵在向右走时,命中框向右移的幅度更大,而当向下走时,命中框的下降幅度也更大。
我该如何解决?
在pygame的矩形有四个参数( x, y, width, height )
。您的hitbox()
函数正在返回由定义的矩形( x1, y1, x2, y2 )
。
也许最好的方法是从函数中返回Rect。这也将允许您使用出色的PyGame Rect碰撞处理功能。
BOX_WIDTH = 100
BOX_HEIGHT = 100
def hitbox(self):
# Create the box at 0,0 with correct size
box_rect = pygame.Rect( 0, 0, BOX_WIDTH, BOX_HEIGHT )
# transpose it around the current position
box_rect.center = ( self.x, self.y )
return box_rect
然后绘制Rect:
pygame.draw.rect( screen, ( 122, 122, 0 ), self.hitbox(), 3 )
我认为将Rect作为类的成员存储是一个好主意。然后,要移动精灵,只需self.box_rect
直接更新对象即可。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句