185185# transparent pixels in the corners for the rounded corner effect
186186exit_btn_bmp .pixel_shader .make_transparent (0 )
187187
188- # centered within the display, offset to the right
189- exit_btn .x = display .width // scale_factor // 2 - (exit_btn_bmp .width ) // 2 + 30
190-
191- # inside the bounds of the game over label, so it looks like a dialog visually
192- exit_btn .y = 100
193-
194188# add the play again and exit buttons to the game over group
195189game_over_group .append (play_again_btn )
196- game_over_group .append (exit_btn )
197190main_group .append (game_over_group )
198191
192+ # Along right border
193+ exit_btn .x = display .width // scale_factor - (exit_btn_bmp .width )
194+ exit_btn .y = 100
195+ main_group .append (exit_btn )
196+
199197# wait a second for USB devices to be ready
200198time .sleep (1 )
201199
289287 # The mouse might have glitched and may not be detected but at least we don't crash
290288 print (e )
291289
290+ if len (mice ) >= 2 :
291+ break
292+
293+ if len (mice ) < 2 :
294+ for device in usb .core .find (find_all = True ):
295+ if device in mice :
296+ print ('found device twice' )
297+ continue
298+ # check if current device is has a boot mouse endpoint
299+ try :
300+ mouse_interface_index , mouse_endpoint_address = (
301+ adafruit_usb_host_descriptors .find_report_mouse_endpoint (device )
302+ )
303+ if mouse_interface_index is not None and mouse_endpoint_address is not None :
304+ if mouse_interface_index in mouse_interface_indexes and mouse_endpoint_address in mouse_endpoint_addresses :
305+ print ('found index/address twice' )
306+ continue
307+ # if it does have a boot mouse endpoint then add information to the
308+ # usb info lists
309+ mouse_interface_indexes .append (mouse_interface_index )
310+ mouse_endpoint_addresses .append (mouse_endpoint_address )
311+
312+ # add the mouse device instance to list
313+ mice .append (device )
314+ print (
315+ f"mouse interface: { mouse_interface_index } "
316+ + f"endpoint_address: { hex (mouse_endpoint_address )} "
317+ )
318+ mouse_sync .append (- 1 )
319+
320+ # detach kernel driver if needed
321+ kernel_driver_active_flags .append (device .is_kernel_driver_active (0 ))
322+ if device .is_kernel_driver_active (0 ):
323+ device .detach_kernel_driver (0 )
324+
325+ # set the mouse configuration so it can be used
326+ device .set_configuration ()
327+
328+ except usb .core .USBError as e :
329+ # The mouse might have glitched and may not be detected but at least we don't crash
330+ print (e )
331+
332+ if len (mice ) >= 2 :
333+ break
334+
292335def is_mouse1_left_clicked ():
293336 """
294337 Check if mouse 1 left click is pressed
@@ -356,7 +399,11 @@ def get_mouse_deltas(buffer, read_count, sync):
356399 :param read_count: the number of bytes read from the mouse
357400 :return: tuple containing x and y delta values
358401 """
359- if read_count == 4 or (read_count == 8 and sync > 50 ):
402+
403+ if read_count == 6 and sync == - 1 :
404+ delta_x = buffer [2 ]
405+ delta_y = buffer [3 ]
406+ elif read_count == 4 or (read_count == 8 and sync > 50 ):
360407 delta_x = buffer [1 ]
361408 delta_y = buffer [2 ]
362409 elif read_count == 8 :
@@ -407,6 +454,8 @@ def atexit_callback():
407454 )
408455 mouse_deltas = get_mouse_deltas (mouse_bufs [i ], data_len , mouse_sync [i ])
409456 mouse_sync [i ] = mouse_deltas [2 ]
457+ if mouse_sync [i ] == - 1 :
458+ mouse_bufs [i ][0 ] = mouse_bufs [i ][1 ]
410459 # if we got data, then update the mouse cursor on the display
411460 # using min and max to keep it within the bounds of the display
412461 mouse_tg .x = max (
@@ -445,6 +494,11 @@ def atexit_callback():
445494 # get the current mouse coordinates
446495 coords = (mouse_tg .x , mouse_tg .y , 0 )
447496
497+ # if the mouse point is within the exit
498+ # button bounding box
499+ if exit_btn .contains (coords ):
500+ supervisor .reload ()
501+
448502 # if the current state is GAMEOVER
449503 if match3_game .cur_state != STATE_GAMEOVER :
450504 # let the game object handle the click event
@@ -458,11 +512,6 @@ def atexit_callback():
458512 # reload
459513 supervisor .reload ()
460514
461- # if the mouse point is within the exit
462- # button bounding box
463- if exit_btn .contains (coords ):
464- supervisor .reload ()
465-
466515 # if the game is over
467516 except GameOverException :
468517 # check for a winner
@@ -483,6 +532,10 @@ def atexit_callback():
483532 # show a tie game message
484533 message = "\n Game Over\n Tie Game Everyone Wins!"
485534
535+ # centered within the display, offset to the right
536+ # inside the bounds of the game over label, so it looks like a dialog visually
537+ exit_btn .x = display .width // scale_factor // 2 - (exit_btn_bmp .width ) // 2 + 30
538+ exit_btn .y = 100
486539 # make the gameover group visible
487540 game_over_group .hidden = False
488541
0 commit comments