1

我正在开发一个非常基本的引擎,基于 Pygame 的教程,我在“流畅性”方面遇到了一点问题。如何让我的玩家走得“更顺畅”?

我的事件处理程序非常基本,非常标准,没有什么新东西,我什至想出了如何为测试进行“提升”(运行)。但问题是,在 0 处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()
4

2 回答 2

2

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

如果您正在调用,pygame.display.flip()那么您不使用pygame.display.update(). 事实上,使用两者可能会减慢它的速度。

我用了你的x_coord变量。但它会简化使用元组或向量来定位玩家位置的事情。您可以使用浮点数,以获得更平滑的运动精度。然后它作为一个 int 闪烁到屏幕上。

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
于 2012-11-14T14:13:22.710 回答
0

您正在以每秒 20 帧的速度计时。这可能是导致波涛汹涌的原因。将其更改为更大的值,例如 70:

clock.tick(70)
于 2012-11-14T12:32:44.220 回答