kollisionen
Deutsch   English   

5. COLLISIONS IN GRIDS

 

 

YOU LEARN HERE...

 

that interaction between game characters is extremely important in game programming. Collisions in grids allow you to check whether multiple actors are located in the same grid cell.

The getActorsAt() method returns all actors located in a specific cell in a list. If there is only one actor or no actor in a cell, you can use the getOneActorAt() method, which returns the actor or None if there is no actor in the cell.

 

 

EXAMPLES

 

 

Example1: Pacman eats pills
 

At the beginning, 10 pills of class Pill are placed in randomly selected free positions in the grid using the method getRandomEmptyLocation(). Pacman runs through the grid and eats the pills. The method getOneActorAt(self.getLocation(), Pill)can be used to check whether there is a pill at the current position. If so, it is made to disappear with actor.hide().

Instead of the hide() method, actor.removeSelf() can also be used. This permanently destroys the actor and it can no longer be retrieved with show().
 

Program:

# Gg5.py
from gamegrid import *

# ------------- class Pacman --------------------------
class Pacman(Actor):
     def __init__(self):
         Actor.__init__(self, "sprites/pacman.gif", 2)
     def act(self):
         self.move()
         self.tryToEat()
         self.showNextSprite()
         if self.getX() == 9:
             self.turn(90)
             self.setHorzMirror(True)
         if self.getX() == 0:
             self.turn(270)
             self.setHorzMirror(False)                
     def tryToEat(self):
        self.show(0)
        actor = getOneActorAt(self.getLocation(), Pill)
        if actor != None:
            actor.hide()
            self.show(1)                  
                    
 # ------------- class Pill ---------------------------
class Pill(Actor):
     def __init__(self):
         Actor.__init__(self, "sprites/pill_0.gif")
                        
# ---------------- main ------------------------------
makeGameGrid(10, 10, 60, Color.red, False)
paki = Pacman()
addActor(paki, Location(0, 0))
for i in range(20):
    addActor(Pill(), getRandomEmptyLocation())
show()
doRun() 
► Copy to clipboard

 

 

REMEMBER YOU...

 

With actor = getOneActorAt(self.getLocation, Pill), you can check whether there is a pill at the current position. If so (if (actor != None): returns True), you can make it disappear with actor.hide().

 

 

TO SOLVE BY YOURSELF

 

1.

Create an animated actor Fish() with the sprites fish_0.gif and fish_1.gif and an actor Alga() with the sprite alga.gif. Twenty algae are generated at randomly selected positions in the game window with the background image reef.gif. The fish should clean the sea, i.e. swim through the entire playing field and eat all the algae.  
 

2.
Create an Actor Ghost() with the sprite ghost.png and an animated Actor Creature() with the sprites creature_0.gif and creature_1.gif. 30 Creature actors are generated at randomly selected positions. The ghost starts in cell (0, 0) and runs through the entire game window. When it encounters a creature, it changes its colour.