【问题标题】:How do i get my character to accelerate and decelerate instead of just moving with a certain velocity (in pygame)?我如何让我的角色加速和减速,而不是仅仅以一定的速度移动(在 pygame 中)?
【发布时间】:2020-08-04 19:35:17
【问题描述】:

谁能告诉我要改变什么,以便我的船使用推力机制而不是像现在这样的静态运动?

我希望运动就像在小行星中一样,在你所面对的方向上加速并且速度在某个点上达到上限,然后如果你停止加速,它的速度会慢慢降低直到停止。

rn如果我按下前进按钮,它会直接以最大速度开始移动,当我松开时立即停止。

这是我的船级代码

class Ship:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 0
        self.vel = 0
        self.angle = 0

    def draw(self):
        ship_img = pygame.image.load("sprites/ship_off.png")
        ship_img_copy = pygame.transform.rotate(ship_img, self.angle)
        window.blit(ship_img_copy,
                    (self.x - (ship_img_copy.get_width()) / 2, self.y - (ship_img_copy.get_height()) / 2))

        keys = pygame.key.get_pressed()

        if keys[pygame.K_w]:
            ship_img = pygame.image.load("sprites/ship_on.png")
            ship_img_copy = pygame.transform.rotate(ship_img, self.angle)
            window.blit(ship_img_copy,
                        (self.x - (ship_img_copy.get_width()) / 2, self.y - (ship_img_copy.get_height()) / 2))

    def move(self):
        keys = pygame.key.get_pressed()
        # todo acceleration and thrust mechanics
        if keys[pygame.K_w]:
            self.x += self.vel * cos(self.angle * (pi / 180) + (90 * pi / 180))
            self.y -= self.vel * sin(self.angle * (pi / 180) + 90 * (pi / 180))
            # So that if it leaves one side it comes from the other
            if self.y < 0:
                self.y = (self.y - self.vel) % 600

            elif self.y > 600:
                self.y = (self.y + self.vel) % 600

            elif self.x < 0:
                self.x = (self.x - self.vel) % 800

            elif self.x > 800:
                self.x = (self.x + self.vel) % 800

        if keys[pygame.K_a]:
            self.angle += 7

        if keys[pygame.K_d]:
            self.angle -= 7

我试过了,但我做不到,所以这是我的代码

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您必须在按下 ws 时更改 self.vel,但您必须在每一帧中更改 self.xself.y,具体取决于 @ 987654325@:

    class Ship:
        def __init__(self, x, y):
            # [...] 
    
            self.vel = 0
            self.max_vel = 10
    
        # [...]
    
        def move(self):
            keys = pygame.key.get_pressed()
            if keys[pygame.K_w]:
                self.vel = min(self.vel+1, self.max_vel)
            if keys[pygame.K_s]:
                self.vel = max(self.vel-1, 0)
    
            if keys[pygame.K_a]:
                self.angle += 7
            if keys[pygame.K_d]:
                self.angle -= 7
    
            self.x += self.vel * cos(self.angle * (pi / 180) + (90 * pi / 180))
            self.y -= self.vel * sin(self.angle * (pi / 180) + 90 * (pi / 180))
            if self.y < 0:
                self.y = (self.y - self.vel) % 600
            elif self.y > 600:
                self.y = (self.y + self.vel) % 600
            elif self.x < 0:
                self.x = (self.x - self.vel) % 800
            elif self.x > 800:
                self.x = (self.x + self.vel) % 800
    

    【讨论】:

    • 这很好用,但问题是,如果我在 不推进 的情况下转向,它不仅会改变船面对的方向而不改变路径。通常,就像在小行星中一样,它会继续沿原来的方向漂浮,而只会改变飞船所面对的方向。但在这里,它变成了一辆汽车。有没有办法解决这个问题?
    • @BenDover 对不起,我不明白你的意思。无论如何,这似乎是一个新问题。评论部分无意提出其他问题。请Ask a public question
    猜你喜欢
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多