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

2015/12/9

Can a loop be programmed to turn itself on and off?

Poland Poland

2015/12/9

#
I think I can get my random food generator to work (without creating an infinite loop) if I can get a loop to contstantly change a variable every couple of seconds or so. That way I can circle through my if statement without change it to a while and creating an infinite loop. I could also limit food production using and AND condition in my if statement. Here's what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void addFood(){
    if(snake.move=true){//This is supposed to be the condition for generating more food. It should create food only if there needs to be more food in the world (no more than 2 food items, but 4 is acceptable because of the way this code is made). I can't get this to be a while statement or it won't work right.
        while(Greenfoot.getRandomNumber(100) < 20){//This creates cherries 20% of the time
            int x=Greenfoot.getRandomNumber(500);
            int y=Greenfoot.getRandomNumber(500);
            addObject(new cherry(), x, y);
        }
        while(Greenfoot.getRandomNumber(100) < 15){//This creates apples 15% of the time
            int x=Greenfoot.getRandomNumber(500);
            int y=Greenfoot.getRandomNumber(500);
            addObject(new apple(), x, y);
        }
        while(Greenfoot.getRandomNumber(100) < 10){//This creates pizza 10% of the time
            int x=Greenfoot.getRandomNumber(500);
            int y=Greenfoot.getRandomNumber(500);
            addObject(new pizza(), x, y);
        }
        while(Greenfoot.getRandomNumber(100) < 5){//This creates bread 5% of the time
            int x=Greenfoot.getRandomNumber(500);
            int y=Greenfoot.getRandomNumber(500);
            addObject(new bread(), x, y);
        }
    }
danpost danpost

2015/12/9

#
I do not see how a loop could possibly turn itself on. If it is off, then no code within in could possibly execute to turn it back on.
Poland Poland

2015/12/9

#
Is there no other code I could use similarly?
danpost danpost

2015/12/10

#
Poland wrote...
Is there no other code I could use similarly?
As long as you call 'addFood' from the 'act' method, you will be spawning food. Your condition within the 'addFood' method, 'snake.move=true' will always be 'true' because you are assigning 'true' to 'snake.move'. To compare 'snake.move' to 'true' you need to use the conditional equality symbol, '==' (a double equal sign). This check is the thing that allows the code in the method to execute or not.
Poland Poland

2015/12/10

#
I changed the code now. I think I figured out a simple way to fix the problem, but I don't think it's reacting to the change in the boolean. It still won't work. I think this is the easiest way to do it. For the world:
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
private snake snake= new snake();
/**
 * Constructor for objects of class snakeWorld.
 *
 */
public snakeWorld()
{   
    // Create a new world with 500x500 cells with a cell size of 1x1 pixels.
    super(500, 500, 1);
    addObject(new snake(), 62, 62);
    addObject(new cherry(), 280, 380);
    addFood();
}
public void addFood(){
    if(snake.lessFood){
        if(Greenfoot.getRandomNumber(100) < 15){//Adds 1 apple 15% of the time
            int x=Greenfoot.getRandomNumber(500);
            int y=Greenfoot.getRandomNumber(500);
            addObject(new apple(), x, y);
        }
        if(Greenfoot.getRandomNumber(100) < 10){//Adds 1 pizza 10% of the time
            int x=Greenfoot.getRandomNumber(500);
            int y=Greenfoot.getRandomNumber(500);
            addObject(new pizza(), x, y);
        }
        if(Greenfoot.getRandomNumber(100) < 5){//Adds 1 bread 5% of the time
            int x=Greenfoot.getRandomNumber(500);
            int y=Greenfoot.getRandomNumber(500);
            addObject(new bread(), x, y);
        }
        if(numberOfObjects() < 2){//If by chance, no other food is generated, it will add a cherry.
            int x=Greenfoot.getRandomNumber(500);
            int y=Greenfoot.getRandomNumber(500);
            addObject(new cherry(), x, y);
        }
        snake.lessFood = false; //Reset
    }
}
For the actor with the boolean:
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
public class snake extends Actor
{
    private SimpleTimer timer = new SimpleTimer();
    /**
     * Act - do whatever the snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(2);// Constantly moving
        playerControl();//Sets controls
        eat();//Counts points and removes collected food
        death();//Ends game upon death
    }   
    public static boolean move=true;//Is the snake moving? Control Boolean
    public void death(){//Stops the game when you die
        boolean snakeDead=false;//Controls game death
        if(isAtEdge()==true){
            snakeDead=true;
        }
        if(snakeDead==true){
            move=false;
            Greenfoot.stop();
            Greenfoot.playSound("Pacman-death-sound.mp3");
        }
    }
         
    public void playerControl(){// Sets the keys that control the direction of the snake.
        if(Greenfoot.isKeyDown("left")){
            setRotation(180);
        }
        if(Greenfoot.isKeyDown("right")){
            setRotation(0);
        }
        if(Greenfoot.isKeyDown("down")){
            setRotation(90);
        }
        if(Greenfoot.isKeyDown("up")){
            setRotation(270);
        }
    }
    public boolean lessFood = false;//Boolean that controls the production of food
    public void eat(){//Adds points and removes eaten cherries
        if(isTouching(cherry.class)){
            removeTouching(cherry.class);
            lessFood = true;//Tells the world that it needs more food
            //1 point
        }
        if(isTouching(apple.class)){
            removeTouching(apple.class);
            lessFood = true;//Tells the world that it needs more food
            //2 points
        }
        if(isTouching(pizza.class)){
            removeTouching(pizza.class);
            lessFood = true;//Tells the world that it needs more food
            //5 points
        }
        if(isTouching(bread.class)){
            removeTouching(bread.class);
            lessFood = true;//Tells the world that it needs more food
            //10 points
        }
    }
 
}
danpost danpost

2015/12/10

#
I believe you need to make the 'lessFood' field a 'static' field at line 42 in the 'snake' class.
Poland Poland

2015/12/10

#
Unfortunately that was not it. The change made no difference. I will try restarting the program.
Super_Hippo Super_Hippo

2015/12/10

#
Change line 10 in the world code to this:
1
addObject(snake, 62, 62);
You are comparing and changing the 'lessFood' variable from an object, that is not the snake in the world. Do you have an act method in your world class which calls the 'addFood' method? As an alternative (which would make sense in my opinion), you could just call the 'addFood' method from the snake class when the snake eats food. Then you don't need this 'lessFood' variable.
Poland Poland

2015/12/10

#
You can't add objects from the Actor class, at least not from my experience. Your change doesn't fix it either. The addFood method is called in the snakeWorld class. I provided both the codes from the world and the snake classes separtely, snake is instantiated in the snakeWorld class, allowing me to call on methods and variables from the snake class. It won't allow methods specific to Actors be called in a world class and vice-versa.
Super_Hippo Super_Hippo

2015/12/10

#
Poland wrote...
snake is instantiated in the snakeWorld class, allowing me to call on methods and variables from the snake class.
This just didn't make any sense because you have to call the methods on the snake object and not to any instance of the snake class (you created a new one and called methods on it) to affect the snake in your world. Of course you can use world methods in actor subclasses. When eating food, you can create for example a new apple with this:
1
getWorld().addObject(new apple(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
Of course you can just use 500 in it too. But this shows you again how to use world methods in actor subclasses.
Poland Poland

2015/12/10

#
I don't understand! Why does it work now that I cut and pasted the addFood code to the eat() method if blocks!? I'm just gonna roll with it, at least it works. Thanks a lot, this is what I was trying to do.
danpost danpost

2015/12/10

#
Poland wrote...
Unfortunately that was not it. The change made no difference. I will try restarting the program.
Sorry -- I did not see that you had a 'snake' field in the world class. Actually, if you had followed convention in starting class names with an uppercase letter, my oversight could have been averted.
Super_Hippo Super_Hippo

2015/12/11

#
Poland wrote...
I don't understand! Why does it work now that I cut and pasted the addFood code to the eat() method if blocks!?
It is because this line in 'addFood' method in the world class
1
if(snake.lessFood){
is never 'true', so it won't be executed. That's what I was trying to tell you. You created two snake objects. One is added to the world and you get the 'lessFood' variable from the other object.
Poland Poland

2015/12/11

#
Thanks guys Unfortunately because I spent so much time on this problem, I didn't completely finish in time to turn my project in. I had a lot of fun with it though, and plan to complete it! Thanks guys. If you couldn't tell, I am new to this ^^;
You need to login to post a reply.