diff --git a/DEV_NOTES.md b/DEV_NOTES.md index dfe12ee..e1087df 100644 --- a/DEV_NOTES.md +++ b/DEV_NOTES.md @@ -47,3 +47,5 @@ - Documentation on language does not have an explicit section on adding comments. - Nice to have tips for syntax-highlighting inside Kv files for PyCharm - Thanks @kjell for pointing out [PyCharm settings](https://github.com/kivy/kivy/wiki/Setting-Up-Kivy-with-various-popular-IDE's#kv-lang-auto-completion-and-highlighting) + +- The resulting game is quite sluggish... maybe framerate issues? diff --git a/main.py b/main.py index 7efa784..2993962 100644 --- a/main.py +++ b/main.py @@ -7,23 +7,34 @@ from kivy.uix import widget class PongGame(widget.Widget): # ADD note: why the None needed... the API allows for *args, **kwargs ball = properties.ObjectProperty(None) + player_one = properties.ObjectProperty(None) + player_two = properties.ObjectProperty(None) - def serve_ball(self): + def serve_ball(self, vel=(4, 0)): self.ball.center = self.center - # NOTE: Breaking this out - random_ball_direction = random.randint(0, 360) - self.ball.velocity = vector.Vector(4, 0).rotate(random_ball_direction) + self.ball.velocity = vel def update(self, dt): self.ball.move() - # bounce off top and bottom - if (self.ball.y < 0) or (self.ball.top > self.height): + # bounce ball off bottom or top + if (self.ball.y < self.y) or (self.ball.top > self.top): self.ball.velocity_y *= -1 - # bounce off left and right - if (self.ball.x < 0) or (self.ball.right > self.width): - self.ball.velocity_x *= -1 + # went of to a side to score point? + if self.ball.x < self.x: + self.player_two.score += 1 + self.serve_ball(vel=(4, 0)) + if self.ball.x > self.width: + self.player_one.score += 1 + self.serve_ball(vel=(-4, 0)) + + def on_touch_move(self, touch): + if touch.x < self.width / 3: + self.player_one.center_y = touch.y + + if touch.x > self.width - self.width / 3: + self.player_two.center_y = touch.y class PongBall(widget.Widget): @@ -36,6 +47,18 @@ class PongBall(widget.Widget): self.pos = vector.Vector(*self.velocity) + self.pos +class PongPaddle(widget.Widget): + score = properties.NumericProperty(0) + + def bounce_ball(self, ball): + if self.collide_widget(ball): + vx, vy = ball.velocity + offset = (ball.center_y - self.center_y) / (self.height / 2) + bounced = vector.Vector(-1 * vx, vy) + vel = bounced * 1.1 + ball.velocity = vel.x, vel.y + offset + + class PongApp(app.App): def build(self): diff --git a/pong.kv b/pong.kv index a89ba0e..08a49d2 100644 --- a/pong.kv +++ b/pong.kv @@ -3,6 +3,8 @@ : # This needs to be up top, otherwise the properties are not defined. ball: pong_ball + player_one: player_left + player_two: player_right canvas: Rectangle: @@ -25,9 +27,26 @@ id: pong_ball center: self.parent.center + PongPaddle: + id: player_left + x: root.x + center_y: root.center_y + + PongPaddle: + id: player_right + x: root.width-self.width + center_y: root.center_y + : size: 50, 50 canvas: Ellipse: pos: self.pos size: self.size + +: + size: 25, 200 + canvas: + Rectangle: + pos:self.pos + size:self.size