Game-Apps für Smartphones und Tablets |
|
Bern University oh Teacher Education |
Wenn das Jumpy-Girl mal abstürzt, können Sie das Spiel mit einem Tap auf das Display neu starten.
|
![]() |
// JumpyGame.java package app.jumpy; import android.graphics.Color; import android.graphics.Point; import android.hardware.Sensor; import ch.aplu.android.*; public class JumpyGame extends GameGrid implements GGTouchListener { private GGStatusBar status; private Jumpy jumpy; private TextActor speedUpSign; private static final int LEVEL_UP_TIME = 300; public JumpyGame() { super(WHITE, windowZoom(700)); status = addStatusBar(30); } public void main() { setWakeLockEnabled(true); setSimulationPeriod(30); GGSensor sensor = new GGSensor(this, Sensor.TYPE_ACCELEROMETER); jumpy = new Jumpy(sensor, status); jumpy.addActorCollisionListener(jumpy); addActor(jumpy, new Location(300, 100)); for (int i = 0; i < 8; i++) { Pad pad = new Pad(); addActorNoRefresh(pad, new Location(i * 100 + 100, i * 100)); jumpy.addCollisionActor(pad); } for (int i = 0; i < 3; i++) { Coin c = new Coin(); addActorNoRefresh(c, getRandomLocation()); jumpy.addCollisionActor(c); } setActOrder(Jumpy.class, TextActor.class, Coin.class, Pad.class); status.setText("Tilt your device left/right to control the jumps direction."); doRun(); addTouchListener(this, GGTouch.click); setTouchEnabled(false); } public void act() { int cycles = getNbCycles(); if (cycles % LEVEL_UP_TIME == 0 && cycles != 0) Pad.speedUp(1); } public boolean touchEvent(GGTouch arg0) { doReset(); status.setText("Game reset"); setTouchEnabled(false); setActEnabled(true); doRun(); return true; } } // class Jumpy extends Actor implements GGActorCollisionListener { private static final float FACTOR = 0.05f; private static final float JUMP_VELOCITY = -.7f / FACTOR; private float x; private float y; private float vx; private float vy; private float ax; private float ay = 9.81f * FACTOR; private GGSensor sensor; private int score; private GGStatusBar status; public Jumpy(GGSensor sensor, GGStatusBar status) { super("sara", 4); this.sensor = sensor; this.status = status; } public void act() { float[] values = GGSensor.toDeviceRotation(gameGrid, sensor.getValues(), Sensor.TYPE_ACCELEROMETER); vx = -values[0] * FACTOR * 30; x = x + vx; y = y + vy; vx = vx + ax; vy = vy + ay; int activeSprite = Math.max(Math.min(Math.round(-vy / 8f + 1.5f), 3), 0); show(activeSprite); setHorzMirror(vx < 0); updateLocation(); } private void updateLocation() { Location newLoc = new Location(Math.round(x), Math.round(y)); if (newLoc.y > gameGrid.getNbVertCells()) { status.setText("Final score: " + score + " -- Touch screen to play again"); gameGrid.refresh(); gameGrid.setActEnabled(false); gameGrid.setTouchEnabled(true); } else { setLocation(newLoc); } } public void jump() { vy = JUMP_VELOCITY; } public void reset() { x = getX(); y = getY(); score = 0; } public int collide(Actor jumpy, Actor colPartner) { if (colPartner.getClass() == Pad.class) { // only jump if jumpy was falling and not at the top of screen if (vy > 0 && y > 50) jump(); } else { Coin c = (Coin) (colPartner); if (c.isInGrid()) { score++; status.setText("Score: " + score); c.reset(); } } return 5; } } // class Pad extends Actor { private float x, y; private static float vy; //all pads have the same speed public Pad() { super("pad"); } public void act() { if (isInGrid()) { y += vy; } else { y = 0; x = (float) (Math.random() * (gameGrid.getNbHorzCells() - 200)) + 100; } setLocation(new Location(Math.round(x), Math.round(y))); } public static void speedUp(float amount) { vy += amount; } public void reset() { vy = 3; this.x = getX(); this.y = getY(); setCollisionLine(new Point(-50, -8), new Point(50, -8)); } } // class Coin extends Actor { public Coin() { super("coin"); } public void act() { if (getY() < gameGrid.getNbVertCells()) { setY(getY() + 1); } else { reset(); } } public void reset() { setCollisionCircle(new Point(0, 0), 14); setY((int) (-Math.random() * 300) - 50); int x = (int) (Math.random() * (gameGrid.getNbHorzCells() - 100)) + 50; setX(x); setActEnabled(true); } } |