Game apps for smartphones and tablets

Research project PHBern  
HomeStart online editorAndroid TurtlegraphicsPrintJava-Online

Collisions in the grid


Interactions between the game figures is very important when programming a game. In JGameGrid, collisions are implemented in two ways:

The second proceeding is explained in the section collisions of figures.

When there are collisions in the grid, the method getActorsAt() gives back all actors that are in a certain cell in an ArrayList. You may restrict that list for a certain actor typ by indicating its class name. If there is none or only one actor in a cell, you can use the method getOneActorAt() that gives back the actor reference or zero.

Example 1: Eliminate actors: Pacman eats pills

At the beginning, 10 objects of the class pill are placed at randomly chosen free positions in the grid with the method getRandomEmptyLocation(). The pacman moves through the grid and eats the algae. With the method getOneActorAt(getLocation(), Pill.class) it can be checked if there is a pill at the current position. If yes, you can make it disappear with actor.hide().

Instead of using the method hide() also actor.removeSelf() can be used. Thereby, the actor is permanently destroyed and cannot be recovered.

Edit this example in the Online Editor

App installieren auf Smartphone oder Tablet

QR-Code

Download sources (AndroidEx10.zip)

 

// AndroidEx10.java

package app.ex10;

import ch.aplu.android.*;

public class AndroidEx10 extends GameGrid 
{
  public AndroidEx10()
  {
    super(8, 8, 0, RED);
  }

  public void main()
  {
    addActor(new Pacman(), new Location(0, 0));
    for (int = 0; i < 10; i++)
      addActor(new Pill(), getRandomEmptyLocation());
    doRun();
  }
}

class Pacman extends Actor
{
  public Pacman()
  {
    super("pacman"2);
  }

  public void act()
  {
    show(0);
    if (isMoveValid())
       move();
    if (getX() == 7)
    {
      turn(90);
      setHorzMirror(true);
    }
    if (getX() == 0)
    {
      turn(270);
      setHorzMirror(false);
    }
    tryToEat();
  }

  private void tryToEat()
  {
    Actor actor = gameGrid.getOneActorAt(getLocation(), Pill.class);
    if (actor != null)
    {
      show(1);
      actor.hide();
    }
  }
}

class Pill extends Actor
{

  public Pill()
  {
    super("pill");
  }
}

Explanations to the program code:
tryToEat() Tests if there is a pill at the current position and eliminates it. Thereby, the pacman is displayed with an open mouth
getOneActorAt(getLocation(), Pillclass) Gives back an actor or zero
if (actor != null) This condition is met when the there is a pill in the cell
actor.hide() The actor becomes invisible, but stays in the grid and can be reactivated with reset()
actor.removeSelf() Can be used instead of hide(). The actor is eliminated from the grid.

 

 

Example 2: Evade. Nemo has to evade the moving crab

Nemo swims back and forth horizontally, the crab appears at random x-coodinates and moves back and forth vertically. So that several collisions can be observed, the crab stays on the y-coordinate during 5 simulation periods. If there is a collision, the fish turns around and swims in the opposite direction.

It is tested with getOneActorAt(getNextMoveLocation(), Crab.class) if there is a crab at the next position to which the fish intends to move to. It's important that the crab moves first and only then the fish checks whether he is allowed to move. Therefore, the
act() of the crab has to be run before the act() of the fish. The order of selection of the act methods is set so that later added actors run their act() before the others. Therefore, addActor() with the crab has to be selected later on (the ac order can be changed with setActOrder()).

 

Edit this example in the Online Editor

App installieren auf Smartphone oder Tablet

QR-Code

Download sources (AndroidEx11.zip)

 

// AndroidEx11.java

package app.ex11;

import ch.aplu.android.*;

public class AndroidEx11 extends GameGrid
{
  public AndroidEx11()
  {
    super(8, 8, 40, RED, "reef"false);
  }

  public void main()
  {
    Crab crab = new Crab();
    addActor(crab, new Location(4, 6));
    addActor(new Fish(), new Location(0, 3));
    crab.setDirection(Location.NORTH);
    doRun();
  }
}

class Fish extends Actor
{
  public Fish()
  {
    super("nemo");
  }

  public void act()
  {
    Actor actor = gameGrid.getOneActorAt(getNextMoveLocation(), Crab.class);
    if (actor != null)
    {
      turn(180);
      setHorzMirror(!isHorzMirror());
    }
    move();
    if (isNearBorder())
    {
      turn(180);
      setHorzMirror(!isHorzMirror());
    }
  }
}

class Crab extends Actor
{
  public Crab()
  {
    super("crab");
  }

  public void act()
  {
    if (getY() == || getY() == 7)
      turn(180);
    move();
  }
}

Explanations to the program code:
getOneActorAt(nextMoveLocation, Crab.class) If there is an object of the class crab on the next position, this method gives back an actor, otherwise zero