Relation

Adding the Ball

At this point, we will add a new PongBall class to create a widget that will be our ball and make it bounce around.

A PongBall class will need to created. The code is as follows.

class PongBall(Widget): # velocity of the ball on x and y axis velocity_x = NumericProperty(0) velocity_y = NumericProperty(0) # referencelist property so we can use ball.velocity as # a shorthand, just like e.g. w.pos for w.x and w.y velocity = ReferenceListProperty(velocity_x, velocity_y) # ``move`` function will move the ball one step. This # will be called in equal intervals to animate the ball def move(self): self.pos = Vector(*self.velocity) + self.pos

Next, the kv rule used to draw the ball is as follows.

<PongBall>: size: 50, 50 canvas: Ellipse: pos: self.pos size: self.size

Our files should look like this:

main.py:

from kivy.app import App from kivy.uix.widget import Widget from kivy.properties import NumericProperty, ReferenceListProperty from kivy.vector import Vector 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): pass class PongApp(App): def build(self): return PongGame() if __name__ == '__main__': PongApp().run()

pong.kv:

#:kivy 1.0.9 <PongBall>: size: 50, 50 canvas: Ellipse: pos: self.pos size: self.size <PongGame>: 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: center: self.parent.center

0

1

Updated 2021-09-03

Tags

Python Programming Language

Data Science