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

2014/11/14

Ending the Game when all Trees are collected by the Camel

Kai_LiKeAfIsH Kai_LiKeAfIsH

2014/11/14

#
Hi This may seem a little noobie, but i started with programming just a few weeks ago in school. Now I'm at our open day to present the subject itself to younger students which are interested to come to our school. I thought of programming a simple game in which the player is a camel and needs to collect all trees in 5 seconds (for testing purpose i put it to 60). My problem ist that the console won't give me the text it should ones collected all trees. Heres the code:
public class Camel extends Actor
{
    private long startTime;
    public Camel(){
         startTime = getCurrentTimeMillis();
    }

    public void act() 
    {
       move(5);
     
       if(Greenfoot.isKeyDown("left"))
       {
           turn(-3);
       }
       if(Greenfoot.isKeyDown("right"))
       {
           turn(3);
       }
       
       int coordX = getX();
       int coordY = getY();
       
       if(coordX >= 580 || coordX <= 20 || coordY >= 380 || coordY <= 20)
       {
           this.setRotation(this.getRotation()+180);
       }

       int Trees = 0;
       
       if(getElapsedTimeInSeconds() >= 60)
       {
           System.out.println("Zeit vorbei - Ende");
           Greenfoot.stop();
       }
       
       if(isTouching(Tree.class))
       {
        removeTouching(Tree.class);
        Trees++;
       }
       
       if(Trees >= 5)
       {
        System.out.println("Gewonnen!");
        Greenfoot.stop();
       }
    }    

    private long getCurrentTimeMillis(){
        return System.currentTimeMillis();
    }
    

    public long getElapsedTimeInSeconds(){
        return (getCurrentTimeMillis() - startTime)/1000;
    }
}
In advance: Thanks for helping! Kai
Super_Hippo Super_Hippo

2014/11/14

#
Put line 29 at the beginning outside the act method or you will set the variable to 0 every act, so it will never reach 5.
Kai_LiKeAfIsH Kai_LiKeAfIsH

2014/11/14

#
Thanks a lot for that quick answer! I tried it out and it worked. Tank you!
You need to login to post a reply.