16
16
17
17
**Hardware:**
18
18
19
- * PyPortal
20
- https://www.adafruit.com/product/4116
19
+ * PyPortal https://www.adafruit.com/product/4116
20
+ * PyPortal Pynt https://www.adafruit.com/product/4465
21
+ * PyPortal Titano https://www.adafruit.com/product/4444
22
+ * PyBadge https://www.adafruit.com/product/4200
23
+ * PyGamer https://www.adafruit.com/product/4242
24
+ * HalloWing M4 https://www.adafruit.com/product/4300
21
25
22
26
**Software and Dependencies:**
23
27
24
28
* Adafruit CircuitPython firmware for the supported boards:
25
29
https://github.com/adafruit/circuitpython/releases
26
30
"""
27
31
28
- # pylint: disable=too-many-instance-attributes,no-self-use,line-too-long
29
-
30
32
# imports
31
33
import time
32
34
import json
51
53
52
54
53
55
class PYOA_Graphics :
56
+ # pylint: disable=too-many-instance-attributes
54
57
"""A choose your own adventure game framework."""
55
58
56
59
def __init__ (self ):
@@ -113,7 +116,7 @@ def __init__(self):
113
116
def load_game (self , game_directory ):
114
117
"""Load a game.
115
118
116
- :param game_directory: where the game files are stored
119
+ :param str game_directory: where the game files are stored
117
120
"""
118
121
self ._gamedirectory = game_directory
119
122
self ._text_font = terminalio .FONT
@@ -131,42 +134,42 @@ def load_game(self, game_directory):
131
134
button_width = 120
132
135
button_height = 40
133
136
if self ._display .height < 200 :
134
- button_y /= 2
137
+ button_y // = 2
135
138
button_y += 10
136
- button_width /= 2
137
- button_height /= 2
138
- btn_right /= 2
139
- btn_mid /= 2
139
+ button_width // = 2
140
+ button_height // = 2
141
+ btn_right // = 2
142
+ btn_mid // = 2
140
143
elif self ._display .height > 250 :
141
- button_y *= 0.75
144
+ button_y = ( button_y * 3 ) // 4
142
145
button_y -= 20
143
- button_width *= 0.75
144
- button_height *= 0.75
145
- btn_right *= 0.75
146
- btn_mid *= 0.75
146
+ button_width = ( button_width * 3 ) // 4
147
+ button_height = ( button_height * 3 ) // 4
148
+ btn_right = ( btn_right * 3 ) // 4
149
+ btn_mid = ( btn_right * 3 ) // 4
147
150
self ._left_button = Button (
148
- x = int ( btn_left ) ,
149
- y = int ( button_y ) ,
150
- width = int ( button_width ) ,
151
- height = int ( button_height ) ,
151
+ x = btn_left ,
152
+ y = button_y ,
153
+ width = button_width ,
154
+ height = button_height ,
152
155
label = "Left" ,
153
156
label_font = self ._text_font ,
154
157
style = Button .SHADOWROUNDRECT ,
155
158
)
156
159
self ._right_button = Button (
157
- x = int ( btn_right ) ,
158
- y = int ( button_y ) ,
159
- width = int ( button_width ) ,
160
- height = int ( button_height ) ,
160
+ x = btn_right ,
161
+ y = button_y ,
162
+ width = button_width ,
163
+ height = button_height ,
161
164
label = "Right" ,
162
165
label_font = self ._text_font ,
163
166
style = Button .SHADOWROUNDRECT ,
164
167
)
165
168
self ._middle_button = Button (
166
- x = int ( btn_mid ) ,
167
- y = int ( button_y ) ,
168
- width = int ( button_width ) ,
169
- height = int ( button_height ) ,
169
+ x = btn_mid ,
170
+ y = button_y ,
171
+ width = button_width ,
172
+ height = button_height ,
170
173
label = "Middle" ,
171
174
label_font = self ._text_font ,
172
175
style = Button .SHADOWROUNDRECT ,
@@ -283,7 +286,7 @@ def _wait_for_press(self, card):
283
286
def display_card (self , card_num ):
284
287
"""Display and handle input on a card.
285
288
286
- :param card_num: the index of the card to process
289
+ :param int card_num: the index of the card to process
287
290
"""
288
291
card = self ._game [card_num ]
289
292
print (card )
@@ -296,11 +299,7 @@ def display_card(self, card_num):
296
299
self ._display_background_for (card )
297
300
self .backlight_fade (1.0 )
298
301
self ._display_text_for (card )
299
- try :
300
- self ._display .refresh (target_frames_per_second = 60 )
301
- except AttributeError :
302
- self ._display .refresh_soon ()
303
- self ._display .wait_for_frame ()
302
+ self ._display .refresh (target_frames_per_second = 60 )
304
303
305
304
self ._play_sound_for (card )
306
305
@@ -325,9 +324,10 @@ def display_card(self, card_num):
325
324
def play_sound (self , filename , * , wait_to_finish = True , loop = False ):
326
325
"""Play a sound
327
326
328
- :param filename: The filename of the sound to play
329
- :param wait_to_finish: Whether playing the sound should block
330
- :param loop: Whether the sound should loop
327
+ :param Union(None,str) filename: The filename of the sound to play. Use `None` to stop
328
+ playing anything.
329
+ :param bool wait_to_finish: Whether playing the sound should block
330
+ :param bool loop: Whether the sound should loop
331
331
"""
332
332
self ._speaker_enable .value = False
333
333
self .audio .stop ()
@@ -339,10 +339,7 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
339
339
return # nothing more to do, just stopped
340
340
filename = self ._gamedirectory + "/" + filename
341
341
print ("Playing sound" , filename )
342
- try :
343
- self ._display .refresh (target_frames_per_second = 60 )
344
- except AttributeError :
345
- self ._display .wait_for_frame ()
342
+ self ._display .refresh (target_frames_per_second = 60 )
346
343
try :
347
344
self ._wavfile = open (filename , "rb" ) # pylint: disable=consider-using-with
348
345
except OSError as err :
@@ -362,8 +359,8 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
362
359
def set_text (self , text , color ):
363
360
"""Display the test for a card.
364
361
365
- :param text: the text to display
366
- :param color: the text color
362
+ :param Union(None,str) text: the text to display
363
+ :param Union(None,int) color: the text color
367
364
368
365
"""
369
366
if self ._text_group :
@@ -393,7 +390,9 @@ def set_text(self, text, color):
393
390
def set_background (self , filename , * , with_fade = True ):
394
391
"""The background image to a bitmap file.
395
392
396
- :param filename: The filename of the chosen background
393
+ :param Union(None,str) filename: The filename of the chosen background
394
+ :param bool with_fade: If `True` fade out the backlight while loading the new background
395
+ and fade in the backlight once completed.
397
396
"""
398
397
print ("Set background to" , filename )
399
398
if with_fade :
@@ -407,32 +406,31 @@ def set_background(self, filename, *, with_fade=True):
407
406
with open (
408
407
self ._gamedirectory + "/" + filename , "rb"
409
408
) as self ._background_file :
409
+ # TODO: Once CP6 is no longer supported, pass combined filename into OnDiskBitmap
410
410
background = displayio .OnDiskBitmap (self ._background_file )
411
- self ._background_sprite = displayio .TileGrid (
412
- background ,
413
- pixel_shader = getattr (
414
- background , "pixel_shader" , displayio .ColorConverter ()
415
- ),
416
- # TODO: Once CP6 is no longer supported, replace the above line with below
417
- # pixel_shader=background.pixel_shader,
418
- x = 0 ,
419
- y = 0 ,
420
- )
421
- self ._background_group .append (self ._background_sprite )
411
+ self ._background_sprite = displayio .TileGrid (
412
+ background ,
413
+ pixel_shader = getattr (
414
+ background , "pixel_shader" , displayio .ColorConverter ()
415
+ ),
416
+ # TODO: Once CP6 is no longer supported, replace the above line with below
417
+ # pixel_shader=background.pixel_shader,
418
+ )
419
+ self ._background_group .append (self ._background_sprite )
422
420
if with_fade :
423
- try :
424
- self ._display .refresh (target_frames_per_second = 60 )
425
- except AttributeError :
426
- self ._display .refresh_soon ()
427
- self ._display .wait_for_frame ()
421
+ self ._display .refresh (target_frames_per_second = 60 )
428
422
self .backlight_fade (1.0 )
429
423
430
424
def backlight_fade (self , to_light ):
431
- """Adjust the TFT backlight. Fade from one value to another"""
425
+ """
426
+ Adjust the TFT backlight. Fade from the current value to the ``to_light`` value
427
+
428
+ :param float to_light: the desired backlight brightness between :py:const:`0.0` and
429
+ :py:const:`1.0`.
430
+ """
432
431
from_light = self ._display .brightness
433
432
from_light = int (from_light * 100 )
434
- to_light = max (0 , min (1.0 , to_light ))
435
- to_light = int (to_light * 100 )
433
+ to_light = max (0 , min (100 , int (to_light * 100 )))
436
434
delta = 1
437
435
if from_light > to_light :
438
436
delta = - 1
@@ -442,7 +440,6 @@ def backlight_fade(self, to_light):
442
440
self ._display .brightness = to_light / 100
443
441
444
442
# return a list of lines with wordwrapping
445
- # pylint: disable=invalid-name
446
443
@staticmethod
447
444
def wrap_nicely (string , max_chars ):
448
445
"""A helper that will return a list of lines with word-break wrapping.
@@ -457,10 +454,10 @@ def wrap_nicely(string, max_chars):
457
454
the_line = ""
458
455
for w in words :
459
456
if "\n " in w :
460
- w1 , w2 = w .split ("\n " )
461
- the_line += " " + w1
457
+ _w1 , _w2 = w .split ("\n " )
458
+ the_line += " " + _w1
462
459
the_lines .append (the_line )
463
- the_line = w2
460
+ the_line = _w2
464
461
elif len (the_line + " " + w ) > max_chars :
465
462
the_lines .append (the_line )
466
463
the_line = "" + w
0 commit comments