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

2015/11/21

Stop the game when there are no actors left.

1
2
sanderbakker sanderbakker

2015/11/21

#
Hello, I'm making a game just like angry birds. I want it to move on to the next level when there are no pigs left in the world or if all of the 3 birds which I've lined up are shooted. How can I do this because I have no idea how I can do this Also how can I make a actor see the ground. So it won't go through objects
danpost danpost

2015/11/21

#
1
if (getObjects(Pig.class).isEmpty())
can be used to check if no pigs are in the world. If the birds are removed after being shot, then a similar condition can be used for their check and you can combine them into one set of conditions like this:
1
if (getObjects(Pig.class).isEmpty() || getObjects(Bird.class).isEmpty())
sanderbakker sanderbakker

2015/11/22

#
Thanks for this,this helped me alot. I have another question. I want to the game to show a game over screen if there are 3 birds shooted and if there are still pigs left in the world. How can I do this
danpost danpost

2015/11/22

#
sanderbakker wrote...
Thanks for this,this helped me alot. I have another question. I want to the game to show a game over screen if there are 3 birds shooted and if there are still pigs left in the world. How can I do this
Then you would use:
1
if (!getObjects(Pig.class).isEmpty() && getObjects(Bird.class).isEmpty())
again, provided the birds are removed from the world when shot.
sanderbakker sanderbakker

2015/11/22

#
The birds aren't removed from the world after they are shooted because every time there will spawn a new one in the catapult. It is not a line up to shoot them
danpost danpost

2015/11/22

#
Then you need to count the number of shot birds. Place the following line in your World subclass:
1
static int birdKillCount;
Be sure to add the following line in the World subclass constructor:
1
birdKillCount = 0;
When a bird is killed, increment this field. Use:
1
WorldClassName.birdKillCount++;
Then, use its value to determine game over.
sanderbakker sanderbakker

2015/11/22

#
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class Level1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level1 extends World
{
    Bird bird;
    Block block;
    Pig pig;
    Ground1 ground1;
    static int birdKillCount;
     
    /**
     * Constructor for objects of class Level1.
     *
     */
    public Level1()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(950, 600, 1);
         
        addObject(new TNT(),170,464);
        addObject(new Katapult(),161,406);
        addObject(new Bird(), 30,470);
        addObject(new Bird(), 76,470);
         
        bird = new Bird();
        addObject(bird, 120,467);
        block = new Block();
        addObject(block, 600,475);
        block = new Block();
        addObject(block, 627,475);
        block = new Block();
        addObject(block, 654,475);
        block = new Block();
        addObject(block, 681,475);
        block = new Block();
        addObject(block, 708,475);
        block = new Block();
        addObject(block, 735,475);
        block = new Block();
        addObject(block, 762,475);
        block = new Block();
        addObject(block, 789,475);
        block = new Block();
        addObject(block, 816,475);
        block = new Block();
        addObject(block, 843,475);
        block = new Block();
        addObject(block, 870,475);
        block = new Block();
        addObject(block, 627,448);
        block = new Block();
        addObject(block, 654,448);
        block = new Block();
        addObject(block, 681,448);
        block = new Block();
        addObject(block, 708,448);
        block = new Block();
        addObject(block, 735,448);
        block = new Block();
        addObject(block, 762,448);
        block = new Block();
        addObject(block, 789,448);
        block = new Block();
        addObject(block, 816,448);
        block = new Block();
        addObject(block, 843,448);
        block = new Block();
        addObject(block, 654,421);
        block = new Block();
        addObject(block, 681,421);
        block = new Block();
        addObject(block, 708,421);
        block = new Block();
        addObject(block, 735,421);
        block = new Block();
        addObject(block, 762,421);
        block = new Block();
        addObject(block, 789,421);
        block = new Block();
        addObject(block, 816,421);
        block = new Block();
        addObject(block, 681,394);
        block = new Block();
        addObject(block, 708,394);
        block = new Block();
        addObject(block, 735,394);
        block = new Block();
        addObject(block, 762,394);
        block = new Block();
        addObject(block, 789,394);
         
        block = new Block();
        addObject(block, 708,367);
        block = new Block();
        addObject(block, 735,367);
        block = new Block();
        addObject(block, 762,367);
        block = new Block();
        addObject(block, 735,340);
        pig = new Pig();
        addObject(pig, 735,301);
        ground1 = new Ground1();
        addObject(ground1, 729,495);
        
        addObject(new PauseButton(), 27, 27);
       birdKillCount = 0;
    }
     
    public void act ()
    {
    if(Greenfoot.mouseClicked(null)){
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse.getButton() <2)
            {
                removeObject(bird);
                 
                bird = new Bird();
                addObject(bird,157,365);
                if(365-mouse.getY() > -150)
                    bird.velocityY = (365-mouse.getY())/10;
                else
                    bird.velocityY = -5;
                if(157-mouse.getX() < 125)
                    bird.velocityX = (157-mouse.getX())/10;
                else
                    bird.velocityX = 10;
                }  
       }
        
      if (getObjects(Pig.class).isEmpty()) {
          Greenfoot.setWorld(new Level2());
        }
        // Laat de vogel vanaf een bepaald x en y punt starten, en laat de vogel tot een bepaald punt vliegen, hoe lager de waarde hoe minder ver de vogel komt - Sander
        if(removeObject(bird)) {
            Level1.birdKillCount++;
        }
         
        if(birdKillCounter == 3)
        {  
            Greenfoot.setWorld(new Level2());
        }
I've tried this but it won't work. What is wrong with this ?
danpost danpost

2015/11/22

#
The code, lines 139 through 142, should be in the class of the actor that kills the birds. Also, you should not be asking 'if' a bird is removed, but, line 141 should be executed when a bird is removed.
sanderbakker sanderbakker

2015/11/22

#
But the bird isn't killed. The bird is removed when there is a new one shooted. and how do I make it when instead of if
danpost danpost

2015/11/22

#
sanderbakker wrote...
But the bird isn't killed. The bird is removed when there is a new one shooted. and how do I make it when instead of if
Is this issue solved by way of your other discussion thread?
sanderbakker sanderbakker

2015/11/22

#
No, because I still want the game to show a game over screen after I shooted 3 birds.
danpost danpost

2015/11/22

#
sanderbakker wrote...
No, because I still want the game to show a game over screen after I shooted 3 birds.
The counting of birds shot is not working, then?
sanderbakker sanderbakker

2015/11/22

#
No because when I start the game there are no birds in the world. I add them when I click the mouse. Then I a bird will shoot and will be removed after I clicked the mouse for the second time. So I want after I clicked the mouse 3 times that the game stops and moves to the next level or give a screen which says game over.
danpost danpost

2015/11/22

#
sanderbakker wrote...
No because when I start the game there are no birds in the world.
It should not matter how many bird are in or not in the world at the start of the game. The birdKillCount field should only be incremented when a bird it hit (or killed). Line 144 above has 'birdKillCounter', but the field name is 'birdKillCount'.
sanderbakker sanderbakker

2015/11/22

#
Still not working even if I change birdkillcounter into birdkillcount. Is there a way to count the time I clicked the mouse and set a game over screen after I clicked it three times? because this could also solve this problem.
There are more replies on the next page.
1
2