diff options
| author | Tom Harley | 2017-12-02 20:45:21 +0000 |
|---|---|---|
| committer | Tom Harley | 2017-12-02 20:45:21 +0000 |
| commit | c8c62a0a4de9134792368534e91d6d2431602713 (patch) | |
| tree | 41caf274e88057c85f4fdad8aedb011275b6b056 /client | |
| parent | maybe more balls? (diff) | |
| parent | Better bouncing off paddles (diff) | |
| download | longpong-c8c62a0a4de9134792368534e91d6d2431602713.tar.gz longpong-c8c62a0a4de9134792368534e91d6d2431602713.zip | |
Merge branch ‘master’ of https://github.com/magnostherobot/longpong
Diffstat (limited to 'client')
| -rw-r--r-- | client/client.py | 40 | ||||
| -rw-r--r-- | client/game.py | 29 |
2 files changed, 43 insertions, 26 deletions
diff --git a/client/client.py b/client/client.py index 5614bb9..167a73e 100644 --- a/client/client.py +++ b/client/client.py @@ -14,22 +14,29 @@ class Client: def listen(self): try: - msg = self.sock.recv(4096) - jsonMSG = json.loads(msg.decode()) - print(jsonMSG) - if 'command' in jsonMSG: - val = jsonMSG['command'] - if val == 'start': - self.started = True - self.startupMessage = jsonMSG - self.msgs.append(jsonMSG) - elif val =='stop': - self.stopped = True - self.stoppedMessage = jsonMSG - else: + msg = self.sock.recv(4096).decode() + for message in msg.split('|'): + if len(message) <= 0: + continue + try: + jsonMSG = json.loads(message) + except json.decoder.JSONDecodeError as e: + print(message) + sys.exit(1) + print(jsonMSG) + if 'command' in jsonMSG: + val = jsonMSG['command'] + if val == 'start': + self.started = True + self.startupMessage = jsonMSG self.msgs.append(jsonMSG) - else: - print("It really shouldn't be here") + elif val =='stop': + self.stopped = True + self.stoppedMessage = jsonMSG + else: + self.msgs.append(jsonMSG) + else: + print("It really shouldn't be here") except socket.error as e: pass @@ -60,7 +67,8 @@ class Client: def send_ballchange(self, msg): msg['command'] = "bchange" jsonVal = json.dumps(msg) - self.sock.sendall(str(jsonVal).encode()) + message = str(jsonVal) + "|" + self.sock.sendall(message.encode()) def has_started(self): return self.started diff --git a/client/game.py b/client/game.py index 866e434..577b420 100644 --- a/client/game.py +++ b/client/game.py @@ -6,16 +6,22 @@ import sys class Ball: def __init__(self, pos, vel, size): self.pos = (pos['x'], pos['y']) + self.init_pos = (pos['x'], pos['y']) self.vel = (vel['x'], vel['y']) + self.init_vel = (vel['x'], vel['y']) self.size = (size['x'], size['y']) + def reset(self): + self.pos = self.init_pos + self.vel = self.init_vel + def __str__(self): return str(self.pos) + str(self.vel) + str(self.size) def move(self, dt): self.pos = ( - self.pos[0] + (self.vel[0] * dt) / 5, - self.pos[1] + (self.vel[1] * dt) / 5, + self.pos[0] + (self.vel[0] * dt) / 2, + self.pos[1] + (self.vel[1] * dt) / 2, ) def is_touching(self, paddle): @@ -154,8 +160,10 @@ class Game: # bounce the balls off the paddles for paddle in self.paddle_list: if ball.is_touching(paddle): - ball.vel = (-1.1 * ball.vel[0], - ball.vel[1] + 1 * ((paddle.pos[1] + (paddle.size[1] / 2)) - (ball.pos[1] + (ball.size[1] / 2)))) + paddle_centre = paddle.pos[1] + paddle.size[1] / 2 + ball_centre = ball.pos[1] + ball.size[1] / 2 + ball_vel_sign = ball.vel[1] / abs(ball.vel[1]) + ball.vel = (-1.1 * ball.vel[0], ball.vel[1] + ball_vel_sign * 5 * -(paddle_centre - ball_centre)) msg = { 'ball_id': i, 'vel': { @@ -172,15 +180,16 @@ class Game: # ball goes off the edge of the screen if ball.pos[0] <= 0: self.score = (self.score[0], self.score[1] + 1) - balls_to_remove.append(i) + balls_to_remove.append((i, 1)) elif ball.pos[0] + ball.size[0] >= self.renderer.get_rightmost_edge(): self.score = (self.score[0] + 1, self.score[1]) - balls_to_remove.append(i) - for ball in balls_to_remove: - del self.ball_list[ball] + balls_to_remove.append((i, -1)) + for (ball, direction) in balls_to_remove: + b = self.ball_list[ball] + b.reset() + b.vel = (b.vel[0] * direction, b.vel[1]) # self.events.has_key - paddle_speed = 0.2 * deltaT - print(self.paddle_list[0].pos[1]) + paddle_speed = 0.4 * deltaT if len(self.paddle_list) >= 1: if self.events.has_key(sdl2.SDLK_w): if self.paddle_list[0].pos[1] > 0: |
