私はロボットをプログラミングしており、pygame を使用して Xbox コントローラーを使用したいと考えています。これまでのところ、これは私が得たものです (元のコードは Daniel J. Gonzalez の功績によるものです):
"""
Gamepad Module
Daniel J. Gonzalez
dgonz@mit.edu
Based off code from: http://robots.dacloughb.com/project-1/logitech-game-pad/
"""
import pygame
"""
Returns a vector of the following form:
[LThumbstickX, LThumbstickY, Unknown Coupled Axis???,
RThumbstickX, RThumbstickY,
Button 1/X, Button 2/A, Button 3/B, Button 4/Y,
Left Bumper, Right Bumper, Left Trigger, Right Triller,
Select, Start, Left Thumb Press, Right Thumb Press]
Note:
No D-Pad.
Triggers are switches, not variable.
Your controller may be different
"""
def get():
out = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
it = 0 #iterator
pygame.event.pump()
#Read input from the two joysticks
for i in range(0, j.get_numaxes()):
out[it] = j.get_axis(i)
it+=1
#Read input from buttons
for i in range(0, j.get_numbuttons()):
out[it] = j.get_button(i)
it+=1
first = out[1]
second = out[2]
third = out[3]
fourth = out[4]
return first, second, third, fourth
def test():
while True:
first, second, third, fourth = get()
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
print 'Initialized Joystick : %s' % j.get_name()
test()
「out」というリストが見えますか?その中の各要素は、Xbox コントローラーのボタンです。これらの要素を抽出して変数に配置し、各要素/ボタンに 1 つの変数を配置して、ロボットを制御できるようにします。
どうすればできますか?グローバル変数を使用しようとしましたが、すべてが混乱しました。私はPythonの初心者です。