-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
189 lines (173 loc) · 7.19 KB
/
example.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
from pizzoo import Pizzoo, WindowRenderer, ImageRenderer
from time import sleep
'''Create the main object and the connection to the Pixoo device'''
pizzoo = Pizzoo('192.168.50.225', debug=True)
pizzoo.switch(on=True) # Turn on the Pixoo device screen
def example_draw_pixel():
'''
Draw three simple pixels on the Pixoo device.
'''
pizzoo.cls() # Clear the screen with black color (By default)
pizzoo.draw_pixel((30, 31), '#ff0000') # Red pixel, using the hex color format
pizzoo.draw_pixel((31, 31), (0, 255, 0)) # Green pixel, using the RGB tuple format
pizzoo.draw_pixel((32, 31), 12) # An integer color between 0 and 15 is a pico-8 palette color (https://lospec.com/palette-list/pico-8)
pizzoo.render() # Render the frame to the Pixoo device, this is mandatory to see the changes on the device
def example_draw_shapes():
'''
Draw different shapes on the Pixoo device.
Shapes can be drawn with different colors and filled or not.
'''
pizzoo.cls()
pizzoo.draw_circle((31, 31), 10, '#00ff00', filled=True) # Green circle, filled
pizzoo.draw_rectangle((26, 26), 11, 11, '#ff0000', filled=False) # Red rectangle, not filled
pizzoo.draw_line((31, 0), (31, 63), '#0000ff') # Blue line
pizzoo.draw_line((0, 31), (63, 31), (255, 255, 0)) # Yellow line, tuple rgb color format
pizzoo.render()
def example_draw_text():
'''
Draw text with different fonts and colors.
The default bdf font is used to draw text on the Pixoo device.
New fonts can be added to the running instance using the load_font method (Only on .bdf extension).
Text then can be drawn on a given position, with the defined font and an optional max line width (So it will be wrapped).
'''
pizzoo.load_font('artos', './files/ArtosSans-8.bdf')
pizzoo.load_font('amstrad', './files/amstrad_cpc_extended.bdf')
pizzoo.cls((255, 255, 255))
pizzoo.draw_text('This is a pretty big text using the default font for the library', xy=('center', 24), color='#000000', line_width=62) # Default font, custom line width
pizzoo.draw_text('Amstrad', xy=(2, 2), font='amstrad', color='#00ff00') # Custom amstrad font, no wrap is defined
pizzoo.draw_text('Artos', xy=('right', 12), font='artos', color=(255, 0, 0), line_width='auto') # Custom artos font, wrap is 'auto' so full device width (64px)
pizzoo.render()
def example_draw_image():
'''
Draw an image on the current buffer.
Images can be drawn with a given position and size.
'''
pizzoo.cls()
pizzoo.draw_image('./files/test_image.png', xy=(0, 0)) # Draw a 16x16 image on the center of the screen
pizzoo.draw_image('./files/test_image.png', xy=(16, 16), size=(32, 32)) # Image can be resized to any width and/or height
pizzoo.render()
def example_draw_gif():
'''
The Pizzoo library uses an animation buffer, so it can also draw gifs on the device.
Gifs can be drawn with a given position and size, as any other image.
'''
pizzoo.cls()
pizzoo.draw_gif('./files/test_gif.gif', xy=(15, 15), size=(32, 32), loop=True) # Draw a gif on the center of the screen
pizzoo.render()
def example_scoreboard():
'''
Activate the scoreboard on the Pixoo device.
No cls or render method is needed as this is a built-in method of the Pixoo64 device.
'''
pizzoo.set_scoreboard(1, 2) # Set the scoreboard with the numbers 1 for the Blue team and 2 for the Red team
def example_buzzer():
'''
Play a sound on the Pixoo device.
The buzzer method can play a sound with a given frequency and duration.
'''
pizzoo.buzzer(1, 1, 4) # Play a sound with a frequency of 1 second ON, 1 second OFF and a duration of 4 seconds.
def example_countdown():
'''
Activate the countdown on the Pixoo device.
No cls or render method is needed as this is a built-in method of the Pixoo64 device.
When stopping the countdown, the elapsed time is also returned by the method.
'''
from time import sleep
pizzoo.start_countdown(10) # Sets a countdown of 5 seconds
sleep(5)
elapsed_time = pizzoo.stop_countdown() # Stops the countdown prematurely and returns the elapsed time
print(f'Elapsed time: {elapsed_time} seconds')
def example_template():
'''
Render an XML template on the Pixoo device.
'''
pizzoo.load_font('amstrad', './files/amstrad_cpc_extended.bdf')
pizzoo.render_template('''
<pizzoo>
<rectangle x="0" y="0" width="100%" height="100%" color="8" filled="true">
<section x="0" y="2">
<date format="DD" x="1" color="#ffc107" font="small"></date>
<text x="9" color="#ffc107">-</text>
<date format="MM" x="13" color="#ffc107" font="small"></date>
<time format="HH:mm" x="26" font="small"></time>
<date format="WWW" width="63" align="right" color="#ffc107" font="small"></date>
</section>
<section x="5" y="10" width="53" height="43">
<rectangle x="0" y="0" width="100%" height="100%" color="#FFFFFF" filled="true" />
<section x="1" y="1" width="51" height="51">
<text x="0" y="0" color="#000000" wrap="true">Long text wrapped</text>
<text x="0" y="50%" color="#000000" wrap="true">2.8K</text>
<text x="10" y="80%" shadow="diagonal" color="#FF0000" font="amstrad">Cool</text>
<image x="17" y="40%" src="./files/test_image.png" />
</section>
</section>
</rectangle>
</pizzoo>
''')
def example_template_bg():
'''
Render an XML template on the Pixoo device with a background gif.
'''
pizzoo.render_template('''
<pizzoo background="./files/test_gif.gif">
<rectangle x="0" y="0" width="64" height="8" color="0">
<section x="0" y="1">
<date format="DD" x="1" color="#ffc107" font="small"></date>
<text x="9" color="#ffc107">-</text>
<date format="MM" x="13" color="#ffc107" font="small"></date>
<time format="HH:mm" x="26" font="small"></time>
<date format="WWW" width="63" x="50" color="#ffc107" font="small"></date>
</section>
</rectangle>
</pizzoo>
''')
def example_advanced_animation():
'''
Advanced animation example for creating complex animations adding frames to the buffer.
'''
frames = 54
for i in range(0, frames):
pizzoo.cls()
pizzoo.draw_circle((i + 4, i + 4), 2, '#00ff00', filled=True)
pizzoo.add_frame()
pizzoo.render(frame_speed=100)
def power_off():
'''
Turn off the Pixoo device.
'''
pizzoo.turn_screen(on=False)
def example_game():
from games.zombieGame import ZombieGame
game = ZombieGame(pizzoo)
game.start()
if __name__ == '__main__':
# create a terminal menu to select the example to run
selected_option = None
menu = {
'1': {'Draw Pixel': example_draw_pixel},
'2': {'Draw Shapes': example_draw_shapes},
'3': {'Draw Text': example_draw_text},
'4': {'Draw Image': example_draw_image},
'5': {'Draw Gif': example_draw_gif},
'6': {'Scoreboard': example_scoreboard},
'7': {'Buzzer': example_buzzer},
'8': {'Countdown': example_countdown},
'9': {'Render an XML template': example_template},
'10': {'Render a template with a gif background': example_template_bg},
'11': {'Advanced Animation': example_advanced_animation},
'12': {'Game': example_game}
}
print('Select an example to run:')
for key, value in menu.items():
print(f'{key}. {list(value.keys())[0]}')
print('0. Exit')
option = input('Option: ')
selected_option = option
if option in menu:
menu[option][list(menu[option].keys())[0]]()
elif option == '0':
print('Exiting...')
power_off()
else:
print('Invalid option, please try again.')
sleep(3)