带有归一化向量的钥匙压力加速
我已经在其他平台游戏中实现了加速度,但是现在我正在努力将其实施以一种自上而下的RPG样式,在该样式中,对角运动的归一化。
我有以下代码可行,但是一旦添加了归一化的向量,就会显然将其评估为一个。我已经尝试添加速度变量并将其递增,但是当它达到最大速度时,它可以交换方向,因为我将其乘以向量以获取结果。
我希望这是有道理的,并且关于如何添加加速度或“摩擦”的任何想法都将非常感谢。
def input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.vec.y -= 1
if self.vec.y <= -10:
self.vec.y = -10
self.facing = 'up'
elif keys[pygame.K_DOWN]:
self.vec.y += 1
if self.vec.y >= 10:
self.vec.y = 10
self.facing = 'down'
else:
self.vec.y = 0
if keys[pygame.K_LEFT]:
self.vec.x -= 1
if self.vec.x <= -10:
self.vec.x = -10
self.facing = 'left'
elif keys[pygame.K_RIGHT]:
self.vec.x += 1
if self.vec.x >= 10:
self.vec.x = 10
self.facing = 'right'
else:
self.vec.x = 0
def moving(self):
self.hitbox.x += self.vec.x
self.hitbox.y += self.vec.y
self.rect.center = self.hitbox.center
I have implemented an acceleration in other platform games, but I am now struggling to implement this for a top down rpg style where diagonal movement is normalized.
I have the following code which works, but as soon as I add normalised vectors it obvioulsy evaluates to one. I have tried adding a speed variable and incrementing this instead, but as it reaches max speed, it swaps direction as I multiplied it by the vector to get the result.
I hope this makes sense and any ideas on how to add acceleration or 'friction' to this mechanic would be good thanks.
def input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.vec.y -= 1
if self.vec.y <= -10:
self.vec.y = -10
self.facing = 'up'
elif keys[pygame.K_DOWN]:
self.vec.y += 1
if self.vec.y >= 10:
self.vec.y = 10
self.facing = 'down'
else:
self.vec.y = 0
if keys[pygame.K_LEFT]:
self.vec.x -= 1
if self.vec.x <= -10:
self.vec.x = -10
self.facing = 'left'
elif keys[pygame.K_RIGHT]:
self.vec.x += 1
if self.vec.x >= 10:
self.vec.x = 10
self.facing = 'right'
else:
self.vec.x = 0
def moving(self):
self.hitbox.x += self.vec.x
self.hitbox.y += self.vec.y
self.rect.center = self.hitbox.center
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于那些有兴趣的人,我在发布后大约1分钟就解决了这个问题!我只是在移动方法中添加了以下内容。然后根据输入方法加速加速,但仅在达到最大速度时才评估归一化向量:)
For those interested I have solved this about 1 minute after I posted! I just added the following in the move method. This then accelerates as per the input method, but evaluates to a normalised vector only when max speed is reached :)