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

2019/3/24

Scoreboard - Broken!

CreatorMoon CreatorMoon

2019/3/24

#
Hi, so I am working on a game for school and I need to make a scoreboard. When I first made it, it wasn't actually counting whenever I picked something up like it was meant to. I asked my friends for help but for some reason their code didn't work for me. What can I do? I need the answer ASAP since I am going on holiday tomorrow and can't work on it there. Lines 45 - 60 below are related to my scoreboard.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import greenfoot.*;
 
/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    private String[] down;
    private String[] up;
    public String[] left;
    public String[] right;
    public int frameCounterLeft;
    public int frameCounterRight;
    public int frameCounterDown;
    public int frameCounterUp;
    public int wormsEaten;
    public Crab()
    {
        down = new String[4];
        for(int n = 0; n<down.length;n++)
        {
            down[n]="SansDown"+n+".gif";
        }
        up = new String[4];
        for(int n = 0; n<up.length;n++)
        {
            up[n]="SansUp"+n+".gif";
        }
        left = new String[4];
        for(int n = 0; n<left.length;n++)
        {
            left[n]="SansLeft"+n+".gif";
        }
        right = new String[4];
        for(int n = 0; n<right.length;n++)
        {
            right[n]="SansRight"+n+".gif";
        }
        frameCounterLeft=0;
        frameCounterRight=0;
        frameCounterDown=0;
        frameCounterUp=0;
    }
 
    public void act()
    {
        wormsEaten = 0;
        movement ();
        getWorld().showText("Score: "+wormsEaten, 100, 50);
        if ( isTouching(Worm.class) )
        {
            wormsEaten = wormsEaten +1;
            removeTouching(Worm.class);
            Greenfoot.playSound("slurp.wav");
        }
 
        if (wormsEaten == 6)
        {
            WinWorld win = new WinWorld();
            Greenfoot.setWorld(win);
        }
    }
 
    public void movement ()
    {
        if (Greenfoot .isKeyDown("a"))
        {
            move(-5);
            if (frameCounterLeft<4)
            {
                setImage (left[frameCounterLeft]);
                frameCounterLeft++;
            }
            else
            {
                frameCounterLeft=0;
            }
        }
        if (Greenfoot .isKeyDown("d"))
        {
            move(5);
            if (frameCounterRight<4)
            {
                setImage (right[frameCounterRight]);
                frameCounterRight++;
            }
            else
            {
                frameCounterRight=0;
            }
        }
        if (Greenfoot .isKeyDown("s"))
        {
            setLocation(getX(),getY()+5);
            if (frameCounterDown<4)
            {
                setImage (down[frameCounterDown]);
                frameCounterDown++;
            }
            else
            {
                frameCounterDown=0;
            }
        }
        if (Greenfoot .isKeyDown("w"))
        {
            setLocation(getX(),getY()-5);
            if (frameCounterUp<4)
            {
                setImage (up[frameCounterUp]);
                frameCounterUp++;
            }
            else
            {
                frameCounterUp=0;
            }
        }
    }
}
That's my code ^^^^^
Iz.\uo Iz.\uo

2019/3/24

#
"put this in your level" /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { addObject(new Scoreboard(0),75,25); } "make a class called scoreboard, and put this in: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * the score of the game * * @author (****) * @version (**-**-****) */ public class Scoreboard extends Actor { public static final float FONTSIZE = 24.0f; public static final int WIDTH = 150; public static final int HEIGHT = 50; public static final int TRANSPARENCY = 128; public static final int RED = 255; public static final int GREEN = 255; public static final int BLUE = 255; private int score; public Scoreboard(int newScore) { score=newScore; makeScoreBoard(); } public void act() { // Add your action code here. } public void makeScoreBoard() { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); //create a new image with Java Color color = new Color (RED,GREEN,BLUE,TRANSPARENCY ); //create a new color image.setColor(color); //set the color of the image with the object color image.fillRect(0, 0, WIDTH, HEIGHT); //fill the rectangle with the setted color setImage(image); //create the image image.setColor(color); //set the color of the image with the object color image.fillRect(0, 0, WIDTH, HEIGHT); //fill the rectangle with the setted color image.setColor(new Color(RED,0,0, TRANSPARENCY)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); image.setColor(new Color(0, GREEN, 0, TRANSPARENCY)); image.fillRect(10, 10, WIDTH-20, HEIGHT-20); Font font = image.getFont(); font = font.deriveFont(FONTSIZE); image.setFont(font); image.setColor(new Color(0,0,0,TRANSPARENCY)); image.drawString("score: " + score, 30, 32); setImage(image); //create the image } } This should get you started
Super_Hippo Super_Hippo

2019/3/24

#
Remove line 47 or you set the variable to 0 every act-cycle. It is also enough to use the "showText"-method after the variable changes (and once at the start). No need to replace it every time when nothing changes.
CreatorMoon CreatorMoon

2019/3/30

#
Thanks, but is there any way that I can have the exact same appearance for my scoreboard, it's just the scoring element that's broken.
Super_Hippo Super_Hippo

2019/3/30

#
Not exactly sure what you mean with "exact same appearance". One little problem I see is that you use showText before you check for worms you eat. So the counter is always one act cycle too late.
danpost danpost

2019/3/31

#
One problem I see is that line 47 needs to be removed.
CreatorMoon CreatorMoon

2019/4/1

#
Sorry, the "exact same appearance" thing was weird. It works now, thanks!
You need to login to post a reply.