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

2019/12/17

need help with project concerning the actors

Vishnu19 Vishnu19

2019/12/17

#
Hello again, I have a couple of questions with the game im creating right now For the following actor, I would like to have an random initial rotation (using a random number generator and give it a greater random turn capability than what it is currently shown as below.
public void randomTurn() //Turns at random intervals
    {
        move(1);
        //create condition of probability, occurs 1/20 times or 5%.
        if(Greenfoot.getRandomNumber(20)==1) 
        {
            //generate degrees from -30 to 30 inclusive.
            turn(Greenfoot.getRandomNumber(61)-30); 
        }
    }
Next, I need help with getting the following method to create a condition in which when the actor touches the borders, it will respawn in random areas ONLY in the top half of the world (btw my world is 800x600). The code I have below allows the actor to respawn in random areas anywhere in the world, which I would like to change to what I described above.
public void bounceAtEdges(int width,int height) //
    {

        int worldX = getWorld().getWidth();
        int worldY = getWorld().getHeight();

        if(getY()==height) //top border
        {
            setLocation(Greenfoot.getRandomNumber(worldX) , Greenfoot.getRandomNumber(worldY));
        }
        if(getX()==width) //left border
        {
            setLocation(Greenfoot.getRandomNumber(worldX) , Greenfoot.getRandomNumber(worldY));
        }
        if(getX()==worldX-width) //right border
        {
            setLocation(Greenfoot.getRandomNumber(worldX) , Greenfoot.getRandomNumber(worldY));
        }
        if(getY()==worldY-height)//bottom border
        {
            setLocation(Greenfoot.getRandomNumber(worldX) , Greenfoot.getRandomNumber(worldY));
        }
    }
Lastly, how would I reset all my variables when the game is either won or lost? Whenever I win/lose, I press reset but the "GAME OVER" or "YOU WIN" text does not disappear, and I have to recompile scenario in order to do so.
danpost danpost

2019/12/17

#
Vishnu19 wrote...
I would like to have an random initial rotation (using a random number generator
Use a class constructor to set an initial random rotation.
and give it a greater random turn capability than what it is currently shown as below. << Code Omitted .>>
Increase the values on line 8.
I need help with getting the following method to create a condition in which when the actor touches the borders, it will respawn in random areas ONLY in the top half of the world (btw my world is 800x600). The code I have below allows the actor to respawn in random areas anywhere in the world, which I would like to change to what I described above. << Code Omitted >>
In your setLocation lines, divide the world height value in half.
how would I reset all my variables when the game is either won or lost? Whenever I win/lose, I press reset but the "GAME OVER" or "YOU WIN" text does not disappear, and I have to recompile scenario in order to do so.
Reset static field values in your initial world constructor.
You need to login to post a reply.