Hi, i'm new in here. Help me to solve my program. I make my first game, I wanna add score counter in my game, but the counter doesn't work. I hope my score counter work when the bullet from survivor killing the zombie.
It's the code from the background world
It's the bullet code
and this is code from counter
I hope you can help me. thank you before
import greenfoot.*;
/**
* Write a description of class bg here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bg extends World
{
/**
* Constructor for objects of class bg.
*
*/
public bg()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
prepare();
}
private void prepare()
{
Counter counter = new Counter ();
addObject (counter, 626,586);
Bullet bullet = new Bullet(counter);
addObject (bullet, 103,535);
spawn spawn = new spawn();
addObject(spawn, 794, 18);
spawn2 spawn2 = new spawn2();
addObject(spawn2, 12, 15);
mansurvivor mansurvivor = new mansurvivor();
addObject(mansurvivor, 103, 535);
car car = new car();
addObject(car, 92,297);
brick brick = new brick();
addObject(brick, 764,575);
brick2 brick2 = new brick2();
addObject(brick2, 406,372);
brick3 brick3 = new brick3();
addObject(brick3, 200,106);
return;
}
}
import greenfoot.*;
/**
* Write a description of class bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullet extends players
{
private int points = 0;
private Counter counter;
public Bullet(Counter pointCounter)
{
counter = pointCounter;
}
public Bullet(int rotation)
{
setRotation(rotation);
}
/**
* Act - do whatever the bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(10);
killing();
remove();
}
public void killing ()
{
if(canSee(zombie.class))
{
kill(zombie.class);
counter.add(5);
}
if(canSee(zombie3.class))
{
kill(zombie3.class);
counter.add(20);
}
}
public void remove()
{
setLocation(getX(), getY());
if(getY()>595)
{
getWorld().removeObject(this);
if (getWorld() == null) return;
}
setLocation(getX(), getY());
if(getY()<5)
{
getWorld().removeObject(this);
if (getWorld() == null) return;
}
setLocation(getX(), getY());
if(getX()<5)
{
getWorld().removeObject(this);
if (getWorld() == null) return;
}
setLocation(getX(), getY());
if(getX()>795)
{
getWorld().removeObject(this);
if (getWorld() == null) return;
}
Actor actor = getOneIntersectingObject(Walls.class);
if(actor != null)
{
getWorld().removeObject(this);
if (getWorld() == null) return;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* A simple counter with graphical representation as an actor on screen.
*
* @author mik
* @version 1.0
*/
public class Counter extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
/**
* Create a new counter, initialised to 0.
*/
public Counter()
{
background = getImage(); // get image from class
value = 0;
target = 0;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
if (value < target) {
value++;
updateImage();
}
else if (value > target) {
value--;
updateImage();
}
}
/**
* Add a new score to the current counter value.
*/
public void add(int score)
{
target += score;
}
/**
* Return the current counter value.
*/
public int getValue()
{
return value;
}
/**
* Set a new counter value.
*/
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
