1

私は 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()
4

2 に答える 2

2

キーステート ポーリングを使用すると、コードがよりシンプル/明確になります。ゲームの他の部分で「オン プレス」ロジックが使用されている場合は、イベント処理を使用できます。したがって、あなたの動きは次のようになります。

呼び出している場合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 に答える