#! /usr/bin/env python # -*- coding: utf-8 -*- """ Move a small ball (a sprite) with the WiiMote. Very simple, based on the tutorial PyGame Sprite Tutorial By PyMike (pymike93@gmail.com) March 1, 2008 and also the Wiiewer by Sam Hocevar This Source Code is Licensed under the GPL Álvaro Manera Pajarón il_bambino( at ) yute.com """ import pygame from pygame.locals import * from bluetooth import BluetoothSocket, BluetoothError, L2CAP import thread, time ADDRESS = '00:19:1D:C2:B3:E2' # Change this one, of course sensor = [127] * 3 #los ultimos valores del sensor connected = 0 # Listening thread def listener(): global connected, sensor fdin.settimeout(0.1) while connected == 1: try: msg = fdin.recv(23) #Llamada bloqueante except BluetoothError: continue if len(msg) >= 7: for c in range(3): sensor[c] = ord(msg[4 + c]) connected = -1 class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self,) self.image = pygame.Surface((32, 32)) self.image.fill((0, 0, 0)) pygame.draw.circle(self.image,(255,0,0),(16,16),16,0) self.rect = self.image.get_rect(topleft = (0, 0)) """This function is called once every frame in the main loop.""" def update(self): movx = sensor[0]-132 movy = sensor[1]-132 self.rect.move_ip(movx,movy) """Create a rect of the screen, then clamp the player's rect to it. Rect(0, 0, 640, 480) <-- first arg = x pos, second = y pos, third = width, fourth = height.""" SCREENRECT = Rect(0, 0, 640, 480) self.rect.clamp_ip(SCREENRECT) class Texto(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self,) self.text = "" self.pos = 0 self.update() self.x = 0 self.y = 0 def position(self,x,y): self.x = x self.y = y def texto(self,text): self.text = text """This function is called once every frame in the main loop.""" def update(self): self.image = pygame.font.Font(None,20).render(self.text, 1, (255, 255, 255),(0,0,0)) self.rect = self.image.get_rect() """Main game script that sets everything up.""" def main(): """Initialise pygame.""" pygame.init() """Set up the display mode.""" screen = pygame.display.set_mode((640, 480)) background = pygame.Surface([640, 480]) background.fill([0, 0, 0]) screen.blit(background, [0, 0]) pygame.display.set_caption("Wii Bola") all = pygame.sprite.RenderUpdates() """Clock() is another great module with pygame. With it you can cap framerate, get the frames per second, and some other cool stuff.""" clock = pygame.time.Clock() """Create the player sprite!""" player = Player() valores = Texto() all.add(player,valores) valores.texto("X: " + str(sensor[0]) + " Y: " + str(sensor[1]) + " Z: " + str(sensor[2])) """Main loop. This keeps the game running.""" while 1: """Cap the framerate at 60 frames per second""" clock.tick(50) """Quit the game if the X button on the window bar is pressed or if the Escape key is pressed.""" for e in pygame.event.get(): if e.type == QUIT: return if e.type == KEYDOWN: if e.key == K_ESCAPE: return valores.texto("X: " + str(sensor[0]) + " Y: " + str(sensor[1]) + " Z: " + str(sensor[2])) all.update() rectlist = all.draw(screen) pygame.display.update(rectlist) all.clear(screen,background) """Run the game script if this script is executed.""" if __name__ == "__main__": # Try to connect to Wiimote print 'connecting to Wiimote ' + ADDRESS + ', please press buttons 1 and 2' fdin = BluetoothSocket(L2CAP) fdin.connect((ADDRESS, 0x13)) fdout = BluetoothSocket(L2CAP) fdout.connect((ADDRESS, 0x11)) if not fdin or not fdout: raise 'could not connect to Wiimote, check the address' connected = 1 thread.start_new_thread(listener, ()) fdout.send("\x52\x11\x10") fdout.send("\x52\x12\x00\x31") #motion sensor on #fdout.send("\x52\x12\x00\x30") #motion sensor off main() connected = 0 fdin.close() fdout.close() pygame.display.quit()