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

2022/1/4

Ending a programm

1
2
3
jdkdoen05 jdkdoen05

2022/1/4

#
So i have a race car game and i want it to end, when a car drove 3 times the circuit. How do I do it? Has someone a code for it? How can the game endlike this.
danpost danpost

2022/1/4

#
First, you will need an int distanceCompleted field to track the progress of the car as it travels the circuit. Now, consider the finish line as being a "check point" and each time it is touched, the int field will increment. With that alone, there exists no condition that can limit the incrementing of the field. That is, if the car touches the finish line in 3 consecutive act steps, the counter will already be at 3, which is no good. With a minimum of one more check point on the circuit, you can "know" when a possible circuit has been made. This give a way to check whether a check point should increment the int field or not, as the value of the field should be 1 less than what that check point can make it. The thing is, the int field will end up at twice the number of circuits (or 6) when the game is to end. A third check point will allow you to "know" that the circuit was completed in the proper direction (or the circuit would have to be complete 6 times in the opposite direction). So, now the int field will end up at 9 to end the game. However, best is probably to have 4 additional check point at extreme points in the circuit (aside from the finish line) to ensure no extreme short cuts have been made. The check point should extend across and beyond track limits at least to where the car is limited to go to get by that check point. With 4 check point AND a finish line, the game should end when the int field has a value of 15. Each check point AND the finish line can be given an identification number equal to its order of crossing, with the finish line's id num at zero. This will facilitate in checking if an increment is in order or not:
if(isTouching(CheckPoint.class) && distanceCompleted + 1)%numberOfCheckPoints == identificationNumber)
{
    distanceCompleted++;
    if (distanceCompleted == 3*numberOfCheckPoints) gameOver();
}
Of course, numberOfCheckPoints should be assigned a value prior to the code given. It can be done in a previous line:
int numberOfCheckPoints = getWorld().getObjects(CheckPoint.class).size();
or, provided the check points are added into the world prior to the car, a instance field can be used to hold the value, which can be assigned within an overriding addedToWorld(World) method.
jdkdoen05 jdkdoen05

2022/1/5

#
I have now used the code, but Grennfoot shows me that it is an illegal start of expression. The problem occurs with the percentage sign. In addition, Greenfoot indicates that the double equal sign(==) is not a statement. I don't know why. Maybe you could help me again. In addition, I would like to thank you for your help.
danpost danpost

2022/1/5

#
jdkdoen05 wrote...
I have now used the code, but Grennfoot shows me that it is an illegal start of expression. The problem occurs with the percentage sign.
Sorry. I missed an open parenthesis -- "(" -- after the "&&". Should be "&& (distanceCompleted+1)%numberOfCheckPoints"...
In addition, Greenfoot indicates that the double equal sign(==) is not a statement. I don't know why. Maybe you could help me again. In addition, I would like to thank you for your help.
That may be a consequence of the above also.
jdkdoen05 jdkdoen05

2022/1/5

#
First of all, thanks for your help. But how do you declare identificationNumber?
danpost danpost

2022/1/5

#
jdkdoen05 wrote...
how do you declare identificationNumber?
A simple CheckPoint class might be:
import greenfoot.*;

public class CheckPoint extends Actor
{
    int identificationNumber;
    
    public CheckPoint(int id)
    {
        identificationNumber = id;
        if (id != 0) getImage().clear();
    }
}
Then, in your world constructor (or prepare method), you would create the check points giving them their IDs: For example:
addObject(new CheckPoint(0), 200, 100);
Finally, for the code given above, use this instead:
CheckPoint checkPoint = (CheckPoint)getOneIntersectingObject(CheckPoint.class);
if (checkPoint != null && (distanceCompleted+1)%numberOfCheckPoints == checkPoint.identificationNumber)
{
    ...//etc (continued as above).
jdkdoen05 jdkdoen05

2022/1/5

#
WOW! It now say that there are many identiers expected. And I dont know whta i should write ,when you say continued as above. I am still lerning and i hope you could help me another time. Maybe you can have a look at the code down below and tell what i have to do, so that the game doesnt end after 2 seconds. So when i start the game ist even ends after 2 seconds. Both drivers win and yeah.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * Dies Klasse beschreibt eine Krabbe.
 * Krabben leben am Strand. Sie lieben Sandwürmer ...
 * 
 * @author Michael Kolling
 * @author Gunnar Johannesmeyer (Übersetzung und kleinere Anpassung)
 * @version 1.0
 */
public class Spieler1  extends Object
      
{
   private int distanceCompleted;
   private int identificationNumber;
   public Spieler1()
   {
   distanceCompleted=0;
   identificationNumber=1000;
   }
    
   public void act()
    {
     int numberOfCheckPoints = getWorld().getObjects(Checkpoint.class).size();
    if(isTouching(Checkpoint.class) && (distanceCompleted + 1)%numberOfCheckPoints == identificationNumber);
    {
    distanceCompleted++;
    if (distanceCompleted == 18*numberOfCheckPoints) 
     {
       setImage("spieler1win.png");
       setLocation(500, 400);
       setRotation(0);
       Greenfoot.stop();
       Greenfoot.stop();

      }
        if (Greenfoot.isKeyDown("left"))
     {
        turn(-2);
     }
     if (Greenfoot.isKeyDown("right"))
     {
        turn(2);
     }
     if (Greenfoot.isKeyDown("up"))
     {
         move();
       }
     if(Greenfoot.isKeyDown("down"))
     {
        backward();
     }
    }
    
    }
    

   
}
danpost danpost

2022/1/6

#
I certainly hope you do not have an Object class in your project. The root class of ALL objects in java IS the Object class. It would be best not to repeat "Object" as a class name. With the CheckPoint class supplied above, something like the following might work for Spieler1:
import greenfoot.*;

public class Spieler1 extends Actor // note below code
{
    private int distanceCompleted;
    
    public void act()
    {
        if (Greenfoot.isKeyDown("left")) turn(-2);
        if (Greenfoot.isKeyDown("right"))turn(2);
        if (Greenfoot.isKeyDown("up")) move();
        if (Greenfoot.isKeyDown("down")) backward();
        int numberOfCheckPoints = getWorld().getObjects(Checkpoint.class).size();
        CheckPoint checkPoint = (CheckPoint)getOneIntersectingObject(CheckPoint.class);
        if (checkPoint != null && (distanceCompleted + 1)%numberOfCheckPoints == checkPoint.identificationNumber);
        {
            distanceCompleted++;
            if (distanceCompleted == 18*numberOfCheckPoints) 
            {
                setImage("spieler1win.png");
                setLocation(500, 400);
                setRotation(0);
                Greenfoot.stop();
            }
        }
    }
}
The class should probably extend the class that has the move and backward methods in it.
jdkdoen05 jdkdoen05

2022/1/6

#
So we did that, but now we have to declare mve and backward. But where should we declare it. We cant declare it in Actor. So where are we supossed to declare it?
danpost danpost

2022/1/6

#
jdkdoen05 wrote...
So we did that, but now we have to declare mve and backward. But where should we declare it. We cant declare it in Actor. So where are we supossed to declare it?
What classes do you now have between Actor and Spieler1? ... and what are there codes?
jdkdoen05 jdkdoen05

2022/1/7

#
We have declined in object move and backward, turn and the movingspeed. Aswell we have a start button for the game and also checkpoints, so that nobody can cut the circuit.
danpost danpost

2022/1/8

#
jdkdoen05 wrote...
We have declined in object move and backward, turn and the movingspeed. Aswell we have a start button for the game and also checkpoints, so that nobody can cut the circuit.
That did not answer my request. Are you saying that the move and backward methods were in your Object class? What class did the Object class extend?
jdkdoen05 jdkdoen05

2022/1/8

#
Yes , we declared move and backward in Object. The class object, aswell as Spieler1(Player1), Spieler2(Player2) all extend from acteur
danpost danpost

2022/1/8

#
jdkdoen05 wrote...
Yes , we declared move and backward in Object. The class object, aswell as Spieler1(Player1), Spieler2(Player2) all extend from acteur
Okay. Put the class back in, but rename it something like Mover.
jdkdoen05 jdkdoen05

2022/1/25

#
So we now have a problem. The "%" is an illegal start of expression and "==" is not a statement. Sowhat do we do now?
There are more replies on the next page.
1
2
3