commit d4d7e21db9a60764671bd6ba387e8fe32f177122 Author: Dorian Pula Date: Fri Jun 3 12:14:05 2016 -0700 Add in initial work on Pong tutorial. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/DEV_NOTES.md b/DEV_NOTES.md new file mode 100644 index 0000000..8ba36ff --- /dev/null +++ b/DEV_NOTES.md @@ -0,0 +1,14 @@ +# Notes while working on Kivy + +## Tutorial Improvements: +- [Adding Ball Animation](https://kivy.org/docs/tutorials/pong.html#adding-ball-animation) + - Add in placeholder variable to describe the random direction of the ball. + +## Kivy File +- Properties need to be declared at the top of a section in Kv. +- Nothing in the Kv language specification on the order of properties / sections. + +## Kivy language +- [Documentation for Kv language](https://kivy.org/docs/guide/lang.html) +- Documentation on language does not have an explicit section on adding comments. +- Nice to have tips for syntax-highlighting inside Kv files for PyCharm. diff --git a/main.py b/main.py new file mode 100644 index 0000000..7efa784 --- /dev/null +++ b/main.py @@ -0,0 +1,53 @@ +import random + +from kivy import app, clock, properties, vector +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) + + def serve_ball(self): + 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) + + def update(self, dt): + self.ball.move() + + # bounce off top and bottom + if (self.ball.y < 0) or (self.ball.top > self.height): + 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 + + +class PongBall(widget.Widget): + velocity_x = properties.NumericProperty(0) + velocity_y = properties.NumericProperty(0) + + velocity = properties.ReferenceListProperty(velocity_x, velocity_y) + + def move(self): + self.pos = vector.Vector(*self.velocity) + self.pos + + +class PongApp(app.App): + + def build(self): + game = PongGame() + game.serve_ball() + clock.Clock.schedule_interval(game.update, 1.0 / 60.0) + return game + + +def run_pong_game(): + PongApp().run() + + +if __name__ == '__main__': + run_pong_game() diff --git a/pong.kv b/pong.kv new file mode 100644 index 0000000..a89ba0e --- /dev/null +++ b/pong.kv @@ -0,0 +1,33 @@ +#:kivy 1.0.9 + +: + # This needs to be up top, otherwise the properties are not defined. + ball: pong_ball + + canvas: + Rectangle: + pos: self.center_x - 5, 0 + size: 10, self.height + + Label: + font_size: 70 + center_x: root.width / 4 + top: root.top - 50 + text: "0" + + Label: + font_size: 70 + center_x: root.width * 3 / 4 + top: root.top - 50 + text: "0" + + PongBall: + id: pong_ball + center: self.parent.center + +: + size: 50, 50 + canvas: + Ellipse: + pos: self.pos + size: self.size