Python Arcade - Handling Mouse Inputs
Last Updated :
23 Sep, 2021
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.
Movement using Mouse Inputs
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.
- on_draw():- Inside this function, we will start the rendering using arcade.start_render() and then we will draw our player.
- on_mouse_motion():- This function will be called whenever the user moves the mouse. Inside this function, we will change the x and y coordinate of the player.
Below is the implementation:
Python3
# Importing arcade module
import arcade
# Creating MainGame class
class MainGame(arcade.Window):
def __init__(self):
super().__init__(600, 600, title="Keyboard Inputs")
# Starting location of player
self.x = 100
self.y = 100
# Creating on_draw() function to draw on the screen
def on_draw(self):
arcade.start_render()
# Drawing our player
arcade.draw_circle_filled(self.x, self.y, 25,
arcade.color.GREEN)
# Creating function to check the position
# of the mouse
def on_mouse_motion(self, x, y, dx, dy):
"""
Called whenever the mouse moves.
"""
self.x = x
self.y = y
# Calling MainGame class
MainGame()
arcade.run()
Output:

Handle Mouse Clicks
Now 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.
Python3
# Importing arcade module
import arcade
# Creating MainGame class
class MainGame(arcade.Window):
def __init__(self):
super().__init__(600, 600, title="Keyboard Inputs")
# Starting location of player
self.x = 100
self.y = 100
# Creating on_draw() function to draw on the screen
def on_draw(self):
arcade.start_render()
# Drawing our player
arcade.draw_circle_filled(self.x, self.y,25,
arcade.color.GREEN )
# Creating function to check the position
# of the mouse
def on_mouse_motion(self, x, y, dx, dy):
"""
Called whenever the mouse moves.
"""
self.x = x
self.y = y
# Creating function to check the mouse clicks
def on_mouse_press(self, x, y, button, modifiers):
print("Mouse button is pressed")
# Calling MainGame class
MainGame()
arcade.run()
Output:
Similar Reads
Python Arcade - Handling Keyboard Input In this article, we will discuss how to handle keyboard inputs in Python arcade module. In Arcade, you can easily check which keyboard button is pressed and perform tasks according to that. For this, we are going to use these functions: on_key_press()on_key_release() Syntax: on_key_press(symbol,mod
4 min read
Python VLC MediaPlayer â Enabling Mouse Input Handling In this article we will see how we can enable mouse input handling the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediPlyer object is the basic obj
2 min read
Handle mouse events in Python - OpenCV OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is of paramount importance. Note: For more information, refer to Introduction to OpenCV Handling Mouse Evenets OpenC
2 min read
Pygame - Input Handling Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. The sys module in Python provides various functions and variables that are used to manipulate different parts
5 min read
PyQt5 Input Dialog | Python PyQt5 provides a class named QInputDialog which is used to take input from the user. In most of the application, there comes a situation where some data is required to be entered by the user and hence input dialog is needed. Input can be of type String or Text, Integer, Double and item.Used methods:
2 min read
Mouse Library in Python In this article, we will learn about the mouse library. In contrast to other Python modules, the Mouse Module enables us to fully control our mouse through a variety of features, including hooking global events, registering hotkeys, simulating mouse movement and clicks, and much more. The following
2 min read