PYGAME在没有显示的情况下不返回操纵杆轴移动
原学程将引见PYGAME在出有显示的情形下没有前往把持杆轴挪动的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
我睹过这个成绩的其余处理计划,好比您须要挪用pygame.vent.pump(),或许在While轮回内部初初化把持杆。但是,即便有了这些处理计划,我照样获得了把持杆轴值的0。
假如我只撤消对于pygame.display.set_mode((一, 一))
的正文,则代码将按预期任务,而且值将输入到掌握台。
能否无方法依然不妨在没有创立额定窗心的情形下夺取轴值?
别的,我在Windows 一0上运转的是python三.六。
import pygame
FRAMES_PER_SECOND = 二0
pygame.init()
pygame.joystick.init()
# pygame.display.set_mode((一,一))
# Used to manage how fast the screen updates.
clock = pygame.time.Clock()
xboxController = pygame.joystick.Joystick(0)
xboxController.init()
# Loop until the user presses menu button
done = False
print('Found controller
Starting loop...')
while not done:
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.JOYBUTTONDOWN and event.button == 七:
print(f'Exiting controller loop')
done = True
for i in range(xboxController.get_numaxes()):
print(f'Axis {i}: {xboxController.get_axis(i)}')
# pygame.display.flip()
clock.tick(FRAMES_PER_SECOND)
输入:
pygame 一.九.六
Hello from the pygame co妹妹unity. https://www.pygame.org/contribute.html
Found controller
Starting loop...
Axis 0: 0.0
Axis 一: 0.0
Axis 二: 0.0
Axis 三: 0.0
Axis 四: 0.0
.
.
.
推举谜底
除非您须要全部下层GL
功效,不然我能够没有会应用它,由于该库是用于二D/三D游戏开辟的。虽然不妨将其用于这些目标,但是终究的成绩或者多或者少是弗成防止的。1种能够更简略的办法是应用Python的input库,它不妨处置游戏脚柄(把持杆)。
from inputs import get_gamepad
while True:
events = get_gamepad()
for event in events:
if event.ev_type == 'Absolute':
if event.code == 'ABS_X':
print(f'Left joystick x: {event.state}')
elif event.code == 'ABS_Y':
print(f'Left joystick y: {event.state}')
elif event.code == 'ABS_RX':
print(f'Right joystick x: {event.state}')
elif event.code == 'ABS_RY':
print(f'Right joystick y: {event.state}')
佳了闭于PYGAME在出有显示的情形下没有前往把持杆轴挪动的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。