Game apps for smartphones and tablets |
|
Research project PHBern |
Example 1: The fish should swim back and forth and thereby always look ahead. If the fish is in a border cell, he has to turn his direction of movement by 180° and thereby mirror his sprite image. With the method setHorzMirror(), an image can be mirrored. Since we have to distinguish in this example if the fish is top right or top left, we use method getX() in order to check the border cells. This indicates the x coordinate of the current cell. |
![]() |
![]() |
Edit this example in the Online Editor
App installieren auf Smartphone oder Tablet
Download sources (AndroidEx2.zip)
// AndroidEx2.java package app.ex2; import ch.aplu.android.*; public class AndroidEx2 extends GameGrid { public AndroidEx2() { super(8, 8, 0, RED); } public void main() { Fish nemo = new Fish(); addActor(nemo, new Location(1, 1)); doRun(); } } class Fish extends Actor { public Fish() { super("nemo"); } public void act() { move(); if (getX() == 7) { turn(180); setHorzMirror(true); } if (getX() == 0) { turn(180); setHorzMirror(false); } } } |
Explanations to the program code:
getX() | Gives back the current cell coordinate of the actor |
setHorzMirror(true) |
Mirrors the sprite image horizontally |
setHorzMirror(false) |
The sprite image is not mirrored (default) |
Example 2: Pass through the cells of the grid/ use background image
|
![]() |
// AndroidEx3.java package app.ex3; import ch.aplu.android.*; public class AndroidEx3 extends GameGrid { public AndroidEx3() { super(8, 8, cellZoom(60), RED, "reef", false); } public void main() { Fish nemo = new Fish(); addActor(nemo, new Location(0, 0)); doRun(); } } class Fish extends Actor { public Fish() { super("nemo"); } public void act() { move(); if (getX() == 7) { turn(90); setHorzMirror(true); } if (getX() == 0) { turn(270); setHorzMirror(false); } } } |
super(8, 8, 40, Color.RED, "reef", true) | As background image, we use reef.gif. Since the image has to the size of 320x320 pixel, it is better to use a fix cell size. With bigger displays (e.g. with tablets), the whole game window, including the background image is enlarged. |
Example 3: Move figure on an arc/ Rotatable sprites
So that the car moves on an arc, there has to be a rotation by a small angle with every step (turn(2.5)). In order to turn the sprite image before every step into the moving direction, there is entered the parameter true when the car is initialized. super(true,"redcar") The parameter true causes the direction of the sprites to be calculated and updated continuously. The speed of the car can be changed by using the volume control of the phone.
| ![]() |
// AndroidEx4.java package app.ex4; import ch.aplu.android.*; public class AndroidEx4 extends GameGrid { public AndroidEx4() { super(300, 300, 1, 0, true); } public void main() { RedCar car = new RedCar(); addActor(car, new Location(150, 20)); doRun(); } } class RedCar extends Actor { public RedCar() { super(true,"redcar"); } public void act() { move(); turn(2.5); } } |
Explanations to the program code:
super(300, 300, 1, 0, true) | Pixel resolution without grid with activated navigation |
super(true,"redcar") | Rotatable sprite |