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

2020/5/20

How to make a counter that counts every time my player collects a gem

neoaspect neoaspect

2020/5/20

#
I'm currently in the midst of completing a school project and I'm struggling with creating a counter. I want a counter that keeps track of the amount of gems my player collects a gem. I'm not quite sure where to place it or start it. I have already created a Counter class but I'm not sure where to go next. Some help would be great This is the code for my player
public class Karmen extends Characters
{
    private final int GRAVITY = 1;
    private int velocity;
    private int karmengemEaten;
    private int lives = 3;
    private int score;
    SimpleTimer shotTimer = new SimpleTimer();
    public Karmen(){
        velocity = 0;
    }

    public void act() 
    {
        if (Greenfoot.isKeyDown("D")) {
            move(4);
            setImage(right);
        }
        else if (Greenfoot.isKeyDown("A")) {
            move(-4);
            setImage(left);
        } 
        else if (Greenfoot.isKeyDown("shift") && shotTimer.millisElapsed() > 250) {
            getWorld().addObject(new KarmenBullet(), getX(), getY());
            shotTimer.mark();

        } 
        fall();
        if(Greenfoot.isKeyDown("W") && isOnSolidGround())jump();
        karmengemEaten();
        detectEndPortal();
        detectEndPortal2();
        detectBossBullet();
    }
    private GreenfootImage left = new GreenfootImage("p1 pt1.png");
    private GreenfootImage right= new GreenfootImage("p1 pt12.png");
    public void fall(){
        setLocation(getX(), getY() + velocity);
        if(isOnSolidGround())velocity = 0;
        else velocity += GRAVITY;
    }

    public void move(){
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("A")) x -=3;
        if(Greenfoot.isKeyDown("D")) x += 3;
        setLocation(x, y);
    }

    public void jump(){
        velocity = -15;
    }

    public boolean isOnSolidGround(){
        boolean isOnGround = false;

        if(getY() > getWorld().getHeight() - 75)isOnGround = true;

        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, Platform.class) !=null || 
        getOneObjectAtOffset(imageWidth * -2, imageHeight / -2, Platform.class) != null)
            isOnGround = true;
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, Platform2.class) !=null || 
        getOneObjectAtOffset(imageWidth / 2 , imageHeight / 2, Platform2.class) != null)
            isOnGround = true;
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, Platform_2.class) !=null || 
        getOneObjectAtOffset(imageWidth / 2 , imageHeight / 2, Platform_2.class) != null)
            isOnGround = true;
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, EndIsland.class) !=null || 
        getOneObjectAtOffset(imageWidth / 2 , imageHeight / 2, EndIsland.class) != null)
            isOnGround = true;
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, ThirdIsland.class) !=null || 
        getOneObjectAtOffset(imageWidth / 2 , imageHeight / 2, ThirdIsland.class) != null)
            isOnGround = true;
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, FloatPlate.class) !=null || 
        getOneObjectAtOffset(imageWidth / 2 , imageHeight / 2, FloatPlate.class) != null)
            isOnGround = true;
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, FloatPlate3.class) !=null || 
        getOneObjectAtOffset(imageWidth / 2 , imageHeight / 2, FloatPlate3.class) != null)
            isOnGround = true;
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, FloatPlate2.class) !=null || 
        getOneObjectAtOffset(imageWidth / 2 , imageHeight / 2, FloatPlate2.class) != null)
            isOnGround = true;
        if (getOneObjectAtOffset(imageWidth / -2 , imageHeight / 2, FloatPlate4.class) !=null || 
        getOneObjectAtOffset(imageWidth / 2 , imageHeight / 2, FloatPlate4.class) != null)
            isOnGround = true;
        return isOnGround;
    }

    public void karmengemEaten(){
        if (isTouching(KarmenGem.class)){
            Greenfoot.playSound("gem sound.mp3");
            removeTouching(KarmenGem.class);
            karmengemEaten++;
            increaseScore();

        }
    }

    public void increaseScore(){
        score++;
    }

    public void detectEndPortal(){
        if(isTouching(EndPortal.class)){
            Greenfoot.setWorld(new Level_2());
        }

    }

    public void detectEndPortal2(){
        if(isTouching(EndPortal2.class)){
            Greenfoot.setWorld(new Level_3());
        }

    }

    public void detectEnemy1Collision(){
        if  (isTouching(Enemy1.class)){
            removeLife();
        }
    }

    public void removeLife(){
        lives--;
        showStatus();
    }

    public void showStatus() {
        getWorld().showText("Lives : "+lives, 70,540);
        getWorld().showText("Score : "+score, 70,560);
    }

    public void detectBossBullet(){
        if(isTouching(BossBullet.class)){
            removeLife();
        }
    }
    
    

}
This is the code for the world
public class Level_1 extends World
{
    private int score = 0;
    /**
     * Constructor for objects of class Level_1.
     * 
     */
    public Level_1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 

        prepare();
    }

    private void prepare()
    {
        Level_1_Floor level_1_floor = new Level_1_Floor();
        addObject(level_1_floor, 399, 299);
        Karmen karmen = new Karmen();
        addObject(karmen, 100, 533);
        Yoichi yoichi = new Yoichi();
        addObject(yoichi, 140, 538);
        Platform platform = new Platform(150, 80);
        addObject(platform, 353, 469);
        Platform_2 platform_2 = new Platform_2();
        addObject(platform_2, 615, 379);
        addObject(new Platform (150, 80), 453, 278);
        KarmenGem karmengem = new KarmenGem();
        addObject(karmengem, 349, 423);
        EndIsland endisland = new EndIsland(250, 80);
        addObject(endisland, 155, 116);
        EndPortal endportal = new EndPortal(150, 80);
        addObject(endportal, 151, 51);
        ThirdIsland thirdisland = new ThirdIsland();
        addObject(thirdisland, 309, 199);
        YoichiGem yoichigem = new YoichiGem(60,40);
        addObject(yoichigem, 610, 335);
        Enemy1 enemy1 = new Enemy1();
        addObject(enemy1,  289, 329);
        addObject(new Enemy1(), 578, 169);
        Pause pause = new Pause();
        addObject(pause,764, 34);
        addObject(new Level1Music(), 758, 560);
    }

    public void act()
    {
        showText("Score = " + score, 410, 15);
    }
   

}
danpost danpost

2020/5/20

#
Your karmengemEaten field declared at line 5 in the Karmen class IS (apparently) the counter. What you want, I believe is an actor to display the counter value (or the state and its value -- like "Gems: 0"). A true Counter class will have the int field included within it (which is not what you show). What you show, however is okay. You just need a simple Actor object to display an image in your world that shows the state. Please refer to my Value Display Tutorial scenario for details.
You need to login to post a reply.