Python Arcade——处理鼠标输入
在本文中,我们将学习如何在Python的 Arcade 模块中处理鼠标输入。
在 Arcade 中,您可以使用以下函数轻松处理鼠标输入:
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()函数。同样,只要用户按下鼠标按钮,就会调用 on_mouse_press()。
使用鼠标输入移动
在这里,我们将使用 Arcade 模块创建一个简单的程序,以使用鼠标输入移动我们的字符。
在下面的示例中,我们将创建一个 MainGame() 类。首先在这个类中,我们将初始化玩家的起始 x 和 y 坐标。然后我们将在这个类中创建三个函数。
- on_draw():-在这个函数,我们将使用 arcade.start_render() 开始渲染,然后我们将绘制我们的玩家。
- on_mouse_motion():-只要用户移动鼠标,就会调用这个函数。在这个函数,我们将改变玩家的 x 和 y 坐标。
下面是实现:
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()
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()
输出:
处理鼠标点击
现在为了处理鼠标点击,我们将创建一个名为“on_mouse_press”的新函数。每次用户单击鼠标按钮时都会调用此函数。
蟒蛇3
# 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()
输出: