Skip to Content
Creating Apps in Kivy
book

Creating Apps in Kivy

by Dusty Phillips
April 2014
Beginner to intermediate content levelBeginner to intermediate
188 pages
4h 17m
English
O'Reilly Media, Inc.
Content preview from Creating Apps in Kivy

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Kivy - Interactive Applications and Games in Python

Kivy - Interactive Applications and Games in Python

Roberto Ulloa
Kivy Cookbook

Kivy Cookbook

Hugo Solis
Kivy Blueprints

Kivy Blueprints

Mark Vasilkov

Publisher Resources

ISBN: 9781491947333ErrataSupplemental Content