In my game, I need a scoring system but I'm not entirely sure how to create one. Could somebody please give me some advice. Thanks.
public static final RED = 0, BLUE = 1; // continue for more players
private int[] scores = new int[2]; // size to number of players
public int getScore(int player)
{
return scores[player);
}
public void addScore(int player, int amount)
{
scores[player] += amount;
}
public int[] getScores()
{
return scores;
}// get a single score int redScore = ((MyWorld)getWorld()).getScore(MyWorld.RED); // add to a score ((MyWorld)getWorld()).addScore(MyWorld.BLUE, 10); // or get both scores int finalScores = ((MyWorld)getWorld()).getScores();
private int score = 0;
public int time = 0;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1100, 600, 1);
GreenfootImage back = getBackground();
back.setColor(Color.BLACK);
back.fill();
prepare();
showScore();
score++;
}
public void act()
{
countTime();
}
public void countTime()
{
time++;
}
public void addScore(int points)
{
score = score + points;
showScore();
}
public void showScore()
{
showText("Ship 1: " + score, 75, 25);
}public void Stuff()
{
boolean kill = true;
if(kill)
if (isTouching(UFO.class))
{
kill = false;
removeTouching(UFO.class);
MyWorld world = (MyWorld)getWorld();
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
Greenfoot.playSound("Explosion copy.wav");
world.addScore(50);
}
if(kill)
if (isTouching(Arwing.class))
{
kill = false;
removeTouching(Arwing.class);
MyWorld world = (MyWorld)getWorld();
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
Greenfoot.playSound("Explosion copy.wav");
world.addScore(-50);
}
if(kill)
if (isTouching(UFO0.class))
{
kill = false;
removeTouching(UFO0.class);
MyWorld world = (MyWorld)getWorld();
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
Greenfoot.playSound("Explosion copy.wav");
world.addScore(50);
}
if(kill)
if (isTouching(Mario.class))
{
kill = false;
removeTouching(Mario.class);
MyWorld world = (MyWorld)getWorld();
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
world.addScore(200);
Greenfoot.playSound("Mario 64 Falling Death Scream (320 kbps).mp3");
}
if(kill)
if (isTouching(Mario2.class))
{
kill = false;
removeTouching(Mario2.class);
MyWorld world = (MyWorld)getWorld();
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
world.addScore(200);
Greenfoot.playSound("Mario 64 Falling Death Scream (320 kbps).mp3");
}
if(kill)
if (isTouching(Arwing1.class))
{
kill = false;
removeTouching(Arwing1.class);
MyWorld world = (MyWorld)getWorld();
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
Greenfoot.playSound("Explosion copy.wav");
world.addScore(-50);
}
}private int score;
public int getScore()
{
return score;
}
public void addScore(int amt)
{
score += amt;
}// getting score int score = ((MyWorld)getWorld()).getScore(); // changing score ((MyWorld)getWorld()).addScore(10);
public Bullet()
{
GreenfootImage g = new GreenfootImage("beeper.png");
g.scale(50, 50);
setImage(g);
}
public void act()
{
int score = ((MyGame)getWorld()).getScore();
Actor t;
t = getOneObjectAtOffset(0, 0, Igloo.class);
setLocation(getX() + 50, getY());
if(t!=null)
{
((MyGame)getWorld()).addScore(1);
World w = getWorld();
w.removeObject(t);
getWorld().removeObject(this);
}
else if(isAtEdge())
{
getWorld().removeObject(this);
}
}