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

2014/1/5

How to Count Objects/Points

UltimateCoder UltimateCoder

2014/1/5

#
I have this game where once the ball is eaten by the bullet, you get one point. The games is over when you get 7 points. However, when I apply the code, (this code is in the Bullet.class), the game stops once you have shot 7 bullets. How can I correct this? Here is my code for the bullet: public class Bullet extends Animal { private int ballEaten = 0; /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveUpwards(); ballGone(); outOfScreen(); gameOver(); } /** * The bullets move in an upwards direction. */ public void moveUpwards() { setLocation(getX(), getY()-3); } /**When the bullet touches the ball, the ball will disappear. * */ public void ballGone() { if (canSee(Ball.class)) { eat(Ball.class); ballEaten++; if (ballEaten == 2) { gameOver(); } } } public void gameOver() { Greenfoot.stop(); } /** * When the bullets reach the top of the World/Screen, they will disappear. */ public void outOfScreen() { if (atWorldEdge()) { removeTouching(Bullet.class); } } }
Gevater_Tod4711 Gevater_Tod4711

2014/1/5

#
I think the problem is that you call the gameOver method from the act method. So every time the bullet acts it stopps the execution of the game. If you want to count the points and stop the game when you shot seven balls you should change the code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//change the variable ballEaten to static:
private static int ballEaten = 0;
 
//change the act method so that the gameOver method is only called when you have shot seven balls:
public void act() {
    moveUpwards();
    ballGone();
    outOfScreen();
    if (ballEaten >= 7) {
        gameOver();
    }
}
 
 
//I'm not shure if the ballGone() method will work like you want it to;
//When one bullet hit's two balls before it touches the worlds edge the game stops; no matter how many points you have reached;
//If you don't want the game to stop in this case you should change the method like this:
public void ballGone() {
    if (canSee(Ball.class)) {
        eat(Ball.class);
        ballEaten++;
    }
}
UltimateCoder UltimateCoder

2014/1/5

#
I used what you said : //change the act method so that the gameOver method is only called when you have shot seven balls: public void act() { moveUpwards(); ballGone(); outOfScreen(); if (ballEaten >= 7) { gameOver(); } } and it works perfectly. My code now looks like this, is it alright? { private static int ballEaten = 0; /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveUpwards(); ballGone(); outOfScreen(); if (ballEaten >= 11) { gameOver(); } } /** * The bullets move in an upwards direction. */ public void moveUpwards() { setLocation(getX(), getY()-3); } /**When the bullet touches the ball, the ball will disappear. * */ public void ballGone() { if (canSee(Ball.class)) { eat(Ball.class); ballEaten++; } } public void gameOver() { Greenfoot.stop(); } /** * When the bullets reach the top of the World/Screen, they will disappear. */ public void outOfScreen() { if (atWorldEdge()) { removeTouching(Bullet.class); } } }
UltimateCoder UltimateCoder

2014/1/5

#
Also, at the end, if you did not eat all the balls, I want something to say that 'You Didnt Eat All The Balls etc.' What code do I need for that?
danpost danpost

2014/1/5

#
All except for the last line ('removeTouching(Bullet.class)'), which will not remove itself. Change that line to: 'getWorld().removeObject(this);'
danpost danpost

2014/1/5

#
UltimateCoder wrote...
Also, at the end, if you did not eat all the balls, I want something to say that 'You Didnt Eat All The Balls etc.' What code do I need for that?
What will determine the 'end' if all the balls are not eaten? is there a time, miss or shot limit?
UltimateCoder UltimateCoder

2014/1/5

#
danpost wrote...
UltimateCoder wrote...
Also, at the end, if you did not eat all the balls, I want something to say that 'You Didnt Eat All The Balls etc.' What code do I need for that?
What will determine the 'end' if all the balls are not eaten? is there a time, miss or shot limit?
There will be 11 balls. If one ball touches the floor, the ball disappears and therefore you cannot eat all of them. The game is where you have to shoot all the balls and prevent them from touchng the floor
Gevater_Tod4711 Gevater_Tod4711

2014/1/5

#
If you want the game to stop when all the balls are removed (because they hit the floor) you can use this code in your world class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//For this code you need to import java.awt.Color;
//just add this line at the top of the code:
import java.awt.Color;
 
//put this in the act method of your world class:
if (getObjects(Ball.class).isEmpty()) {
    if (Bullet.ballEaten < 7) {
        //All the balls are removed from the world and the player has less than 7 points:
        Actor image = new Actor(){};
        image.setImage(new GreenfootImage("You Loose", 40, Color.white, null));//You can also add any other text you want here;
        addObject(image, getWidth()/2, getHeight()/2);
        Greenfoot.stop();
    }
    else {
        //In this case you won the game;
        //The gameOver method in your Bullet class probably was already executed and stoped the game;
    }
}
This code will make a message occour in the middle of the world. The text and the color of the text can be changed to whatever you want. You also need to change the variable ballEaten in your Bullet class to public static int ballEaten. Otherwhise that will cause compiler errors.
UltimateCoder UltimateCoder

2014/1/5

#
Here is my code for my world and bullet class. Could you please put what you have said in my code? Code for world: public class GreenScape extends World { /** * Constructor for objects of class GreenScape. * */ public GreenScape() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Paddle paddle = new Paddle(); addObject(paddle, 337, 339); Ball ball = new Ball(); addObject(ball, 137, 203); ball.setLocation(172, 53); Paddle paddle2 = new Paddle(); addObject(paddle2, 296, 354); removeObject(paddle2); removeObject(paddle); Paddle paddle3 = new Paddle(); addObject(paddle3, 337, 354); paddle3.setLocation(336, 344); paddle3.setLocation(333, 344); paddle3.setLocation(323, 376); paddle3.setLocation(329, 374); Ball ball2 = new Ball(); addObject(ball2, 559, 192); Ball ball3 = new Ball(); addObject(ball3, 72, 256); Ball ball4 = new Ball(); addObject(ball4, 373, 126); ball3.setLocation(53, 101); ball4.setLocation(353, 61); ball2.setLocation(520, 99); paddle3.setLocation(286, 376); paddle3.setLocation(278, 375); Brick brick = new Brick(); addObject(brick, 28, 239); Brick brick2 = new Brick(); addObject(brick2, 144, 150); Brick brick3 = new Brick(); addObject(brick3, 281, 78); Brick brick4 = new Brick(); addObject(brick4, 330, 231); Brick brick5 = new Brick(); addObject(brick5, 492, 62); Brick brick6 = new Brick(); addObject(brick6, 580, 178); Brick brick7 = new Brick(); addObject(brick7, 39, 67); Brick brick8 = new Brick(); addObject(brick8, 46, 171); Brick brick9 = new Brick(); addObject(brick9, 133, 56); Brick brick10 = new Brick(); addObject(brick10, 298, 139); Brick brick11 = new Brick(); addObject(brick11, 168, 259); Brick brick12 = new Brick(); addObject(brick12, 436, 247); Brick brick13 = new Brick(); addObject(brick13, 412, 39); Brick brick14 = new Brick(); addObject(brick14, 429, 148); brick7.setLocation(39, 31); brick9.setLocation(120, 19); brick7.setLocation(16, 12); brick3.setLocation(281, 78); brick3.setLocation(281, 78); brick3.setLocation(281, 78); brick3.setLocation(281, 78); brick3.setLocation(281, 78); brick3.setLocation(60, 12); brick8.setLocation(18, 106); brick8.setLocation(33, 93); brick.setLocation(38, 204); brick2.setLocation(235, 77); brick11.setLocation(124, 150); brick10.setLocation(225, 211); brick4.setLocation(310, 148); brick12.setLocation(338, 248); brick14.setLocation(490, 232); brick13.setLocation(430, 142); Brick brick15 = new Brick(); addObject(brick15, 90, 11); Brick brick16 = new Brick(); addObject(brick16, 114, 23); Brick brick17 = new Brick(); addObject(brick17, 578, 5); removeObject(brick16); removeObject(brick9); removeObject(brick15); Brick brick18 = new Brick(); addObject(brick18, 92, 14); Brick brick19 = new Brick(); addObject(brick19, 149, 10); Brick brick20 = new Brick(); addObject(brick20, 122, 15); Brick brick21 = new Brick(); addObject(brick21, 144, 24); Brick brick22 = new Brick(); addObject(brick22, 156, 27); Brick brick23 = new Brick(); addObject(brick23, 162, 24); Brick brick24 = new Brick(); addObject(brick24, 161, 22); Brick brick25 = new Brick(); addObject(brick25, 191, 31); removeObject(brick25); removeObject(brick24); removeObject(brick23); removeObject(brick22); removeObject(brick21); removeObject(brick20); removeObject(brick18); Brick brick26 = new Brick(); addObject(brick26, 112, 18); brick26.setLocation(104, 12); Brick brick27 = new Brick(); addObject(brick27, 183, 19); Brick brick28 = new Brick(); addObject(brick28, 199, 12); Brick brick29 = new Brick(); addObject(brick29, 308, 11); Brick brick30 = new Brick(); addObject(brick30, 534, 5); Brick brick31 = new Brick(); addObject(brick31, 532, 6); Brick brick32 = new Brick(); addObject(brick32, 453, 9); Brick brick33 = new Brick(); addObject(brick33, 397, 10); Brick brick34 = new Brick(); addObject(brick34, 340, 11); Brick brick35 = new Brick(); addObject(brick35, 313, 12); Brick brick36 = new Brick(); addObject(brick36, 266, 10); Brick brick37 = new Brick(); addObject(brick37, 242, 8); Brick brick38 = new Brick(); addObject(brick38, 241, 8); Brick brick39 = new Brick(); addObject(brick39, 238, 9); brick27.setLocation(189, 18); brick19.setLocation(147, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(199, 12); brick28.setLocation(186, 1); brick27.setLocation(189, 12); brick39.setLocation(231, 12); brick36.setLocation(264, 12); brick35.setLocation(306, 12); brick34.setLocation(340, 12); brick33.setLocation(381, 12); brick32.setLocation(423, 12); brick31.setLocation(466, 12); brick17.setLocation(503, 12); brick30.setLocation(541, 12); Brick brick40 = new Brick(); addObject(brick40, 580, 16); brick40.setLocation(580, 12); Ball ball5 = new Ball(); addObject(ball5, 217, 161); Ball ball6 = new Ball(); addObject(ball6, 116, 227); Ball ball7 = new Ball(); addObject(ball7, 424, 251); Ball ball8 = new Ball(); addObject(ball8, 369, 170); ball3.setLocation(35, 55); ball6.setLocation(33, 165); ball7.setLocation(90, 130); ball7.setLocation(119, 92); brick10.setLocation(147, 234); brick12.setLocation(300, 222); brick2.setLocation(262, 100); ball.setLocation(233, 54); ball8.setLocation(500, 175); ball5.setLocation(317, 169); ball5.setLocation(389, 173); brick4.setLocation(261, 161); ball5.setLocation(354, 148); brick4.setLocation(221, 159); Ball ball9 = new Ball(); addObject(ball9, 190, 188); Ball ball10 = new Ball(); addObject(ball10, 282, 283); Ball ball11 = new Ball(); addObject(ball11, 390, 244); ball2.setLocation(539, 90); ball10.setLocation(414, 96); ball10.setLocation(427, 90); ball11.setLocation(191, 106); removeObject(paddle3); removeObject(ball9); removeObject(ball6); removeObject(ball3); removeObject(ball11); removeObject(ball7); removeObject(ball); removeObject(ball4); removeObject(ball5); removeObject(ball8); removeObject(ball2); removeObject(ball10); OutZone outzone = new OutZone(); addObject(outzone, 337, 350); outzone.setLocation(301, 381); outzone.setLocation(301, 382); Paddle paddle4 = new Paddle(); addObject(paddle4, 355, 339); paddle4.setLocation(326, 375); Ball ball12 = new Ball(); addObject(ball12, 99, 57); Ball ball13 = new Ball(); addObject(ball13, 62, 142); Ball ball14 = new Ball(); addObject(ball14, 324, 156); Ball ball15 = new Ball(); addObject(ball15, 229, 230); Ball ball16 = new Ball(); addObject(ball16, 187, 30); Ball ball17 = new Ball(); addObject(ball17, 360, 76); Ball ball18 = new Ball(); addObject(ball18, 539, 138); Ball ball19 = new Ball(); addObject(ball19, 398, 265); Ball ball20 = new Ball(); addObject(ball20, 193, 89); Ball ball21 = new Ball(); addObject(ball21, 454, 192); ball16.setLocation(566, 61); ball19.setLocation(254, 56); removeObject(brick19); removeObject(brick39); removeObject(brick28); removeObject(brick27); removeObject(brick26); removeObject(brick3); removeObject(brick7); removeObject(brick38); removeObject(brick36); removeObject(brick37); removeObject(brick35); removeObject(brick29); removeObject(brick34); removeObject(brick33); removeObject(brick32); removeObject(brick31); removeObject(brick17); removeObject(brick30); removeObject(brick40); brick2.setLocation(318, 56); brick11.setLocation(140, 95); brick10.setLocation(140, 180); brick12.setLocation(350, 182); brick11.setLocation(186, 42); brick10.setLocation(132, 117); brick5.setLocation(542, 47); brick14.setLocation(439, 69); ball12.setLocation(46, 33); ball20.setLocation(126, 75); ball15.setLocation(157, 180); ball19.setLocation(209, 99); ball14.setLocation(312, 129); ball17.setLocation(404, 23); ball21.setLocation(453, 190); brick13.setLocation(447, 171); ball21.setLocation(434, 126); ball19.setLocation(237, 62); ball20.setLocation(183, 107); ball15.setLocation(157, 180); ball15.setLocation(158, 180); ball16.setLocation(490, 21); ball15.setLocation(161, 180); ball20.setLocation(127, 67); ball15.setLocation(159, 185); ball12.setLocation(516, 201); brick12.setLocation(325, 202); ball18.setLocation(539, 138); ball18.setLocation(539, 138); ball18.setLocation(539, 138); ball18.setLocation(539, 138); ball18.setLocation(539, 138); ball18.setLocation(539, 138); ball18.setLocation(539, 138); ball18.setLocation(539, 138); ball18.setLocation(563, 93); ball12.setLocation(516, 187); Ball ball22 = new Ball(); addObject(ball22, 61, 51); Counter counter = new Counter(); addObject(counter, 58, 26); } } and my code for the bullet is here: public class Bullet extends Actor { private static int ballEaten = 0; /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveUpwards(); ballGone(); outOfScreen(); if (ballEaten == 11) { gameOver(); } } /** * Return true if we can see an object of class 'clss' right where we are. * False if there is no such object here. * (Copied and pasted from Animal class) */ public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } /** * Try to eat an object of class 'clss'. This is only successful if there * is such an object where we currently are. Otherwise this method does * nothing. * (Copied and pasted from Animal class) */ public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } /** * Test if we are close to one of the edges of the world. Return true if we are. * (Copied and pasted from Animal class) */ public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) return true; else return false; } /** * The bullets move in an upwards direction. */ public void moveUpwards() { setLocation(getX(), getY()-3); } /**When the bullet touches the ball, the ball will disappear. * */ public void ballGone() { if (canSee(Ball.class)) { eat(Ball.class); ballEaten++; } } /** * Ends the game when called. */ public void gameOver() { Greenfoot.stop(); } /** * When the bullets reach the top of the World/Screen, they will disappear. */ public void outOfScreen() { if (atWorldEdge()) { getWorld().removeObject(this); } } }
Gevater_Tod4711 Gevater_Tod4711

2014/1/5

#
If you post code you should use the code tags underneath the textfield. That makes it much easier to read your code and you can copy the code. If you copy the code you posted without the code tags there are no line breaks. However, here is the code you need:
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
import greenfoot.*;
import java.awt.Color;
 
public class GreenScape extends World
{
 
 
public void act() {
    if (getObjects(Ball.class).isEmpty()) { 
        if (Bullet.ballEaten < 7) { 
            //All the balls are removed from the world and the player has less than 7 points: 
            Actor image = new Actor(){}; 
            image.setImage(new GreenfootImage("You Loose", 40, Color.white, null));//You can also add any other text you want here; 
            addObject(image, getWidth()/2, getHeight()/2); 
            Greenfoot.stop(); 
        
        else
            //In this case you won the game; 
            //The gameOver method in your Bullet class probably was already executed and stoped the game; 
        
    
}
 
//you can leave the prepare method and the constructor as it is now;
 
}
 
 
 
public class Bullet extends Actor {
     
    public static int ballEaten = 0;
 
    //all other methods don't need to be changed;
 
}
UltimateCoder UltimateCoder

2014/1/5

#
Thanks, and also, how can I create a counter?
UltimateCoder UltimateCoder

2014/1/5

#
I tried what you said and it worked, and I wanted the same to happen when you win, but instead it says 'you win' Here is the code but the 'you win' does not appear when you win. Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void act()
    
        if (getObjects(Ball.class).isEmpty())
        {   
            if (Bullet.ballEaten < 11)
            {Actor actor = new Actor(){};   
               actor.setImage(new GreenfootImage("You Loose", 40, Color.white, null));
                addObject(actor, getWidth()/2, getHeight()/2);   
                Greenfoot.stop();   
            }
          else
            {
                if (Bullet.ballEaten ==  11)
                {Actor image = new Actor(){};   
                    image.setImage(new GreenfootImage("You Win", 40, Color.white, null));
                    addObject(image, getWidth()/2, getHeight()/2);   
                    Greenfoot.stop();
                }
            }
        }
    }
Gevater_Tod4711 Gevater_Tod4711

2014/1/5

#
I think the problem is that the game stops before the execution of the code that shows the win message. When you shoot 11 balls the gameOver method in your Bullet class is executed and the game is stoped. If you change the gameOver method in your Bullet class like this it should work:
1
2
3
4
5
6
7
8
public void gameOver() {
    if (getWorld() != null) {
        Actor image = new Actor(){};     
        image.setImage(new GreenfootImage("You Win", 40, Color.white, null)); 
        getWorld().addObject(image, getWorld().getWidth()/2, getWorld().getHeight()/2);     
        Greenfoot.stop();
    }
}
danpost danpost

2014/1/5

#
Actually, you should remove all the 'game over code' from the Bullet class. And a simplified act method for your world class would be:
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
    if (getObjects(Ball.class).isEmpty())
    {
        String text = "You Win";
        if (Bullet.ballEaten < 11) text = "You Lose";
        Actor actor = new Actor(){};   
        actor.setImage(new GreenfootImage(text, 40, Color.white, null));
        addObject(actor, getWidth()/2, getHeight()/2);   
        Greenfoot.stop();   
    }
}
UltimateCoder UltimateCoder

2014/1/5

#
thanks, it works perfectly
You need to login to post a reply.