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

2013/8/13

Point System

1
2
3
ich99 ich99

2013/8/13

#
Hey, Guys I'd like to programm a game like pacman in greenfoot, but i dont know how to create a Point System. First I want to place some coins in the level which i could take. And when i take a coin, I want that that the number of coins that i have taken in the left hand corner appears. And when the game is finished the points that you have collected should be displayed on the end screen. Thanks for your help, and Sorry for my bad english :D
SPower SPower

2013/8/13

#
Well, it would be a lot easier if you would tell what you mean by a 'point system'. Do you mean how to know when you intersect coins, or how to know where you are?
Gevater_Tod4711 Gevater_Tod4711

2013/8/13

#
To implement a point counter you should use the greenfoot class Counter. To import it into your project click on the Edit menu of your scenario. Then choose the menu item 'import class', select the Counter class and click 'import'. To check whether your packman touches a coin and to remove the coin you can use this mehtod:
public void getCoin() {
    Actor coin = getOneObjectAtOffset(0, 0, Coin.class);
    if (coin != null) {
        getWorld().removeObject(coin);
    }
    //count up the value of your counter here;
}
ich99 ich99

2013/8/13

#
I mean something like money, but without a shop, so you collect the coins and at the end you an see how many coins you have collectet. for example you've collectet 20 coins, but in the level are 50 coins, then at the end should be displayed: " you've collectet 20/50 coins". So i mean "Point System" like a highscore where you can see how good you were in the game
ich99 ich99

2013/8/13

#
I now imported the counter to my project and cpied the text from gevater_Tod in the counter, but now i dont know what to write in "//count up the value of your counter here". when i compile now no errors appears but i cant collect the coin. Sorry when i sound stupid to you but i'm a real beginner. Thanks for helping me
SPower SPower

2013/8/13

#
You should create an instance variable in the class of your 'packman' (the thing that collects coins):
private int coinsCollected = 0;
and in the part of Gevater's code which says 'count up the value...', put this:
coinsCollected++; // adds 1 to coinsCollected
and you can create a method:
public int getCoinsCollected()
{
    return coinsCollected;
}
and use that in your world class to know how many coins are collected. Displaying it is a bit more complicated, but I haven't got time to explain that now, so maybe someone else can do that for you.
SPower SPower

2013/8/13

#
Ok, so in my scenario Library I've got a class, I think it's called 'Text'. Copy the text in it, create a new subclass of Actor in your scenario and paste it. Change this line:
public class Text extends SuperActor
to this:
public class Text extends Actor
Now, in your World subclass, create these instance variables:
private Text coinsCollectedText;
private int coinsCollected;
in your constructor:
coinsCollectedText = new Text("You have collected 0/50 coins", 20); // size 20, may be too small or too big
addObject(coinsCollectedText, x,y); // x and y need to be filled in, depends on where you want it.
then add this method:
>Player< packman = getObjects(>Player<.class).get(0);
if (coinsCollected < packman.getCoinsCollected()) {
    coinsCollected = packman.getCoinsCollected();
    coinsCollectedText.setText("You have collected "+coinsCollected+"/50 coins");
}
Replace >Player< with the name of your 'packman' I think I haven't missed anything, so this should work.
ich99 ich99

2013/8/14

#
I'm now in my end world and want to add
    Player packman = getObjects(>Player<.class).get(0);  
    if (coinsCollected < packman.getCoinsCollected()) {  
        coinsCollected = packman.getCoinsCollected();  
        coinsCollectedText.setText("You have collected "+coinsCollected+"/50 coins");  
    }  
(My player is named Player) but when i click compile an error appears : "Illegal start of Type" and the place in front of the if is marked in red.
SPower SPower

2013/8/14

#
Change this:
Player packman = getObjects(>Player<.class).get(0);
into this:
Player packman = getObjects(Player.class).get(0); 
You also has to replace >Player< inside the ()
ich99 ich99

2013/8/14

#
Okay i now have this :
  Player packman = getObjects(Player.class).get(0);
   if (coinsCollected < packman.getCoinsCollected()) {
     
    coinsCollected = packman.getCoinsCollected();  
    coinsCollectedText.setText("You have collected "+coinsCollected+"/50 coins");  
}  
but the problem is the same.
SPower SPower

2013/8/14

#
Oh, I see. Try this:
Player packman = (Player) getObjects(Player.class).get(0);
ich99 ich99

2013/8/14

#
Hm the problem is there again. is it right that i am in the world where the code should be displayed after game? so ipost the whole code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class endlvl here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class endlvl extends World
{
    private Text coinsCollectedText;
    private int coinsCollected;
    /**
     * Constructor for objects of class endlvl.
     * 
     */
    
    public endlvl()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(10, 10, 50); 
        coinsCollectedText = new Text("You have collected 0/50 coins", 20);
        addObject(coinsCollectedText, 20, 20);
    }
        Player packman = (Player) getObjects(Player.class).get(0);  
   if (coinsCollected < packman.getCoinsCollected()) {
     
    coinsCollected = packman.getCoinsCollected();  
    coinsCollectedText.setText("You have collected "+coinsCollected+"/50 coins");  
}  
}  
i hope now we can find the problem :D
SPower SPower

2013/8/14

#
Well, first I advice to change it all to this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
/** 
 * Write a description of class endlvl here. 
 *  
 * @author (your name)  
 * @version (a version number or a date) 
 */  
public class endlvl extends World  
{  
    private Text coinsCollectedText;  
    private int coinsCollected = 0;
    
    /** 
     * Constructor for objects of class endlvl. 
     *  
     */
    public endlvl()
    {
        super(10, 10, 50);   
        coinsCollectedText = new Text("You have collected 0/50 coins", 20);  
        addObject(coinsCollectedText, 20, 20);  
    }
    
    public void act()
    {
        Player packman = (Player) getObjects(Player.class).get(0);
        if (coinsCollected < packman.getCoinsCollected()) {
            coinsCollected = packman.getCoinsCollected();
            coinsCollectedText.setText("You have collected "+coinsCollected+"/50 coins");
        }
    }
}
Then I'll look at it later on, since I've got to do something now. Sorry!
ich99 ich99

2013/8/14

#
Okay Thanks for your help
SPower SPower

2013/8/14

#
Add this to the very top:
import java.util.List; // imports the List class used by getObjects
And just for verification, is your packman class really called 'Player', I mean: starting with an uppercase and then only lower cases?
There are more replies on the next page.
1
2
3