From 0960c2e0200e522524364532c077765c0c6662da Mon Sep 17 00:00:00 2001 From: schneider Date: Thu, 15 Dec 2011 21:08:23 +0100 Subject: [PATCH] added basic r0ket support to py-pong --- tools/game/py-pong/main.py | 9 ++-- tools/game/py-pong/pypong/player.py | 64 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/tools/game/py-pong/main.py b/tools/game/py-pong/main.py index fc0d78d..12fbdab 100644 --- a/tools/game/py-pong/main.py +++ b/tools/game/py-pong/main.py @@ -1,5 +1,5 @@ import pygame, pypong -from pypong.player import BasicAIPlayer, KeyboardPlayer, MousePlayer +from pypong.player import BasicAIPlayer, KeyboardPlayer, MousePlayer, Rem0te def run(): configuration = { @@ -34,11 +34,12 @@ def run(): input_state = {'key': None, 'mouse': None} # Prepare game - player_left = KeyboardPlayer(input_state, pygame.K_w, pygame.K_s) + #player_left = KeyboardPlayer(input_state, pygame.K_w, pygame.K_s) #~ player_right = MousePlayer(input_state) - #player_left = BasicAIPlayer() - player_right = BasicAIPlayer() + player_left = BasicAIPlayer() + #player_right = BasicAIPlayer() + player_right = Rem0te() game = pypong.Game(player_left, player_right, configuration) # Main game loop diff --git a/tools/game/py-pong/pypong/player.py b/tools/game/py-pong/pypong/player.py index 076bd0d..1835c4a 100644 --- a/tools/game/py-pong/pypong/player.py +++ b/tools/game/py-pong/pypong/player.py @@ -1,5 +1,69 @@ import pygame, random +import r0ketrem0te.game +import r0ketrem0te.bridge +import r0ketrem0te.packets +import time +import Queue +import threading +class Rem0te(object): + def __init__(self): + self.maxplayer = 1 + self.players = {} + self.game = r0ketrem0te.game.Game('/dev/ttyACM0', "pong", 83, 81, (1,2,3,2,1)) + + self.queue = Queue.Queue() + self.game.bridge.registerQueue(self.queue) + self.game.bridge.registerCallback(self.receivedPacket) + self.state = 0 + self.checkPlayers() + + def checkPlayers(self): + toremove = [] + for player in self.players: + self.players[player]-=1 + if self.players[player] == 0: + toremove.append(player) + for player in toremove: + print "removing player", player + del self.players[player] + self.timer = threading.Timer(1, self.checkPlayers) + self.timer.start() + + def receivedPacket(self, packet): + if isinstance(packet, r0ketrem0te.packets.Join): + # flags = 1: join ok + # flags = 0: join not ok + flags = 0 + if len(self.players) < self.maxplayer: + flags = 1 + self.players[packet.id] = 10 + ack = r0ketrem0te.packets.Ack(packet.id, packet.ctr, flags) + qp = r0ketrem0te.bridge.QueuePacket( + self.game.channel, self.game.playermac, False, ack) + self.game.bridge.putInQueue(self.queue, qp) + elif packet.id in self.players: + self.players[packet.id] = 10 + if isinstance(packet, r0ketrem0te.packets.Button): + self.state = packet.button + + def update(self, paddle, game): + if self.state == 1: + paddle.direction = -1 + elif self.state == 2: + paddle.direction = 1 + else: + paddle.direction = 0 + + def hit(self): + pass + + def lost(self): + pass + + def won(self): + pass + class BasicAIPlayer(object): def __init__(self): self.bias = random.random() - 0.5