Learn Before
Adding the Ball Animations
Now that we have a ball, we need to make our ball move. To do this, we need the move method of the ball to be called regularly. We can do this by using Kivy's builtin Clock function. For example, Clock.schedule_interval(game.update, 1.0/60.0) would cause the update function of the game object to be called once every 60th of a second.
Even though we have a way to call the PongBall's move function regularly, we don't have any references to the ball object in our game besides the reference in the build method. Since we need to do more than just move the ball (e.g. bouncing the balls off racquets and walls), we will need an update method for our PongGame class.
class PongGame(Widget): def update(self, dt): # call ball.move and other stuff pass class PongApp(App): def build(self): game = PongGame() Clock.schedule_interval(game.update, 1.0/60.0) return game
However, this still doesn't change the fact that we don't have a reference to the PongBall child widget created by the kv rule. To fix this, we can add an ObjectProperty to the PongGame class, and hook it up to the widget created in the kv rule. Then, we can easily refernce the ball property inside the update method and even make it bounce off the edges.
class PongGame(Widget): ball = ObjectProperty(None) 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
And remember to hook it up to the kv file by giving the child widget an id and setting the PongGame’s ball ObjectProperty to that id.
<PongGame>: ball: pong_ball # ... (canvas and Labels) PongBall: id: pong_ball center: self.parent.center
The code should look like this so far.
main.py:
from kivy.app import App from kivy.uix.widget import Widget from kivy.properties import ( NumericProperty, ReferenceListProperty, ObjectProperty ) from kivy.vector import Vector from kivy.clock import Clock from random import randint class PongBall(Widget): velocity_x = NumericProperty(0) velocity_y = NumericProperty(0) velocity = ReferenceListProperty(velocity_x, velocity_y) def move(self): self.pos = Vector(*self.velocity) + self.pos class PongGame(Widget): ball = ObjectProperty(None) def serve_ball(self): self.ball.center = self.center self.ball.velocity = Vector(4, 0).rotate(randint(0, 360)) 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 PongApp(App): def build(self): game = PongGame() game.serve_ball() Clock.schedule_interval(game.update, 1.0 / 60.0) return game if __name__ == '__main__': PongApp().run()
pong.kv:
#:kivy 1.0.9 <PongBall>: size: 50, 50 canvas: Ellipse: pos: self.pos size: self.size <PongGame>: 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
0
1
Contributors are:
Who are from:
Tags
Python Programming Language
Data Science