import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * # Define vertices, edges, and colors for the cube vertices = [ [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), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4), (1, 5), (2, 6), (3, 7) ] colors = [ (1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1), (1, 1, 1), (0, 0, 0) ] def draw_cube(): glBegin(GL_QUADS) for i in range(8): glColor3fv(colors[i]) glVertex3fv(vertices[i]) glEnd() glBegin(GL_LINES) glColor3fv((1, 1, 1)) for edge in edges: for vertex in edge: glVertex3fv(vertices[vertex]) glEnd() 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) x_rotation = 0 y_rotation = 0 clock = pygame.time.Clock() while True: 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: y_rotation = -1 elif event.key == pygame.K_RIGHT: y_rotation = 1 elif event.key == pygame.K_UP: x_rotation = -1 elif event.key == pygame.K_DOWN: x_rotation = 1 if event.type == pygame.KEYUP: if event.key in (pygame.K_LEFT, pygame.K_RIGHT): y_rotation = 0 if event.key in (pygame.K_UP, pygame.K_DOWN): x_rotation = 0 glRotatef(x_rotation, 1, 0, 0) glRotatef(y_rotation, 0, 1, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) draw_cube() pygame.display.flip() clock.tick(60) if __name__ == "__main__": main()