如何使运动更平滑?

时间:2012-11-14 12:29:27

标签: python animation pygame

我正在开发一个非常基本的引擎,基于Pygame的教程,我对“平滑”有一点问题。如何让我的玩家走路“更顺畅”?

我的事件处理程序非常基本,非常标准,没什么新东西,我甚至想出了如何为测试进行“提升”(运行)。但问题是,在pygame.KEYUP,那些零点会破坏我的小玩家的“光滑度”,我不希望这样,但我不想让它无限制地行走。

import pygame
import gfx

# Main Class

class Setup:

    background = gfx.Images.background
    player = gfx.Images.player

    pygame.init()

    # Configuration Variables:

    black = (0,0,0)
    white = (255,255,255)
    green = (0,255,0)
    red  = (255,0,0)
    title = "Ericson's Game"

    # Setup:

    size = [700,700]
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption(title)
    done = False
    clock = pygame.time.Clock()

    # Logic Variables

    x_speed = 0
    y_speed = 0
    x_speed_boost = 0
    y_speed_boost = 0
    x_coord = 350
    y_coord = 350
    screen.fill(white)

    # Main Loop:

    while done == False:

        screen.blit(background,[0,0])
        screen.blit(player,[x_coord,y_coord])

        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                done = True

            if event.type == pygame.KEYDOWN:               
                if event.key == pygame.K_ESCAPE:
                    done = True

                if event.key == pygame.K_a:
                    x_speed = -6
                    x_speed_boost = 1
                if event.key == pygame.K_d:
                    x_speed = 6
                    x_speed_boost = 2
                if event.key == pygame.K_w:
                    y_speed = -6
                    y_speed_boost = 1
                if event.key == pygame.K_s:
                    y_speed = 6
                    y_speed_boost = 2

                if event.key == pygame.K_LSHIFT:

                    if x_speed_boost == 1:
                        x_speed = -10
                    if x_speed_boost == 2:
                        x_speed = 10
                    if y_speed_boost == 1:
                        y_speed = -10
                    if y_speed_boost == 2:
                        y_speed = 10                  

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a:
                    x_speed = 0
                    x_speed_boost = 0
                if event.key == pygame.K_d:
                    x_speed = 0
                    x_speed_boost = 0
                if event.key == pygame.K_w:
                    y_speed = 0
                    y_speed_boost = 0
                if event.key == pygame.K_s:
                    y_speed = 0
                    y_speed_boost = 0

        x_coord = x_coord + x_speed
        y_coord = y_coord + y_speed

        pygame.display.flip()
        pygame.display.update()

        clock.tick(20)

    pygame.quit()

2 个答案:

答案 0 :(得分:2)

使用keystate轮询代码将更简单/更清晰。如果游戏的其他部分使用“按下”逻辑,则可以使用事件处理。所以你的运动将是:

如果您致电pygame.display.flip(),则不要使用pygame.display.update()。事实上,它可能会减慢使用它们。

我使用了您的x_coord变量。但它会简化使用元组或向量进行玩家定位的事情。您可以使用浮子,以获得更平稳的移动精度。然后它作为屏幕的int进行blits。

while not done:
    for event in pygame.event.get():
        # any other key event input
        if event.type == QUIT:
            done = True        
        elif event.type == KEYDOWN:
            if event.key == K_ESC:
                done = True

    vel_x = 0
    vel_y = 0
    speed = 1

    if pygame.key.get_mods() & KMOD_SHIFT
        speed = 2


    # get key current state
    keys = pygame.key.get_pressed()
    if keys[K_A]:
        vel_x = -1
    if keys[K_D]:
        vel_x = 1
    if keys[K_W]:
        vel_y = -1
    if keys[K_S]:
        vel_y = 1


    x_coord += vel_x * speed
    y_coord += vel_y * speed

答案 1 :(得分:0)

您正在以每秒20帧的速度打勾。这可能会造成波动。把它改成像70这样更大的东西:

clock.tick(70)