Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

import pygame

from pygame.locals import *


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

# Initialize cube position


cube_x = 0.0

def draw_cube():
glBegin(GL_QUADS)

# Front face - Color: Red


glColor3f(1, 0, 0)
glVertex3f(1, 1, 1)
glVertex3f(1, -1, 1)
glVertex3f(-1, -1, 1)
glVertex3f(-1, 1, 1)

# Back face - Color: Green


glColor3f(0, 1, 0)
glVertex3f(1, 1, -1)
glVertex3f(1, -1, -1)
glVertex3f(-1, -1, -1)
glVertex3f(-1, 1, -1)

# Top face - Color: Blue


glColor3f(0, 0, 1)
glVertex3f(1, 1, 1)
glVertex3f(1, 1, -1)
glVertex3f(-1, 1, -1)
glVertex3f(-1, 1, 1)

# Bottom face - Color: Yellow


glColor3f(1, 1, 0)
glVertex3f(1, -1, 1)
glVertex3f(1, -1, -1)
glVertex3f(-1, -1, -1)
glVertex3f(-1, -1, 1)

# Right face - Color: Magenta


glColor3f(1, 0, 1)
glVertex3f(1, 1, 1)
glVertex3f(1, 1, -1)
glVertex3f(1, -1, -1)
glVertex3f(1, -1, 1)

# Left face - Color: Cyan


glColor3f(0, 1, 1)
glVertex3f(-1, 1, 1)
glVertex3f(-1, 1, -1)
glVertex3f(-1, -1, -1)
glVertex3f(-1, -1, 1)

glEnd()
glBegin(GL_TRIANGLES)
# Triangle 1 - Color: Red
glColor3f(1, 0, 0)
glVertex3f(1, 1, 1)
glVertex3f(1, 1, -1)
glVertex3f(1, -1, -1)

# Triangle 2 - Color: Red


glVertex3f(1, 1, 1)
glVertex3f(1, -1, -1)
glVertex3f(1, -1, 1)

# Triangle 3 - Color: Green


glColor3f(0, 1, 0)
glVertex3f(1, 1, 1)
glVertex3f(-1, 1, 1)
glVertex3f(-1, -1, 1)

# Triangle 4 - Color: Green


glVertex3f(1, 1, 1)
glVertex3f(-1, -1, 1)
glVertex3f(1, -1, 1)

# Triangle 5 - Color: Blue


glColor3f(0, 0, 1)
glVertex3f(1, 1, 1)
glVertex3f(1, 1, -1)
glVertex3f(-1, 1, 1)

# Triangle 6 - Color: Blue


glVertex3f(1, 1, -1)
glVertex3f(-1, 1, -1)
glVertex3f(-1, 1, 1)

# Triangle 7 - Color: Yellow


glColor3f(1, 1, 0)
glVertex3f(1, 1, 1)
glVertex3f(1, 1, -1)
glVertex3f(1, -1, 1)

# Triangle 8 - Color: Yellow


glVertex3f(1, 1, -1)
glVertex3f(1, -1, -1)
glVertex3f(1, -1, 1)

# Triangle 9 - Color: Magenta


glColor3f(1, 0, 1)
glVertex3f(1, -1, 1)
glVertex3f(1, -1, -1)
glVertex3f(1, 1, -1)

# Triangle 10 - Color: Magenta


glVertex3f(1, -1, -1)
glVertex3f(1, 1, -1)
glVertex3f(-1, -1, -1)

# Triangle 11 - Color: Cyan


glColor3f(0, 1, 1)
glVertex3f(1, 1, -1)
glVertex3f(-1, 1, -1)
glVertex3f(-1, -1, -1)

# Triangle 12 - Color: Cyan


glVertex3f(1, -1, -1)
glVertex3f(1, 1, -1)
glVertex3f(-1, -1, -1)

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)

global cube_x # Declare cube_x as a global variable

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
cube_x -= 1 # Move the cube to the left when 'A' key is
pressed

glRotatef(1, 3, 1, 1) # Rotate the cube


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

# Apply translation to the cube


glPushMatrix()
glTranslatef(cube_x, 0, 0)
draw_cube()
glPopMatrix()

pygame.display.flip()
pygame.time.wait(10)

if __name__ == "__main__":
main()

You might also like