remote: pong: multiplayer

This commit is contained in:
schneider 2011-12-16 00:34:55 +01:00
parent 47f7b3ac38
commit b5026b5b63
2 changed files with 109 additions and 100 deletions

View File

@ -1,8 +1,11 @@
import pygame, pypong
from pypong.player import BasicAIPlayer, KeyboardPlayer, MousePlayer, Rem0te
from pypong.player import BasicAIPlayer, KeyboardPlayer, MousePlayer, Rem0tePlayer
import r0ketrem0te.game
import time
def run():
configuration = {
class Pong:
def __init__(self):
self.configuration = {
'screen_size': (686,488),
'paddle_image': 'assets/paddle.png',
'paddle_left_position': 84.,
@ -24,45 +27,80 @@ def run():
}
pygame.mixer.pre_init(22050, -16, 2, 1024)
pygame.init()
display_surface = pygame.display.set_mode(configuration['screen_size'])
output_surface = display_surface.copy().convert_alpha()
output_surface.fill((0,0,0))
self.rem0te = r0ketrem0te.game.Game('/dev/ttyACM0', "pong", 83, 81, (1,2,3,2,1), 2)
self.rem0te.registerPlayerCallback(self.playercallback)
self.player_right = Rem0tePlayer(self.rem0te)
self.player_left = Rem0tePlayer(self.rem0te)
self.stop = True
self.start = False
self.restart()
def playercallback(self, action, player):
if action == 'added':
if self.player_left.player == 0:
self.player_left.player = player
elif self.player_right.player == 0:
self.player_right.player = player
if self.player_left.player and self.player_right.player:
self.start = True
elif action == 'removed':
if self.player_left.player == player:
self.player_left.player = 0
elif self.player_right.player == player:
self.player_right.player = 0
if self.player_left.player == 0 or self.player_right.player == 0:
self.stop = True
def restart(self):
self.display_surface = pygame.display.set_mode(self.configuration['screen_size'])
self.output_surface = self.display_surface.copy().convert_alpha()
self.output_surface.fill((0,0,0))
#~ debug_surface = output_surface.copy()
#~ debug_surface.fill((0,0,0,0))
debug_surface = None
clock = pygame.time.Clock()
input_state = {'key': None, 'mouse': None}
self.debug_surface = None
self.clock = pygame.time.Clock()
self.input_state = {'key': None, 'mouse': None}
# Prepare game
#player_left = KeyboardPlayer(input_state, pygame.K_w, pygame.K_s)
#~ player_right = MousePlayer(input_state)
player_left = BasicAIPlayer()
#player_right = BasicAIPlayer()
player_right = Rem0te()
game = pypong.Game(player_left, player_right, configuration)
self.game = pypong.Game(self.player_left, self.player_right, self.configuration)
def run(self):
# Main game loop
timestamp = 1
while game.running:
clock.tick(60)
while self.game.running:
if self.start:
self.restart()
self.start = False
self.stop = False
if self.stop:
time.sleep(0.1)
continue
self.clock.tick(60)
now = pygame.time.get_ticks()
if timestamp > 0 and timestamp < now:
timestamp = now + 5000
print clock.get_fps()
input_state['key'] = pygame.key.get_pressed()
input_state['mouse'] = pygame.mouse.get_pos()
game.update()
game.draw(output_surface)
print self.clock.get_fps()
self.input_state['key'] = pygame.key.get_pressed()
self.input_state['mouse'] = pygame.mouse.get_pos()
self.game.update()
self.game.draw(self.output_surface)
#~ pygame.surfarray.pixels_alpha(output_surface)[:,::2] = 12
display_surface.blit(output_surface, (0,0))
if debug_surface:
display_surface.blit(debug_surface, (0,0))
self.display_surface.blit(self.output_surface, (0,0))
if self.debug_surface:
self.display_surface.blit(self.debug_surface, (0,0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game.running = False
self.game.running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
game.running = False
self.game.running = False
if __name__ == '__main__': run()
if __name__ == '__main__':
pong = Pong()
pong.run()

View File

@ -6,44 +6,15 @@ 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)
class Rem0tePlayer(object):
def __init__(self, rem0te):
self.rem0te = rem0te
self.rem0te.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()
self.player = 0
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 packet.id == self.player:
if isinstance(packet, r0ketrem0te.packets.Button):
self.state = packet.button