-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcubes.py
More file actions
160 lines (118 loc) · 4.35 KB
/
cubes.py
File metadata and controls
160 lines (118 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# ICON deployed-code
# NAME Flying Cubes
# DESC Cubes that are flying!
import math
import time
from random import randint, randrange
from presto import Presto
# Setup for the Presto display
presto = Presto()
display = presto.display
WIDTH, HEIGHT = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)
WHITE = display.create_pen(255, 255, 255)
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
# Set the layer we're going to be drawing to.
display.set_layer(0)
# Clear the screen and set the pen colour for the cubes
display.set_pen(BLACK)
display.clear()
display.set_pen(WHITE)
display.text("Flying Cubes!", 90, 110, 320, 1)
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)
# Finally we update the screen with our changes :)
presto.update()
time.sleep(0.01)