Skip to content
Advertisement

PyGame: issue with MOUSEBUTTONDOWN events

I’m trying to build a game of Chess in PyGame. My code is something like this:

JavaScript

What I’m trying to do is this: The first MOUSEDOWNEVENT will select a piece that the user clicks on. If the user didn’t click on the piece, it will simply return None. The second MOUSEDOWNEVENT will actually move the selected piece to the new address that the user clicked on.

The problem is, both events run almost instantaneously. As soon as the first MOUSEDOWNEVENT has selected the piece, the second MOUSEDOWNEVENT runs and immediately moves it, before the user has the chance to click on a new square and move it there.

So I need to make it so that the user first clicks to select a piece, then clicks on a different location to move that piece there. How would I do that? Thank you! <3

Advertisement

Answer

You cannot implement a “first” and a “second” MOUSEBUTTONDOWN event. The event doesn’t know whether it is the first or the second. You have to implement 1 MOUSEBUTTONDOWN event and distinguish whether a piece is currently selected or not:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement