This is my first time posting on this site, but I have been a lurker for about a year now.
Back story: I'm struggling with a confusing bug in a program meant to simulate a variation on the game of chess. I had to write this program as a final project last term and there it didn't require any sort of user interface. That part, the bit that I had to turn in, works just fine for my purposes. However, now as a little side project, I'm trying to add a UI using python arcade as a framework!
TL;DR: I'm trying to add a UI to a chess game I wrote.
Everything seems to work fine at first, all the pieces move correctly, follow the rules that I laid out, capture opponents correctly, etc.. Then, all of a sudden, after anywhere from 4 to 14 moves, the held sprite will stop tracking properly with the mouse and refuse to "placed" on the board, irrecoverably stopping gameplay dead in its tracks. It's worth noting that no error is thrown on the console when this happens.
I've been working and reworking the code for a few days now without any success. After testing my functions fairly thoroughly (and happily discovering a couple of other bugs), I believe I've narrowed down the issue to somewhere in on_mouse_press() or on_mouse_drag(). I'm fairly certain there is a flaw in my logic somewhere, but after staring at the code for so long I think I've become blind to any alternatives. Here is the code for those two functions:
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
"""
Upon pressing the left mouse button, you will select a piece to move.
"""
# Get whatever sprite is at the point
white_pieces: list[Sprite] = arcade.get_sprites_at_point((x, y), self._white_sprites)
black_pieces: list[Sprite] = arcade.get_sprites_at_point((x, y), self._black_sprites)
# Assign values to appropriate movement data members
if len(white_pieces) > 0:
self._moving_piece = white_pieces[0]
self._moving_piece_og_pos = self._moving_piece.position
white_pieces.clear()
elif len(black_pieces) > 0:
self._moving_piece = black_pieces[0]
self._moving_piece_og_pos = self._moving_piece.position
black_pieces.clear()
else:
print('That isn\'t a piece!')
def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int):
"""
Drag the piece to where you want it to go.
"""
if (self._moving_piece in self._black_sprites) \
or (self._moving_piece in self._white_sprites): # Are we moving a piece?
self._moving_piece.center_x += dx
self._moving_piece.center_y += dy
If you all can't tell, I'm pretty new to coding but I love to learn, so any help would be greatly appreciated!
I'd be happy to provide more of the code if necessary.