Zeichne Pixel auf eine gemeinsame Zeichenfläche! Deine Pixel bleiben so lange stehen, bis jemand anderes etwas darüber zeichnet.
Die Zeichenfläche ist 256 Pixel breit und 144 Pixel hoch. Der Pixel links oben hat die Koordinaten (0, 0), der Pixel rechts unten hat die Koordinaten (255, 143). Verwende die Methode self.set_pixel(x, y, r, g, b), um einen Pixel zu setzen, wobei r, g und b jeweils die Intensität von rot, grün und blau im Bereich von 0 bis 255 angeben.
Du darfst natürlich auch die ganze Zeichenfläche überschreiben!
import math
import time
import os
# Bildschirmgröße
WIDTH, HEIGHT = 40, 20
# 3D-Würfel-Eckpunkte
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]
]
# 3D-Würfel-Kanten
edges = [
(0, 1), (1, 2), (2, 3), (3, 0),
(4, 5), (5, 6), (6, 7), (7, 4),
(0, 4), (1, 5), (2, 6), (3, 7)
]
def project(vertex):
# Perspektivische Projektion
factor = 5 / (vertex[2] + 5)
x = int(WIDTH / 2 + vertex[0] * factor * WIDTH / 2)
y = int(HEIGHT / 2 - vertex[1] * factor * HEIGHT / 2)
return x, y
def draw_cube():
canvas = [[' ' for _ in range(WIDTH)] for _ in range(HEIGHT)]
for edge in edges:
p1 = project(vertices[edge[0]])
p2 = project(vertices[edge[1]])
if p1[0] >= 0 and p1[0] < WIDTH and p1[1] >= 0 and p1[1] < HEIGHT:
canvas[p1[1]][p1[0]] = '*'
if p2[0] >= 0 and p2[0] < WIDTH and p2[1] >= 0 and p2[1] < HEIGHT:
canvas[p2[1]][p2[0]] = '*'
os.system('cls' if os.name == 'nt' else 'clear')
for row in reversed(canvas):
print(''.join(row))
angle_x, angle_y = 0
while True:
angle_x += 0.05
angle_y += 0.05
# Rotationsmatrizen
cos_x, sin_x = math.cos(angle_x), math.sin(angle_x)
cos_y, sin_y = math.cos(angle_y), math.sin(angle_y)
rotated_vertices = []
for v in vertices:
x, y, z = v
x_rot = x * cos_y - z * sin_y
y_rot = y * cos_x - z * sin_x
z_rot = x * sin_y + z * cos_y
x_final = x_rot
y_final = y_rot
z_final = z_rot
rotated_vertices.append([x_final, y_final, z_final])
vertices = rotated_vertices
draw_cube()
time.sleep(0.1)