Having completed the Pong Game tutorial from the official Kivy site I went on with their Crash Course. There in the very first video I saw this magic thing they call Scatter that pretty much enables you out-of-the-box to make UI things move with your mouse.
I thought this should provide a smoother way to control paddles in the Pong Game. The original way was to put the paddles-moving logic in on_touch_move()
method of PongGame object (PongGame class inherits from Widget) and it was a simple:
if touch.x < self.width / 3: # if you clicked in 1/3 of the window to the left
player1.center_y = touch.y # level the first paddle's center vertically with the mouse click position
This results in a jarring start if you happen to start moving your mouse cursor way below or above a paddle. I thought Scatter would work better. Alas so far I've failed to make it work.
What I started with was commenting out the on_touch_move()
method, then adding a Scatter object as a child of PongGame class in the pong.kv
file and making the existing PongPaddle objects children of the Scatter object. Like this:
<PongGame>:
Scatter:
do_scale: False
do_rotation: False
do_translation_x: False
PongPaddle:
id: player_left
x: root.x
center_y: root.center_y
PongPaddle:
id: player_right
x: root.width - self.width
center_y: root.center_y
As I used a single Scatter object and both paddles need to move independently I envisioned this will probably cause a problem (clicking one would make both move at the same time) but thought nevertheless it would be a good start.
No luck! This does not make paddles move with a mouse cursor. They still bounce the ball (even though they moved down in the widget tree and I haven't changed the Python code other than commenting out the on_touch_move()
method in the PongGame class body - I guess the references to ObjectProperty instances for paddles hooked up in the pong.kv
file still work), but they won't move.
Whole runnable code (my own version with the scatter)
Whole runnable code (my own version without the scatter)
Any ideas how to make it work?