42
42
https://github.com/adafruit/circuitpython/releases
43
43
"""
44
44
45
- #pylint: disable=too-many-instance-attributes,no-self-use,line-too-long
45
+ # pylint: disable=too-many-instance-attributes,no-self-use,line-too-long
46
46
47
47
# imports
48
48
import time
51
51
from digitalio import DigitalInOut
52
52
import displayio
53
53
import adafruit_touchscreen
54
- try : # No need for Cursor Control on the PyPortal
54
+
55
+ try : # No need for Cursor Control on the PyPortal
55
56
from adafruit_cursorcontrol .cursorcontrol import Cursor
56
57
from adafruit_cursorcontrol .cursorcontrol_cursormanager import CursorManager
57
58
except ImportError :
64
65
__version__ = "0.0.0-auto.0"
65
66
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PYOA.git"
66
67
67
- class PYOA_Graphics ():
68
+
69
+ class PYOA_Graphics :
68
70
"""A choose your own adventure game framework."""
69
71
70
72
def __init__ (self ):
@@ -83,12 +85,12 @@ def __init__(self):
83
85
84
86
self ._speaker_enable = DigitalInOut (board .SPEAKER_ENABLE )
85
87
self ._speaker_enable .switch_to_output (False )
86
- if hasattr (board , ' AUDIO_OUT' ):
88
+ if hasattr (board , " AUDIO_OUT" ):
87
89
self .audio = audioio .AudioOut (board .AUDIO_OUT )
88
- elif hasattr (board , ' SPEAKER' ):
90
+ elif hasattr (board , " SPEAKER" ):
89
91
self .audio = audioio .AudioOut (board .SPEAKER )
90
92
else :
91
- raise AttributeError (' Board does not have an audio output!' )
93
+ raise AttributeError (" Board does not have an audio output!" )
92
94
93
95
self ._background_file = None
94
96
self ._wavfile = None
@@ -98,18 +100,22 @@ def __init__(self):
98
100
self ._display .show (self .root_group )
99
101
self .touchscreen = None
100
102
self .mouse_cursor = None
101
- if hasattr (board , 'TOUCH_XL' ):
102
- self .touchscreen = adafruit_touchscreen .Touchscreen (board .TOUCH_XL , board .TOUCH_XR ,
103
- board .TOUCH_YD , board .TOUCH_YU ,
104
- calibration = ((5200 , 59000 ),
105
- (5800 , 57000 )),
106
- size = (self ._display .width ,
107
- self ._display .height ))
108
- elif hasattr (board , 'BUTTON_CLOCK' ):
109
- self .mouse_cursor = Cursor (self ._display , display_group = self .root_group , cursor_speed = 8 )
103
+ if hasattr (board , "TOUCH_XL" ):
104
+ self .touchscreen = adafruit_touchscreen .Touchscreen (
105
+ board .TOUCH_XL ,
106
+ board .TOUCH_XR ,
107
+ board .TOUCH_YD ,
108
+ board .TOUCH_YU ,
109
+ calibration = ((5200 , 59000 ), (5800 , 57000 )),
110
+ size = (self ._display .width , self ._display .height ),
111
+ )
112
+ elif hasattr (board , "BUTTON_CLOCK" ):
113
+ self .mouse_cursor = Cursor (
114
+ self ._display , display_group = self .root_group , cursor_speed = 8
115
+ )
110
116
self .cursor = CursorManager (self .mouse_cursor )
111
117
else :
112
- raise AttributeError (' PYOA requires a touchscreen or cursor.' )
118
+ raise AttributeError (" PYOA requires a touchscreen or cursor." )
113
119
self ._gamedirectory = None
114
120
self ._gamefilename = None
115
121
self ._game = None
@@ -120,7 +126,6 @@ def __init__(self):
120
126
self ._right_button = None
121
127
self ._middle_button = None
122
128
123
-
124
129
def load_game (self , game_directory ):
125
130
"""Load a game.
126
131
@@ -136,8 +141,8 @@ def load_game(self, game_directory):
136
141
137
142
# Button Attributes
138
143
btn_left = 10
139
- btn_right = btn_left + 180
140
- btn_mid = btn_left + 90
144
+ btn_right = btn_left + 180
145
+ btn_mid = btn_left + 90
141
146
button_y = 195
142
147
button_width = 120
143
148
button_height = 40
@@ -149,26 +154,44 @@ def load_game(self, game_directory):
149
154
btn_right /= 2
150
155
btn_mid /= 2
151
156
elif self ._display .height > 250 :
152
- button_y *= .75
157
+ button_y *= 0 .75
153
158
button_y -= 20
154
- button_width *= .75
155
- button_height *= .75
156
- btn_right *= .75
157
- btn_mid *= .75
158
- self ._left_button = Button (x = int (btn_left ), y = int (button_y ), width = int (button_width ), height = int (button_height ),
159
- label = "Left" , label_font = self ._text_font ,
160
- style = Button .SHADOWROUNDRECT )
161
- self ._right_button = Button (x = int (btn_right ), y = int (button_y ), width = int (button_width ), height = int (button_height ),
162
- label = "Right" , label_font = self ._text_font ,
163
- style = Button .SHADOWROUNDRECT )
164
- self ._middle_button = Button (x = int (btn_mid ), y = int (button_y ), width = int (button_width ), height = int (button_height ),
165
- label = "Middle" , label_font = self ._text_font ,
166
- style = Button .SHADOWROUNDRECT )
167
- self ._gamefilename = game_directory + "/cyoa.json"
159
+ button_width *= 0.75
160
+ button_height *= 0.75
161
+ btn_right *= 0.75
162
+ btn_mid *= 0.75
163
+ self ._left_button = Button (
164
+ x = int (btn_left ),
165
+ y = int (button_y ),
166
+ width = int (button_width ),
167
+ height = int (button_height ),
168
+ label = "Left" ,
169
+ label_font = self ._text_font ,
170
+ style = Button .SHADOWROUNDRECT ,
171
+ )
172
+ self ._right_button = Button (
173
+ x = int (btn_right ),
174
+ y = int (button_y ),
175
+ width = int (button_width ),
176
+ height = int (button_height ),
177
+ label = "Right" ,
178
+ label_font = self ._text_font ,
179
+ style = Button .SHADOWROUNDRECT ,
180
+ )
181
+ self ._middle_button = Button (
182
+ x = int (btn_mid ),
183
+ y = int (button_y ),
184
+ width = int (button_width ),
185
+ height = int (button_height ),
186
+ label = "Middle" ,
187
+ label_font = self ._text_font ,
188
+ style = Button .SHADOWROUNDRECT ,
189
+ )
190
+ self ._gamefilename = game_directory + "/cyoa.json"
168
191
try :
169
192
game_file = open (self ._gamefilename , "r" )
170
193
except OSError :
171
- raise OSError ("Could not open game file " + self ._gamefilename )
194
+ raise OSError ("Could not open game file " + self ._gamefilename )
172
195
self ._game = json .load (game_file )
173
196
game_file .close ()
174
197
@@ -190,8 +213,8 @@ def _display_buttons(self, card):
190
213
191
214
:param card: The active card
192
215
"""
193
- button01_text = card .get (' button01_text' , None )
194
- button02_text = card .get (' button02_text' , None )
216
+ button01_text = card .get (" button01_text" , None )
217
+ button02_text = card .get (" button02_text" , None )
195
218
self ._left_button .label = button01_text
196
219
self ._middle_button .label = button01_text
197
220
self ._right_button .label = button02_text
@@ -207,18 +230,18 @@ def _display_background_for(self, card):
207
230
208
231
:param card: The active card
209
232
"""
210
- self .set_background (card .get (' background_image' , None ), with_fade = False )
233
+ self .set_background (card .get (" background_image" , None ), with_fade = False )
211
234
212
235
def _display_text_for (self , card ):
213
236
"""Display the main text of a card.
214
237
215
238
:param card: The active card
216
239
"""
217
- text = card .get (' text' , None )
218
- text_color = card .get (' text_color' , 0x0 ) # default to black
240
+ text = card .get (" text" , None )
241
+ text_color = card .get (" text_color" , 0x0 ) # default to black
219
242
if text :
220
243
try :
221
- text_color = int (text_color ) # parse the JSON string to hex int
244
+ text_color = int (text_color ) # parse the JSON string to hex int
222
245
except ValueError :
223
246
text_color = 0x0
224
247
self .set_text (text , text_color )
@@ -228,8 +251,8 @@ def _play_sound_for(self, card):
228
251
229
252
:param card: The active card
230
253
"""
231
- sound = card .get (' sound' , None )
232
- loop = card .get (' sound_repeat' , False )
254
+ sound = card .get (" sound" , None )
255
+ loop = card .get (" sound_repeat" , False )
233
256
if sound :
234
257
loop = loop == "True"
235
258
print ("Loop:" , loop )
@@ -242,8 +265,8 @@ def _wait_for_press(self, card):
242
265
243
266
Return the id of the destination card.
244
267
"""
245
- button01_text = card .get (' button01_text' , None )
246
- button02_text = card .get (' button02_text' , None )
268
+ button01_text = card .get (" button01_text" , None )
269
+ button02_text = card .get (" button02_text" , None )
247
270
point_touched = None
248
271
while True :
249
272
if self .touchscreen is not None :
@@ -253,21 +276,23 @@ def _wait_for_press(self, card):
253
276
if self .cursor .is_clicked is True :
254
277
point_touched = self .mouse_cursor .x , self .mouse_cursor .y
255
278
if point_touched is not None :
256
- point_touched = (point_touched [0 ] // self ._button_group .scale ,
257
- point_touched [1 ] // self ._button_group .scale )
279
+ point_touched = (
280
+ point_touched [0 ] // self ._button_group .scale ,
281
+ point_touched [1 ] // self ._button_group .scale ,
282
+ )
258
283
print ("touch: " , point_touched )
259
284
if button01_text and not button02_text :
260
285
# showing only middle button
261
286
if self ._middle_button .contains (point_touched ):
262
287
print ("Middle button" )
263
- return card .get (' button01_goto_card_id' , None )
288
+ return card .get (" button01_goto_card_id" , None )
264
289
if button01_text and button02_text :
265
290
if self ._left_button .contains (point_touched ):
266
291
print ("Left button" )
267
- return card .get (' button01_goto_card_id' , None )
292
+ return card .get (" button01_goto_card_id" , None )
268
293
if self ._right_button .contains (point_touched ):
269
294
print ("Right button" )
270
- return card .get (' button02_goto_card_id' , None )
295
+ return card .get (" button02_goto_card_id" , None )
271
296
time .sleep (0.1 )
272
297
273
298
def display_card (self , card_num ):
@@ -277,9 +302,9 @@ def display_card(self, card_num):
277
302
"""
278
303
card = self ._game [card_num ]
279
304
print (card )
280
- print ("*" * 32 )
281
- print (' ****{:^24s}****' .format (card [' card_id' ]))
282
- print ("*" * 32 )
305
+ print ("*" * 32 )
306
+ print (" ****{:^24s}****" .format (card [" card_id" ]))
307
+ print ("*" * 32 )
283
308
284
309
self ._fade_to_black ()
285
310
self ._display_buttons (card )
@@ -294,21 +319,23 @@ def display_card(self, card_num):
294
319
295
320
self ._play_sound_for (card )
296
321
297
- auto_adv = card .get (' auto_advance' , None )
322
+ auto_adv = card .get (" auto_advance" , None )
298
323
if auto_adv is not None :
299
324
auto_adv = float (auto_adv )
300
325
print ("Auto advancing after %0.1f seconds" % auto_adv )
301
326
time .sleep (auto_adv )
302
- return card_num + 1
327
+ return card_num + 1
303
328
304
329
destination_card_id = self ._wait_for_press (card )
305
330
306
331
self .play_sound (None ) # stop playing any sounds
307
332
for card_number , card_struct in enumerate (self ._game ):
308
- if card_struct .get (' card_id' , None ) == destination_card_id :
309
- return card_number # found the matching card!
333
+ if card_struct .get (" card_id" , None ) == destination_card_id :
334
+ return card_number # found the matching card!
310
335
# eep if we got here something went wrong
311
- raise RuntimeError ("Could not find card with matching 'card_id': " , destination_card_id )
336
+ raise RuntimeError (
337
+ "Could not find card with matching 'card_id': " , destination_card_id
338
+ )
312
339
313
340
def play_sound (self , filename , * , wait_to_finish = True , loop = False ):
314
341
"""Play a sound
@@ -324,8 +351,8 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
324
351
self ._wavfile = None
325
352
326
353
if not filename :
327
- return # nothing more to do, just stopped
328
- filename = self ._gamedirectory + "/" + filename
354
+ return # nothing more to do, just stopped
355
+ filename = self ._gamedirectory + "/" + filename
329
356
print ("Playing sound" , filename )
330
357
try :
331
358
self ._display .refresh (target_frames_per_second = 60 )
@@ -357,12 +384,12 @@ def set_text(self, text, color):
357
384
if self ._text_group :
358
385
self ._text_group .pop ()
359
386
if not text or not color :
360
- return # nothing to do!
387
+ return # nothing to do!
361
388
text_wrap = 37
362
389
if self ._display .height < 130 :
363
390
text_wrap = 25
364
391
text = self .wrap_nicely (text , text_wrap )
365
- text = ' \n ' .join (text )
392
+ text = " \n " .join (text )
366
393
print ("Set text to" , text , "with color" , hex (color ))
367
394
text_x = 8
368
395
text_y = 95
@@ -392,11 +419,11 @@ def set_background(self, filename, *, with_fade=True):
392
419
if filename :
393
420
if self ._background_file :
394
421
self ._background_file .close ()
395
- self ._background_file = open (self ._gamedirectory + "/" + filename , "rb" )
422
+ self ._background_file = open (self ._gamedirectory + "/" + filename , "rb" )
396
423
background = displayio .OnDiskBitmap (self ._background_file )
397
- self ._background_sprite = displayio .TileGrid (background ,
398
- pixel_shader = displayio .ColorConverter (),
399
- x = 0 , y = 0 )
424
+ self ._background_sprite = displayio .TileGrid (
425
+ background , pixel_shader = displayio .ColorConverter (), x = 0 , y = 0
426
+ )
400
427
self ._background_group .append (self ._background_sprite )
401
428
if with_fade :
402
429
try :
@@ -410,20 +437,19 @@ def backlight_fade(self, to_light):
410
437
"""Adjust the TFT backlight. Fade from one value to another
411
438
"""
412
439
from_light = self ._display .brightness
413
- from_light = int (from_light * 100 )
440
+ from_light = int (from_light * 100 )
414
441
to_light = max (0 , min (1.0 , to_light ))
415
- to_light = int (to_light * 100 )
442
+ to_light = int (to_light * 100 )
416
443
delta = 1
417
444
if from_light > to_light :
418
445
delta = - 1
419
446
for val in range (from_light , to_light , delta ):
420
- self ._display .brightness = val / 100
447
+ self ._display .brightness = val / 100
421
448
time .sleep (0.003 )
422
- self ._display .brightness = to_light / 100
423
-
449
+ self ._display .brightness = to_light / 100
424
450
425
451
# return a list of lines with wordwrapping
426
- #pylint: disable=invalid-name
452
+ # pylint: disable=invalid-name
427
453
@staticmethod
428
454
def wrap_nicely (string , max_chars ):
429
455
"""A helper that will return a list of lines with word-break wrapping.
@@ -432,22 +458,22 @@ def wrap_nicely(string, max_chars):
432
458
:param int max_chars: The maximum number of characters on a line before wrapping.
433
459
434
460
"""
435
- #string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
436
- words = string .split (' ' )
461
+ # string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
462
+ words = string .split (" " )
437
463
the_lines = []
438
464
the_line = ""
439
465
for w in words :
440
- if ' \n ' in w :
441
- w1 , w2 = w .split (' \n ' )
442
- the_line += ' ' + w1
466
+ if " \n " in w :
467
+ w1 , w2 = w .split (" \n " )
468
+ the_line += " " + w1
443
469
the_lines .append (the_line )
444
470
the_line = w2
445
- elif len (the_line + ' ' + w ) > max_chars :
471
+ elif len (the_line + " " + w ) > max_chars :
446
472
the_lines .append (the_line )
447
- the_line = '' + w
473
+ the_line = "" + w
448
474
else :
449
- the_line += ' ' + w
450
- if the_line : # last line remaining
475
+ the_line += " " + w
476
+ if the_line : # last line remaining
451
477
the_lines .append (the_line )
452
478
# remove first space from first line:
453
479
the_lines [0 ] = the_lines [0 ][1 :]
0 commit comments