I am making a platform based game, but I don't know how to do a live counter. Can anyone help me?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //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 ... } ... |
1 2 3 4 5 6 7 8 9 10 11 | // 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(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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(); } } |