微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pygame 和 PyOpenGL:屏幕上不显示任何形状

如何解决Pygame 和 PyOpenGL:屏幕上不显示任何形状

我之前使用 C++ 和 OpenGL 创建了一个完整的蛇游戏,我想使用 Python、pygame 和 PyOpenGL 做同样的事情。我目前遇到的问题是我生成水果后,它没有出现在屏幕上。这是我的主要功能代码

def get_dataset_keys(f):
    keys = []
    f.visit(lambda key : keys.append(key) if isinstance(f[key],h5py.Dataset) else None)
    return keys

我可能缺少 pygame 或 pyopengl 函数,但我不确定。我也尝试将 def main(): # Main function # Initialize game components game = Game(800,600) test_fruit = game.spawn_fruit(Point(100,100)) # Initialize pygame module pygame.init() pygame.display.set_mode(game.get_window_size(),DOUBLEBUF | OPENGL) pygame.display.set_caption("Python Game") # Define variable to control main loop running = True # Main loop while running: # event handling,gets all event from the event queue for event in pygame.event.get(): # only do something if the event is of type QUIT if event.type == pygame.QUIT: # change the value to False,to exit the main loop running = False # Modify game properties glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) game.draw_shapes() pygame.display.flip() pygame.time.wait(5) 更改为 pygame.display.flip(),但它却给了我一个错误(“无法更新 OpenGL 显示”)。

这是我试图显示的形状的代码

pygame.display.update()

解决方法

OpenGL 坐标在 [-1.0,1.0] 范围内(标准化设备空间)。 Normalized device space 是一个独特的立方体,从左、底、近 (-1,-1,-1) 到右、顶、远 (1,1,1)。
如果要使用“窗口”坐标,则必须使用 Orthographic projection 指定 glOrtho

glOrtho(0,800,600,1)

使用 glMatrixMode 选择矩阵模式并使用 Identity matrix 加载 glLoadIdentity

示例:

def main():     # Main function
    # Initialize game components
    game = Game(800,600)
    test_fruit = game.spawn_fruit(Point(100,100))

    # Initialize pygame module
    pygame.init()
    pygame.display.set_mode(game.get_window_size(),DOUBLEBUF | OPENGL)
    pygame.display.set_caption("Python Game")

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity() 

    # Define variable to control main loop
    running = True

    # [...]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。