18
18
import supervisor
19
19
import terminalio
20
20
import 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 )
23
48
24
49
# group to hold visual elements
25
50
main_group = Group ()
71
96
buf = array .array ("b" , [0 ] * 4 )
72
97
73
98
# 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 ))
81
100
82
101
# load the tic-tac-toe spritesheet
83
102
tictactoe_spritesheet = OnDiskBitmap ("tictactoe_spritesheet.bmp" )
95
114
# loop over columns
96
115
for x in range (3 ):
97
116
# 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
+ )
102
126
103
127
# add the new TileGrid to the board grid at the current position
104
128
board_grid .add_content (new_tg , grid_position = (x , y ), cell_size = (1 , 1 ))
@@ -122,40 +146,52 @@ def check_for_winner():
122
146
# check rows
123
147
for row_idx in range (3 ):
124
148
# 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
+ ):
129
155
return board_grid [0 + (row_idx * 3 )][0 ]
130
156
131
157
# 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
+ ):
135
163
found_empty = True
136
164
137
165
# check columns
138
166
for col_idx in range (3 ):
139
167
# 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
+ ):
144
174
return board_grid [0 + col_idx ][0 ]
145
175
146
176
# 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
+ ):
150
182
found_empty = True
151
183
152
184
# 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
+ ):
155
189
return board_grid [0 ][0 ]
156
190
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
+ ):
159
195
return board_grid [2 ][0 ]
160
196
161
197
if found_empty :
0 commit comments