I have an ammo counter for my zombie apocalypse game, but the image of the ammo counter won't update everytime i lose ammo. I double checked my codes but I can seem to find the problem. Can someone help me? :'(
Here is my Human Code:
and here is my ammo counter code:
Also, if you need my World Class code here it is:
import greenfoot.*;
import java.awt.Color;
/**
* Write a description of class Human here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Human extends Actor
{
/**
* Act - do whatever the Human wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
Zombie zombie = new Zombie();
boolean touchingzombie = false;
public Human()
{
}
public boolean Munition(){
Game game = (Game)getWorld();
AmmoCounter ammoc = game.getAmmoCounter();
if(ammoc.getAmmo()>0){
return true;
}
else{
return false;
}
}
public void act()
{
move();
reload();
TurnTowardsMouse();
bounceOffWorldEdge();
death();
collectAmmo();
CheckCollision();
}
public void move()
{
if(Greenfoot.isKeyDown("d")) setLocation(getX()+3, getY());
if(Greenfoot.isKeyDown("a")) setLocation(getX()-3, getY());
if(Greenfoot.isKeyDown("s")) setLocation(getX(), getY()+3);
if(Greenfoot.isKeyDown("w")) setLocation(getX(), getY()-3);
}
public void CheckCollision()
{
Actor crate = getOneIntersectingObject(Crate.class);
if (crate != null)
{
turnTowardsCrate();
move(-5);
}
}
public void turnTowardsCrate()
{
int dist = 1000;
Actor closest = null;
if(!getObjectsInRange(dist, Crate.class).isEmpty())
{
for (Object obj: getObjectsInRange(dist, Crate.class))
{
Actor guy = (Actor) obj;
int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY());
if (closest == null || guyDist< dist)
{
closest = guy;
dist = guyDist;
}
}
turnTowards(closest.getX(),closest.getY());
}
}
public void death()
{
Actor zombie = getOneIntersectingObject(Zombie.class);
if(zombie != null)
{
if(touchingzombie == false)
{
Game game = (Game)getWorld();
Bar bar = game.getBar();
Counter counter = game.getCounter();
bar.subtract(10);
touchingzombie = true;
if(bar.value<=0)
{
game.removeObject(this);
}
}
} else {
touchingzombie = false;
}
}
public void shoot()
{
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse == null) return;
int x = mouse.getX();
int y = mouse.getY();
Bullet bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY());
bullet.turnTowards(x,y);
}
public void reload()
{
Game game = (Game)getWorld();
AmmoCounter ammoc = game.getAmmoCounter();
if (Munition() == true)
{
if(Greenfoot.mouseClicked(null))
{
shoot();
ammoc.subtract();
}
else
{
}
}
}
public void TurnTowardsMouse()
{
// the following two statements were added to the code
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse == null) return;
int x = mouse.getX();
int y = mouse.getY();
turnTowards( x, y);
}
public void bounceOffWorldEdge() {
if (getX() < 20) {
if (getRotation() < 180) {
setRotation(180-getRotation());
}
else if (getRotation() >= 180) {
setRotation((180-getRotation()));
}
if (getX() < 20) {
for (int i = 0; i < 5 && getX() < 20; i++) {
setLocation(getX()+5, getY());
}
}
}
else if (getX() > getWorld().getWidth()-20) {
if (getRotation() <= 90) {
setRotation(180-getRotation());
}
else if (getRotation() > 270) {
setRotation(180+(360-getRotation()));
}
if (getX() > getWorld().getWidth()-20) {
for (int i = 0; i < 5 && getX() > getWorld().getWidth()-20; i++) {
setLocation(getX()-5, getY());
}
}
}
else if (getY() < 20) {
if (getRotation() <= 270) {
setRotation(90+(270-getRotation()));
}
else if (getRotation() > 270) {
setRotation(90-(getRotation()-270));
}
if (getY() < 20) {
for (int i = 0; i < 5 && getY() < 20; i++) {
setLocation(getX(), getY()+5);
}
}
}
else if (getY() > getWorld().getHeight() - 20) {
if (getRotation() <= 90) {
setRotation(270+(90-getRotation()));
}
else if (getRotation() > 90) {
setRotation(270-(getRotation()-90));
}
if (getY() > getWorld().getHeight() - 20) {
for (int i = 0; i < 5 && getY() > getWorld().getHeight() - 20; i++) {
setLocation(getX(), getY()-5);
}
}
}
}
public void AmmoUp()
{
Game game = (Game)getWorld();
AmmoCounter ammoc = game.getAmmoCounter();
ammoc.add(20);
}
public void collectAmmo()
{
Actor ammunition = getOneObjectAtOffset(0, 0, Ammo.class);
if(ammunition !=null){
getWorld().removeObject(ammunition);
AmmoUp();
}
else
{
return;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* A Counter class that allows you to display a numerical ammo on screen.
*
* The Counter is an actor, so you will need to create it, and then add it to
* the world in Greenfoot. If you keep a reference to the Counter then you
* can adjust its ammo. Here's an example of a world class that
* displays a counter with the number of act cycles that have occurred:
*
* <pre>
* class CountingWorld
* {
* private Counter actCounter;
*
* public CountingWorld()
* {
* super(600, 400, 1);
* actCounter = new Counter("Act Cycles: ");
* addObject(actCounter, 100, 100);
* }
*
* public void act()
* {
* actCounter.setAmmo(actCounter.getAmmo() + 1);
* }
* }
* </pre>
*
* @author Neil Brown and Michael Kölling
* @version 1.0
*/
public class AmmoCounter extends Actor
{
int ammo = 20;
public AmmoCounter()
{
updateImage();
}
/**
* Create a new counter, initialised to 0.
*/
public void act()
{
updateImage();
}
public void add(int value)
{
ammo+=value;
}
public void subtract()
{
ammo--;
}
public int getAmmo()
{
return ammo;
}
public void setAmmo(int newAmmo)
{
ammo = newAmmo;
updateImage();
}
private void updateImage()
{
setImage(new GreenfootImage("Ammo : " + ammo, 18, Color.BLACK, new Color(0, 0, 0, 0)));
}
}
import greenfoot.*;
/**
* Write a description of class floor here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Game extends World
{
Counter counter = new Counter();
private int rate = 50, timer = 0;
public Bar bar = new Bar("", "HP", 25, 100);
Human human = new Human();
AmmoCounter ammocounter = new AmmoCounter();
/**
* Constructor for objects of class floor.
*
*/
public Game()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1, false);
prepare();
}
public Counter getCounter()
{
return counter;
}
public Bar getBar()
{
return bar;
}
public Human getHuman()
{
return human;
}
public AmmoCounter getAmmoCounter()
{
return ammocounter;
}
public void act()
{
spawnZombie2();
spawnZombie();
spawnAmmo();
detectHuman();
}
public void detectHuman()
{
if (getObjects(Human.class).isEmpty())
Greenfoot.setWorld(new GameOver(counter));
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
addObject(counter, 100, 40);
Human human = new Human();
addObject(human, 400, 292);
addObject(bar, 250, 40);
counter.setLocation(100, 49);
counter.setLocation(70, 32);
Crate crate = new Crate();
addObject(crate, 635, 517);
Crate crate2 = new Crate();
addObject(crate2, 641, 472);
crate2.setLocation(635, 466);
Crate crate3 = new Crate();
addObject(crate3, 691, 495);
crate3.setLocation(686, 490);
Crate crate4 = new Crate();
addObject(crate4, 269, 396);
Crate crate5 = new Crate();
crate5.setRotation(90);
addObject(crate5, 210, 123);
Crate crate6 = new Crate();
addObject(crate6, 268, 129);
Crate crate7 = new Crate();
addObject(crate7, 336, 134);
crate6.setLocation(262, 123);
crate7.setLocation(316, 122);
crate7.setRotation(90);
crate6.setRotation(90);
Crate crate8 = new Crate();
addObject(crate8, 95, 508);
Crate crate9 = new Crate();
addObject(crate9, 102, 567);
crate9.setRotation(90);
crate9.setLocation(95, 560);
Crate crate10 = new Crate();
addObject(crate10, 639, 92);
crate10.setRotation(90);
Crate crate11 = new Crate();
addObject(crate11, 697, 99);
crate11.setLocation(691, 93);
Crate crate12 = new Crate();
crate12.setRotation(90);
addObject(crate12, 701, 162);
crate12.setLocation(692, 146);
crate3.setLocation(734, 491);
crate3.setLocation(728, 491);
crate3.setLocation(528, 402);
crate10.setLocation(598, 151);
crate7.setLocation(273, 167);
crate6.setLocation(252, 89);
crate7.setLocation(309, 144);
crate8.setLocation(103, 392);
crate3.setLocation(631, 361);
crate10.setLocation(664, 209);
crate12.setLocation(635, 147);
crate11.setLocation(632, 91);
crate7.setLocation(220, 169);
crate6.setLocation(226, 68);
crate3.setLocation(564, 399);
crate5.setLocation(258, 212);
crate6.setLocation(222, 254);
crate4.setLocation(597, 341);
crate12.setLocation(440, 463);
crate3.setLocation(486, 503);
crate2.setLocation(568, 500);
crate.setLocation(690, 413);
crate9.setLocation(179, 448);
crate8.setLocation(114, 426);
crate7.setLocation(115, 118);
crate6.setLocation(244, 354);
crate8.setLocation(157, 165);
crate6.setLocation(177, 356);
crate4.setLocation(215, 405);
crate10.setLocation(727, 370);
crate11.setLocation(685, 331);
crate5.setLocation(609, 124);
crate2.setLocation(561, 83);
crate9.setLocation(180, 482);
crate6.setLocation(144, 303);
crate4.setLocation(186, 342);
crate6.setLocation(354, 212);
human.setLocation(386, 303);
crate.setLocation(573, 272);
crate10.setLocation(645, 372);
bar.setLocation(183, 32);
counter.setLocation(334, 53);
bar.setLocation(70, 27);
counter.setLocation(729, 23);
AmmoCounter ammocounter = new AmmoCounter();
addObject(ammocounter, 255, 44);
ammocounter.setLocation(255, 44);
ammocounter.setLocation(255, 44);
ammocounter.setLocation(255, 44);
ammocounter.setLocation(255, 44);
ammocounter.setLocation(255, 44);
ammocounter.setLocation(242, 51);
crate8.setLocation(157, 179);
crate7.setLocation(119, 137);
ammocounter.setLocation(73, 62);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(52, 58);
ammocounter.setLocation(51, 51);
}
public void spawnZombie()
{
if(Greenfoot.getRandomNumber(100) == 1)
addObject(new Zombie(), -10, Greenfoot.getRandomNumber(getHeight()));
if(Greenfoot.getRandomNumber(100) == 1)
addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), -10);
}
public void spawnZombie2()
{
if(Greenfoot.getRandomNumber(100) == 1)
addObject(new Zombie2(), -10, Greenfoot.getRandomNumber(getHeight()));
if(Greenfoot.getRandomNumber(100) == 1)
addObject(new Zombie2(), Greenfoot.getRandomNumber(getWidth()), -10);
}
public void spawnAmmo()
{
if (Greenfoot.getRandomNumber(100) == 1)
{
addObject(new Ammo(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
}
}
}

