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:



Python Arcade – Handling Mouse Inputs

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.

Similar Reads

Movement using Mouse Inputs

Here we will create a simple program using the arcade module to move our character using mouse inputs....

Handle Mouse Clicks

...

Contact Us