Paste Code
Paste Blends
Paste Images
import pygame, sys
from math import *

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Spinning cube! 8-)")
clock = pygame.time.Clock()

counter = 0


def Radians(angle):
return ((float(angle) / 180.0) * pi)

def Transform(vertex, matrix):
Vx = (vertex[0]*matrix[0][0]) + (vertex[1] * matrix[1][0]) + (vertex[2] * matrix[2][0])
Vy = (vertex[0]*matrix[0][1]) + (vertex[1] * matrix[1][1]) + (vertex[2] * matrix[2][1])
Vz = (vertex[0]*matrix[0][2]) + (vertex[1] * matrix[1][2]) + (vertex[2] * matrix[2][2])

return (Vx, Vy, Vz)


class Cube():
def __init__(self, position, size):
self.position = position
self.size = float(size) / 2.0
self.vertices = [ # Creating the eight vertices
[self.position[0] + self.size, self.position[1] + self.size, self.position[2] + self.size],
[self.position[0] + self.size, self.position[1] + self.size, self.position[2] - self.size],
[self.position[0] + self.size, self.position[1] - self.size, self.position[2] + self.size],
[self.position[0] + self.size, self.position[1] - self.size, self.position[2] - self.size],
[self.position[0] - self.size, self.position[1] + self.size, self.position[2] + self.size],
[self.position[0] - self.size, self.position[1] + self.size, self.position[2] - self.size],
[self.position[0] - self.size, self.position[1] - self.size, self.position[2] + self.size],
[self.position[0] - self.size, self.position[1] - self.size, self.position[2] - self.size]
]
# Defining which vertices to connect with lines - written by a process of trial and error; edit at your peril
self.lines = [[0, 1], [1, 3], [2, 3], [2, 0], [0, 4], [4, 5], [5, 1], [4, 6], [6, 7], [7, 5], [6, 2], [7, 3]]

def Rotate(self, axis, angle):
angle = Radians(angle)# Convert the angle to radians (Python doesn't like degrees, apparently)

if axis == 0: # X
matrix = [
[1, 0, 0],
[0, cos(angle), sin(angle)],
[0, -sin(angle), cos(angle)]
]
elif axis == 1: # Y
matrix = [
[cos(angle), 0, -sin(angle)],
[0, 1, 0],
[sin(angle), 0, cos(angle)]
]
elif axis == 2: # Z
matrix = [
[cos(angle), sin(angle), 0],
[-sin(angle), cos(angle), 0],
[0, 0, 1]
]

temp = [] # Temporary list of transformed vertices
for vertex in self.vertices:
temp.append(Transform(vertex, matrix))

self.vertices = temp

def Scale(self, axis, k):
if axis == 0: # X
matrix = [
[k, 0, 0],
[0, 1, 0],
[0, 0, 1]
]
if axis == 1: # Y
matrix = [
[1, 0, 0],
[0, k, 0],
[0, 0, 1]
]
if axis == 0: # Z
matrix = [
[1, 0, 0],
[0, 1, 0],
[0, 0, k]
]

temp = []
for vertex in self.vertices:
temp.append(Transform(vertex, matrix))

self.vertices = temp


cubes = [] # The list of cubes in the scene

cube1 = Cube((0, 0, 0), 250)
cube1.Rotate(0, 20)
cube1.Rotate(2, 35)

cube2 = Cube((200, 100, 500), 250)
cube2.Rotate(2, 55)

cubes.append(cube1)
cubes.append(cube2)


def DimensionChange(vertex):
# Converts a 3D point into a 2D coordinate by projecting onto the XY axis
# (a n00bish solution that renders camera movement impossible for the moment :P)
matrix = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 0]
]

Vx, Vy, Vz = Transform(vertex, matrix)

return (Vx, Vy)

def Redraw():
# Keep the cubes spinning
cube1.Rotate(1, 3)
cube1.Rotate(2, -2)
cube2.Rotate(1, 3)
cube2.Rotate(2, -2)

for cube in cubes:
points = [] # List of 2D points
for vertex in cube.vertices: # Get the 2D points to draw
points.append(DimensionChange(vertex))

temp = []
for point in points: # Centre the points on-screen (such that the point (0, 0, 0) is displayed at the centre of the screen)
temp.append([400+point[0], 300+point[1]])
points = temp

for point in points: # Draw points
pygame.draw.circle(screen, (255,255,255), (point[0], point[1]), 2)

for line in cube.lines: # Draw lines
pygame.draw.aaline(screen, (255, 255, 255), points[line[0]], points[line[1]])


while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

screen.fill((0, 0, 0))

Redraw()

counter += 1 # Was once used, but isn't now. But you never know when it could come in handy

pygame.display.update()
clock.tick(25)
  1. import pygame, sys
  2. from math import *
  3.  
  4. pygame.init()
  5. screen = pygame.display.set_mode((800, 600))
  6. pygame.display.set_caption("Spinning cube! 8-)")
  7. clock = pygame.time.Clock()
  8.  
  9. counter = 0
  10.  
  11.  
  12. def Radians(angle):
  13.     return ((float(angle) / 180.0) * pi)
  14.  
  15. def Transform(vertex, matrix):
  16.     Vx = (vertex[0]*matrix[0][0]) + (vertex[1] * matrix[1][0]) + (vertex[2] * matrix[2][0])
  17.     Vy = (vertex[0]*matrix[0][1]) + (vertex[1] * matrix[1][1]) + (vertex[2] * matrix[2][1])
  18.     Vz = (vertex[0]*matrix[0][2]) + (vertex[1] * matrix[1][2]) + (vertex[2] * matrix[2][2])
  19.  
  20.     return (Vx, Vy, Vz)
  21.  
  22.  
  23. class Cube():
  24.     def __init__(self, position, size):
  25.         self.position = position
  26.         self.size = float(size) / 2.0
  27.         self.vertices = [  # Creating the eight vertices
  28.             [self.position[0] + self.size, self.position[1] + self.size, self.position[2] + self.size],
  29.             [self.position[0] + self.size, self.position[1] + self.size, self.position[2] - self.size],
  30.             [self.position[0] + self.size, self.position[1] - self.size, self.position[2] + self.size],
  31.             [self.position[0] + self.size, self.position[1] - self.size, self.position[2] - self.size],
  32.             [self.position[0] - self.size, self.position[1] + self.size, self.position[2] + self.size],
  33.             [self.position[0] - self.size, self.position[1] + self.size, self.position[2] - self.size],
  34.             [self.position[0] - self.size, self.position[1] - self.size, self.position[2] + self.size],
  35.             [self.position[0] - self.size, self.position[1] - self.size, self.position[2] - self.size]
  36.         ]
  37.         # Defining which vertices to connect with lines - written by a process of trial and error; edit at your peril
  38.         self.lines = [[0, 1], [1, 3], [2, 3], [2, 0], [0, 4], [4, 5], [5, 1], [4, 6], [6, 7], [7, 5], [6, 2], [7, 3]]
  39.  
  40.     def Rotate(self, axis, angle):
  41.         angle = Radians(angle)# Convert the angle to radians (Python doesn't like degrees, apparently)
  42.  
  43.         if axis == 0:  # X
  44.             matrix = [
  45.                 [1, 0, 0],
  46.                 [0, cos(angle), sin(angle)],
  47.                 [0, -sin(angle), cos(angle)]
  48.             ]
  49.         elif axis == 1:  # Y
  50.             matrix = [
  51.                 [cos(angle), 0, -sin(angle)],
  52.                 [0, 1, 0],
  53.                 [sin(angle), 0, cos(angle)]
  54.             ]
  55.         elif axis == 2:  # Z
  56.             matrix = [
  57.                 [cos(angle), sin(angle), 0],
  58.                 [-sin(angle), cos(angle), 0],
  59.                 [0, 0, 1]
  60.             ]
  61.  
  62.         temp = []  # Temporary list of transformed vertices
  63.         for vertex in self.vertices:
  64.             temp.append(Transform(vertex, matrix))
  65.  
  66.         self.vertices = temp
  67.    
  68.     def Scale(self, axis, k):
  69.         if axis == 0:  # X
  70.             matrix = [
  71.                 [k, 0, 0],
  72.                 [0, 1, 0],
  73.                 [0, 0, 1]
  74.             ]
  75.         if axis == 1:  # Y
  76.             matrix = [
  77.                 [1, 0, 0],
  78.                 [0, k, 0],
  79.                 [0, 0, 1]
  80.             ]
  81.         if axis == 0:  # Z
  82.             matrix = [
  83.                 [1, 0, 0],
  84.                 [0, 1, 0],
  85.                 [0, 0, k]
  86.             ]
  87.  
  88.         temp = []
  89.         for vertex in self.vertices:
  90.             temp.append(Transform(vertex, matrix))
  91.  
  92.         self.vertices = temp
  93.  
  94.  
  95. cubes = []  # The list of cubes in the scene
  96.  
  97. cube1 = Cube((0, 0, 0), 250)
  98. cube1.Rotate(0, 20)
  99. cube1.Rotate(2, 35)
  100.  
  101. cube2 = Cube((200, 100, 500), 250)
  102. cube2.Rotate(2, 55)
  103.  
  104. cubes.append(cube1)
  105. cubes.append(cube2)
  106.  
  107.  
  108. def DimensionChange(vertex):
  109.     # Converts a 3D point into a 2D coordinate by projecting onto the XY axis
  110.     # (a n00bish solution that renders camera movement impossible for the moment :P)
  111.     matrix = [
  112.         [1, 0, 0],
  113.         [0, 1, 0],
  114.         [0, 0, 0]
  115.     ]
  116.  
  117.     Vx, Vy, Vz = Transform(vertex, matrix)
  118.    
  119.     return (Vx, Vy)
  120.  
  121. def Redraw():
  122.     # Keep the cubes spinning
  123.     cube1.Rotate(1, 3)
  124.     cube1.Rotate(2, -2)
  125.     cube2.Rotate(1, 3)
  126.     cube2.Rotate(2, -2)
  127.  
  128.     for cube in cubes:
  129.         points = []  # List of 2D points
  130.         for vertex in cube.vertices:  # Get the 2D points to draw
  131.             points.append(DimensionChange(vertex))
  132.  
  133.         temp = []
  134.         for point in points:  # Centre the points on-screen (such that the point (0, 0, 0) is displayed at the centre of the screen)
  135.             temp.append([400+point[0], 300+point[1]])
  136.         points = temp
  137.  
  138.         for point in points:  # Draw points
  139.             pygame.draw.circle(screen, (255,255,255), (point[0], point[1]), 2)
  140.  
  141.         for line in cube.lines:  # Draw lines
  142.             pygame.draw.aaline(screen, (255, 255, 255), points[line[0]], points[line[1]])
  143.  
  144.  
  145. while True:
  146.     for event in pygame.event.get():
  147.         if event.type == pygame.QUIT:
  148.             sys.exit()
  149.  
  150.     screen.fill((0, 0, 0))
  151.  
  152.     Redraw()
  153.    
  154.     counter += 1  # Was once used, but isn't now. But you never know when it could come in handy
  155.    
  156.     pygame.display.update()
  157.     clock.tick(25)
  158.  
go to heaven