1818import supervisor
1919import terminalio
2020import usb .core
21- # use the built-in HSTX display for Metro RP2350
22- display = supervisor .runtime .display
21+
22+ if hasattr (supervisor .runtime , "display" ) and supervisor .runtime .display is not None :
23+ # use the built-in HSTX display for Metro RP2350
24+ display = supervisor .runtime .display
25+ else :
26+ from displayio import release_displays
27+ import picodvi
28+ import board
29+ import framebufferio
30+
31+ # initialize display
32+ release_displays ()
33+
34+ fb = picodvi .Framebuffer (
35+ 320 ,
36+ 240 ,
37+ clk_dp = board .CKP ,
38+ clk_dn = board .CKN ,
39+ red_dp = board .D0P ,
40+ red_dn = board .D0N ,
41+ green_dp = board .D1P ,
42+ green_dn = board .D1N ,
43+ blue_dp = board .D2P ,
44+ blue_dn = board .D2N ,
45+ color_depth = 16 ,
46+ )
47+ display = framebufferio .FramebufferDisplay (fb )
2348
2449# group to hold visual elements
2550main_group = Group ()
7196buf = array .array ("b" , [0 ] * 4 )
7297
7398# set up a 3x3 grid for the tic-tac-toe board
74- board_grid = GridLayout (
75- x = 40 ,
76- y = 40 ,
77- width = 128 ,
78- height = 128 ,
79- grid_size = (3 , 3 )
80- )
99+ board_grid = GridLayout (x = 40 , y = 40 , width = 128 , height = 128 , grid_size = (3 , 3 ))
81100
82101# load the tic-tac-toe spritesheet
83102tictactoe_spritesheet = OnDiskBitmap ("tictactoe_spritesheet.bmp" )
95114 # loop over columns
96115 for x in range (3 ):
97116 # create a TileGrid for this cell
98- new_tg = TileGrid (bitmap = tictactoe_spritesheet , default_tile = 0 ,
99- tile_height = 32 , tile_width = 32 ,
100- height = 1 , width = 1 ,
101- pixel_shader = tictactoe_spritesheet .pixel_shader )
117+ new_tg = TileGrid (
118+ bitmap = tictactoe_spritesheet ,
119+ default_tile = 0 ,
120+ tile_height = 32 ,
121+ tile_width = 32 ,
122+ height = 1 ,
123+ width = 1 ,
124+ pixel_shader = tictactoe_spritesheet .pixel_shader ,
125+ )
102126
103127 # add the new TileGrid to the board grid at the current position
104128 board_grid .add_content (new_tg , grid_position = (x , y ), cell_size = (1 , 1 ))
@@ -122,40 +146,52 @@ def check_for_winner():
122146 # check rows
123147 for row_idx in range (3 ):
124148 # if the 3 cells in this row match
125- if (board_grid [0 + (row_idx * 3 )][0 ] != 0 and
126- board_grid [0 + (row_idx * 3 )][0 ] ==
127- board_grid [1 + (row_idx * 3 )][0 ] ==
128- board_grid [2 + (row_idx * 3 )][0 ]):
149+ if (
150+ board_grid [0 + (row_idx * 3 )][0 ] != 0
151+ and board_grid [0 + (row_idx * 3 )][0 ]
152+ == board_grid [1 + (row_idx * 3 )][0 ]
153+ == board_grid [2 + (row_idx * 3 )][0 ]
154+ ):
129155 return board_grid [0 + (row_idx * 3 )][0 ]
130156
131157 # if any of the cells in this row are empty
132- if 0 in (board_grid [0 + (row_idx * 3 )][0 ],
133- board_grid [1 + (row_idx * 3 )][0 ],
134- board_grid [2 + (row_idx * 3 )][0 ]):
158+ if 0 in (
159+ board_grid [0 + (row_idx * 3 )][0 ],
160+ board_grid [1 + (row_idx * 3 )][0 ],
161+ board_grid [2 + (row_idx * 3 )][0 ],
162+ ):
135163 found_empty = True
136164
137165 # check columns
138166 for col_idx in range (3 ):
139167 # if the 3 cells in this column match
140- if (board_grid [0 + col_idx ][0 ] != 0 and
141- board_grid [0 + col_idx ][0 ] ==
142- board_grid [3 + col_idx ][0 ] ==
143- board_grid [6 + col_idx ][0 ]):
168+ if (
169+ board_grid [0 + col_idx ][0 ] != 0
170+ and board_grid [0 + col_idx ][0 ]
171+ == board_grid [3 + col_idx ][0 ]
172+ == board_grid [6 + col_idx ][0 ]
173+ ):
144174 return board_grid [0 + col_idx ][0 ]
145175
146176 # if any of the cells in this column are empty
147- if 0 in (board_grid [0 + col_idx ][0 ],
148- board_grid [3 + col_idx ][0 ],
149- board_grid [6 + col_idx ][0 ]):
177+ if 0 in (
178+ board_grid [0 + col_idx ][0 ],
179+ board_grid [3 + col_idx ][0 ],
180+ board_grid [6 + col_idx ][0 ],
181+ ):
150182 found_empty = True
151183
152184 # check diagonals
153- if board_grid [0 ][0 ] != 0 and \
154- board_grid [0 ][0 ] == board_grid [4 ][0 ] == board_grid [8 ][0 ]:
185+ if (
186+ board_grid [0 ][0 ] != 0
187+ and board_grid [0 ][0 ] == board_grid [4 ][0 ] == board_grid [8 ][0 ]
188+ ):
155189 return board_grid [0 ][0 ]
156190
157- if board_grid [2 ][0 ] != 0 and \
158- board_grid [2 ][0 ] == board_grid [4 ][0 ] == board_grid [6 ][0 ]:
191+ if (
192+ board_grid [2 ][0 ] != 0
193+ and board_grid [2 ][0 ] == board_grid [4 ][0 ] == board_grid [6 ][0 ]
194+ ):
159195 return board_grid [2 ][0 ]
160196
161197 if found_empty :
0 commit comments