带有归一化向量的钥匙压力加速

发布于 2025-02-09 19:18:05 字数 1052 浏览 1 评论 0原文

我已经在其他平台游戏中实现了加速度,但是现在我正在努力将其实施以一种自上而下的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2025-02-16 19:18:05

对于那些有兴趣的人,我在发布后大约1分钟就解决了这个问题!我只是在移动方法中添加了以下内容。然后根据输入方法加速加速,但仅在达到最大速度时才评估归一化向量:)

if self.vec.magnitude() >= self.speed:
        self.vec = self.vec.normalize() * self.speed

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 :)

if self.vec.magnitude() >= self.speed:
        self.vec = self.vec.normalize() * self.speed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文