I am making a platform based game, but I don't know how to do a live counter. Can anyone help me?
//instance variables private int timer = 6000; //running at 60 fps that should be 100 seconds ... //constructor, other methods ... public void act() { if (timer > 0) timer--; //Counts down else gameOver(); //calls a game over method when it's 0 or less ... } ...
// using this instance field in the world class (aka WorldName) private Counter livesCounter = new Counter("Lives", 3); // and this method in the world class public Counter getLivesCounter() { return livesCounter; } // in your player class, when life lost Counter lives = ((WorldName)getWorld()).getLivesCounter(); lives.add(-1); if (lives.getValue() == 0) gameOver();
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; //Imports Color Class from the java library. /** * Write a description of class Poppetje here. * * @author Harmen * @version (a version number or a date) */ public class Poppetje extends Actor { /** * Act - do whatever the Poppetje wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("down")) { setLocation(getX(), getY()+5); } if (Greenfoot.isKeyDown("up")) { setLocation(getX(), getY()-5); } if (Greenfoot.isKeyDown("left")) { setLocation(getX()-5, getY()); } if (Greenfoot.isKeyDown("right")) { setLocation(getX()+5, getY()); } } public void wonGame() { if ( getY() < 15 ) { getWorld().removeObjects(getWorld().getObjects(Car1.class)); getWorld().removeObjects(getWorld().getObjects(Car2.class)); getWorld().removeObjects(getWorld().getObjects(Leraar.class)); getWorld().removeObjects(getWorld().getObjects(Leraar2.class)); GreenfootImage bg = getWorld().getBackground(); bg.setColor(Color.blue); bg.drawString("u heeft gewonnen!", 300, 400); Greenfoot.stop(); } } public void eat() { Actor Car1; Car1 = getOneObjectAtOffset(0, 0,Car1.class); if (Car1 != null) { World world; world = getWorld(); world.removeObject(Car1); } } if(getX() == 17 && getY()==15) { ((background)getWorld()).board(); Greenfoot.stop(); } else if(getX() == 18 && getY()==15) { ((background)getWorld()).board(); Greenfoot.stop(); } }