pygame中的摩擦 [英] Friction in pygame

查看:20
本文介绍了pygame中的摩擦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的平台游戏,我想让它在移动时慢慢变慢.我尝试了几种方法,但没有用,它只是停留在原地.有人可以帮我吗?

for my platformer game i want to make it so when it moves it slowly slows down. i tried to do it a couple ways but it didn't work it just stays on spot. can someone help me?

class Player(pygame.sprite.Sprite):

    def __init__(self, game):
        pygame.sprite.Sprite.__init__(self)
        self.game = game
        self.speedx = 0
        self.speedy = 0
        self.alive = True

        self.image = pygame.Surface((30, 40))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        self.rect.bottom = HEIGHT - 10
        self.rect.centerx = WIDTH / 2

    def update(self):
        self.speedx = 0
        self.speedy = 0
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_a]:
            self.speedx = -10
        elif key_pressed[pygame.K_d]:
            self.speedx = 10
        if key_pressed[pygame.K_SPACE]:
            self.speedy = -10

        self.speedx += self.rect.x
        self.speedx *= FRICTION
        self.speedy += self.rect.y

        if self.rect.left < 0:
            self.rect.x = 0
        elif self.rect.right > WIDTH:
            self.rect.right = WIDTH

推荐答案

就我个人而言,我更喜欢这样计算摩擦:

Personally, I prefer calculating friction like that:

  • 如果按下一个键,我补充说,比方说每秒 3000 像素的速度
  • 每一帧,我都会通过将速度乘以一个非常接近 1 的数字来减慢精灵的速度,例如当游戏以每秒 100 帧的速度运行时为 0.95.

通过使用这种技术,如果你让精灵移动,它会越来越快地加速.如果你停止移动它,它会平稳地减速.另外,如果你让精灵在它仍然向右移动时向左移动,它会转身".更快.

By using this technique, if you make the sprite move, it will accelerate more and more quickly. And if you stop moving it, it will slow down smoothly. Also, if you make the sprite move to the left while it is still moving right, it will "turn around" quicker.

你可以乱用这些值:如果你增加第一个数字,速度就会增加.如果第二个数字更接近 1,则摩擦不太显着.

You can mess aorund with the values: if you increase the first number, the speed will increase. If the second number is closer to 1, the friction is less significant.

以下是在以每秒 100 帧运行时的编码方法:

Here is how to code this, when running at 100 frames per second:

x_speed 变量以每秒像素为单位.只需将其除以 100 即可得到每帧像素.

The x_speed variable is in pixels per second. Simply divide it by 100 to get it in pixels per frame.

# in the game loop

pressed = pygame.key.get_pressed()
if pressed[K_RIGHT]:
    x_speed += 30
if pressed[K_LEFT]:
    x_speed -= 30

x_speed *= 0.95

这里是如何使用它,为了以任何帧率运行游戏(你只需要一个变量,time_passed,它对应于在一个帧上花费的时间,以秒为单位:你可以使用 pygame.time.Clock().

Here is how to use it, in order to run the game at any framerate (you just need a variable, time_passed, which corresponds to the time spent on a frame, in seconds: you can use pygame.time.Clock().

x_speed 变量以每秒像素为单位.

The x_speed variable is in pixels per second.

# in the game loop

pressed = pygame.key.get_pressed()
if pressed[K_RIGHT]:
    x_speed += 3000 * time_passed
if pressed[K_LEFT]:
    x_speed -= 3000 * time_passed

x_speed *= 0.95**(100 * time_passed)

可运行的最小、可重现示例:

这篇关于pygame中的摩擦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆