This site requires JavaScript, please enable it in your browser!
Greenfoot back
HelpAccount2
HelpAccount2 wrote ...

2026/1/15

I am trying to make a kill counter but it resets every time a new bullet spawns.

HelpAccount2 HelpAccount2

2026/1/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import greenfoot.World;
/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Plane
{
    private int count;
    private GreenfootImage image1;
    private GreenfootImage image2;

    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    @Override
    protected void addedToWorld(World world) {
        //summons the bullet so it point where the plane is pointing

        List<PlayerPlane> planesInWorld = world.getObjects(PlayerPlane.class);

        if (!planesInWorld.isEmpty()) {
            PlayerPlane targetPlane = planesInWorld.get(0);

            int planeAngle = targetPlane.getRotation();
            this.setRotation(planeAngle);

        }
    }

    public Bullet(){
        //initialization
        count = 0;
        image1 = new GreenfootImage("BulletImage.png");
        image2= new GreenfootImage("Explosion.png");
        setImage(image1);
        killsNeeded=4;

    }

    public void act() {
        move(20);
        //displays the kills needed to win
        getWorld().showText("You need " +killsNeeded+ " kills to win",165, 175);
        //if the bullet touches the edge the bullet will get removed
        if (isAtEdge()) {
            getWorld().removeObject(this); 

            return; 
        }
        lookForEnemy();
        win();
        ImageChecker();
    }
    //if the bullet touches the enemy it removes the enemy, plays a sound, and sets the image to an explosion.
    public void lookForEnemy(){
        if(getImage() == image1){
            if(isTouching(EnemyPlane.class)){

                removeTouching(EnemyPlane.class);
                setImage(image2);
                Greenfoot.playSound("explosion-sound-effect-made-with-Voicemod.mp3");
                killsNeeded --;

            }
        }
        else{

        }
    }
    //if the explosion image is on screen the bullet becomes harmless and is removed after 5 counts

    public void ImageChecker(){
        if(getImage() == image2){
            count++;
            if(count == 5){
                getWorld().removeObject(this);
            }
        }
    }
    //if kills needed is met the game stops and a win sound effect plays

    public void win(){
        if(killsNeeded == 0){
            Greenfoot.playSound("fanfare.wav");
            Greenfoot.stop();

        }
    }
}
that is the code for my bullet. this is the player code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import greenfoot.World;
/**
 * Write a description of class PlayerPlane here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerPlane extends Plane
{
    private Bullet myNewObject;

    private int counter;        

    /**
     * Act - do whatever the PlayerPlane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public PlayerPlane(){
        //initializes the bullets remaining
        counter = 0;
        BulletsRemaining = 40;
    }

    public void act()
    {
        //automatically moves the plane and displays the remaining bullets
        move(5);
        getWorld().showText("You have " +BulletsRemaining+ " remaining bullets",200, 150);
        checkKeyPress();
        FireBullet();
        getRotation();
    }

    public void checkKeyPress(){
        //turns up, down, and gives a speed boost
        if(Greenfoot.isKeyDown("w")){
            turn(-4);

        }
        if(Greenfoot.isKeyDown("s")){
            turn(4);

        }
        if(Greenfoot.isKeyDown("e")){
            move(10);
        }

    }

    public void FireBullet(){
        //fires a bullet if you have more than 1 bullet remaining. 
        //Sets a delay for the bullets and only spawns one every 10 counts. 
        //If you have no bulelts remaining it will automatically stop shooting.

        if(BulletsRemaining >=1){

            if(Greenfoot.isKeyDown("space")){
                counter +=1;
                if(counter == 10){

                    Bullet bullet = new Bullet();
                    getWorld().addObject(bullet, getX(), getY());
                    counter = 0;
                    BulletsRemaining -=1;
                }
            }

        }
        else{
            getWorld().showText("You ran out of bullets!",500, 500);
            getWorld().showText("Game Over!",500, 525);
            Greenfoot.stop();
        }
    }

}
this is the enemy plane code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class EnemyPlane here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class EnemyPlane extends Plane
{
    private GreenfootImage image1;
    private boolean touching;

    /**
     * Act - do whatever the EnemyPlane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public EnemyPlane(){
        image1= new GreenfootImage("EnemyPlane.jpeg");

        setImage(image1);

    }

    public void act()
    { 
        //turns at edge and randomly turns
        if(isAtEdge()){
            turn(Greenfoot.getRandomNumber(180) +90);
        }
        if(Greenfoot.getRandomNumber(100) ==1){
            turn(Greenfoot.getRandomNumber(90)-45);

        }
        move(10);

    }
}
Here is the world code just in case:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class WorldP here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class WorldP extends World
{

    /**
     * Constructor for objects of class WorldP.
     * 
     */
    public WorldP()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 1000, 1); 
        PlayerPlane myP = new PlayerPlane();
        addObject(myP, 0, Greenfoot.getRandomNumber(1000));
        for(int m =0; m<10; m++){
            addObject(new EnemyPlane(), Greenfoot.getRandomNumber(1000)+250, Greenfoot.getRandomNumber(1000));
        }

    }}
danpost danpost

2026/1/16

#
It would help if you documented the classes with comments to indicate what each code set does. Also, it is hard to tell which counter you are referring to. The Plane class codes might be needed as well, since some variables seem to be defined there.
You need to login to post a reply.