Browse Source

overall improvements

feature/balanceutils
Fr3deric 8 years ago committed by Frederic
parent
commit
03077f7f74
  1. 225
      balancep0ng.py
  2. 56
      pong.py
  3. BIN
      pongblib1.wav

225
balancep0ng.py

@ -0,0 +1,225 @@
#!/usr/bin/python3
import time
import threading
import sys
import os
import configparser
import socket
import struct
import blup.frame
import blup.output
import pong
import subprocess
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]
]
logo_go = [
[0,0,0,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,0,0],
[0,0,0,0,1,0,0,1,0,1,0,0,1,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0],
[0,0,0,0,1,0,1,1,0,1,0,0,1,0,0,0,0,0],
[0,0,0,0,1,0,0,1,0,1,0,0,1,0,0,0,0,0],
[0,0,0,0,0,1,1,0,0,1,1,1,1,0,0,0,0,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]
]
__MAX_SCORE__ = 5
def mk_logo_frame(dimension, logo):
frame = blup.frame.Frame(dimension)
color = pong.getRandomColor(dimension.depth - 1)
xoffs = (dimension.width - len(logo[0])) // 2
yoffs = (dimension.height - len(logo)) // 2
for x in range(len(logo[0])):
for y in range(len(logo)):
if logo[y][x] == 1:
frame.setPixel(xoffs + x, yoffs + y, color)
return frame
def convertPixels(dimension, pixels, invert=False):
p = []
maxval = dimension.depth - 1
color = pong.getRandomColor(maxval)
for i in range(len(pixels)):
row = []
for j in range(len(pixels[i])):
if pixels[i][j] == (1 if not invert else 0):
row.append(color)
else:
row.append((0,0,0))
p.append(row)
return p
def get_balance_from_sock(sock, player_id):
print('will balance haben...')
sock.send(b'a')
data = sock.recv(4)
p1x, p1y, p2x, p2y = struct.unpack('bbbb', data)
if player_id == 0:
bal = p1y
elif player_id == 1:
bal = p2y
print('balance id=%s balance=%d' % (player_id, bal))
return bal
class BalanceBoardPlayer(object):
def __init__(self, playground, ownPaddle, wiimote_sock, player_id):
self.__playground = playground
self.__ownPaddle = ownPaddle
self.__wiimote = wiimote_sock
self.__ready = False
self.__playground.addGameTickCallback(self.gametickcb)
self.__wiimote_sock = wiimote_sock
self.__player_id = player_id
@property
def ownPaddle(self):
return self.__ownPaddle
@property
def ready(self):
return true
def gametickcb(self):
bal = get_balance_from_sock(self.__wiimote_sock, self.__player_id)
if bal == -128:
print("player %d has quit" % (self.__player_id))
self.__wiimote_sock.close()
sys.exit(1)
MAX_AMPLITUDE = 65
if bal < -MAX_AMPLITUDE:
bal = -MAX_AMPLITUDE
elif bal > MAX_AMPLITUDE:
bal = MAX_AMPLITUDE
bal += MAX_AMPLITUDE
pos = int((bal / (2 * MAX_AMPLITUDE)) * self.__playground.height)
print("player %d pos=%d" % (self.__player_id, pos))
if self.__ownPaddle.ypos > pos:
self.__ownPaddle.nextMoveUp()
elif self.__ownPaddle.ypos < pos:
self.__ownPaddle.nextMoveDown()
class B4lancePong(object):
def __init__(self, out, color=True):
self.__playground = None
self.__out = out
self.__color = color
if color:
self.__dimension = blup.frame.FrameDimension(22, 16, 256, 3)
else:
self.__dimension = blup.frame.FrameDimension(18, 8, 2, 1)
def runGame(self):
print('starting a game...')
scoreLeft = 0
scoreRight = 0
self.__playground = pong.Playground(self.__dimension.width, self.__dimension.height, 5)
pp = pong.PlaygroundPainter(out, self.__dimension, self.__playground)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#sock.connect(('localhost', 4711))
sock.connect(('blinkenbunt', 4711))
print('connected!')
while get_balance_from_sock(sock, 0) == -128:
print('waiting for player...')
time.sleep(1)
self.__players = []
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.leftPaddle, sock, 0))
global logo
for i in range(5):
frame = mk_logo_frame(self.__dimension, onePlayer)
self.__out.sendFrame(frame)
print('waiting for second player...')
if get_balance_from_sock(sock, 1) > -128:
self.__players.append(BalanceBoardPlayer(self.__playground, self.__playground.rightPaddle, sock, 1))
break
time.sleep(1)
frame = mk_logo_frame(self.__dimension, logo)
self.__out.sendFrame(frame)
time.sleep(2)
def blib(obj):
if isinstance(obj, pong.Paddle):
subprocess.Popen(['mplayer','pongblib1.wav'])
self.__playground.ball.addHitCallback(blib)
frame = mk_logo_frame(self.__dimension, logo_go)
self.__out.sendFrame(frame)
time.sleep(1)
if len(self.__players) == 1:
bot = pong.PongBot(self.__playground, self.__playground.rightPaddle, 3)
self.__players.append(bot)
while max(scoreLeft, scoreRight) < __MAX_SCORE__:
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 = []
sock.close()
if len(sys.argv) > 1:
outspec = sys.argv[1]
else:
outspec = 'e3blp:localhost:4242'
out = blup.output.getOutput(outspec)
p0ng = B4lancePong(out, color=True)
while True:
p0ng.runGame()
sys.exit(0)

56
pong.py

@ -179,8 +179,8 @@ class Playground(object):
self.__width = width self.__width = width
self.__height = height self.__height = height
paddleLeft = Paddle(self, 0, (height - paddlesize)/2, paddlesize) paddleLeft = Paddle(self, 0, (height - paddlesize)//2, paddlesize)
paddleRight = Paddle(self, width - 1, (height - paddlesize)/2, paddlesize) paddleRight = Paddle(self, width - 1, (height - paddlesize)//2, paddlesize)
self.__paddles = [paddleLeft, paddleRight] self.__paddles = [paddleLeft, paddleRight]
self.__ball = Ball(self, 0, 0, 1, 1) self.__ball = Ball(self, 0, 0, 1, 1)
@ -211,7 +211,7 @@ class Playground(object):
def addGameTickCallback(self, callback): def addGameTickCallback(self, callback):
self.__gameTickCallbacks.append(callback) self.__gameTickCallbacks.append(callback)
print 'registered callback',callback print('registered callback',callback)
def addNewRoundCallback(self, callback): def addNewRoundCallback(self, callback):
self.__newRoundCallbacks.append(callback) self.__newRoundCallbacks.append(callback)
@ -235,8 +235,8 @@ class Playground(object):
if serve not in self.__paddles: if serve not in self.__paddles:
serve = self.__paddles[random.randint(0, 1)] serve = self.__paddles[random.randint(0, 1)]
ball.ypos = self.__height / 2 ball.ypos = self.__height // 2
ball.xpos = self.__width / 2 ball.xpos = self.__width // 2
if serve == rightPaddle and self.__width % 2 == 1: if serve == rightPaddle and self.__width % 2 == 1:
ball.xpos += 1 ball.xpos += 1
@ -249,8 +249,11 @@ class Playground(object):
for callback in self.__newRoundCallbacks: for callback in self.__newRoundCallbacks:
callback() callback()
ticks = 0
while True: while True:
time.sleep(0.1) ticks += 1
time.sleep(0.08)
if ticks %2 == 0:
ball.move() ball.move()
leftPaddle.doNextMove() leftPaddle.doNextMove()
rightPaddle.doNextMove() rightPaddle.doNextMove()
@ -259,7 +262,7 @@ class Playground(object):
break break
for callback in self.__gameTickCallbacks: for callback in self.__gameTickCallbacks:
print callback print(callback)
callback() callback()
if ball.xpos >= self.width: if ball.xpos >= self.width:
@ -269,7 +272,7 @@ class Playground(object):
def getRandomColor(maxval): def getRandomColor(maxval):
return map(lambda x: int(round(x*maxval)), colorsys.hsv_to_rgb(random.random(), 1, 1)) return list(map(lambda x: int(round(x*maxval)), colorsys.hsv_to_rgb(random.random(), 1, 1)))
#return map(lambda x: int(round(x*maxval)), colorsys.hsv_to_rgb(random.random(), random.random(), random.random())) #return map(lambda x: int(round(x*maxval)), colorsys.hsv_to_rgb(random.random(), random.random(), random.random()))
@ -347,7 +350,7 @@ class PongBot(object):
self.__hitpoint = hitpoint self.__hitpoint = hitpoint
def onHit(self, obj): def onHit(self, obj):
print 'hit!', obj print('hit!', obj)
if isinstance(obj, Paddle) and obj is not self.__ownPaddle: if isinstance(obj, Paddle) and obj is not self.__ownPaddle:
self.updateHitpoint() self.updateHitpoint()
@ -363,7 +366,7 @@ class PongBot(object):
def onGameTick(self): def onGameTick(self):
#self.updateHitpoint() #self.updateHitpoint()
print 'hitpoint', self.__hitpoint print('hitpoint', self.__hitpoint)
(hitx, hity) = self.__hitpoint (hitx, hity) = self.__hitpoint
@ -372,14 +375,14 @@ class PongBot(object):
if abs(self.__playground.ball.xpos - self.__ownPaddle.xpos) < 15: if abs(self.__playground.ball.xpos - self.__ownPaddle.xpos) < 15:
if not self.__dull: if not self.__dull:
if not self.__ownPaddle.containsPoint(hitx, hity): if not self.__ownPaddle.containsPoint(hitx, hity):
print 'moving!!' print('moving!!')
if self.__ownPaddle.ypos < hity: if self.__ownPaddle.ypos < hity:
self.__ownPaddle.nextMoveDown() self.__ownPaddle.nextMoveDown()
else: else:
self.__ownPaddle.nextMoveUp() self.__ownPaddle.nextMoveUp()
else: else:
if self.__ownPaddle.containsPoint(hitx, hity): if self.__ownPaddle.containsPoint(hitx, hity):
print 'moving!!' print('moving!!')
if hity < self.__ownPaddle.size: if hity < self.__ownPaddle.size:
self.__ownPaddle.nextMoveDown() self.__ownPaddle.nextMoveDown()
else: else:
@ -406,21 +409,24 @@ def displayScore(out, dimension, leftScore, rightScore, delay=0):
dotcolor = getRandomColor(dimension.depth - 1) dotcolor = getRandomColor(dimension.depth - 1)
numcolor = getRandomColor(dimension.depth - 1) numcolor = getRandomColor(dimension.depth - 1)
frame.setPixel(8, 1, dotcolor) xoffs = (dimension.width - 18) // 2
frame.setPixel(9, 1, dotcolor) yoffs = (dimension.height - 8) // 2
frame.setPixel(8, 2, dotcolor)
frame.setPixel(9, 2, dotcolor) frame.setPixel(xoffs + 8, yoffs + 1, dotcolor)
frame.setPixel(8, 4, dotcolor) frame.setPixel(xoffs + 9, yoffs + 1, dotcolor)
frame.setPixel(9, 4, dotcolor) frame.setPixel(xoffs + 8, yoffs + 2, dotcolor)
frame.setPixel(8, 5, dotcolor) frame.setPixel(xoffs + 9, yoffs + 2, dotcolor)
frame.setPixel(9, 5, dotcolor) frame.setPixel(xoffs + 8, yoffs + 4, dotcolor)
frame.setPixel(xoffs + 9, yoffs + 4, dotcolor)
frame.setPixel(xoffs + 8, yoffs + 5, dotcolor)
frame.setPixel(xoffs + 9, yoffs + 5, dotcolor)
for x in range(5): for x in range(5):
for y in range(5): for y in range(5):
if leftNumber[y][x] == 1: if leftNumber[y][x] == 1:
frame.setPixel(x+1, y+1, numcolor) frame.setPixel(xoffs + x+1, yoffs + y+1, numcolor)
if rightNumber[y][x] == 1: if rightNumber[y][x] == 1:
frame.setPixel(x+12, y+1, numcolor) frame.setPixel(xoffs + x+12, yoffs + y+1, numcolor)
out.sendFrame(frame) out.sendFrame(frame)
if delay > 0: if delay > 0:
@ -429,10 +435,10 @@ def displayScore(out, dimension, leftScore, rightScore, delay=0):
def main(): def main():
print 'running a \'Bot vs. Bot\' round...' print('running a \'Bot vs. Bot\' round...')
#out = blup.output.getOutput('blp:localhost:42421') #out = blup.output.getOutput('blp:localhost:42421')
#out = blup.output.getOutput('e3blp:bastel0:4242') #out = blup.output.getOutput('e3blp:bastel0:4242')
out = blup.output.getOutput('e3blp:localhost:4242') out = blup.output.getOutput('e3blp:blinkenbunt:4242')
#out = blup.output.getOutput('e3blp:bbunt:42429') #out = blup.output.getOutput('e3blp:bbunt:42429')
#dim = blup.frame.FrameDimension(18, 8, 8, 3) #dim = blup.frame.FrameDimension(18, 8, 8, 3)
dim = blup.frame.FrameDimension(22, 16, 255, 3) dim = blup.frame.FrameDimension(22, 16, 255, 3)
@ -447,7 +453,7 @@ def main():
leftBot = PongBot(playground, leftPaddle, 1) leftBot = PongBot(playground, leftPaddle, 1)
rightBot = PongBot(playground, rightPaddle, 1) rightBot = PongBot(playground, rightPaddle, 1)
print playground.play() print(playground.play())
if __name__ == '__main__': if __name__ == '__main__':

BIN
pongblib1.wav

Binary file not shown.
Loading…
Cancel
Save