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

2019/6/23

Need help with Score Counter Transfer trough levels

StalinRule34 StalinRule34

2019/6/23

#
In my Game there is the Player (Car) who can shoot a ball that takes 20 out of 100 health on each hit. Then i have my CPU wich shoots automatic every like 3 and a half seconds with the same interactions as the Player and when one of them dies they either get into the MyWorld2 ( when CPU dies) or tp MyWorld2v2. The thing is that right when one of them dies they get 1 added to the Score Counter. If the Score counter reaches 2 in the Car.class the GameOverScreen will be opend and if the CPU.class reaches the Score 2 the Victory Screen will apear. But everytime when im on one of the 2nd levels it doesnt work like it Should. When i kill the CPU it just restarts on the 2nd World again and i think it has sth to do with the Counter not being transfered or so... Pls help im kinda screwed rn cause of it being a school project Car Class:
public class Car extends Actor
{
    private int ShotTimer = 0;  // schuss verzögerungs anzeiger                            
    int health = 100;  // Lebensanzeige
    int Score2 = 0;
    private CPU myCPU ;
    public void act()
    {
        double entfernung = Math.sqrt(Math.pow(getX()-640, 2)+Math.pow(getY()- 402.5 ,2)); // Wurzel aus -> -640 hoch 2 | 402.5 hoch 2  
        // dadurch kriegen wir die entfernung unserer figur zum Mittelpunkt

        if(entfernung >= 363.5){   // wenn es kleiner / = 363.5 ist dann soll er um 25 nach hinten gehen ||kann somit nicht drüber 
            //und wird von einer unsichtbaren wand festgehalten

            move(-25); // gehe 25 nach hinten

        }
        else{

            if (ShotTimer > 0) {    //wenn der shottimer größer als 1 ist ,dann
                ShotTimer = ShotTimer - 1;  // in bestimmten zeitabstand immerwieder -1 bis 0 erreicht wird
            }
            else if (Greenfoot.isKeyDown("space")) {     // wenn taste Space gedrückt wird dann...
                getWorld().addObject(new Shot(this), getX(), getY());   // füge Object.class Shot zu der x y koordinate des Autos hinzu 
                ShotTimer = 200; // Timer auf 200 stellen um somit eine schuß verzögerung zu erzeugen  
            }

            MouseInfo mi = Greenfoot.getMouseInfo();   // Das Greenfoot programm soll die Koordinate der Maus aufnehmen welche durch den code unter dieser entommen wurde
            if(mi!=null)   // wenn die Daten nicht = 0 sind dann...
                turnTowards(mi);   // zur Maus drehen
            if (Greenfoot.isKeyDown("w"))   // wenn taste w gedrückt wird dann...
                move(2);   // bewege dich 2 nach vorne 

        }
        if (isTouching(Shot2.class)){    //wenn "car" "shot2" berührt ,dann...
            removeTouching(Shot2.class);    //entferne die schleife "isTouching"
            loseHealth(20);     //+ verliere 20 Leben
        }
    }    

    // if is Touching schleifen in einzelnen public void... weil dieses removeTouching die anderen Schleifen auch löscht
    // und wenn die gelöscht sind gibt es ein error ( eine lücke im System ) welche das spiel beendet

    // Update (in der Präsentationsdatei wird diese Update nachricht gelöscht) : 
    // Beim CPU hab ich bemerkt ,dass ich die Schleife isTouching mit dem removeTouching Befehl einfach als letztes benutzen muss
    // wodurch die anderen letzten endes nicht entfernt werden bevor sie ihre Ziele erreichen

    public void cpu () {   
        if (isTouching(block.class)) {   // wenn "car" "block" berührt ,dann...
            getOneIntersectingObject(CPU.class);   //lasse dieses Object mit der CPU.class interagieren
            move (-27);   //bewege dich 27 nach hinten 
        }
    }

    public void block () {   
        if (isTouching(block.class)) {  //wenn "car" "block" berührt ,dann...
            getOneIntersectingObject(block.class);  //lasse dieses Object mit der CPU.class interagieren
            move (-27);  //bewege dich 27 nach hinten 
        }
    }

    public void turnTowards (MouseInfo mi)  // coding um herauszufinden wo die Maus ist, um im nachhinein sich in die Richtung zu drehen
    {
        turnTowards(mi.getX(), mi.getY());  // drehe dich zur nun entommenen Maus x y koordinate
    }
    
     public void Score (){

         if (Score2 >= 2) {
            Greenfoot.setWorld(new GameOverScreen());
       }
    }
    
    public void loseHealth(int amount) // wenn die Lebensanzeige 0 erreicht ,wird dieser Spieler aus dem Spiel genommen
    {
        health -= amount;  
        if (health < 1)  // wenn "health" kleiner als 1 ist ( also 0 ), dann...
        {
            getWorld().removeObject (this);  // entferne dieses Object von der Welt
            Greenfoot.setWorld(new MyWorld2v2());
            Score2+=1;

        }
    }
     
    
}
StalinRule34 StalinRule34

2019/6/23

#
CPU Class:

public class CPU extends Actor
{
    int health1 = 100;        // Lebensanzeige vom CPU 
    private int ShotTimer = 0;      //schuss verzögerungs anzeiger
    private Car myCar;
    int Score = 0;
    private MyWorld2 myMyWorld2;
    public void act(){
        double entfernung = Math.sqrt(Math.pow(getX()-640, 2)+Math.pow(getY()- 402.5 ,2));  //Pythagoras: wurzel aus x hoch 2 + --- wurzel aus x hoch 2 
        if(entfernung >= 363.6){     //entfernung = radius 
            turn(180);  //drehe dich um 180 grad
            move(3);  // bewege dich um 3 nach vorne
        }

        else {
           moveAround();
            
        }

        if (isTouching(Car.class)){   //wenn der CPU den Spieler berührt ,dann...
            Car cr = (Car) getOneIntersectingObject(Car.class);
            cr.move(-27);  //soll er sich um 27 (pixel / units?) nach hinten bewegen
        }

        if(isTouching(block.class)){  //wenn der CPU einen Block berührt ,dann 
            move(-27);  //soll er sich um 27 nach hinten bewegen
        }
         
        if (ShotTimer > 0) {    //wenn der shottimer größer als 1 ist ,dann
            ShotTimer = ShotTimer - 1;  // in bestimmten zeitabstand immerwieder -1 bis 0 erreicht wird
        }
        
        else if (ShotTimer <= 0) {     // wenn taste Space gedrückt wird dann...
            
            getWorld().addObject(new Shot2(this), getX(), getY());   // füge Object.class Shot2 zu der x y koordinate des Autos hinzu 
            ShotTimer = 200; // Timer auf 200 stellen um somit eine schuß verzögerung zu erzeugen  
        }

        if(isTouching(Shot.class)){  //wenn "cpu" "shot" berührt ,dann...
            removeTouching(Shot.class);  //entferne Shot.class
            loseHealth(20);  //verliere 20 Leben
        }
        
    }
    
    public void moveAround(){
        move(2);
        if (Greenfoot.getRandomNumber (363) < 30)
        {
            turn(Greenfoot.getRandomNumber (90) -45 );
        }
        if (getX() <= 40 || getX() >= getWorld().getWidth() -35)
        {
             turn(90);
        }
        if(getY() <= 40 || getY() >= getWorld().getHeight() - 25)
        {
         turn(180);
        }
    }
    public void Score (){

        if (Score >= 2) {
            Greenfoot.setWorld(new VictoryScreen());
        }
    }
    public void loseHealth(int amount)  // Nutzt int variable im folgenden coding...
    {
        health1 -= amount; 

        if (health1 < 1)      //wenn health kleiner als 1 ist ( also 0 ) ,dann...
        {
            getWorld().removeObject (this);   // entfernt die Welt dieses Auto
            Greenfoot.setWorld(new MyWorld2());
            Score+= 1;
            
        }
    }
    
    
}
danpost danpost

2019/6/23

#
I suggest you move the scores and level changing codes into your world classes. Since all 3 game playing worlds will have the same codes (as far as these two things are concerned), it would make sense to create a super class that these three can extend:
import greenfoot.*;

public abstract class GameWorld extends World
{
    int [] scores = new int[2]; // car score, cpu score (deaths)
    
    public GameWorld(int w, int h, int c) { super(w, h, c); }
    
    public void act()
    {
        if (getObjects(CPU.class).isEmpty())
        {
            scores[1]++;
            if (scores[1] == 2) { Greenfoot.setWorld(new VictoryScreen()); return; }
            MyWorld2 mw2 = new MyWorld2();
            mw2.scores = scores;
            Greenfoot.setWorld(mw2);
        }
        else if (getObjects(Car.class).isEmpty())
        {
            scores[0]++;
            if (scores[0] == 2) { Greenfoot.setWorld(new GameOverScreen()); return; }
            MyWorld2v2 mv2 = new MyWorld2v2();
            mw2.scores = scores;
            Greenfoot.setWorld(mw2);
        }
    }
}
StalinRule34 StalinRule34

2019/6/24

#
danpost danpost

2019/6/24

#
StalinRule34 wrote...
This is my game compressed in a Zip folder and I would be so happy if you danpost or sb else could take a look at it and help me out with the Score and that stuff i dont understand how to use the things you´ve written down danpost sry and thanks again
Might take a look if you upload the game on this site. However, maybe you should take a look at my Super Level Support Class Demo scenario.
StalinRule34 StalinRule34

2019/6/25

#
Thanks for the tip I will try to find it out trough your "Super Level Support Class Demo" scenario and i tought about uploading it on this Greenfoot page but...im either stupid or blind because I dont know how to upload it.
StalinRule34 StalinRule34

2019/6/25

#
I just uploaded it and I will still first try to do it on my own with the help of your scenario and If you have the Time to look in it would be really nice and as always Thanks again
danpost danpost

2019/6/25

#
After taking a look, I find a much better way to "score" -- without any score counters!!! Since your MyWorld2 and your (future) MyWorld2v2 worlds will only be run once each (if any is attempted to run twice, then somebody already won), we can add static boolean fields to track whether they have run or not. The MyWorld class constructor can set them to false. The MyWorld2 and MyWorld2v2 class constructors can set their respective field to true. If a MyWorld2 or MyWorld2v2 level is won and to potential world to proceed has already been run, then game over. In MyWorld2 and MyWorld2v2:
// as field
public static boolean hasRun;

// in constructor
hasRun = true;
In MyWorld:
// in constructor
MyWorld2.hasRun = false;
MyWorld2v2.hasRun = false;
In Car and similarly in CPU class:
if (health < 1)
{
    if (MyWorld2.hasRun) Greenfoot.setWorld(new GameOver());
    else Greenfoot.setWorld(new MyWorld2());
}
Double-check the spellings/names of all class and field names. Also the level world may be the other one (in line 4 -- and line 3).
StalinRule34 StalinRule34

2019/6/25

#
THANK YOUUUUUUUUUUUUUUUUUUUUU you are truely insane thanks :D
You need to login to post a reply.