Browse Source

Merge branch 'master' of blinkenbunt:gitrepos/blup3

feature/balanceutils
Fr3deric 8 years ago committed by Frederic
parent
commit
fcc2fa9af7
  1. 21
      gamemenu.py
  2. 37
      wii-pair/daemon.py

21
gamemenu.py

@ -177,6 +177,7 @@ InputEvent = enum.Enum('InputEvent', ['UP', 'DOWN', 'LEFT',
class AbstractInput(): class AbstractInput():
def __init__(self): def __init__(self):
self.player_present = False
pass pass
def get_event(self): def get_event(self):
@ -203,6 +204,8 @@ class TestInput(AbstractInput):
pygame = self.__pygame pygame = self.__pygame
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if self.control.get(event.key, None) == InputEvent.STEP_ON:
self.player_present = True
return self.controls.get(event.key, None) return self.controls.get(event.key, None)
return None return None
@ -238,7 +241,6 @@ class BalanceInput(AbstractInput, threading.Thread):
elif self.player_id == 1: elif self.player_id == 1:
xbal, ybal = p1x, p1y xbal, ybal = p1x, p1y
print('player_id=%d xbal=%d ybal=%d' % (self.player_id, xbal, ybal))
THRESHOLD = 40 THRESHOLD = 40
MIN_TIMES = { MIN_TIMES = {
InputEvent.LEFT: 0.05, InputEvent.LEFT: 0.05,
@ -277,12 +279,13 @@ class BalanceInput(AbstractInput, threading.Thread):
self.evt = evt self.evt = evt
self.lastevt = evt self.lastevt = evt
print('player_id=%d event=%s' % (self.player_id, self.evt)) print('player_id=%d event=%s' % (self.player_id, self.evt))
time.sleep(0.005)
else: else:
if xbal != -128 and ybal != -128: if xbal != -128 and ybal != -128:
self.evt = InputEvent.STEP_ON self.evt = InputEvent.STEP_ON
self.player_present = True self.player_present = True
continue continue
time.sleep(0.05)
def get_event(self): def get_event(self):
evt = self.evt evt = self.evt
@ -325,7 +328,12 @@ if __name__ == '__main__':
inp1 = BalanceInput((host, int(port)), 0) inp1 = BalanceInput((host, int(port)), 0)
inp2 = BalanceInput((host, int(port)), 1) inp2 = BalanceInput((host, int(port)), 1)
while inp1.get_event() != InputEvent.STEP_ON: while True:
evt = inp1.get_event()
if evt == InputEvent.STEP_ON:
break
elif evt == InputEvent.QUIT:
sys.exit()
time.sleep(0.05) time.sleep(0.05)
init_frame = create_init_screen(dim) init_frame = create_init_screen(dim)
@ -360,13 +368,16 @@ if __name__ == '__main__':
p2_frame = create_2player_screen(dim) p2_frame = create_2player_screen(dim)
out.sendFrame(p2_frame) out.sendFrame(p2_frame)
wait_time = 3 wait_time = 2
start = time.time() start = time.time()
while time.time() < start+wait_time: while time.time() < start+wait_time:
if inp2.get_event() == InputEvent.STEP_ON: evt = inp2.get_event()
if inp2.player_present:
inp1.running = False inp1.running = False
inp2.running = False inp2.running = False
start_game(game, 2) start_game(game, 2)
elif evt == InputEvent.QUIT:
sys.exit()
time.sleep(0.1) time.sleep(0.1)
inp1.running = False inp1.running = False

37
wii-pair/daemon.py

@ -73,6 +73,7 @@ class StatusThread(Thread):
print(b1.ljust(colw) + b2) print(b1.ljust(colw) + b2)
print(w1.ljust(colw) + w2) print(w1.ljust(colw) + w2)
print(p1.ljust(colw) + p2) print(p1.ljust(colw) + p2)
sys.stdout.flush()
time.sleep(1) time.sleep(1)
@ -188,18 +189,36 @@ try:
conns.remove(sock) conns.remove(sock)
continue continue
if len(m) == 0:
conns.remove(sock)
continue
r = b'' r = b''
for w in wiis: for w in wiis:
if w.pos_x is None: if m[0] == ord('w'):
v = -128 # Return weight
v = 0
if w.weight is not None:
v = round(w.weight)
r += struct.pack('b', v)
elif m[0] == ord('b'):
# Return battery
v = 0
if w.board.battery is not None:
v = round(w.board.battery*100)
r += struct.pack('b', v)
else: else:
v = int(round(w.pos_x*100)) # Return balance
r += struct.pack('b', v) if w.pos_x is None:
if w.pos_y is None: v = -128
v = -128 else:
else: v = int(round(w.pos_x*100))
v = int(round(w.pos_y*100)) r += struct.pack('b', v)
r += struct.pack('b', v) if w.pos_y is None:
v = -128
else:
v = int(round(w.pos_y*100))
r += struct.pack('b', v)
try: try:
sock.send(r) sock.send(r)

Loading…
Cancel
Save