Test your Python skills with a Kivy-based runner game

Posted by


In this tutorial, we will be creating a simple endless runner game using Python and the Kivy library. Kivy is an open-source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.

To get started, make sure you have Kivy installed on your system. You can install Kivy using pip by running the following command:

pip install kivy

Once you have installed Kivy, you can start creating your runner game.

  1. Create the main.py file:
    Create a new Python file called main.py and import the necessary modules:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.properties import NumericProperty, ListProperty
from random import randint
  1. Create the RunnerGame class:
    Next, create a class called RunnerGame that inherits from the Widget class. In this class, we will define the runner and obstacles that the player will encounter.
class RunnerGame(Widget):
    runner_pos = ListProperty([0, 0])
    obstacle_pos = ListProperty([Window.width, 0])
    obstacle_speed = NumericProperty(5)

    def move_obstacle(self):
        self.obstacle_pos[0] -= self.obstacle_speed

        if self.obstacle_pos[0] + 100 <= 0:
            self.obstacle_pos = [Window.width, 0]

    def move_runner(self):
        pass

In the RunnerGame class, we have defined the runner’s position and the obstacle’s position. We have also defined methods to move the obstacle and the runner. The move_obstacle method moves the obstacle from right to left across the screen, and resets its position once it goes off the screen.

  1. Create the RunnerGameApp class:
    Next, create a class called RunnerGameApp that inherits from the App class. In this class, we will define the layout of the game and start the game loop.
class RunnerGameApp(App):
    def build(self):
        game = RunnerGame()
        Clock.schedule_interval(game.move_obstacle, 1.0/60.0)
        return game

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

In the RunnerGameApp class, we have defined the build method that creates an instance of the RunnerGame class and starts the game loop using the Clock.schedule_interval method.

  1. Create the kv file:
    Lastly, create a file called runner.kv that defines the layout of the game using the Kivy language. In this file, we will create a canvas with the runner and obstacle sprites.
<RunnerGame>:
    canvas:
        Rectangle:
            pos: self.obstacle_pos
            size: 100, 100

        Rectangle:
            pos: self.runner_pos
            size: 100, 100

In the kv file, we have defined the layout of the game using the Rectangle widget. The runner’s position is bound to the runner_pos property of the RunnerGame class, and the obstacle’s position is bound to the obstacle_pos property.

Now you can run the main.py file, and you should see a simple endless runner game with the runner and obstacle moving across the screen. You can customize the game by adding more obstacles, changing the speed of the obstacles, or adding more features such as score tracking or power-ups.

I hope this tutorial helps you get started with creating your runner game using Python and Kivy. Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@gumalgamul
21 days ago

the image is awesome

1
0
Would love your thoughts, please comment.x
()
x