Skip to content

Instantly share code, notes, and snippets.

@K4CZP3R
Created May 14, 2018 12:50
Show Gist options
  • Save K4CZP3R/114c4f79df2144c8176f5e60505a69ae to your computer and use it in GitHub Desktop.
Save K4CZP3R/114c4f79df2144c8176f5e60505a69ae to your computer and use it in GitHub Desktop.
Project for school
#https://imgur.com/a/AU4mXIt
import pygame, math
def main(w,h,fps):
pygame.init()
pygame.font.init()
pygame.display.set_caption('X-as=UP/DOWN Y-as=LEFT/RIGHT Z-as=O/K')
myfont = pygame.font.SysFont('Arial',16)
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
key_x=0
key_y=0
key_z=0
speed=0.01
while True:
screen.fill((0, 0, 0))
cube_points = [
[-1, -1, 1], #0
[-1, 1, 1], #1
[1, 1, 1], #2
[1, -1, 1], #3
[-1, -1, -1],#4
[-1, 1, -1], #5
[1, 1, -1], #6
[1, -1, -1] #7
]
connections = [
(0, 1),
(1, 2),
(2, 3),
(3, 0),
(4, 5),
(5, 6),
(6, 7),
(7, 4),
(0, 4),
(1, 5),
(2, 6),
(3, 7)
]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit();
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
key_x -= speed
if event.key == pygame.K_DOWN:
key_x += speed
if event.key == pygame.K_LEFT:
key_y -= speed
if event.key == pygame.K_RIGHT:
key_y += speed
if event.key == pygame.K_o:
key_z += speed
if event.key == pygame.K_k:
key_z -= speed
points = rotate3dPoints(cube_points,key_x,key_y,key_z)
flattened_points = []
for point in points:
flattened_points.append(
(point[0] * (1+1.0/(point[2]+3)),
point[1] * (1+1.0/(point[2]+3)))
)
for con in connections:
p1 = flattened_points[con[0]]
p2 = flattened_points[con[1]]
x1 = p1[0]*60+200
y1 = p1[1]*60+200
x2 = p2[0]*60+200
y2 = p2[1]*60+200
pygame.draw.line(screen,(255,0,0),(x1,y1),(x2,y2),1)
pygame.display.flip()
clock.tick(fps)
def rotate3dPoints(points,angX,angY,angZ):
new_points=[]
for point in points:
x=point[0]
y=point[1]
z=point[2]
new_x = x * math.cos(angY) - z * math.sin(angY)
new_z = x * math.sin(angY) + z * math.cos(angY)
x = new_x
z = new_z
new_y = y * math.cos(angX) - z * math.sin(angX)
new_z = y * math.sin(angX) + z * math.cos(angX)
y = new_y
z = new_z
new_x = x * math.cos(angZ) - y * math.sin(angZ)
new_y = x * math.sin(angZ) + y * math.cos(angZ)
x = new_x
y = new_y
new_points.append([x, y, z])
return new_points
main(800,800,60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment