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

2019/5/15

Hi, I need help creating a score counter for my game and I don't know what to do

Josh3560 Josh3560

2019/5/15

#
public class score extends Actor { /** * Act - do whatever the score wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ int score = 0; public void act() { setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK)); } public void addScore() { score++; } }
danpost danpost

2019/5/15

#
Please show code where you create a score object (where "new score()" is).
Josh3560 Josh3560

2019/5/16

#
sorry that I didn't reply yesterday I'm not sure if this is what you are talking about import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class score here. * * @author (your name) * @version (a version number or a date) */ public class score extends Actor { /** * Act - do whatever the score wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ int score = 0; public void act() { setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK)); } public void addScore() { score++; } }
Josh3560 Josh3560

2019/5/16

#
would it help for me to explain what my game is and show the code for all of my objects and classes?
danpost danpost

2019/5/16

#
Josh3560 wrote...
I'm not sure if this is what you are talking about << Code Omitted >>
No -- I am probably talking about your World subclass.
Josh3560 wrote...
would it help for me to explain what my game is and show the code for all of my objects and classes?
I would most probably get a good idea about the game from the forthcoming code. Btw, please put code tags around your code or use the code link below the reply box.
Josh3560 Josh3560

2019/5/16

#
Josh3560 Josh3560

2019/5/16

#
when I try to use code tags, the text box posts blank
Josh3560 Josh3560

2019/5/16

#
[code]
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private int Counter = 0;
    private int SCROLL_SPEED = -3;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
    Counter counter = new Counter();
    public Counter getCounter()
    { 
        return counter; 
    }

    public void addPizza()
    {
        int speed = Greenfoot.getRandomNumber(3) + 1;               // random speed
        int y = Greenfoot.getRandomNumber(getHeight() - 50) + 25 ;  // random y location
        int direction = Greenfoot.getRandomNumber(80);               // random direction
        if (direction == 2)
        {
            Pizza pizza = new Pizza(-speed);        // make speed negative
            pizza.getImage().mirrorHorizontally();
            addObject(pizza, getWidth(), y);      // add fish to right edge
        }
    }

    public void addRocket()
    {
        int speed = Greenfoot.getRandomNumber(3) + 1;               // random speed
        int y = Greenfoot.getRandomNumber(getHeight() - 50) + 25 ;  // random y location

        int direction = Greenfoot.getRandomNumber(80);               // random direction
        if (direction == 2)  
        {
            Rocket rocket = new Rocket(-speed);        // make speed negative
            rocket.getImage().mirrorHorizontally();
            addObject(rocket, getWidth(), y);      // add fish to right edge
        }
    }

    public void act()
    {
        scrollBackground();
        addPizza();
        addRocket();
    }

    public void scrollBackground()
    {
        GreenfootImage copy = new GreenfootImage(getBackground());
        getBackground().drawImage(copy, SCROLL_SPEED, 0);
        getBackground().drawImage(copy, getBackground().getWidth()+ SCROLL_SPEED, 0);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        Dolphin dolphin = new Dolphin();
        addObject(dolphin,56,196);
    }
}
[code]
Josh3560 Josh3560

2019/5/16

#
nevermind, I got it
danpost danpost

2019/5/16

#
I am a bit taken aback. You have shown a score class which does not appear to have anything to do with your game. Now, explain that.
Josh3560 Josh3560

2019/5/16

#
this is my Myworld class
Josh3560 Josh3560

2019/5/16

#
do you want me to show you my other classes?
Josh3560 Josh3560

2019/5/16

#
actually, my school period is about to end so I have to go
Josh3560 Josh3560

2019/5/17

#
Hello, sorry, this is the only time I'm able to access my project.
danpost danpost

2019/5/17

#
Josh3560 wrote...
Hello, sorry, this is the only time I'm able to access my project.
I wrote:
You have shown a score class which does not appear to have anything to do with your game. Now, explain that.
You need to login to post a reply.