Python Arcade - 处理键盘输入
在本文中,我们将讨论如何在Python街机模块中处理键盘输入。
在 Arcade 中,您可以轻松检查按下了哪个键盘按钮并根据该按钮执行任务。
为此,我们将使用这些函数:
- on_key_press()
- on_key_release()
Syntax:
- on_key_press(symbol,modifiers)
- on_key_release (symbol, modifiers)
Parameters:
- symbol: Key that was hit
- modifiers: Bitwise ‘and’ of all modifiers (shift, ctrl, num lock) pressed during this event.
只要用户按下键盘按钮,就会调用 on_key_press()函数。同样,只要用户释放键盘按钮,就会调用 on_key_released()。
示例 1:
在本例中,我们将使用 Arcade 模块创建一个简单的程序,该程序将检查是否按下了上箭头键。
然后我们将在这个类中创建三个函数。
- on_draw():在这个函数,我们将使用 arcade.start_render() 开始渲染。
- on_key_press():每当按下键盘键时都会调用此函数。在这个函数,我们将检查按下的键是否是向上箭头键,然后我们将打印“向上箭头键被按下”。
- on_key_release():每当释放键盘键时都会调用此函数。在这个函数,我们将检查释放的键是否是向上箭头键,然后我们将打印“向上箭头键被释放”。
下面是实现:
Python3
# Importing arcade module
import arcade
# Creating MainGame class
class MainGame(arcade.Window):
def __init__(self):
super().__init__(600, 600, title="Keyboard Inputs")
# Creating on_draw() function to draw on the screen
def on_draw(self):
arcade.start_render()
# Creating function to check button is pressed
# or not
def on_key_press(self, symbol,modifier):
# Checking the button pressed
# is up arrow key or not
if symbol == arcade.key.UP:
print("Upper arrow key is pressed")
# Creating function to check button is released
# or not
def on_key_release(self, symbol, modifier):
# Checking the button pressed
# is up arrow key or not
if symbol == arcade.key.UP:
print("Upper arrow key is released")
# 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="Player Movement")
# Initializing the initial x and y coordinated
self.x = 250
self.y = 250
# Initializing a variable to store
# the velocity of the player
self.vel_x = 0
self.vel_y = 0
# Creating on_draw() function to draw on the screen
def on_draw(self):
arcade.start_render()
# Drawing the rectangle using
# draw_rectangle_filled function
arcade.draw_circle_filled(self.x, self.y,25,
arcade.color.GREEN )
# Creating on_update function to
# update the x coordinate
def on_update(self,delta_time):
self.x += self.vel_x * delta_time
self.y += self.vel_y * delta_time
# Creating function to change the velocity
# when button is pressed
def on_key_press(self, symbol,modifier):
# Checking the button pressed
# and changing the value of velocity
if symbol == arcade.key.UP:
self.vel_y = 300
print("Up arrow key is pressed")
elif symbol == arcade.key.DOWN:
self.vel_y = -300
print("Down arrow key is pressed")
elif symbol == arcade.key.LEFT:
self.vel_x = -300
print("Left arrow key is pressed")
elif symbol == arcade.key.RIGHT:
self.vel_x = 300
print("Right arrow key is pressed")
# Creating function to change the velocity
# when button is released
def on_key_release(self, symbol, modifier):
# Checking the button released
# and changing the value of velocity
if symbol == arcade.key.UP:
self.vel_y = 0
elif symbol == arcade.key.DOWN:
self.vel_y = 0
elif symbol == arcade.key.LEFT:
self.vel_x = 0
elif symbol == arcade.key.RIGHT:
self.vel_x = 0
# Calling MainGame class
MainGame()
arcade.run()
输出:
示例 2:
在这个例子中,我们根据键盘输入移动玩家。
为此,我们将创建一个 MainGame() 类。首先在这个类中,我们将初始化玩家精灵的 x 和 y 坐标以及玩家的 x 和 y 速度的一些变量,然后我们将在这个类中创建 4 个函数。
- on_draw():在这个函数,我们将绘制我们的播放器并开始渲染。
- setup():在这个函数,我们将初始化我们的相机和场景对象,然后我们将加载我们的播放器和平台的精灵。之后,我们将调用 PhysicsEnginePlatformer()函数。
- on_update():在这个函数,我们将通过添加 vel_x 和 vel_y 变量的值来更新玩家精灵的 x 和 y 坐标,
- on_key_press():在这个函数,我们将根据按下的键盘键来改变 vel_x 和 vel_y 变量的值。
- on_key_release():在这个函数,我们会根据释放的键盘按键来改变vel_x和vel_y变量的值。
下面是实现:
蟒蛇3
# Importing arcade module
import arcade
# Creating MainGame class
class MainGame(arcade.Window):
def __init__(self):
super().__init__(600, 600, title="Player Movement")
# Initializing the initial x and y coordinated
self.x = 250
self.y = 250
# Initializing a variable to store
# the velocity of the player
self.vel_x = 0
self.vel_y = 0
# Creating on_draw() function to draw on the screen
def on_draw(self):
arcade.start_render()
# Drawing the rectangle using
# draw_rectangle_filled function
arcade.draw_circle_filled(self.x, self.y,25,
arcade.color.GREEN )
# Creating on_update function to
# update the x coordinate
def on_update(self,delta_time):
self.x += self.vel_x * delta_time
self.y += self.vel_y * delta_time
# Creating function to change the velocity
# when button is pressed
def on_key_press(self, symbol,modifier):
# Checking the button pressed
# and changing the value of velocity
if symbol == arcade.key.UP:
self.vel_y = 300
print("Up arrow key is pressed")
elif symbol == arcade.key.DOWN:
self.vel_y = -300
print("Down arrow key is pressed")
elif symbol == arcade.key.LEFT:
self.vel_x = -300
print("Left arrow key is pressed")
elif symbol == arcade.key.RIGHT:
self.vel_x = 300
print("Right arrow key is pressed")
# Creating function to change the velocity
# when button is released
def on_key_release(self, symbol, modifier):
# Checking the button released
# and changing the value of velocity
if symbol == arcade.key.UP:
self.vel_y = 0
elif symbol == arcade.key.DOWN:
self.vel_y = 0
elif symbol == arcade.key.LEFT:
self.vel_x = 0
elif symbol == arcade.key.RIGHT:
self.vel_x = 0
# Calling MainGame class
MainGame()
arcade.run()
输出: