Thursday, December 26, 2024
Google search engine
HomeLanguagesPython Arcade – Handling Mouse Inputs

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.

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:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments