如何在pygame中实现平滑移动?

3

如何使我的pygame角色平滑移动?当我创建常规程序时,我似乎可以通过减小速度(每步角色移动的距离)和减小时间延迟来获得平滑运动,以便它快速地移动小步。

代码可能如下所示(这里只有一个简单的矩形在屏幕上移动):

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))

x = 400
y = 400
width = 60
height = 60
vel = 2

run = True

while run:
    pygame.time.delay(10)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > vel:
        x -= vel

    if keys[pygame.K_RIGHT] and x < 500 - width - vel:
        x += vel

    win.fill((0, 0, 0))
    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()

pygame.quit()

然而,当我试图将游戏面向对象化时,我没有让角色平滑移动。代码如下。我做错了什么?

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))

class player():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 2
        self.left = False
        self.right = False

    def draw(self):
        pygame.draw.rect(win, (255, 0, 0), (man.x, man.y, man.height, man.width))
        if self.left:
            pygame.draw.rect(win, (255, 0, 0), (man.x, man.y, man.height, man.width))
        elif self.right:
            pygame.draw.rect(win, (255, 0, 0), (man.x, man.y, man.height, man.width))


def re_draw():
    win = pygame.display.set_mode((500, 500))
    man.draw()
    pygame.display.update()


man = player(400, 400, 60, 60)

run = True


while run:
        pygame.time.delay(10)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT] and man.x > man.vel:
            man.x -= man.vel
            man.left = True
            man.right = False

        elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
            man.x += man.vel
            man.left = False
            man.right = True

        re_draw()

pygame.quit()

1
当你改变方法时,是什么没有起作用?你期望发生什么,实际上又发生了什么? - Ted Klein Bergman
1个回答

3

你可以使用 clock.tick 代替使用 pygame.time.delay

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))

class player():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 2
        self.left = False
        self.right = False

    def draw(self):
        pygame.draw.rect(win, (255, 0, 0), (man.x, man.y, man.height, man.width))
        if self.left:
            pygame.draw.rect(win, (255, 0, 0), (man.x, man.y, man.height, man.width))
        elif self.right:
            pygame.draw.rect(win, (255, 0, 0), (man.x, man.y, man.height, man.width))


def re_draw():

    man.draw()
    pygame.display.update()


man = player(400, 400, 60, 60)

run = True
clock = pygame.time.Clock()

while run:

        win.fill((0, 0, 0)) # fill the window with your desired color
        # pygame.time.delay(10) use clock.tick instead

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT] and man.x > man.vel:
            man.x -= man.vel
            man.left = True
            man.right = False

        elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
            man.x += man.vel
            man.left = False
            man.right = True

        man.draw() # this might be better

        pygame.display.update()

        clock.tick(60) 

pygame.quit()

希望这有所帮助,欢迎来到StackOverflow。

clock.tick(60)并不会增加FPS,相反地!它会延迟游戏运行,使其最多只能以60帧每秒的速度运行。 - Ted Klein Bergman
对不起,我会进行编辑。但是我已经测试了该代码,并且确实可以更加流畅地运行。 - gerard Garvey
没问题,很高兴能帮助到你 :) - gerard Garvey
1
@DenFulaAnkungen 我认为你的第二种方法实际上存在问题,因为你的 re_draw 函数有一行 pygame.display.set_mode((500, 500)),这基本上重新格式化了整个显示。这是一个非常昂贵的函数,只应在启动程序或更改显示大小时调用。在你的第一种方法中,你正确地在程序启动时只调用了一次。 - Ted Klein Bergman
@TedKleinBergman如果有其他方法可以使程序更加流畅,而不会造成过多的成本和延迟程序,我会非常感激您发布解决方案。感谢您如此热心地帮助我。 - Den Fula Ankungen
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接