Add in initial work on Pong tutorial.
This commit is contained in:
commit
d4d7e21db9
|
@ -0,0 +1 @@
|
||||||
|
.idea/
|
|
@ -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.
|
|
@ -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()
|
|
@ -0,0 +1,33 @@
|
||||||
|
#:kivy 1.0.9
|
||||||
|
|
||||||
|
<PongGame>:
|
||||||
|
# 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
|
||||||
|
|
||||||
|
<PongBall>:
|
||||||
|
size: 50, 50
|
||||||
|
canvas:
|
||||||
|
Ellipse:
|
||||||
|
pos: self.pos
|
||||||
|
size: self.size
|
Loading…
Reference in New Issue