#! /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) except BluetoothError: continue if len(msg) >= 7: for c in range(3): sensor[c] = ord(msg[4 + c]) connected = -1 """ Things to remember when using the pygame sprite: -- A sprite must always have a .image value and a .rect value. .image should be a pygame surface, and .rect should be a pygame rect. -- The pygame sprite is especially useful with pygame groups. You should always use groups when using the pygame sprite, otherwise the sprite is more or less useless. """ class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self, self.containers) 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]-127 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) """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)) pygame.display.set_caption("Wii Bola") """RenderUpdates is a pygame group, with a fancy draw function. It speeds up drawing a lot.""" all = pygame.sprite.RenderUpdates() """Remember when we called Sprite.__init__(self, self.containers)? We must define containers now.""" Player.containers = all """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() """Main loop. This keeps the game running.""" while 1: """Cap the framerate at 60 frames per second""" clock.tick(50) """This calls the update function of the RenderUpdates group, which calls the update function of all the sprites.""" all.update() """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 """Fill the screen with a color.""" screen.fill((0, 0, 0)) """Call the draw function of the RenderUpdates group, which draws all the sprites to the screen.""" all.draw(screen) """Make stuff appear!""" pygame.display.flip() """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\x12\x00\x31") main() connected = 0 fdin.close() fdout.close() pygame.display.quit()