diff options
| author | Billy Brown | 2017-12-02 20:01:26 +0000 |
|---|---|---|
| committer | Billy Brown | 2017-12-02 20:01:26 +0000 |
| commit | e77969a22ff3190e49084cf86066bd913cb8b9ed (patch) | |
| tree | 9a0c38e059788d48f56a964fb3918c661b41efe2 /client | |
| parent | Fixed total_w error (diff) | |
| download | longpong-e77969a22ff3190e49084cf86066bd913cb8b9ed.tar.gz longpong-e77969a22ff3190e49084cf86066bd913cb8b9ed.zip | |
Ball bounces
Diffstat (limited to 'client')
| -rw-r--r-- | client/client.py | 2 | ||||
| -rw-r--r-- | client/game.py | 31 |
2 files changed, 16 insertions, 17 deletions
diff --git a/client/client.py b/client/client.py index 7dec01e..5614bb9 100644 --- a/client/client.py +++ b/client/client.py @@ -57,7 +57,7 @@ class Client: jsonVal = json.dumps(msg) self.sock.sendall(str(jsonVal).encode()) - def send_ballchange(self,message): + def send_ballchange(self, msg): msg['command'] = "bchange" jsonVal = json.dumps(msg) self.sock.sendall(str(jsonVal).encode()) diff --git a/client/game.py b/client/game.py index 1c7ca89..3880e81 100644 --- a/client/game.py +++ b/client/game.py @@ -14,8 +14,8 @@ class Ball: def move(self, dt): self.pos = ( - self.pos[0] + (self.vel[0] * dt) / 10, - self.pos[1] + (self.vel[1] * dt) / 10, + self.pos[0] + (self.vel[0] * dt) / 5, + self.pos[1] + (self.vel[1] * dt) / 5, ) def is_touching(self, paddle): @@ -56,9 +56,10 @@ class Game: self.ball_list[msg[0]['ball_id']] = Ball(msg[0]['pos'],msg[0]['vel'],msg[0]['size']) print(self.ball_list) if (msg[0]['info']['offset'] == 0): - self.paddle_list.append(Paddle((0.1, 0.1), (0.01, 0.1))) - elif (self.renderer.get_rightmost_edge() == msg[0]['info']['total_w']): - self.paddle_list.append(Paddle((0.1, 0.1), (0.01, 0.1))) + self.paddle_list.append(Paddle((0.1, 0.1), (0.05, 0.2))) + rightmost_edge = self.renderer.get_rightmost_edge() + if (rightmost_edge == msg[0]['info']['total_w']): + self.paddle_list.append(Paddle((rightmost_edge - 0.05 - 0.1, 0.1), (0.05, 0.2))) break # Tell the server to start if the spacebar is pressed self.events.poll() @@ -102,20 +103,18 @@ class Game: for msg in msgs: if (msg['command'] == 'bchange'): touched[msg['ball_id']] = True - - if vel in msg: - ball.vel = (msg['vel'][0], msg['vel'][1]) - if pos in msg: - ball.pos = (msg['pos'][0], msg['pos'][1]) - if size in msg: - ball.size = (msg['size'][0], msg['size'][1]) + ball = self.ball_list[msg['ball_id']] + if 'vel' in msg: + ball.vel = (msg['vel']['x'], msg['vel']['y']) + if 'pos' in msg: + ball.pos = (msg['pos']['x'], msg['pos']['y']) + if 'size' in msg: + ball.size = (msg['size']['x'], msg['size']['y']) l_deltaT = c_time - msg['time'] ball.move(l_deltaT) for i, ball in self.ball_list.items(): - # move the balls - print(ball) if not touched[i]: ball.move(deltaT) # bounce the balls off the walls @@ -126,8 +125,8 @@ class Game: # bounce the balls off the paddles for paddle in self.paddle_list: if ball.is_touching(paddle): - ball.vel[0] = -1.1 * ball.vel[0] - ball.vel[1] = ball.vel[1] + 1 * ((paddle.pos[1] + (paddle.size[1] / 2)) - (ball.pos[1] + (ball.size[1] / 2))) + 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)))) msg = { 'ball_id': i, 'vel': { |
