![]() |
VOOZH | about |
In this article, we will learn how we can handle mouse inputs in the arcade module in Python.
In Arcade, you can easily handle the mouse inputs using these functions:
on_mouse_motion():
Syntax: on_mouse_motion(x, y, dx, dy)
Parameters:
- x : x coordinate
- y : y coordinate
- dx : change in x coordinate
- dy : change in y coordinate
on_mouse_press():
Syntax : on_mouse_press( x, y, button, modifiers)
Parameters:
- x : x coordinate
- y : y coordinate
- button : button that is pressed
- modifiers : Bitwise βandβ of all modifiers (shift, ctrl, num lock) pressed during this event.
on_mouse_motion() function will be called whenever the user moves the mouse. Similarly, on_mouse_press() will be called whenever a user presses a mouse button.
Here we will create a simple program using the arcade module to move our character using mouse inputs.
In the below example, we are going to create a MainGame() class. Inside this class first, we will initialize the starting x and y coordinate of the player. Then we will create three functions inside this class.
Below is the implementation:
Output:
π ImageNow to handle mouse clicks we are going to create a new function called "on_mouse_press". This function will be called every time user clicks a mouse button.