Somebody asked in StackOverflow how to do smooth movements of objects (widgets) in python game platforms. I didn't have the time to reply there because the question was closed because the question was posted in a way that promotes debate not answers. But here is an answer. I think. I hope. I just wanted to say that in Kivy is possible to do smooth movements, not complicated, very flexible and there is a tutorial about it: Pong Game Tutorial

This is a simplified code based of the tutorial where I just make the bool move slowly in diagonal direction from bottom left to top right. I hope it will help somebody.

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 kivy.lang import Builder

Builder.load_string("""
<PongBall>:
    size: 50, 50 
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size

<PongGame>:
    ball: pong_ball

    PongBall:
        id: pong_ball
        pos: 0,0
""")

class PongBall(Widget):
    velocity_x = NumericProperty(1)
    velocity_y = NumericProperty(1)
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    def move(self):
        self.pos = Vector(*self.velocity) + self.pos

class PongGame(Widget):
    ball = ObjectProperty(None)

    def update(self, dt):
        self.ball.move()

class PongApp(App):
    def build(self):
        game = PongGame()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game

if __name__ == '__main__':
    PongApp().run()

Leave a reply