-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflying_toasters.py
308 lines (262 loc) · 11.7 KB
/
flying_toasters.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'''
@ @ @@@@@ @ @@@@@@@@@@
@@@ @@@ @@@@@@@@@@@@@ @@@ @@@@@@@@@@
@@@@@@ @@@@@@ @@@@@@@@@@@@@@@@@ @@@@@@ @@@@@@@@@@
@@@@@@@@ @@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@ @@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@X@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@X@@@@@@@@@ @@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@ @@@@@ @@@@@@@@@@@@@@@@@@@@
@@@@@@ @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ @@@ @@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ @@@ @@@@@@@@@@
@@@@@@@@ @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@
'''
"""
Flying Toasters! Prototype
------------------------
A tribute to the classic After Dark 2.0 screensaver 1-bit prototype from the 1-bit Macintosh era.
This version is platform-independent and can be adapted to different display systems.
Originally created for MicroPython on a RP2040 with LCD display, this version has been
modified to work with any Python graphics system by implementing a simple display interface.
"""
import random
import time
# Sprite definitions for the toaster animation frames
TOASTER_FRAME1 = [
" █ ",
" ███ ██ █",
" ██ █ █ ██ ",
" █ ██████ █ ",
" █ ██ ██ ",
" █ ██ ██ ",
" ██ ██ █ ",
" ██ ██ █ ",
" ██ ██ ██ █ ",
" █ ██ ██ █ ",
"█ ██ ███ ",
"█ ██ ██ ██ ",
"█ ██ ██ ██ █",
"█ ██ ██ ██ █ ",
"█ ██ ██ █ █ ",
"█ █ █ ███ █ █ ",
"█ █ █ █ █ ",
"█ ███ █ █ █ █ ",
"█ ████ █ █ █ ",
"█ ██ █ █ ",
"█ █ █ █ ",
"█ █ █ █ ",
" █ █ ██ ",
" ██ █ ██ ",
" ██ █ ██ ",
" ██ ██ ",
" ██████ "
]
TOASTER_FRAME2 = [
" ██ ",
" █ █",
" █ █ ",
" ██████ ██ ",
" ██ ██ ",
" ██ ██ ",
" ██ ██ █ ",
" ██ ██ █ ",
" ██ ██ ██ █ ",
" █ ██ ██ █ ",
"█ ██ █ █ ",
"█ ██ ██ █ ",
"█ ██ ██ █ ",
"█ ██ ██ █ ",
"█ ██ ██ ███ █",
"█ █ █ ███ █",
"█ █ █ ██ ",
"█ ██ █ █ ████ ",
"█ ████ █ ███ █ ",
"█ ██ █ █ ",
"█ █ █ █ ",
"█ █ █ █ ",
" █ █ ██ ",
" ██ █ ██ ",
" ██ █ ██ ",
" ██ ██ ",
" ██████ "
]
TOAST = [
" ██ ",
" ██ ██ ",
" █ ██ ",
" ███ ██ ",
"██ ██",
"█ █ ██ █",
"██ ██ ███ ██",
" █ ███ █ ██ ",
" ███ ██ ███ ",
" ███ █ ",
" ██ "
]
class DisplayInterface:
"""
Abstract base class for display interfaces.
Implement this class to adapt the animation to your display system.
"""
def __init__(self, width, height):
self.width = width
self.height = height
def fill(self, color):
"""Fill the entire display with a color"""
raise NotImplementedError
def draw_pixel(self, x, y, color):
"""Draw a single pixel at the specified coordinates"""
raise NotImplementedError
def show(self):
"""Update the display with any buffered changes"""
raise NotImplementedError
class FlyingObject:
def __init__(self, display_width, display_height, is_toaster=True, initial_spread=False):
self.is_toaster = is_toaster
self.sprite = TOAST if not is_toaster else TOASTER_FRAME1
self.width = 27 if is_toaster else 18
self.height = 27 if is_toaster else 11
if initial_spread:
# Distribute objects across the screen initially
section = display_width // 4
quadrant = random.randint(0, 3)
self.x = random.randint(quadrant * section - self.width,
(quadrant + 1) * section)
self.y = random.randint(-self.height * 2, display_height//2)
else:
self.x = display_width + random.randint(0, 150)
self.y = random.randint(-self.height * 2, display_height//3)
self.speed_x = -3.0
self.speed_y = 1.5
self.frame = random.randint(0, 1) if is_toaster else 0
self.frame_counter = 0
self.frame_delay = 2
def update(self, display_width, display_height):
"""Update object position and animation frame"""
self.x += self.speed_x
self.y += self.speed_y
if self.is_toaster:
self.frame_counter += 1
if self.frame_counter >= self.frame_delay:
self.frame_counter = 0
self.frame = (self.frame + 1) % 2
# Reset position when object moves off screen
if self.x < -self.width or self.y > display_height:
if random.random() < 0.5:
self.y = random.randint(-self.height * 3, -self.height)
else:
self.y = random.randint(0, display_height//3)
self.x = display_width + random.randint(0, 200)
if self.is_toaster:
self.frame = random.randint(0, 1)
return True
def draw_sprite(display, sprite, x, y, outline_color=0xFFFF, fill_color=0x0000):
"""Draw a sprite with specified fill and outline colors"""
height = len(sprite)
width = len(sprite[0])
# Fill the sprite
for row in range(height):
left_edge = width
right_edge = 0
# Find the edges of each row
for col in range(width):
if sprite[row][col] == '█':
left_edge = min(left_edge, col)
right_edge = max(right_edge, col)
# Fill between the edges
if right_edge >= left_edge:
for col in range(left_edge, right_edge + 1):
pixel_x = int(x) + col
pixel_y = int(y) + row
if (0 <= pixel_x < display.width and
0 <= pixel_y < display.height):
display.draw_pixel(pixel_x, pixel_y, fill_color)
# Draw the outline
for row in range(height):
for col in range(width):
if sprite[row][col] == '█':
pixel_x = int(x) + col
pixel_y = int(y) + row
if (0 <= pixel_x < display.width and
0 <= pixel_y < display.height):
display.draw_pixel(pixel_x, pixel_y, outline_color)
def run_animation(display, duration_ms=180000, num_toasters=5, num_toast=2):
"""
Run the flying toasters animation
Args:
display: Display interface object
duration_ms: Duration to run in milliseconds
num_toasters: Number of toaster objects
num_toast: Number of toast objects
"""
start_time = time.time() * 1000 # Convert to milliseconds
objects = []
# Create toasters
for i in range(num_toasters):
objects.append(FlyingObject(display.width, display.height, True, True))
# Create toast
for i in range(num_toast):
objects.append(FlyingObject(display.width, display.height, False, True))
frame_interval = 30 # milliseconds between frames
last_update = time.time() * 1000
while (time.time() * 1000 - start_time) < duration_ms:
current_time = time.time() * 1000
if current_time - last_update >= frame_interval:
display.fill(0x0000) # Black background
for obj in objects:
obj.update(display.width, display.height)
if obj.is_toaster:
sprite = TOASTER_FRAME2 if obj.frame == 1 else TOASTER_FRAME1
else:
sprite = TOAST
draw_sprite(display, sprite, obj.x, obj.y)
display.show()
last_update = current_time
time.sleep(0.001) # Small delay to prevent busy-waiting
return True
# Example implementation of DisplayInterface using Pygame
def create_pygame_display(width=320, height=240):
"""
Create a Pygame-based display implementation
Requires: pip install pygame
"""
import pygame
class PygameDisplay(DisplayInterface):
def __init__(self, width, height):
super().__init__(width, height)
pygame.init()
self.screen = pygame.display.set_mode((width, height))
self.buffer = pygame.Surface((width, height))
def fill(self, color):
# Convert 16-bit color to RGB
r = (color >> 11) & 0x1F
g = (color >> 5) & 0x3F
b = color & 0x1F
rgb = (r << 3, g << 2, b << 3)
self.buffer.fill(rgb)
def draw_pixel(self, x, y, color):
# Convert 16-bit color to RGB
r = (color >> 11) & 0x1F
g = (color >> 5) & 0x3F
b = color & 0x1F
rgb = (r << 3, g << 2, b << 3)
self.buffer.set_at((int(x), int(y)), rgb)
def show(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return False
self.screen.blit(self.buffer, (0, 0))
pygame.display.flip()
return True
return PygameDisplay(width, height)
if __name__ == '__main__':
# Example usage with Pygame display
display = create_pygame_display(1280, 720)
run_animation(display, duration_ms=30000) # Run for 30 seconds