Created
May 1, 2023 05:38
-
-
Save EncodeTheCode/60489029e11ddc51d714399fe64ffeb2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
from pygame.locals import * | |
from OpenGL.GL import * | |
from OpenGL.GLU import * | |
import numpy as np | |
import random | |
pygame.init() | |
clock = pygame.time.Clock() | |
# Set up the display | |
display = (800, 600) | |
pygame.display.set_mode(display, DOUBLEBUF | OPENGL) | |
# Set up the projection matrix | |
glMatrixMode(GL_PROJECTION) | |
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0) | |
# Set up the modelview matrix | |
glMatrixMode(GL_MODELVIEW) | |
gluLookAt(0, 0, -5, 0, 0, 0, 0, 1, 0) | |
# Define the vertices of the cube | |
vertices = np.array([ | |
[-1, -1, -1], | |
[-1, 1, -1], | |
[1, 1, -1], | |
[1, -1, -1], | |
[-1, -1, 1], | |
[-1, 1, 1], | |
[1, 1, 1], | |
[1, -1, 1], | |
]) | |
# Define the indices of the cube faces | |
indices = np.array([ | |
[0, 1, 2, 3], | |
[4, 5, 6, 7], | |
[1, 5, 6, 2], | |
[0, 4, 7, 3], | |
[3, 2, 6, 7], | |
[0, 1, 5, 4], | |
]) | |
# Define the color values for each face of the cube | |
colors = np.random.rand(6, 3) | |
theta = 0 | |
while True: | |
# Handle events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
quit() | |
# Clear the screen and depth buffer | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
# Set up the rotation matrix | |
theta += 1 | |
rotation_matrix = np.array([ | |
[np.cos(np.radians(theta)), -np.sin(np.radians(theta)), 0], | |
[np.sin(np.radians(theta)), np.cos(np.radians(theta)), 0], | |
[0, 0, 1], | |
]) | |
# Rotate the vertices and project them onto the screen | |
rotated_vertices = np.dot(vertices, rotation_matrix) | |
for i, face in enumerate(indices): | |
glBegin(GL_QUADS) | |
glColor3fv(colors[i]) | |
for vertex_index in face: | |
glVertex3fv(rotated_vertices[vertex_index]) | |
glEnd() | |
# Swap buffers | |
pygame.display.flip() | |
# Change colors every second | |
if pygame.time.get_ticks() % 1000 == 0: | |
colors = np.random.rand(6, 3) | |
# Wait for a short amount of time to limit the frame rate | |
clock.tick(60) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment