actors
Deutsch   English   

2. ACTORS

 

 

YOU LEARN HERE...

 

how to create game characters (actors) in the game window and assign them an image.


 

All game characters are treated as objects of a class derived from the Actor class. Each actor is assigned a sprite image. However, an actor can also have multiple sprites.
The definition of a class in Python begins with the keyword class. The class name is written with a capital letter. If a class is derived, the name of the base class must be enclosed in brackets. Derived classes inherit all methods of the base class, but you can also add additional methods. A method differs from a function in only two ways:
- it is defined within a class
- the first parameter of a method is always self

A class has a special method called a constructor. Constructors in Python are always defined with def __init__(self). The class definition always comes before the main block.



 

EXAMPLES

 

Example1: You define a class Fish, which is derived from the class Actor, and display two objects of this class in the game window.
The sprite image nemo.gif is assigned to the Fish class. To make a fish appear in the game window, you must create an object of this class in the main programme. You name it nemo. With addActor(nemo, Location(1, 3)) nemo is added to the game window in cell (1, 3) (the numbering of the cells starts at 0). You can create multiple objects of the Fish class and add them to the game window.

 

Program:

# Gg2.py
from gamegrid import *

# ------------- class Fish -------------------------------
class Fish(Actor):
    def __init__(self):
        Actor.__init__(self, "sprites/nemo.gif")
 
# ---------------- main ----------------------------------
makeGameGrid(10, 10, 60, Color.red, "sprites/reef.gif")
nemo = Fish()
addActor(nemo, Location(1, 3))
wanda = Fish()
addActor(wanda, Location(6, 7))
show()
► Copy to clipboard

You can also create several actors of the Fish class without naming them individually. Then call the class name Fish() in the addActor() function and specify the desired position.

Program:

# Gg2a.py

from gamegrid import *

# ------------- class Fish -------------------------------
class Fish(Actor):
    def __init__(self):
        Actor.__init__(self, "sprites/nemo.gif")
 
# ---------------- main ----------------------------------
makeGameGrid(10, 10, 60, Color.red, "sprites/reef.gif")
addActor(Fish(), Location(1, 3))
addActor(Fish(), Location(6, 7))
show()
► Copy to clipboard

 

 

REMEMBER YOU...

 

Game characters are created as objects of a class derived from the Actor class. They are added to the game window with addActor(character, Location()).

All actor sprite images used in our tutorial are included in the WebTigerPython distribution. You can find them in the documentation under the link Image Library).

 

 

TO SOLVE BY YOURSELF

 

1.

Create a game window with 10 x 10 cells, each 60 pixels in size, with red grid lines, no background image and no navigation bar. Use the example Gg2.py as a template and write a class Pacman(). Then create three actors paki1, paki2 and paki3 at positions (1, 1), (3, 5) and (7, 3).
 

2.

Create 10 Pacman actors arranged diagonally in the game window. Use a for loop.