1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでXinputのゲームコントローラーを使う

Posted at

pygameというライブラリを使えばXinputのコントローラーがpythonで使えるようになるようです。
とりあえず適当にグラフィカルに表示するアプリを作ってみました。
xinput.gif
ボタン10種とスティック2本とLTとRTです。
Selはセレクトボタンのつもりで書いたけどBackボタンかもしれない

あまり時間がないのでテキトウにコードだけですがこんな感じ(暇になったら解説入れます)

Xinputテスト.py
import pygame
import cv2
import numpy as np
pygame.init()
pygame.joystick.init()

joystick = pygame.joystick.Joystick(0)
joystick.init()

while True:
    A=B=X=Y=L=R=Sel=Sta=L3=R3=False
    frame=np.zeros((400,900,3),np.uint8)
    pygame.event.pump()

    if joystick.get_button(0):
        A=True
    if joystick.get_button(1):
        B=True
    if joystick.get_button(2):
        X=True
    if joystick.get_button(3):
        Y=True
    if joystick.get_button(4):
        L=True
    if joystick.get_button(5):
        R=True
    if joystick.get_button(6):
        Sel=True
    if joystick.get_button(7):
        Sta=True
    if joystick.get_button(8):
        L3=True
    if joystick.get_button(9):
        R3=True

    cv2.putText(frame,"A B X Y L R Sel Sta L3 R3",(10,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(64,64,64),2)
    if A:
        cv2.putText(frame,"A",(10,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if B:
        cv2.putText(frame,"B",(80,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if X:
        cv2.putText(frame,"X",(150,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if Y:
        cv2.putText(frame,"Y",(225,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if L:
        cv2.putText(frame,"L",(290,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if R:
        cv2.putText(frame,"R",(355,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if Sel:
        cv2.putText(frame,"Sel",(435,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if Sta:
        cv2.putText(frame,"Sta",(560,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if L3:
        cv2.putText(frame,"L3",(690,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    if R3:
        cv2.putText(frame,"R3",(795,60),cv2.FONT_HERSHEY_SIMPLEX,2.0,(255,255,255),6)
    
    # 左スティックの値を取得
    left_x = joystick.get_axis(0)
    left_y = joystick.get_axis(1)
    # 右スティックの値を取得
    right_x = joystick.get_axis(2)
    right_y = joystick.get_axis(3)

    LboxX=200 #描画のための変数
    LboxY=100
    cv2.rectangle(frame,(LboxX,LboxY),(LboxX+200,LboxY+200),(128,128,128))
    cv2.drawMarker(frame,(LboxX+100,LboxY+100),(0,255,0))
    cv2.drawMarker(frame,(int(LboxX+100+left_x*100),int(LboxY+100+left_y*100)),(0,0,255)) #cv2はintのみを受け付け
    RboxX=LboxX+300 #左のボックスを基準に
    RboxY=LboxY
    cv2.rectangle(frame,(RboxX,RboxY),(RboxX+200,RboxY+200),(128,128,128))
    cv2.drawMarker(frame,(RboxX+100,RboxY+100),(0,255,0))
    cv2.drawMarker(frame,(int(RboxX+100+right_x*100),int(RboxY+100+right_y*100)),(0,0,255))

    LT = joystick.get_axis(4) #トリガー。任天堂でいうところのZLとZR、プレステ派ならL2とR2
    RT = joystick.get_axis(5)

    cv2.rectangle(frame,(LboxX-150,LboxY),(LboxX-100,LboxY+200),(128,128,128))
    cv2.rectangle(frame,(LboxX-150,LboxY+200),(LboxX-100,int(LboxY+100-LT*100)),(0,0,255),thickness=-1)

    cv2.rectangle(frame,(RboxX+300,RboxY),(RboxX+350,RboxY+200),(128,128,128))
    cv2.rectangle(frame,(RboxX+300,RboxY+200),(RboxX+350,int(RboxY+100-RT*100)),(0,0,255),thickness=-1)


    cv2.imshow("window",frame)
    cv2.waitKey(1)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?