I wanted to make a life counter for my game. Would it be the same as making a score counter you think but instead changing the text into Lives: instead of Score:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
private int time;
private int life = 2;
boolean startMusicc = true;
GreenfootSound backGrounddMusic = new GreenfootSound("Sound2.mp3");
LifeCounter count = new LifeCounter();
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(900, 537, 1);
prepare();
time = 1000;
Music();
}
public void Music()
{
}
public void act()
{
showTheTime();
if (Greenfoot.getRandomNumber(300) <3 )
{
addObject(new Enemy(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
}
if (Greenfoot.getRandomNumber(300) <3)
{
addObject(new Rocket(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
}
if (startMusicc)
{
backGrounddMusic.playLoop();
startMusicc= false;
}
}
public void showTime()
{
showText("Time: " + time, 50, 20);
}
public void showTheTime()
{
time--;
showTime();
if(time==0)
{
Greenfoot.setWorld(new Level2());
backGrounddMusic.stop();
}
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Man man = new Man();
addObject(man,702,241);
Minis mini = new Minis();
addObject(mini, 887, 424);
Minis minis2 = new Minis();
addObject(minis2,884,111);
addObject(count, 798, 20);
}
public LifeCounter getCounter()
{
return count;
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Man here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Man extends Actor
{
public Man()
{
GreenfootImage myImage = getImage();
int myNewHeight = (int)myImage.getHeight()/3;
int myNewWidth = (int)myImage.getWidth()/3;
myImage.scale(myNewWidth, myNewHeight);
}
public void act()
{
checkHit();
checkKeys();
}
private void checkKeys()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-8);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+8);
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+4, getY());
}
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-4, getY());
}
if ("space".equals(Greenfoot.getKey()))
{
shoot();
}
}
private void shoot()
{
Ammo ammo = new Ammo();
getWorld().addObject(ammo, getX(), getY());
}
public void checkHit()
{
Actor a = this.getOneIntersectingObject(Rocket.class);
if (a != null)
{
this.getWorld().removeObject(a);
World myWorld = getWorld();
MyWorld world = (MyWorld)(myWorld);
LifeCounter count = world.getCounter();
count.countLives();
}
Actor b= this.getOneIntersectingObject(Enemy.class);
if (b != null)
{
this.getWorld().removeObject(b);
World myWorld = getWorld();
MyWorld world = (MyWorld)(myWorld);
LifeCounter count = world.getCounter();
count.countLives();
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class LifeCounter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class LifeCounter extends Actor
{
int lives = 2;
boolean startMusic = true;
//GreenfootSound backGrounddMusic = new GreenfootSound("Sound2.mp3");
public void act()
{
setImage(new GreenfootImage("Lives: " + lives, 24, Color.GREEN, Color.BLACK));
gameOver();
//Music();
}
public void countLives()
{
lives--;
}