generated from pimoroni/pga
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import time | ||
from picographics import PicoGraphics, DISPLAY_EXPLORER, PEN_P8 | ||
import math | ||
from random import randint, randrange | ||
|
||
display = PicoGraphics(display=DISPLAY_EXPLORER, pen_type=PEN_P8) | ||
WIDTH, HEIGHT = display.get_bounds() | ||
|
||
# Define colours | ||
BLACK = display.create_pen(0, 0, 0) | ||
WHITE = display.create_pen(255, 255, 255) | ||
GREEN = display.create_pen(0, 255, 0) | ||
|
||
|
||
class Cube(object): | ||
# The corners of the cube | ||
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]] | ||
|
||
# The corners that will be connected together to make a cube :) | ||
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 __init__(self, fov, distance, x, y, speed): | ||
self.tick = time.ticks_ms() / 1000.0 | ||
self.cos = math.cos(self.tick) | ||
self.sin = math.sin(self.tick) | ||
self.fov = fov | ||
self.distance = distance | ||
self.pos_x = x | ||
self.pos_y = y | ||
self.speed = speed | ||
|
||
self.cube_points = [] | ||
|
||
# Project our points | ||
def to_2d(self, x, y, z, pos_x, pos_y, fov, distance): | ||
factor = fov / (distance + z) | ||
x = x * factor + pos_x | ||
y = -y * factor + pos_y | ||
|
||
return int(x), int(y) | ||
|
||
def return_tick(self): | ||
return self.tick | ||
|
||
# Clear our points and recalculate the sin and cos values | ||
def _update(self): | ||
|
||
self.cube_points = [] | ||
|
||
self.tick = time.ticks_ms() / (self.speed * 1000) | ||
self.cos = math.cos(self.tick) | ||
self.sin = math.sin(self.tick) | ||
|
||
def set_fov(self, fov): | ||
self.fov = fov | ||
|
||
def set_distance(self, distance): | ||
self.distance = distance | ||
|
||
def set_speed(self, speed): | ||
self.speed = speed | ||
|
||
def set_x(self, x): | ||
self.pos_x = x | ||
|
||
def set_y(self, y): | ||
self.pos_y = y | ||
|
||
def get_fov(self): | ||
return self.fov | ||
|
||
# Rotate on XYZ and save the new points in our list | ||
def rotate(self): | ||
|
||
for v in self.vertices: | ||
|
||
start_x, start_y, start_z = v | ||
|
||
# X | ||
y = start_y * self.cos - start_z * self.sin | ||
z = start_y * self.sin + start_z * self.cos | ||
|
||
# Y | ||
x = start_x * self.cos - z * self.sin | ||
z = start_x * self.sin + z * self.cos | ||
|
||
# Z | ||
n_y = x * self.sin + y * self.cos | ||
n_x = x * self.cos - y * self.sin | ||
|
||
y = n_y | ||
x = n_x | ||
|
||
point = self.to_2d(x, y, z, self.pos_x, self.pos_y, self.fov, self.distance) | ||
self.cube_points.append(point) | ||
|
||
# Draw the edges of the cube so we can see it on screen! | ||
def draw(self): | ||
|
||
for edge in self.edges: | ||
display.line(self.cube_points[edge[0]][0], self.cube_points[edge[0]][1], self.cube_points[edge[1]][0], self.cube_points[edge[1]][1]) | ||
|
||
self._update() | ||
|
||
|
||
# Setup the first 3 cubes. | ||
cubes = [Cube(16, 4, WIDTH / 2, HEIGHT / 2, 1.0), Cube(32, 4, 100, 100, 0.9), Cube(32, 4, 100, 100, 0.5)] | ||
|
||
# Set our initial pen colour | ||
pen = display.create_pen_hsv(1.0, 1.0, 1.0) | ||
|
||
while 1: | ||
|
||
# We'll use this for cycling through the rainbow | ||
t = time.ticks_ms() / 1000 | ||
|
||
# Clear the screen and set the pen colour for the cubes | ||
display.set_pen(BLACK) | ||
display.clear() | ||
display.reset_pen(pen) | ||
pen = display.create_pen_hsv(t, 1.0, 1.0) | ||
display.set_pen(pen) | ||
|
||
# Now we go through each Cube object we have in 'cubes' | ||
# and increase the FOV angle so it appears closer to the screen. | ||
# We'll also rotate the cube during this loop too. | ||
for i, cube in enumerate(cubes): | ||
fov = cube.get_fov() | ||
fov += 5 | ||
cube.set_fov(fov) | ||
cube.rotate() | ||
cube.draw() | ||
|
||
# We want the cubes to disappear randomly as they appear close to the screen, so we'll decide when this happens based on the current FOV | ||
# We'll replace that cube with a new one and start the process from the beginning! | ||
if fov > randint(250, 600): | ||
cubes[i] = Cube(8, 4, randint(10, WIDTH), randint(10, HEIGHT), randrange(4, 9) / 10) | ||
|
||
display.update() |