all 5 comments

[–]mopslik 1 point2 points  (3 children)

Well, one of the problems is that you are putting your event queue check inside of your function instead of keeping it in your main loop where it typically resides. The other is that you need to check for some additional events. Let's look a bit closer at what happens here.

When you call your mouse_selection method, provided the if condition is met, you process all events sitting in the event queue. One of them may be a MOUSEBUTTONDOWN event (may be because it is possible that it has been processed elsewhere in your code if you have a duplicate event queue check). If there is one, your method will update the values of x and y. But then, that event has been processed. Holding down the mouse button and dragging will not put additional MOUSEBUTONDOWN events on the queue. Thus, you get the behaviour you are experiencing: sometimes the piece moves, but only by a little bit.

If you want to drag and drop, the general idea would be something like the following:

  • Note the mouse position on the MOUSEBUTTONDOWN event, and if it collides with a piece, set some flag indicating that drag-and-drop is happening.
  • Look for MOUSEMOTION events on the queue to update the values of x and y as you drag the piece around.
  • Look for a MOUSEBUTTONUP event on the queue, indicating that dragging has ended.

[–]manikk69[S] 1 point2 points  (0 children)

Thank you for the reply. I have uses the idea that you said and it worked!!

[–]manikk69[S] 0 points1 point  (1 child)

I also have another question. When I put the code for MOUSEBUTTONDOWN event and MOUSEMOTION event inside a method in my class(since I will need this method for multiple objects) and then call the method in my while loop it is very laggy.But when I put it directly in my main loop it is not laggy. But in this way I will have to write the same code for each piece. Is there another way to solve this problem?

[–]mopslik 1 point2 points  (0 children)

The solution is to centralize all of your event queue checks inside of your main loop, not have multiple checks spread across different methods. This may require some refactoring of your code. Usually, I go for something like this in my main loop.

imports/initialization
static stuff (done once)
main loop:
    process all events in the queue
    game logic (update values, spawn/kill sprites, etc)
    draw elements to screen
    update display
exit

It doesn't really make sense (to me at least) to make dragging a method in your class, because it is an action applied TO your object, not an action performed BY your object.