I'm reading the kivy documentations. In it, there's an example on how to make your own pong game.
In general, the tutorial is very clear. However, at some point there was something subtle I wasn't sure about.
They make a ball in the .kv
file:
<PongBall>:
size: 50,50
canvas:
Ellipse:
pos: self.pos
size: self.size
Then, they add an instance of a PongBall
to a previously defined PongGame
class:
<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 / 4 * 3
top : root.top - 50
text: "0"
PongBall:
center: self.parent.center
In the PongGame
labels they added, the set the location using root
, which references the PongGame
For the PongBall
they set the location using self.parent.center
, where self.parent
appears to reference the PongGame
. I am assuming there is a reason they used:
<PongBall>:
PongBall:
center: self.parent.center
instead of
<PongBall>:
PongBall:
center: root.center
Why did they not use root.center
as they did for the labels? Is there a reason for picking one over the other? Is there a difference?
They do this, because the ball should be relative to the position of the game widget. The labels on the other hand are "dead". They just have to "exist" at the point they got in the beginning of the game, but the ball has to "live"