Geeks making the world a bit better.

Using the Wiimote with PyGame

Several of my project teams this semester will (I hope) use PyGame and the Wiimote in their projects. I had it working fine on Ubuntu but struggled to find an interface that works with Python on Windows XP. Luke pointed me to wiiuse so I wrote a very simple wrapper with ctypes and then wrapped that in a module for PyGame. You can find the code in CVS at SourceForge in pywiiuse. I also uploaded a source release. You’ll need wiiuse.dll on Windows or wiiuse.so on Ubuntu.

Here’s a simple example using it in PyGame.

import pygame
import pygame_wiimote # ideally something like this would be part of pygame so the _ would become .
import sys

pygame.init()

# initialze the wiimotes
print 'press 1&2'
pygame_wiimote.init(1, 5) # look for 1, wait 5 seconds
n = pygame_wiimote.get_count() # how many did we get?

if n == 0:
    print 'no wiimotes found'
    sys.exit(1)

wm = pygame_wiimote.Wiimote(0) # access the wiimote object
wm.enable_accels(1) # turn on acceleration reporting

w,h = size = (512,512)
screen = pygame.display.set_mode(size)

run = True

old = [h/2] * 3
maxA = 2.0

colors = [ (255,0,0), (0,255,0), (0,0,255) ]

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print 'quiting'
            run = False
            break
        elif event.type == pygame_wiimote.WIIMOTE_BUTTON_PRESS:
            print event.button, 'pressed'
        elif event.type == pygame_wiimote.WIIMOTE_BUTTON_RELEASE:
            print event.button, 'released'
        elif event.type == pygame_wiimote.WIIMOTE_ACCEL:
            for c in range(3):
                s = int((event.accel[c] * h / maxA + h)/2)
                s = max(0, min(h-1, s))
                pygame.draw.line(screen, colors[c], (w-3, old[c]), (w-2, s))
                old[c] = s
            screen.blit(screen, (-1, 0))
        elif event.type == pygame_wiimote.NUNCHUK_ACCEL:
            print 'n accel', event.accel

    pygame.display.flip()
    pygame.time.wait(10)

wm.disconnect()

6 comments

#1 nim on 01.28.08 at 11:13 pm

thankyou. :-)

#2 links for 2008-04-23 on 04.22.08 at 7:56 pm

[...] Using the Wiimote with PyGame — Gary Bishop (tags: gamedev games library programming python) [...]

#3 Le blog de multimedialab.be » Archive du blog » Wiimote hacks on 05.05.08 at 10:17 am

[...] + Python. Wiimote + PyGame Wii + python + linux (YouTube [...]

#4 manuel on 06.19.08 at 9:27 am

hi, im kind of new using python, and i dont know how to use your example… there is something i should know or do before run this example??

thank you

#5 Websites tagged "pygame" on Postsaver on 12.05.08 at 9:47 pm

[...] - Using the Wiimote with PyGame — Gary Bishop saved by flowerman2008-10-31 - Pygame Tutorial « Keeping it Small and Simple saved by [...]

#6 Alpha release of wiiusecpp and pywii – Mission: Cognition on 06.16.09 at 2:35 am

[...] full featured as the wiiuse library.  There is a ctypes based python wrapper of wiiuse discussed here but it did not work for me and it would not have been very ‘pythonic’ since it really [...]

Leave a Comment