Advertisement
cookertron

Rotating Colored Spinning Cube Using Python and Pygame

Feb 24th, 2023
1,260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | Source Code | 0 0
  1. import pygame
  2. import math
  3.  
  4. def face_distance(face):
  5.     distance = 0
  6.     for i in face:
  7.         for k in range(3):
  8.             distance += (cube_points_rotated[i][k] - camera_position[k])**2
  9.     return distance
  10.  
  11. pygame.init()
  12.  
  13. # set up the window
  14. screen_width, screen_height = 640, 480
  15. screen = pygame.display.set_mode((screen_width, screen_height))
  16. pygame.display.set_caption("Spinning Solid Cube")
  17.  
  18. # set up the cube
  19. cube_size = 50
  20. cube_points = [(-cube_size, -cube_size, -cube_size),
  21.                (cube_size, -cube_size, -cube_size),
  22.                (cube_size, cube_size, -cube_size),
  23.                (-cube_size, cube_size, -cube_size),
  24.                (-cube_size, -cube_size, cube_size),
  25.                (cube_size, -cube_size, cube_size),
  26.                (cube_size, cube_size, cube_size),
  27.                (-cube_size, cube_size, cube_size)]
  28.  
  29. cube_faces = [(0, 1, 2, 3), (0, 4, 5, 1), (1, 5, 6, 2),
  30.               (2, 6, 7, 3), (3, 7, 4, 0), (4, 7, 6, 5)]
  31.  
  32. # create a dictionary that maps each face to its corresponding color
  33. face_colors = {
  34.     (0, 1, 2, 3): (255, 0, 0),   # red
  35.     (0, 4, 5, 1): (0, 255, 0),   # green
  36.     (1, 5, 6, 2): (0, 0, 255),   # blue
  37.     (2, 6, 7, 3): (255, 255, 0), # yellow
  38.     (3, 7, 4, 0): (0, 255, 255), # cyan
  39.     (4, 7, 6, 5): (255, 0, 255)  # magenta
  40. }
  41.  
  42. # set up the rotation angles
  43. angle_x = 0
  44. angle_y = 0
  45. angle_z = 0
  46.  
  47. # set up the camera
  48. camera_position = [0, 0, -500]
  49. camera_orientation = [0, 0, 0]
  50.  
  51. # main game loop
  52. while True:
  53.     # handle events
  54.     for event in pygame.event.get():
  55.         if event.type == pygame.QUIT:
  56.             pygame.quit()
  57.             exit()
  58.  
  59.     # clear the screen
  60.     screen.fill((0, 0, 0))
  61.  
  62.     # rotate the cube
  63.     angle_x += 0.01
  64.     angle_y += 0.02
  65.     angle_z += 0.03
  66.  
  67.     rotation_x = [[1, 0, 0],
  68.                   [0, math.cos(angle_x), -math.sin(angle_x)],
  69.                   [0, math.sin(angle_x), math.cos(angle_x)]]
  70.  
  71.     rotation_y = [[math.cos(angle_y), 0, math.sin(angle_y)],
  72.                   [0, 1, 0],
  73.                   [-math.sin(angle_y), 0, math.cos(angle_y)]]
  74.  
  75.     rotation_z = [[math.cos(angle_z), -math.sin(angle_z), 0],
  76.                   [math.sin(angle_z), math.cos(angle_z), 0],
  77.                   [0, 0, 1]]
  78.  
  79.     cube_points_rotated = []
  80.  
  81.     for point in cube_points:
  82.         # rotate the point using the three rotation matrices
  83.         point_rotated = point
  84.         point_rotated = [sum([rotation_x[i][j] * point_rotated[j] for j in range(3)]) for i in range(3)]
  85.         point_rotated = [sum([rotation_y[i][j] * point_rotated[j] for j in range(3)]) for i in range(3)]
  86.         point_rotated = [sum([rotation_z[i][j] * point_rotated[j] for j in range(3)]) for i in range(3)]
  87.  
  88.         # add the rotated point to the list of rotated points
  89.         cube_points_rotated.append(point_rotated)
  90.  
  91.     # sort the faces based on their distance from the camera
  92.     sorted_faces = sorted(cube_faces, key=face_distance)
  93.  
  94.     # draw the faces of the cube as polygons
  95.     for face in sorted_faces:
  96.         points = [cube_points_rotated[i] for i in face]
  97.         color = face_colors[face]
  98.         pygame.draw.polygon(screen, color, [(p[0] + screen_width / 2, p[1] + screen_height / 2) for p in points])
  99.  
  100.     # update the screen
  101.     pygame.display.update()
  102.  
  103.     pygame.time.Clock().tick(120)
  104.  
Tags: python pygame 3D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement