import pygame
from pygame.locals import *
import sys
from math import cos, sin, atan, atan2, degrees, radians
pygame.init()
# Set up display
WIDTH, HEIGHT = 800, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Rolling 3D Cube')
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (192, 192, 192)
# Cube vertices
vertices = [[-1, -1, -1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, 1],
[1, -1, 1],
[1, 1, 1],
[-1, 1, 1]]
# Road vertices
road_vertices = [[-200, 300, -1],
[200, 300, -1],
[200, 600, -1],
[-200, 600, -1]]
# Camera settings
camera_distance = 200
camera_height = 200
camera_angle_vertical = radians(45)
camera_angle_horizontal = radians(45)
cube_position = [WIDTH // 2, HEIGHT // 2]
cube_size = 100
angle = 0
def project(vertex):
x, y, z = vertex
f = 200
x = x * f / (z + f) + cube_position[0]
y = y * f / (z + f) + cube_position[1]
return x, y
def rotate(vertices, angle):
new_vertices = []
for vertex in vertices:
x, y, z = vertex
new_x = x * cos(angle) - z * sin(angle)
new_z = x * sin(angle) + z * cos(angle)
new_vertices.append([new_x, y, new_z])
return new_vertices
def get_camera_position():
x = cube_position[0] - camera_distance * sin(camera_angle_horizontal) * cos(camera_angle_vertical)
y = cube_position[1] - camera_distance * sin(camera_angle_vertical)
return x, y
def get_camera_angle():
return atan2(camera_height, camera_distance)
def flat_shading(face):
average_z = sum(v[2] for v in face) / 4
if average_z > 0:
return WHITE
else:
return GRAY
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
WINDOW.fill(WHITE)
rotated_vertices = rotate(vertices, angle)
# Calculate face colors based on flat shading
faces = [(rotated_vertices[0], rotated_vertices[1], rotated_vertices[2], rotated_vertices[3]),
(rotated_vertices[4], rotated_vertices[5], rotated_vertices[6], rotated_vertices[7]),
(rotated_vertices[0], rotated_vertices[1], rotated_vertices[5], rotated_vertices[4]),
(rotated_vertices[2], rotated_vertices[3], rotated_vertices[7], rotated_vertices[6]),
(rotated_vertices[0], rotated_vertices[3], rotated_vertices[7], rotated_vertices[4]),
(rotated_vertices[1], rotated_vertices[2], rotated_vertices[6], rotated_vertices[5)]
for face in faces:
color = flat_shading(face)
pygame.draw.polygon(WINDOW, color, [project(v) for v in face])
camera_position = get_camera_position()
camera_angle = get_camera_angle()
pygame.draw.line(WINDOW, BLACK, cube_position, camera_position)
pygame.draw.polygon(WINDOW, GRAY, [project(road_vertices[0]), project(road_vertices[1]), project(road_vertices[2]), project(road_vertices[3])])
pygame.display.flip()
angle += 0.01
clock.tick(60)
import pygame
from pygame.locals import *
import sys
from math import cos, sin, atan, atan2, degrees, radians
import random
pygame.init()
# Set up display
WIDTH, HEIGHT = 800, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Rolling 3D Cube')
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (192, 192, 192)
# Cube vertices
vertices = [[-1, -1, -1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, 1],
[1, -1, 1],
[1, 1, 1],
[-1, 1, 1]]
# Road vertices
road_vertices = [[-200, 300, -1],
[200, 300, -1],
[200, 600, -1],
[-200, 600, -1]]
# Camera settings
camera_distance = 200
camera_height = 200
camera_angle_vertical = radians(45)
camera_angle_horizontal = radians(45)
cube_position = [WIDTH // 2, HEIGHT // 2]
cube_size = 100
angle = 0
def project(vertex):
x, y, z = vertex
f = 200
x = x * f / (z + f) + cube_position[0]
y = y * f / (z + f) + cube_position[1]
return x, y
def rotate(vertices, angle):
new_vertices = []
for vertex in vertices:
x, y, z = vertex
new_x = x * cos(angle) - z * sin(angle)
new_z = x * sin(angle) + z * cos(angle)
new_vertices.append([new_x, y, new_z])
return new_vertices
def get_camera_position():
x = cube_position[0] - camera_distance * sin(camera_angle_horizontal) * cos(camera_angle_vertical)
y = cube_position[1] - camera_distance * sin(camera_angle_vertical)
return x, y
def get_camera_angle():
return atan2(camera_height, camera_distance)
def dithering_shading(face):
dither_matrix = [[0.5, 0.75, 0.25, 0.5],
[0.75, 1.0, 0.5, 0.75],
[0.25, 0.5, 0.0, 0.25],
[0.5, 0.75, 0.25, 0.5]]
average_z = sum(v[2] for v in face) / 4
dither_value = random.random()
if dither_value < dither_matrix[int(3 * (average_z + 1) / 2)][random.randint(0, 3)]:
return WHITE
else:
return GRAY
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
WINDOW.fill(WHITE)
rotated_vertices = rotate(vertices, angle)
# Calculate face colors based on dithering
faces = [(rotated_vertices[0], rotated_vertices[1], rotated_vertices[2], rotated_vertices[3]),
(rotated_vertices[4], rotated_vertices[5], rotated_vertices[6], rotated_vertices[7]),
(rotated_vertices[0], rotated_vertices[1], rotated_vertices[5], rotated_vertices[4]),
(rotated_vertices[2], rotated_vertices[3], rotated_vertices[7], rotated_vertices[6]),
(rotated_vertices[0], rotated_vertices[3], rotated_vertices[7], rotated_vertices[4]),
(rotated_vertices[1], rotated_vertices[2], rotated_vertices[6], rotated_vertices[5)]
for face in faces:
color = dithering_shading(face)
pygame.draw.polygon(WINDOW, color, [project(v) for v in face])
camera_position = get_camera_position()
camera_angle = get_camera_angle()
pygame.draw.line(WINDOW, BLACK, cube_position, camera_position)
pygame.draw.polygon(WINDOW, GRAY, [project(road_vertices[0]), project(road_vertices[1]), project(road_vertices[2]), project(road_vertices[3])])
pygame.display.flip()
angle += 0.01
clock.tick(60)