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

2020/1/22

Making a Counter

mattstg mattstg

2020/1/22

#
Does anyone know what I need to do in order top get the counters to work so that they count a lap every time the car passes the finish line?
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
/**
 * The track.
 *
 * @author (Matthew St. George)
 * @version (1.0)
 */
public class Track extends World
{
    private Counter theCounter;
    private Counter2 theCounter2;
     
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public Track()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 540, 1);
        theCounter = new Counter();
        addObject(theCounter, 40, 500);
        theCounter2 = new Counter2();
        addObject(theCounter2, 50, 450);
        prepare();
    }
     
    public Counter getCounter()
    {
        return theCounter;
    }
     
    public Counter2 getCounter2()
    {
        return theCounter2;
    }
Here is the code form one of the counters:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * Write a description of class Counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int totalCount = 0;
     
    private void Counter()
    {
        setImage(new GreenfootImage("0", 20, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
    }
     
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
    }
Here is the code from one of the cars:
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
/**
 * Write a descriptiocomman of class Car here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Car extends Actor
{
    public double moveVal;
 
    public Car()
    {
        moveVal = 0;
    }
 
    /**
     * Act - do whatever the Car wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment. Delete else
     * commands, in the act method, make it so if speed doesn't equal 4, then
     * increase speed by ex:0.2
     */
    double speed = 4;
    double acceleration = 0.5;
    public void act()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            turn(-5);
        }
        if ( Greenfoot.isKeyDown("right") )
        {
            turn(5);
        }
 
        move((int)moveVal);
 
        if ( Greenfoot.isKeyDown("up")  && moveVal < 4 )
        {
            moveVal+=0.2;
        }
        if ( Greenfoot.isKeyDown("down")  && moveVal > 0 )
        {
            moveVal-=0.2;
        }
 
        int x = getX(), y = getY();
        if(isTouching(Wall.class))
        {
            setLocation(327, 485);
        }
 
        checkSlow();
    }
 
    public void accelerate() {
        if(Greenfoot.isKeyDown("up")) speed += acceleration;
        else speed -= acceleration;
 
        move((int)speed);
        if(speed > 7) speed = 7;
        else if(speed <=0) speed =0;
    }
 
    public void checkSlow()
    {
        if(isTouching(Grass.class))
        {
            speed=2;
        }
        else
        {
            speed=4;
        }
        if(isTouching(Oil_Spot.class))
        {
            speed=2;
        }
        else
        {
            speed=4;
        }
    }
danpost danpost

2020/1/22

#
mattstg wrote...
what I need to do in order top get the counters to work so that they count a lap every time the car passes the finish line? << Code Omitted >>
Add an actor for the finish line.
mattstg mattstg

2020/1/23

#
danpost wrote...
mattstg wrote...
what I need to do in order top get the counters to work so that they count a lap every time the car passes the finish line? << Code Omitted >>
Add an actor for the finish line.
It still isn't working how I want it to, I get a lot more than 1 point whenever I cross the finish line. Why is that? Here is the code from the finish line actor: /** * Write a description of class Start_FinishLine here. * * @author (your name) * @version (a version number or a date) */ public class Start_FinishLine extends Actor { long lastAdded = System.currentTimeMillis(); long lastAdded2 = System.currentTimeMillis(); /** * Act - do whatever the Start_FinishLine wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { long curTime = System.currentTimeMillis(); Actor Car = getOneIntersectingObject(Car.class); if (curTime >= lastAdded + 5000) { if (Car != null) { Track trackWorld = (Track) getWorld(); Counter counter = trackWorld.getCounter(); counter.bumpCount(1); } lastAdded = curTime; } Actor Car2 = getOneIntersectingObject(Car2.class); if (curTime >= lastAdded2 + 5000) { if (Car2 != null) { Track trackWorld = (Track) getWorld(); Counter2 counter2 = trackWorld.getCounter2(); counter2.bumpCount2(1); } lastAdded = curTime; } }
danpost danpost

2020/1/23

#
mattstg wrote...
I get a lot more than 1 point whenever I cross the finish line. Why is that?
Probably because the car touches it for multiple act cycles in one passing. Add a boolean to track touching the line and use it to regulate the scoring.
mattstg mattstg

2020/1/24

#
danpost wrote...
mattstg wrote...
I get a lot more than 1 point whenever I cross the finish line. Why is that?
Probably because the car touches it for multiple act cycles in one passing. Add a boolean to track touching the line and use it to regulate the scoring.
How would I do that? Sorry I am a beginner
danpost danpost

2020/1/24

#
danpost wrote...
Add a boolean to track touching the line and use it to regulate the scoring.
mattstg wrote...
How would I do that? Sorry I am a beginner
Perform checking car crossing line in your Car class (remove the checking from FinishLine class):
1
2
3
4
5
6
7
8
9
// in Car class, but outside any code blocks, add:
private boolean touchingLine;
 
// in action code
if (touchingLine != isTouching(FinishLine.class))
{
    touchingLine = ! touchingLine;
    if (touchingLine) ((Track)getWorld()).getCounter().bumpCount(1);
}
mattstg mattstg

2020/1/28

#
danpost wrote...
danpost wrote...
Add a boolean to track touching the line and use it to regulate the scoring.
mattstg wrote...
How would I do that? Sorry I am a beginner
Perform checking car crossing line in your Car class (remove the checking from FinishLine class):
1
2
3
4
5
6
7
8
9
// in Car class, but outside any code blocks, add:
private boolean touchingLine;
 
// in action code
if (touchingLine != isTouching(FinishLine.class))
{
    touchingLine = ! touchingLine;
    if (touchingLine) ((Track)getWorld()).getCounter().bumpCount(1);
}
Thank you so much!
You need to login to post a reply.