简体   繁体   English

在OpenGL和Pygame中旋转3D多维数据集时遇到问题

[英]Trouble Rotating 3D Cube in OpenGL and Pygame

I am trying to create a 3D cube in OpenGL and Pygame. 我正在尝试在OpenGL和Pygame中创建3D立方体。 I want the cube to rotate when you hold down a button. 当您按住一个按钮时,我希望立方体旋转。 However, right now the cube only rotates when you press a button rather than holding. 但是,现在立方体仅在您按下按钮时才旋转,而不是按住。 Can someone help me with this issue? 有人可以帮我解决这个问题吗? Thanks! 谢谢!

def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

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

    glTranslatef(0.0, 0.0, -5)
    glRotatef(45, 1, 1, 0)

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

            keys = pygame.key.get_pressed()  # checking pressed keys
            if keys[pygame.K_a]:
                glRotatef(5, 1, 0, 0)
            if keys[pygame.K_s]:
                glRotatef(5, 0, 1, 0)
            if keys[pygame.K_d]:
                glRotatef(5, 0, 0, 1)

Your key handling code is within the for loop. 您的密钥处理代码在for循环内。 Hence, if there is no event, you will not handle the keys. 因此,如果没有事件,您将无法处理键。 Move the code outside of the loop: 将代码移出循环:

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

    keys = pygame.key.get_pressed()  # checking pressed keys
    if keys[pygame.K_a]:
        glRotatef(5, 1, 0, 0)
    if keys[pygame.K_s]:
        glRotatef(5, 0, 1, 0)
    if keys[pygame.K_d]:
        glRotatef(5, 0, 0, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM