You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
4.7 KiB
178 lines
4.7 KiB
#!/usr/bin/python |
|
|
|
import time |
|
import threading |
|
import sys |
|
|
|
import blup.frame |
|
import blup.output |
|
sys.path.append('/var/tmp/r0ket/tools/game') |
|
#sys.path.append('/var/tmp/r0ket_misc/game_noprint') |
|
import r0ketrem0te.game |
|
import pong |
|
|
|
logo = [ |
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
|
[1,1,1,0,1,1,1,1,0,1,0,0,1,0,0,1,1,0], |
|
[1,0,1,0,1,0,0,1,0,1,1,0,1,0,1,0,0,1], |
|
[1,0,1,0,1,0,0,1,0,1,0,1,1,0,1,0,0,0], |
|
[1,1,1,0,1,0,0,1,0,1,0,0,1,0,1,0,1,1], |
|
[1,0,0,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1], |
|
[1,0,0,0,1,1,1,1,0,1,0,0,1,0,0,1,1,0], |
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] |
|
] |
|
onePlayer = [ |
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
|
[0,0,1,1,0,0,1,1,1,1,0,0,0,1,1,1,0,0], |
|
[0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0], |
|
[0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,1,0,0], |
|
[0,0,0,1,0,0,1,1,1,1,0,0,0,0,1,0,0,0], |
|
[0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0], |
|
[0,0,1,1,1,0,1,0,0,0,0,0,0,0,1,0,0,0], |
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] |
|
] |
|
|
|
def convertPixels(dimension, pixels): |
|
p = [] |
|
maxval = dimension.depth - 1 |
|
for i in range(len(pixels)): |
|
row = [] |
|
for j in range(len(pixels[i])): |
|
if pixels[i][j] == 1: |
|
row.append((maxval, maxval, maxval)) |
|
else: |
|
row.append((0,0,0)) |
|
p.append(row) |
|
return p |
|
|
|
class R0ketPongPlayer(object): |
|
def __init__(self, playground, ownPaddle, r0ketPlayer): |
|
self.__playground = playground |
|
self.__ownPaddle = ownPaddle |
|
self.__r0ketPlayer = r0ketPlayer |
|
self.__ready = False |
|
|
|
@property |
|
def ownPaddle(self): |
|
return self.__ownPaddle |
|
|
|
@property |
|
def ready(self): |
|
return self.__ready |
|
|
|
def receivedPacket(self, packet): |
|
if self.__r0ketPlayer is not None: |
|
if packet.id == self.__r0ketPlayer.id: |
|
if isinstance(packet, r0ketrem0te.packets.Button): |
|
print 'button=', packet.button |
|
if packet.button == 1: |
|
self.__ownPaddle.nextMoveUp() |
|
print 'up' |
|
if packet.button == 2: |
|
self.__ownPaddle.nextMoveDown() |
|
print 'down' |
|
if packet.button == 16: |
|
self.__ready = True |
|
|
|
class R0ketPong(object): |
|
def __init__(self, out, color=False, port='/dev/ttyACM0', gameName='pong'): |
|
self.__playground = None |
|
self.__out = out |
|
self.__rem0te = r0ketrem0te.game.Game(port, gameName, 83, 87, [ord(x) for x in 'REM0T'], 2, False) |
|
self.__rem0te.registerPlayerCallback(self.playerCallback) |
|
self.__color = color |
|
|
|
self.__players = [] |
|
self.__playersChangedCondition = threading.Condition() |
|
|
|
if color: |
|
self.__dimension = blup.frame.FrameDimension(18, 8, 8, 3) |
|
else: |
|
self.__dimension = blup.frame.FrameDimension(18, 8, 2, 1) |
|
|
|
def playerCallback(self, action, player): |
|
if self.__playground is None: |
|
return |
|
print '\n\n\n\nplayer added',player,'\n\n\n\n' |
|
if action == 'added' and len(self.__players) < 2: |
|
if len(self.__players) == 0: |
|
playerPaddle = self.__playground.leftPaddle |
|
else: |
|
playerPaddle = self.__playground.rightPaddle |
|
player = R0ketPongPlayer(self.__playground, playerPaddle, player) |
|
self.__rem0te.bridge.registerCallback(player.receivedPacket) |
|
self.__players.append(player) |
|
with self.__playersChangedCondition: |
|
self.__playersChangedCondition.notifyAll() |
|
|
|
def runGame(self): |
|
print 'starting a game...' |
|
scoreLeft = 0 |
|
scoreRight = 0 |
|
|
|
self.__playground = pong.Playground(18, 8) |
|
pp = pong.PlaygroundPainter(out, self.__dimension, self.__playground) |
|
|
|
while len(self.__players) == 0: |
|
print 'waiting for player...' |
|
with self.__playersChangedCondition: |
|
self.__playersChangedCondition.wait() |
|
|
|
frame = blup.frame.Frame(self.__dimension) |
|
global onePlayer |
|
if self.__color: |
|
frame.pixels = convertPixels(self.__dimension, onePlayer) |
|
else: |
|
frame.pixels = onePlayer |
|
self.__out.sendFrame(frame) |
|
|
|
while len(self.__players) == 1 and not self.__players[0].ready: |
|
print 'waiting for another player...' |
|
time.sleep(1) |
|
self.__out.sendFrame(frame) |
|
|
|
frame = blup.frame.Frame(self.__dimension) |
|
global logo |
|
if self.__color: |
|
frame.pixels = convertPixels(self.__dimension, logo) |
|
else: |
|
frame.pixels = logo |
|
self.__out.sendFrame(frame) |
|
time.sleep(1) |
|
|
|
if len(self.__players) == 1: |
|
bot = pong.PongBot(self.__playground, self.__playground.rightPaddle, 2) |
|
self.__players.append(bot) |
|
|
|
while max(scoreLeft, scoreRight) < 9: |
|
winner = self.__playground.play() |
|
if winner is self.__players[0].ownPaddle: |
|
scoreLeft += 1 |
|
else: |
|
scoreRight += 1 |
|
pong.displayScore(self.__out, self.__dimension, scoreLeft, scoreRight, 3000) |
|
|
|
for i in range(3): |
|
frame = blup.frame.Frame(self.__dimension) |
|
self.__out.sendFrame(frame) |
|
time.sleep(0.5) |
|
pong.displayScore(self.__out, self.__dimension, scoreLeft, scoreRight, 500) |
|
frame = blup.frame.Frame(self.__dimension) |
|
self.__out.sendFrame(frame) |
|
|
|
self.__playground = None |
|
self.__players = [] |
|
|
|
|
|
|
|
out = blup.output.getOutput('e3blp:localhost:4242') |
|
|
|
|
|
p0ng = R0ketPong(out, color=True) |
|
while True: |
|
p0ng.runGame() |
|
|
|
sys.exit(0) |
|
|
|
|
|
|
|
|