O'Reilly logo

Creating Apps in Kivy by Dusty Phillips

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. Modeling Enemies

The “game” as it stands is probably more fun for you to play, since you wrote it, than for anyone else. Truth be told, it’s kind of boring. What it needs is a little anxiety, a thrill of the chase, and a way to keep score.

The behavior of enemies should be relatively straightforward. Each one will start somewhere off the screen and then drift toward the character in the center at a constant rate of speed. Eventually, the growing character and the moving enemy collide. Depending on whether the character is safe or not, the enemies’ points will be absorbed by the player, or the player will die. The enemy will also have a score associated with it; the farther it is from the center of the screen, the higher the score.

Periodic Random Enemies

The enemy will be a FloatLayout with some basic graphics and a label with the current score. Start by creating this class in main.py. As shown in Example 11-1, you should put a couple properties on the class. The first represents the current score the player can achieve by killing this enemy. Later you’ll set this score depending on the distance the enemy is from the center. The second is the velocity at which the enemy is approaching you. We’ll discuss why the velocity is a list of two values shortly.

You’ll need to import the base class using from kivy.uix.floatlayout import FloatLayout, and the ListProperty using from kivy.properties import ListProperty.

Example 11-1. Enemy class with a score property
class Enemy(FloatLayout ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required