remote: pong: multiplayer
This commit is contained in:
parent
47f7b3ac38
commit
b5026b5b63
|
@ -1,68 +1,106 @@
|
|||
import pygame, pypong
|
||||
from pypong.player import BasicAIPlayer, KeyboardPlayer, MousePlayer, Rem0te
|
||||
from pypong.player import BasicAIPlayer, KeyboardPlayer, MousePlayer, Rem0tePlayer
|
||||
import r0ketrem0te.game
|
||||
import time
|
||||
|
||||
class Pong:
|
||||
def __init__(self):
|
||||
self.configuration = {
|
||||
'screen_size': (686,488),
|
||||
'paddle_image': 'assets/paddle.png',
|
||||
'paddle_left_position': 84.,
|
||||
'paddle_right_position': 594.,
|
||||
'paddle_velocity': 6.,
|
||||
'paddle_bounds': (0, 488), # This sets the upper and lower paddle boundary.The original game didn't allow the paddle to touch the edge,
|
||||
'line_image': 'assets/dividing-line.png',
|
||||
'ball_image': 'assets/ball.png',
|
||||
'ball_velocity': 4.,
|
||||
'ball_velocity_bounce_multiplier': 1.105,
|
||||
'ball_velocity_max': 32.,
|
||||
'score_left_position': (141, 30),
|
||||
'score_right_position': (473, 30),
|
||||
'digit_image': 'assets/digit_%i.png',
|
||||
'sound_missed': 'assets/missed-ball.wav',
|
||||
'sound_paddle': 'assets/bounce-paddle.wav',
|
||||
'sound_wall': 'assets/bounce-wall.wav',
|
||||
'sound': True,
|
||||
}
|
||||
pygame.mixer.pre_init(22050, -16, 2, 1024)
|
||||
pygame.init()
|
||||
|
||||
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 run():
|
||||
configuration = {
|
||||
'screen_size': (686,488),
|
||||
'paddle_image': 'assets/paddle.png',
|
||||
'paddle_left_position': 84.,
|
||||
'paddle_right_position': 594.,
|
||||
'paddle_velocity': 6.,
|
||||
'paddle_bounds': (0, 488), # This sets the upper and lower paddle boundary.The original game didn't allow the paddle to touch the edge,
|
||||
'line_image': 'assets/dividing-line.png',
|
||||
'ball_image': 'assets/ball.png',
|
||||
'ball_velocity': 4.,
|
||||
'ball_velocity_bounce_multiplier': 1.105,
|
||||
'ball_velocity_max': 32.,
|
||||
'score_left_position': (141, 30),
|
||||
'score_right_position': (473, 30),
|
||||
'digit_image': 'assets/digit_%i.png',
|
||||
'sound_missed': 'assets/missed-ball.wav',
|
||||
'sound_paddle': 'assets/bounce-paddle.wav',
|
||||
'sound_wall': 'assets/bounce-wall.wav',
|
||||
'sound': True,
|
||||
}
|
||||
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))
|
||||
#~ 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}
|
||||
|
||||
# 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)
|
||||
|
||||
# Main game loop
|
||||
timestamp = 1
|
||||
while game.running:
|
||||
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)
|
||||
#~ 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))
|
||||
pygame.display.flip()
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
game.running = False
|
||||
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||
game.running = False
|
||||
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))
|
||||
self.debug_surface = None
|
||||
self.clock = pygame.time.Clock()
|
||||
self.input_state = {'key': None, 'mouse': None}
|
||||
|
||||
if __name__ == '__main__': run()
|
||||
# Prepare game
|
||||
|
||||
self.game = pypong.Game(self.player_left, self.player_right, self.configuration)
|
||||
|
||||
def run(self):
|
||||
# Main game loop
|
||||
timestamp = 1
|
||||
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 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
|
||||
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:
|
||||
self.game.running = False
|
||||
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||
self.game.running = False
|
||||
|
||||
if __name__ == '__main__':
|
||||
pong = Pong()
|
||||
pong.run()
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue