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/23

#
How to I reference my timer and players and what did you mean by dispensing with that code. I have my code for my timer. Its in my scorepanel actor subclass
public class Scorepanel extends Actor
{
    private int timer;

    public Scorepanel()
    {
        timer = 3601;
    }

    /**
     * Act - do whatever the Scorepanel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        updateTimer();
    }

    public void updateTimer() 
    {
        timer--;
        getWorld().showText("Time Left : "+timer, 270,875);
        if (timer == 0) 
        {
            World world = getWorld();
            world.addObject(new Gameover(), world.getWidth()/2, world.getHeight()/2);
            Greenfoot.playSound("gameover.mp3");
            Greenfoot.stop();
        }
    }

    protected void addedToWorld(World world)
    {
        updateTimer();
    }
}
danpost danpost

2020/4/23

#
Will respond later (if no one else does before I get back). Gotta' go right now. Sorry.
HairyChickens HairyChickens

2020/4/23

#
danpost wrote...
Will respond later (if no one else does before I get back). Gotta' go right now. Sorry.
All good
danpost danpost

2020/4/24

#
HairyChickens wrote...
How to I reference my timer and players and what did you mean by dispensing with that code. I have my code for my timer. Its in my scorepanel actor subclass
Will need to see your World subclass codes.
HairyChickens HairyChickens

2020/4/24

#
Here it is
public class GameWorld extends World
{
    
    /**
     * Constructor for objects of class GameWorld.
     * 
     */
    public GameWorld()
    {    
        // Create a new world with 1690x900 cells with a cell size of 1x1 pixels.
        super(1650, 900, 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()
    {
        Pluto pluto = new Pluto();
        addObject(pluto,69,72);
        Earth earth2 = new Earth();
        addObject(earth2,1562,826);
        Player_1 player_1 = new Player_1();
        player_1.turn(-90);
        addObject(player_1,1510,820);
        Player_2 player_2 = new Player_2();
        player_2.turn(-90);
        addObject(player_2,1621,820);
        Asteroid asteroid = new Asteroid();
        addObject(asteroid,344,223);
        Asteroid asteroid2 = new Asteroid();
        addObject(asteroid2,746,91);
        Asteroid asteroid3 = new Asteroid();
        addObject(asteroid3,646,305);
        Asteroid asteroid4 = new Asteroid();
        addObject(asteroid4,269,582);
        Asteroid asteroid5 = new Asteroid();
        addObject(asteroid5,216,394);
        Asteroid asteroid6 = new Asteroid();
        addObject(asteroid6,621,521);
        Asteroid asteroid7 = new Asteroid();
        addObject(asteroid7,392,787);
        Asteroid asteroid8 = new Asteroid();
        addObject(asteroid8,925,725);
        Asteroid asteroid9 = new Asteroid();
        addObject(asteroid9,1009,496);
        Asteroid asteroid10 = new Asteroid();
        addObject(asteroid10,906,317);
        Asteroid asteroid11 = new Asteroid();
        addObject(asteroid11,1209,69);
        Asteroid asteroid12 = new Asteroid();
        addObject(asteroid12,1566,254);
        Asteroid asteroid13 = new Asteroid();
        addObject(asteroid13,620,783);
        Asteroid asteroid14 = new Asteroid();
        addObject(asteroid14,1253,285);
        removeObject(asteroid5);
        asteroid.setLocation(231,292);
        WaterBlob waterblob = new WaterBlob();
        addObject(waterblob,811,675);
        WaterBlob waterblob2 = new WaterBlob();
        addObject(waterblob2,770,387);
        WaterBlob waterblob3 = new WaterBlob();
        addObject(waterblob3,376,417);
        WaterBlob waterblob4 = new WaterBlob();
        addObject(waterblob4,135,754);
        WaterBlob waterblob5 = new WaterBlob();
        addObject(waterblob5,384,89);
        WaterBlob waterblob6 = new WaterBlob();
        addObject(waterblob6,966,91);
        Scorepanel scorepanel = new Scorepanel();
        addObject(scorepanel,315,842);
        Alien alien = new Alien();
        addObject(alien,55,55);
        alien.setLocation(70,66);
        WaterBlob waterblob7 = new WaterBlob();
        addObject(waterblob7,1378,393);
        WaterBlob waterblob8 = new WaterBlob();
        addObject(waterblob8,1327,610);
        scorepanel.setLocation(316,868);
        scorepanel.setLocation(316,879);
        scorepanel.setLocation(309,886);
        scorepanel.setLocation(235,885);
    }

}
HairyChickens HairyChickens

2020/4/24

#
Also I think it would be better to try and make the code below to work for both players
        if (player2score > player_1.player1score && isTouching(Pluto.class))
   {
    World world = getWorld();
            world.addObject(new Player_2_Wins(), world.getWidth()/2, world.getHeight()/2);
            Greenfoot.playSound("Victory.mp3");
            Greenfoot.stop();
        }
    }
and leave out the timer because I have it so when the timer ends greenfoot stops and the game is over so i think it isnt necessary compare the player's scores when the timer ends
danpost danpost

2020/4/24

#
Remove lines 73-74 and 82-85; then delete the Scorepanel class. This should remove the timer. Next, with lines 25 and 28, remove only the first word from each. Then, at line 3, add 2 lines beginning with "private " and followed by the first two original words from the two lines. This will have the world retain references to the players . Finally, add two public method called plutoTouched1 and plutoTouched2. These will be called by the respective player when found touching pluto. The retained references will be used access the player actors, so that the scores can be compared.
HairyChickens HairyChickens

2020/4/24

#
danpost wrote...
Remove lines 73-74 and 82-85; then delete the Scorepanel class. This should remove the timer. Next, with lines 25 and 28, remove only the first word from each. Then, at line 3, add 2 lines beginning with "private " and followed by the first two original words from the two lines. This will have the world retain references to the players . Finally, add two public method called plutoTouched1 and plutoTouched2. These will be called by the respective player when found touching pluto. The retained references will be used access the player actors, so that the scores can be compared.
public class GameWorld extends World
{
    private Player_1;
    private Player_2;
    
I typed private and the two names but it says <identifier> expected when i combile
HairyChickens HairyChickens

2020/4/24

#
danpost wrote...
Finally, add two public method called plutoTouched1 and plutoTouched2. These will be called by the respective player when found touching pluto. The retained references will be used access the player actors, so that the scores can be compared.
Also you told me to add a plutoTouched method to each player do i just replace my detectPluto method that i orignally had from both players to that. See code below
public void act() 
    {
        playerTouched1();
    }

    public void plutoTouched1()
    {
        if (score > 9 && isTouching(Pluto.class)) // this was me experimenting with trying to get this to work will replace with the player1score > player_2.player2score line
        {
            World world = getWorld();
            world.addObject(new Player_1_Wins(), world.getWidth()/2, world.getHeight()/2);
            Greenfoot.playSound("Victory.mp3");
            Greenfoot.stop();
        }
    }
danpost danpost

2020/4/24

#
HairyChickens wrote...
I typed private and the two names but it says <identifier> expected when i combile
danpost wrote...
"private " .. followed by the first two original words from the two lines.
Also you told me to add a plutoTouched method to each player do i just replace my detectPluto method that i orignally had from both players to that. See code below << Code Omitted
plutoTouched1 and plutoTouched2 belong in your GameWorld class (obviously with code modified). Remove them from player classes and change act method in Player_1 class with:
public void act()
{
    if (isTouching(Pluto.class))
    {
        ((GameWorld)getWorld()).plutoTouched1();
    }
}
Do similarly in Player_2 class.
HairyChickens HairyChickens

2020/4/24

#
public class GameWorld extends World
{
    private Player_1;
    private Player_2;
ok done that but im still getting the <identifier> expected thing with the two private player_1 and player_2
danpost danpost

2020/4/24

#
HairyChickens wrote...
ok done that but im still getting the <identifier> expected thing with the two private player_1 and player_2
I certainly hope you can count to two. Read my quote again, word for word. Or, maybe I need to say "... from each of the two lines."
HairyChickens HairyChickens

2020/4/24

#
That was embrassing...... but now its showing a "cannot find symbol - variable (bolded below) GameWorld code: public void plutoTouched1() { if (player1score > player_2.player2score && 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 plutoTouched2() { if (player2score > player_1.player1score && isTouching(Pluto.class)) { World world = getWorld(); world.addObject(new Player_2_Wins(), world.getWidth()/2, world.getHeight()/2); Greenfoot.playSound("Victory.mp3"); Greenfoot.stop(); } } Player 1 code: public void act() { rocketMovement(); detectPlayer_2Collision(); playerTouched1(); detectAsteroidCollision(); collectWaterBlob(); if (isTouching(Pluto.class)) { ((GameWorld)getWorld()).plutoTouched1(); } } Player 2 code: public void act() { rocketMovement(); detectPlayer_1Collision(); plutoTouched2(); detectAsteroidCollision(); collectWaterBlob(); if (isTouching(Pluto.class)) { ((GameWorld)getWorld()).plutoTouched2(); } } am i missing something?
danpost danpost

2020/4/24

#
Remove bold lines in player classes. Plus, I did say those methods in the world will need some modifications.
HairyChickens HairyChickens

2020/4/24

#
What do I need to modify it to? And i just remove plutoTouched1 and plutoTouched2 from the players correct?
There are more replies on the next page.
1
2
3