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

2025/1/11

how do I make score show up on the game?

radu_edi radu_edi

2025/1/11

#
I'm making a zombie shooter game with my friend. Whenever you shoot a human your score goes up, and when you shoot a human your score goes down, but I can't get the score to show up, and I think all the other solutions I've found on the internet are outdated because whenever I try them they don't work.
danpost danpost

2025/1/12

#
radu_edi wrote...
I'm making a zombie shooter game with my friend. Whenever you shoot a human your score goes up, and when you shoot a human your score goes down, but I can't get the score to show up, and I think all the other solutions I've found on the internet are outdated because whenever I try them they don't work.
"they don't work" is very vague -- and there is no code given as far as what you tried.
radu_edi radu_edi

2025/1/14

#
I tried this, for instance. It's from the value display tutorial you made, but when I put it on the world, it displayed the Greenfoot logo.
public class Score  extends Actor
{
    // instance variables - replace the example below with your own
    public int score;
    
    private Actor unitsDisplay;
    private int units = 5;
    private int maxUnits = units;

    /**
     * Constructor for objects of class Score
     */
    public void updateImage(String text)
    {
        //Crosshair crosshair = (Crosshair) getWorld().getObjects(Crosshair.class).get(0);
        
        //GreenfootImage image = new GreenfootImage("Score:"+crosshair.score,24, Color.BLACK,new Color(0,0,0,0));
        //setImage(image);
        
        unitsDisplay = new Score();
        updateUnitsDisplay();
        getWorld().addObject(unitsDisplay,80,20);
        
    }
    
    public void adjustUnits(int amount) //changing number of uits
    {
        units += amount;
        if(units < 0){
            units=0;
        }
        
        if(units>maxUnits){
            units = maxUnits;
        }
    }
    
    public void updateUnitsDisplay()
    {
        //create image of unit
        GreenfootImage unitImage = new GreenfootImage("hertz.png");
        unitImage.scale(40,40); //optional
        //dimensions of unit image
        int wide = unitImage.getWidth();
        int high = unitImage.getHeight();
        //create actor image
        GreenfootImage image = new GreenfootImage(wide*maxUnits,high);
        for (int i=0; i>units; i++){
            image.drawImage(unitImage, i*wide,0);
        }
        unitsDisplay.setImage(image);
    }
}
(I haven't implemented how it changes yet, I wanted to see how it looks on the world, but as I said before, it just shows the Greenfoot logo) this is the code for the score changing:
public class Crosshair extends Actor
{
    public int score = 0;
    boolean isEnterDown = false;
    
    public void act()
    {
        if (Greenfoot.isKeyDown("enter")){
              if(!isEnterDown){
                  Crosshair crosshair = (Crosshair) getWorld().getObjects(Crosshair.class).get(0);
        
                    if (crosshair != null){
                        if (crosshair.touchingZombie1()){
                            score = score + 500;
                        }
                
                        if (crosshair.touchingHuman1()){
                            score = score - 1000;
                        }
                  }
              
                  isEnterDown = true;
              }
              else
              {
                  isEnterDown = false;
              }
        }

    public boolean touchingZombie1()
    {
        return isTouching(Zombie1.class);
    }
    
    public boolean touchingHuman1()
    {
        return isTouching(Human1.class);
    }
    }
}
what can I do?
danpost danpost

2025/1/15

#
radu_edi wrote...
I tried this, for instance. It's from the value display tutorial you made, but when I put it on the world, it displayed the Greenfoot logo. << Code Omitted >>
I guess you did not follow instructions very well. One big issue is in the Score class where you use "new Score()" to create a new Score object. It is (almost) never a good idea to create an object within its own class.
radu_edi radu_edi

2025/1/15

#
Sorry, this is my first project. What shiould I do, then?
danpost danpost

2025/1/15

#
radu_edi wrote...
Sorry, this is my first project. What shiould I do, then?
Well, first, let's clean up the Crosshair class:
import greenfoot.*;

public class Crosshair extends Actor
{
    boolean isEnterDown = false;
     
    public void act()
    {
        if (isEnterDown == Greenfoot.isKeyDown("enter")) return;
        isEnterDown = ! isEnterDown;
        if ( ! isEnterDown ) return;
        MyWorld myWorld = (MyWorld) getWorld();
        if (isTouching(Zombie1.class)) {
            removeTouching(Zombie1.class);
            myWorld.adjustScore(500);
        }
        if (isTouching(Human1.class)) {
            removeTouching(Human1.class);
            myWorld.adjustScore(-1000);
        }
    }
}
Now, add the following (entire) class if you do not already have it:
public class Aktor extends greenfoot.Actor {}
Finally, put an int score field along with and adjustScore method in your MyWorld class. The class can create the object (in the constructor) from the Aktor class and retain that object for displaying the score.
radu_edi radu_edi

2025/1/15

#
danpost wrote...
The class can create the object (in the constructor) from the Aktor class and retain that object for displaying the score.
This is a bit confusing for me. What do I do exactly after I create the object?
danpost danpost

2025/1/15

#
radu_edi wrote...
This is a bit confusing for me. What do I do exactly after I create the object?
It is all spelled out in the tutorial:
/**   in class extending World   */
private Actor scoreObj;
private int score;

public MyWorld() {
    super(600, 400, 1);
    // some stuff (maybe)
    scoreObj = new Aktor();
    updateScoreDisplay();
    addObject(scoreObj, 80, 20);
    // some more stuff (maybe)
}

private void updateScoreDisplay() {
    Color trans = new Color(0, 0, 0, 0);
    Color black = Color.BLACK;
    String text = "Score:  "+score;
    GreenfootImage img = new GreenfootImage(text, 32, black, trans);
    scoreObj.setImage(img);
}

public void adjustScore(int adjustment) {
    score += adjustment;
    if (score <= 0) {
        score = 0;
        // maybe end game here as player loses
    }
    // if score > someLimit ... (maybe)
    updateScoreDisplay();
}
radu_edi radu_edi

2025/1/17

#
Oh, thanks a lot; it finally worked! There's a small issue though: when the crosshair is over multiple zombies or humans, the score increases as if there were only one. How do I make the crosshair see how many zombies or humans it's touching it's touching when the shot is fired?
danpost danpost

2025/1/17

#
radu_edi wrote...
when the crosshair is over multiple zombies or humans, the score increases as if there were only one. How do I make the crosshair see how many zombies or humans it's touching it's touching when the shot is fired?
I guess you will have to use something other than isTouching(Class) in the Crosshair act method. Maybe use getIntersectingObjects(Class) instead. Get them (returned as an List object), size it up to adjust score (using the size() method on the list) and remove the listed actors from world using getWorld().removeObjects(List<Actor>).
You need to login to post a reply.