To play a game on the raspberry pi using an Xbox controller.
Xbox 360 controllers use a standard USB interface, and since the raspberry pi has two USB ports, I wanted to hook up an Xbox controller and play a game with it. There are a couple of other projects out there that hook controllers up to the raspberry pi and play games:
However they don’t seem to use Xbox controllers, and well, that’s what I have laying around.
I had also heard great things about the Scratch programming language and how it lets you quickly create games using a graphical interface. Since it came pre-installed on Raspbian, I decided to give it a shot.
The first step was to get the Xbox controller to communicate with the raspberry pi. To do this, I used xboxdrv, a linux Xbox 360 controller driver. This was fairly easy to set up on the raspberry pi, just type:
sudo apt-get install xboxdrv
This will download and install the driver.
The driver is capable of translating button presses on the controller into keyboard keys. I figured this would be the easiest way to integrate with a game since most respond to keyboard input. To map the Xbox controller buttons to keys and run the driver, use the following command:
sudo xboxdrv --ui-clear --ui-buttonmap a=XK_space,b=XK_b,x=XK_x,y=XK_y --ui-buttonmap dl=XK_Left,dr=XK_Right,du=XK_Up,dd=XK_Down
This maps the D-Pad on the controller to the up/down/left/right keyboard arrows and the green A button to the space bar. That’s the only keyboard keys that we care about for this demo.
Looking at the scratch website, there were tons of games submitted by the community, so I decided to give one of them a shot and see how it worked on the raspberry pi. The game that I landed on was Asteroid Avoider. It’s a game where you fly through space and try to avoid asteroids.
Playing the game is a little tricky because if you have a keyboard and a mouse installed in the two USB ports, you need to remove one of them in order to plug in the Xbox controller. It makes navigating a little tricky, but it works.
Scratch running on the raspberry pi is slow. This makes the game very difficult to play. It looks like pygame performs much better. However, connecting an Xbox controller to the raspberry pi is a breeze and seems to work great!
*Products and resources listed are listed to help members build their own Pi Projects. They are suggestions and listed for educational purposes. For substitutions of any parts, please post a question asking the original author.