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

4 days ago

Score Counter Does Not Work

1
2
JaneSmith JaneSmith

4 days ago

#
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
{
    int pipeCounter = 0;
    int flappyCounter = 0;
    int SPACE_BETWEEN_PIPES = 0;
    int score = 0;
    int FIRST_PIPE = 240;
    int randomLoc;
   // int addVal=230;
    int counter =0;
    //int scoreV= 0;

    Score scoreObj = null;

    /**
     * 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, false);

        setPaintOrder(GameOver.class, Score.class, FlappyBird.class, TopPipe.class);

        FlappyBird flappy = new FlappyBird();
        addObject(flappy, 100, 200);

        scoreObj = new Score();
        scoreObj.setScore(0);

        addObject(scoreObj,70,355);
        
    }

    public void act()
    {
        pipeCounter++;

        if(pipeCounter % 100 == 0)
        {
            createPipes();
        }
        if(pipeCounter >= FIRST_PIPE)
        {
            if(flappyCounter % 100 == 0)
            {
                score++;
                scoreObj.setScore(score);        
            }
            flappyCounter++;
        }

    }
    
    //public int addVal =230;
    
    private void createPipes()
    {
        if(pipeCounter==100)
        {
            BottomPipe bottompipe = new BottomPipe();
            randomLoc = Greenfoot.getRandomNumber(200)-100;

            addObject(bottompipe, getWidth(), getHeight() + SPACE_BETWEEN_PIPES /2 + randomLoc);

            TopPipe toppipe = new TopPipe();
            addObject(toppipe, getWidth(), 0 - SPACE_BETWEEN_PIPES /2 + randomLoc);

            pipeCounter = 0;
        }

    }
}
___________________________________________

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
//import java.awt.Color; 
 import greenfoot.Color;
 //import java.awt.Font;
 import greenfoot.Font;

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    public Score()
    {
        GreenfootImage newImage = new GreenfootImage(100, 50);
        setImage(newImage);
    }

    public void setScore(int score)
    {
        GreenfootImage newImage =getImage();
        newImage.clear();
        
        Font f = new Font("Comic sans MS", 32);
        newImage.setFont(f);
        
        Color c = new Color(127,127,127,127);
        newImage.setColor(c);
        newImage.fill();
        newImage.setColor(Color.BLACK);
        newImage.drawString("" + score, 30, 30);
        setImage(newImage);

    }
}
The score does not increase when the flappy bird goes through the pipes. The first set of code is for the world class the second is for the actor class "Score." Any thoughts on this problem would be greatly appreciated.
danpost danpost

4 days ago

#
JaneSmith wrote...
<< Code Omitted >> The score does not increase when the flappy bird goes through the pipes. The first set of code is for the world class the second is for the actor class "Score." Any thoughts on this problem would be greatly appreciated.
The Score class seems to be (or ... is) functional and the Score object is referenced properly. So, the problem must be in the logic of your act method.
JaneSmith JaneSmith

4 days ago

#
Do you mean the act method in the World class or the Actor class?
danpost danpost

4 days ago

#
JaneSmith wrote...
Do you mean the act method in the World class or the Actor class?
You only show one act method above. Alone, line 56 and 57 would show the Score object working.
JaneSmith JaneSmith

4 days ago

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

/**
 * Write a description of class FlappyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FlappyWorld extends World
{
    int pipeCounter = 0;
    int flappyCounter = 0;
    int PIPE_SPACING = 150;
    int score = 0;
    int FIRST_PIPE = 240;
    Score scoreObj = null;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public FlappyWorld()
    {         

        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false); 

        //Set paint order
        //setPaintOrder(Pipe.class, FlappyBird.class, GameOver.class);

        //Create a Flappy Bird Object
        FlappyBird flappy = new FlappyBird();
        addObject(flappy, 100, getHeight()/2); 

        //Create a Score Object
        scoreObj = new Score();
        scoreObj.setScore(0);

        //Add it to our world
        addObject(scoreObj, 70, 355); 

    }

    public void act()
    {
        pipeCounter++;

        if (pipeCounter %  100 == 0)
        {
            createPipes();
        }

        if (pipeCounter >= FIRST_PIPE)
        {
            if (flappyCounter % 100 == 0)
            {
                score++;
                scoreObj.setScore(score);
            }

            flappyCounter++;

        }

    }

    private void createPipes() {
        // Bottom Pipe
        BottomPipe botPipe = new BottomPipe();
        int offset = Greenfoot.getRandomNumber(getHeight()*2/3);
        addObject(botPipe, getWidth() + botPipe.getImage().getWidth()/2, getHeight() + botPipe.getImage().getHeight()/2 /*- 50*/ - offset);

        // Top Pipe
        TopPipe topPipe = new TopPipe();
        addObject(topPipe, botPipe.getX(), botPipe.getY() - topPipe.getImage().getHeight() - PIPE_SPACING);
    }

}
________________________________

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    public Score()
    {
        
        GreenfootImage newImage = new GreenfootImage (100, 50);
        setImage(newImage);
    }
    
    public void setScore(int score)
    {
        GreenfootImage newImage = getImage();
        newImage.clear();
        newImage.drawString("" + score, 30, 30);
        setImage(newImage);
    }
    
       
}
This code works just fine for this game and the act method is exactly the same.
JaneSmith JaneSmith

4 days ago

#
I see the score in the original game it starts out as 0 but never increases.
danpost danpost

4 days ago

#
JaneSmith wrote...
I see the score in the original game it starts out as 0 but never increases.
Are you saying you do not see the Score object at all in your game?
JaneSmith JaneSmith

4 days ago

#
The second game works just fine but it is the first game where you see the Score object but it does not increase when the Flappy Bird goes between the pipes.
danpost danpost

4 days ago

#
JaneSmith wrote...
The second game works just fine but it is the first game where you see the Score object but it does not increase when the Flappy Bird goes between the pipes.
Does it increase at all ... ever?
JaneSmith JaneSmith

4 days ago

#
No the first one does not. The second one works. I just don't see what is getting in the way of seeing the code
danpost danpost

3 days ago

#
JaneSmith wrote...
No the first one does not. The second one works. I just don't see what is getting in the way of seeing the code
Maybe you can upload the scenario with the Publish source code check box checks so it can be experimented with.
suzbo suzbo

3 days ago

#
Will do thank you -
JamesE JamesE

3 days ago

#
https://www.greenfoot.org/scenarios/25189 this is the uploaded game
danpost danpost

3 days ago

#
The problem, as I believe was pointed out in a comment on the scenario, is that you zero the pipeCounter field in your createPipes method. So, it will never gain a value as high as that of FIRST_PIPE.
suzbo suzbo

3 days ago

#
Dan - you were correct. My student found the if statement in the createPipes method. Once the if statement was commented out the program worked.
There are more replies on the next page.
1
2