How to develop a simple Python Kivy bounce ball application

How to develop a simple Python Kivy bounce ball application


Here in this article we will try to build a very basic Kivy bounce ball application.

Test Environment

Fedora 32
Kivy 1.11.1

Python with kivy installed (This example is tested with python 3 and kivy 1.11 which are the latest available as of this writing)

What is Kivy

Kivy is a python framework for GUI based Application development. It is used to develop multitouch application interfaces which are compatible on various devices. Kivy uses a special language called .kv which is used to define the presentation logic and to separate it from the Python which can used for writing business logic and dynamic content generation for interfaces.

Here is the complete source code for this simple game available on github.

Procedure –

Step1: which extends the App base class and other Widget class to define the business logic

[admin@feddesk brick]$ cat

#!/usr/bin/env python
from import App
from kivy.uix.widget import Widget
from import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint

class PongPaddle(Widget):
    score = NumericProperty(0)

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()
    player = ObjectProperty()
    #score = NumericProperty()

    def serve_ball(self): =
        self.ball.velocity = Vector(4, 0).rotate(randint(0, 360))

    def update(self, dt):

        if (self.ball.y < or ( > self.height):
            self.ball.velocity_y *= -1

        if (self.ball.x < 0) or (self.ball.right > self.width):
            self.ball.velocity_x *= -1

        if self.ball.y <
            if self.player.x < self.ball.x < self.player.right:
                self.player.score += 1

    def on_touch_move(self, touch):
        if touch.x < 50:
            self.player.x = touch.x
        if touch.x > 50:
            self.player.right = touch.x

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

if __name__ == "__main__":


Step2: kivy file pong.kv which defines the presentation logic for the Widgets

Main Widget - PongGame
Ball Widget - PongBall
Player Widget - PongPaddle


[admin@feddesk brick]$ cat pong.kv

# File name: pong.kv
  size: 20, 20
      pos: self.pos
      size: self.size

  size: 100, 10
  pos: self.center_x - 50, 0
      pos: self.pos
      size: self.size

  ball: pong_ball
  player: player1

    id: player1
    y: 20
    center_x: root.center_x

    font_size: 20
    center_x: root.width - 30
    top: - 50
    text: "Score"

    font_size: 20
    center_x: root.width - 30
    top: - 70
    text: str(root.player.score)

    id: pong_ball
    pos: self.parent.center_x - 10, 10

Step3: Screenshot of the Game demo

For more detailed explanation of kivy concepts please go through the Kivy documentation for Pong Game Application as a starting point –

Hope you enjoyed reading this article. Thank you..