pyxen.input
访问来自键盘、鼠标、触摸屏和游戏手柄的实时输入。
input = pyxen.input
另请参见:输入。
模块
| 模块 | 描述 |
|---|---|
keyboard | 按键和文本输入 |
mouse | 位置、移动、按钮 |
touches | 多点触控输入 |
gamepads | 控制器 |
按钮模型
键盘按键、鼠标按钮和游戏手柄按钮都使用相同的 Button 接口:
button.down
button.pressed
button.released
button.value
| 属性 | 含义 |
|---|---|
down | 按住时为 True |
pressed | 仅在按下的那一帧为 True |
released | 仅在释放的那一帧为 True |
value | 模拟值(0.0 到 1.0) |
基于帧的行为
输入是逐帧计算的。
pressed 和 released 只在一帧内为 true,然后自动重置。
if input.keyboard.space.pressed:
player.jump()
对于持续行为:
if input.keyboard.space.down:
charge_power()
快速示例
键盘移动
kbd = input.keyboard
if kbd.left.down:
player.x -= 1
if kbd.right.down:
player.x += 1
鼠标点击
mouse = input.mouse
if mouse.left.pressed:
x, y = mouse.pos
spawn_explosion(x, y)
触摸输入
for t in input.touches:
if t.started:
print("Touch at", t.pos)
游戏手柄模拟移动
pad = input.gamepads[0]
if pad.connected:
x, y = pad.left_stick
player.x += x * 2
player.y += y * 2
只读安全
所有输入对象都是只读的。输入状态由引擎管理。