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

2018/4/10

How to add a score's using counter class

DanMyMan DanMyMan

2018/4/10

#
I'm creating a ping pong game, when ball hits "Line1.class" I want the score to be increased by one point, I have used an imported counter class (the default one) but have not yet changed the code. What code do I have to add to make sure when the ball hits line 1 a point is added to the counter??? Thanks to Danpost in advance.
danpost danpost

2018/4/10

#
DanMyMan wrote...
I have used an imported counter class (the default one) but have not yet changed the code.
First, let me point out that the Counter class code is not intended to be modified. In doing so, you ruin its versatility; in particular, its re-usability. This goes with all support classes (provided they are written properly).
I'm creating a ping pong game, when ball hits "Line1.class" I want the score to be increased by one point, What code do I have to add to make sure when the ball hits line 1 a point is added to the counter?
Do you not have two lines and two counters?
DanMyMan DanMyMan

2018/4/11

#
I have two lines and two counters, But I havent changed or modified any code because I dont know how to convert the counter into a score board which functions. What code must I put in order to the score to change by 1, everytime hits line 1, the same for the opposite player? Thanks Dan
danpost danpost

2018/4/11

#
Each line should probably be told (or given a reference to) which counter it will work with. The lines can retain their counter in a field for use when needed. The easiest way to assign a counter to a line is by adding a Counter parameter to the line constructors. So instead of, for example:
1
public Line1()
you would use:
1
public Line1(Counter myCounter)
Above the constructor line, you would add a field to hold the Counter object:
1
private Counter counter;
and in the constructor, you would assign the Counter object to the field:
1
counter = myCounter;
Then when a ball intersects a line, add one to the counter:
1
counter.add(1);
DanMyMan DanMyMan

2018/4/12

#
Sorry I dont understand, What code ensures that the when the ball touches the line, that the score changes by 1? shouldnt I add some code to the ball class? I just added
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class point1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class point1 extends Actor
{
    /**
     * Act - do whatever the point1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
 
    }   
    private Counter counter;
     
    public point1(Counter myCounter);
    counter = myCounter ;
    counter.add(1);
}
     
}
 
}
to the line and but an error appears? Could you help me if I upload it here? I have been stuck with this problem for ages thanks
DanMyMan DanMyMan

2018/4/12

#
http://www.greenfoot.org/scenarios/21093 my game, before adding the scores
danpost danpost

2018/4/12

#
Replace the last three (3) lines in your MyWorld class with the following:
1
2
3
4
5
6
Counter counter = new Counter(); // create a counter
addObject(counter, 150, 50); // place on side of car1
addObject(new point1(counter), 600, 200); // add line1 at car2 end, telling it which counter to use
counter = new Counter(); // create another counter
addObject(counter, 450, 50); // place on side of car2
addObject(new point2(counter), 0, 200); // add line2 at car1 end, telling it which counter to use
Then for the point1 class, you can have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;
 
public point1 extends Actor
{
    private Counter counter;
 
    public point1(Counter c)
    {
        counter = c;
    }
     
    public void act()
    {
        if (isTouching(ball.class)
        {
            counter.add(1);
            removeTouching(ball.class);
        }
    }
}
and similarly for the point2 class. Then add an act method to your MyWorld class to add a new ball into the world if there is none found in the world. It could also check the counters to see if a winning score was made; or count balls added to the world and stop adding after so many (like best of 11 or 21 or something).
DanMyMan DanMyMan

2018/4/12

#
wow! it is perfect, however I dont know how to make the act method which adds the ball into the screen after the score changing, currently after one point the ball disappears. How would I add that code. Also how can I make it best out of 21? thanks so much and sorry for being such a noob!
danpost danpost

2018/4/12

#
DanMyMan wrote...
how can I make it best out of 21?!
In the world class, you will need to count the number of times a ball is found not to be in the world:
1
2
3
4
5
6
7
8
9
10
11
12
// field
private int services;
 
// act method
public void act()
{
    if (getObjects(Ball.class).isEmpty())
    {
        if (++services == 21) Greenfoot.stop();
        else addObject(new Ball(), getWorld()/2, getHeight()/2);
    }
}
DanMyMan DanMyMan

2018/4/12

#
I just changed getWorld to getWidth and the code works fine again! Thanks for your amazing help Dan!
danpost danpost

2018/4/12

#
DanMyMan wrote...
I just changed getWorld to getWidth and the code works fine again!
At least that was an obvious correction. YW
DanMyMan DanMyMan

2018/4/18

#
similarly how would I do the same thing for my snake game? the code in snakeworld (basically my world is this)
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class snakeWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class SnakeWorld  extends World
{
    private final int MAX_DOT = (600*400)/(60*40);
     
    private int[] x = new int[MAX_DOT];
    private int[] y = new int[MAX_DOT];
     
    private Dot head, tail;
     
    private int Dots = 4;
    private boolean firstTime = true;
     
    { Counter counter = new Counter();
      
        addObject(counter, 150, 50);
        addObject(new Dot(counter), 600, 200);
    }
         
    public SnakeWorld()
    {  
        super(600, 400, 1);
         
               for(int i=0;i<1;i++)
            addObject(new Food(), Greenfoot.getRandomNumber (12*50+25), Greenfoot.getRandomNumber (8*50+25));
         
        for(int z = 0; z < Dots; z++)
        {
            x[z] = 100 - z*20;
            y[z] = 20;
        }
       makeSnakeHead();
     
    }
     
    public void act()
    {
        for(int z = Dots; z > 0; z--)
        {
            x[z] = x[z-1];
            y[z] = y[z-1];
        }
    }
 
 
    public void makeSnakeHead()
    {
        head = new Dot(0);
        addObject(head, x[0], x[0]);
    }
 
    public void makeSnake()
    {
        for(int i = 1; i < Dots; i++)
        {
            Dot tail = new Dot(i);
            addObject(tail, x[i], y[i]);
        }
    }
 
    public void addDot()
    {
        int parentX = x[Dots-1];
        int parentY = y[Dots-1];
         
        tail = new Dot(Dots);
        addObject(tail, parentX, parentY);
        Dots++;
         
    }
     
    public void addFood()
    {
        for(int i = 0; i <Greenfoot.getRandomNumber(2)+1; i++)
        {
           addObject(new Food(), Greenfoot.getRandomNumber(12)*50+25, Greenfoot.getRandomNumber(8)*50+25);
        }
    }
     
    public void setDX(int d, int dx)
    {
        x[d] = dx;
    }
     
    public void setDY(int d, int dy)
    {
        y[d] = dy;
    }
     
    public void gameOver()
    {
        Greenfoot.stop();
    }
     
    public int getMyX(int d)
    {
       return x[d];
    }
     
    public int getMyY(int d)
    {
       return y[d];
    }
}
but there is an error in the line addObject(new Dot(counter,600, 200);
danpost danpost

2018/4/18

#
Please show the Dot class codes.
You need to login to post a reply.