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

2020/4/22

How to make person with the highest score win?

1
2
3
HairyChickens HairyChickens

2020/4/22

#
So for my school project, I have two players and I want the player with the highest score to win. In my current code I have it so that if a player collects more 10 "WaterBlobs" they win, but I would like to change it so that the player with the highest score wins. My code for player 2 is the exact same besides the addObject and showText. If this is possible to do please let me know with an example on how to code it. Thanks :)
public class Player_1 extends Actor
{
    private int score;
    collectWaterBlob();

    public void act() 
    {
        detectPluto();
    }
    public void detectPluto()
    {
        if (score > 9 && isTouching(Pluto.class))
        {
            World world = getWorld();
            world.addObject(new Player_1_Wins(), world.getWidth()/2, world.getHeight()/2);
            Greenfoot.playSound("Victory.mp3");
            Greenfoot.stop();
        }
    }
    public void collectWaterBlob()
    {
        if (isTouching(WaterBlob.class))
        {
            removeTouching(WaterBlob.class);
            Greenfoot.playSound("drip.mp3");
            increaseScore();
            World world = getWorld();
            world.addObject(new WaterBlob(), Greenfoot.getRandomNumber(1690), Greenfoot.getRandomNumber(900));
    }
    public void increaseScore()
    {
        score++;
        showStatus();
    }
    public void showStatus()
    {
        getWorld().showText("Player 1 Score : "+score, 93,865);
    }
}
danpost danpost

2020/4/22

#
What would it matter if you still had to touch a Pluto object to win? You will need to explain exactly what you want throughout, in detail.
HairyChickens HairyChickens

2020/4/23

#
danpost wrote...
What would it matter if you still had to touch a Pluto object to win? You will need to explain exactly what you want throughout, in detail.
Yes, so there are WaterBlobs in which the two players have to collect. I want to change my code so that the person who collects the most water blobs and touches pluto wins. However if they get hit by an asteriod or if the two players collide with each other their score will reset.(I have already coded this). Players will need to collect as much WaterBlobs as they can in the 60 secs (3600) timer. Please let me know if you need more clarification.
HairyChickens HairyChickens

2020/4/23

#
danpost wrote...
What would it matter if you still had to touch a Pluto object to win? You will need to explain exactly what you want throughout, in detail.
public class Pluto extends Actor
{
    GreenfootSound backgroundMusic = new GreenfootSound("background.mp3");
    
    /**
     * Act - do whatever the Pluto wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        detectWaterBlob();
        backgroundMusic.playLoop();
    }
    
        public void detectWaterBlob()
    {
        if (isTouching(WaterBlob.class))
        {
            removeTouching(WaterBlob.class);
            World world = getWorld();
            world.addObject(new WaterBlob(), Greenfoot.getRandomNumber(1690), Greenfoot.getRandomNumber(900));
        }
    }
}
Heres my pluto code incase you need it or if I need to add anything to it.
danpost danpost

2020/4/23

#
HairyChickens wrote...
Yes, so there are WaterBlobs in which the two players have to collect. I want to change my code so that the person who collects the most water blobs and touches pluto wins. However if they get hit by an asteriod or if the two players collide with each other their score will reset.(I have already coded this). Players will need to collect as much WaterBlobs as they can in the 60 secs (3600) timer. Please let me know if you need more clarification.
I guess I am confused because there are so many different situations that can occur: (1) neither player touches pluto within the 60 secs.; (2) player with less collected touches pluto and the other doesn't within the 60 secs.; (3) what if one collects just 1 and touches pluto -- is that an instant win? If so, what is the purpose of collecting as many as you can? I guess my problem is that it seems like you are dancing between multiple ways the game can end and it doesn't seem like it was thought out carefully enough. I think, if I was doing something along these lines, I would let the game run the full 60 secs. However, I would only count the blobs collected and only score them when pluto is finally touched. The player can then go back and collect more and take them to pluto to add to the score, as long as time remains. Player with highest score at timer end wins.
HairyChickens HairyChickens

2020/4/23

#
danpost wrote...
HairyChickens wrote...
Yes, so there are WaterBlobs in which the two players have to collect. I want to change my code so that the person who collects the most water blobs and touches pluto wins. However if they get hit by an asteriod or if the two players collide with each other their score will reset.(I have already coded this). Players will need to collect as much WaterBlobs as they can in the 60 secs (3600) timer. Please let me know if you need more clarification.
I guess I am confused because there are so many different situations that can occur: (1) neither player touches pluto within the 60 secs.; (2) player with less collected touches pluto and the other doesn't within the 60 secs.; (3) what if one collects just 1 and touches pluto -- is that an instant win? If so, what is the purpose of collecting as many as you can? I guess my problem is that it seems like you are dancing between multiple ways the game can end and it doesn't seem like it was thought out carefully enough. I think, if I was doing something along these lines, I would let the game run the full 60 secs. However, I would only count the blobs collected and only score them when pluto is finally touched. The player can then go back and collect more and take them to pluto to add to the score, as long as time remains. Player with highest score at timer end wins.
My original idea was to do what you suggested in your second paragraph but I think the reason why I have created so many situations is because of my limited knowledge in coding lol. Can you give me an example as to how I can code that?
danpost danpost

2020/4/23

#
HairyChickens wrote...
My original idea was to do what you suggested in your second paragraph but I think the reason why I have created so many situations is because of my limited knowledge in coding lol. Can you give me an example as to how I can code that?
Start by adding an int blobsCollected field to each of the player classes. Then work on their methods. The value of the int score fields of the players need to be made publicly accessible (either by making them public or by adding a public getScore method).
HairyChickens HairyChickens

2020/4/23

#
danpost wrote...
HairyChickens wrote...
My original idea was to do what you suggested in your second paragraph but I think the reason why I have created so many situations is because of my limited knowledge in coding lol. Can you give me an example as to how I can code that?
Start by adding an int blobsCollected field to each of the player classes. Then work on their methods. The value of the int score fields of the players need to be made publicly accessible (either by making them public or by adding a public getScore method).
Do you mind typing the actual code that I need for this to work because I don't really understand how to do what your saying? Sorry
danpost danpost

2020/4/23

#
HairyChickens wrote...
Do you mind typing the actual code that I need for this to work because I don't really understand how to do what your saying? Sorry
Maybe you should visit the Java tutorials. The first two trails would be the place to start.
HairyChickens HairyChickens

2020/4/23

#
danpost wrote...
HairyChickens wrote...
My original idea was to do what you suggested in your second paragraph but I think the reason why I have created so many situations is because of my limited knowledge in coding lol. Can you give me an example as to how I can code that?
Start by adding an int blobsCollected field to each of the player classes. Then work on their methods. The value of the int score fields of the players need to be made publicly accessible (either by making them public or by adding a public getScore method).
Yep I should have that done, whats next
danpost danpost

2020/4/23

#
HairyChickens wrote...
Yep I should have that done, whats next
Add a method in your subclass of World that compares the players scores when the timer hits zero (I presume the timer is in your World subclass).
HairyChickens HairyChickens

2020/4/23

#
danpost wrote...
HairyChickens wrote...
Yep I should have that done, whats next
Add a method in your subclass of World that compares the players scores when the timer hits zero (I presume the timer is in your World subclass).
No the timer is in the scoreboard actor subclass. Also Id prefer if you tell me everything that I should do at once. Makes things easier if that's ok with you.
danpost danpost

2020/4/23

#
HairyChickens wrote...
No the timer is in the scoreboard actor subclass. Also Id prefer if you tell me everything that I should do at once. Makes things easier if that's ok with you.
Well, with references to the timer and both players in your world, an act method can compare scores when time is up.
HairyChickens HairyChickens

2020/4/23

#
Ok so I placed the timer in my World, "GameWorld". Can you give me an example on how I can code the act method that compares scores. And also I know that there is this code where it checks where if a number is higher then the other eg. if player 1 score > player 2 score && istouching pluto player 1 wins something like that. If I put that along with everything you have told me is that everything I need to do in order for it to work. (Also I'm not sure what the exact code for that is, it would be great if you reminded me what it was)
danpost danpost

2020/4/23

#
HairyChickens wrote...
Ok so I placed the timer in my World, "GameWorld".
Do you mean you now have a field in your GameWorld class that retains a reference to the Timer object:
private Timer timer; // = new Timer(); ??
Can you give me an example on how I can code the act method that compares scores.
Do you have fields in your GameWorld class retaining references to the players?
I know that there is this code where it checks where if a number is higher then the other eg. if player 1 score > player 2 score && istouching pluto player 1 wins something like that. If I put that along with everything you have told me is that everything I need to do in order for it to work.
I thought you were dispensing with that code.
There are more replies on the next page.
1
2
3