在 Pygame 中使用 WASD 在立方体周围移动?

Move around cube using WASD in Pygame?

问题是当我按下一个键时相机继续移动,而不是当我按住键时它移动然后停在某个地方。我该如何解决?这是我导入 opengl 模块并为立方体创建顶点边和曲面的项目的代码。我正在尝试使用 pygame 和 pyopengl 创建一个立方体,我可以在其中使用 WASD 移动在立方体周围移动。

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verts = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1),
)

edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7),
)

surfaces = (
    (0, 1, 2, 3),
    (3, 2, 7, 6),
    (6, 7, 5, 4),
    (1, 5, 7, 2),
    (4, 5, 1, 0),
    (4, 0, 3, 6),
)

colors = (
    (1, 0, 0),
    (0, 1, 0),
    (0, 0, 1),
    (0,0,0),
    (1,1,1),
    (0,1,1),
    (1, 0, 0),
    (0, 1, 0),
    (0, 0, 1),
    (0,0,0),
    (1,1,1),
    (0,1,1),
)

def Cube():
    glBegin(GL_QUADS)
    x = 0
    for surface in surfaces:
        x+=1
        for vertex in surface:
            glColor3fv(colors[x])
            glVertex3fv(verts[vertex])
    glEnd()
    
    
    
    
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verts[vertex])


    glEnd()


def main():
    pygame.init()
    display = (1280, 720)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glRotatef(0, 0, 0, 0)
    pos = 0.0
    back = 0.0
    glTranslatef(pos, 0.0, back)
    while True:

        pressed_left = False
        pressed_right = False
        pressed_up = False
        pressed_down = False
        glTranslatef(pos, 0.0, back)

        x_move = 0
        y_move = 0

        #camera
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    pressed_left = True
                elif event.key == pygame.K_RIGHT:
                    pressed_right = True
                elif event.key == pygame.K_DOWN:
                    pressed_down = True
                elif event.key == pygame.K_UP:
                    pressed_up = True
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    pressed_left = False
                    print('off key')
                elif event.key == pygame.K_RIGHT:
                    pressed_right = False
                    print('off key')
                if event.key == pygame.K_UP:
                    pressed_up = False
                    print('off key')
                elif event.key == pygame.K_DOWN:
                    pressed_down = False
                    print('off key')

        if pressed_left == True:
            pos = -0.1
            print('key left')
        elif pressed_right == True:
            pos = 0.1
            print('key right')
        elif pressed_up == True:
            back = 0.1
            print('key up')
        elif pressed_down == True:
            back = -0.1
            print('key down')
        else:
            pos = pos
            back = back


        #glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)

main()

遗留的 OpenGL 矩阵变换,如 glTranslatef 不设置变换。矩阵运算定义一个新的变换矩阵,并将当前变换矩阵乘以新矩阵。

键盘事件(参见 pygame.event 模块)仅在键状态改变时发生一次。 KEYDOWN 事件在每次按下一个键时发生一次。 KEYUP 每次松开按键时发生一次。
pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() 评估按钮的当前状态并在按住某个键时获得连续移动:

def main():
    pygame.init()
    display = (1280, 720)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    clock = pygame.time.Clock()

    glEnable(GL_DEPTH_TEST)

    glMatrixMode(GL_PROJECTION)
    gluPerspective(90, (display[0]/display[1]), 0.1, 50.0)

    glMatrixMode(GL_MODELVIEW)
    pos, back = 0.0, -5.0
    glTranslatef(pos, 0.0, back)
    
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            glTranslatef(-0.1, 0.0, 0.0)
            print('key left')
        elif keys[pygame.K_RIGHT]:
            glTranslatef(0.1, 0.0, 0.0)
            print('key right')
        elif keys[pygame.K_UP]:
            glTranslatef(0.0, 0.1, 0.0)
            print('key up')
        elif keys[pygame.K_DOWN]:
            glTranslatef(0.0, -0.1, 0.0)
            print('key down')

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()