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

2019/2/20

Declaring a winner out of two players

1
2
MTelesca MTelesca

2019/2/20

#
I have a game where it ends when all five of one actor is gone, and there are two characters competing to eat the actor. There is a individual score counter for both characters, but i want to make it so when the game stops(all of the actor is gone), whichever character has a high score has a win screen that declares them specifically the winner.
danpost danpost

2019/2/20

#
MTelesca wrote...
I have a game where it ends when all five of one actor is gone, and there are two characters competing to eat the actor. There is a individual score counter for both characters, but i want to make it so when the game stops(all of the actor is gone), whichever character has a high score has a win screen that declares them specifically the winner.
Keep a reference to both players in the world class. Make sure the scores are accessible to the world (either by a getter method or directly -- that is, something public).
MTelesca MTelesca

2019/2/21

#
how would I keep a reference to both players in the world class? and what code would I use to make it specific to one winner, rather than to both?
danpost danpost

2019/2/21

#
MTelesca wrote...
how would I keep a reference to both players in the world class? and what code would I use to make it specific to one winner, rather than to both?
Show the code to your subclass of World that has the players in it. It would be good to add some attempted code to specifically declare one player as the winner.
MTelesca MTelesca

2019/2/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class paper here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class paper extends World
{

    /**
     * Constructor for objects of class paper.
     * 
     */
    public paper()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
        if (getObjects(fly.class).isEmpty())
        Greenfoot.stop();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    
    private void prepare()
    {

        if (getObjects(fly.class).isEmpty())
            Greenfoot.stop();
        fly fly = new fly();
        addObject(fly,368,204);
        fly fly2 = new fly();
        addObject(fly2,473,123);
        fly fly3 = new fly();
        addObject(fly3,467,347);
        fly fly4 = new fly();
        addObject(fly4,170,101);
        fly fly5 = new fly();
        addObject(fly5,216,333);
        Label label = new Label("Frog: 0");
        Marker marker= new Marker("Snake: 0");
        Frog frog = new Frog(label);
        addObject(frog, 100, 100);
        Snake snake = new Snake(marker);
        addObject(snake,76,150);
        frog.setLocation(77,223);
        frog.setLocation(69,245);
        addObject(marker,571,21);
        marker.setLocation(563,23);
        marker.setLocation(576,14);
        marker.setLocation(528,23);
        marker.setLocation(573,69);
        marker.setLocation(555,18);
        marker.setLocation(569,15);
        marker.setLocation(559,15);
        marker.setLocation(541,20);
        addObject(label,51,36);
        label.setLocation(63,21);
        marker.setLocation(531,17);
        marker.setLocation(544,19);
        label.setLocation(26,13);
        label.setLocation(52,12);
        marker.setLocation(550,20);
        label.setLocation(96,21);
        marker.setLocation(562,17);
        marker.setLocation(562,26);
        fly.setLocation(328,193);
        snake.setLocation(48,57);
        frog.setLocation(38,359);
    }

    public void act()
    {
        if (getObjects(fly.class).isEmpty())
        {
            Greenfoot.stop();
        }
    }
}
this is the code to my wolrd(paper)
danpost danpost

2019/2/22

#
MTelesca wrote...
<< Code Omitted >> this is the code to my wolrd(paper)
Insert at line 11 the following: Then remove the first word in both lines 47 and 49. With those changes, you now have references to both players within the class. You can remove lines 21 and 22,, as well as lines 33 and 34, which both only execute during world creation. Insert code about line 81 to compare players' scores and show the appropriate win message.
MTelesca MTelesca

2019/2/22

#
What code would I be inserting about line 81 to compare their scores and show an appropriate winner message?
danpost danpost

2019/2/22

#
MTelesca wrote...
What code would I be inserting about line 81 to compare their scores and show an appropriate winner message?
That would be impossible for me to say without seeing your Frog and Snake classes. Apparently, I had an invalid code tag above. Should have read as:
Insert at line 11 the following:
private Frog frog;
private Snake snake;
MTelesca MTelesca

2019/2/22

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Frog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Frog extends Actor
{
    private Label myLabel;
    public int acount= 0;
    public Frog(Label label)
    {
        myLabel = label;
    }

    
    /**
     * Act - do whatever the Frog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int foodAmount;
    public void act() 
    {
      
      
      if(isTouching(fly.class))    
        {           
            foodAmount=foodAmount+1;
            acount++;
            removeTouching(fly.class);
            myLabel.setText("Frog: " + acount);

            
            
        }
     
      if(Greenfoot.isKeyDown("down"))
            move(-3);
      if(Greenfoot.isKeyDown("up"))
            move(3);
      if(Greenfoot.isKeyDown("left")) 
            turn(-3);
      if(Greenfoot.isKeyDown("right")) 
            turn(3);
            
        
            
        
        
        
        
        
        
        // Add your action code here.
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Snake extends Actor
{
    public int bcount= 0;
    private Marker myMarker;
    public Snake(Marker marker)
    {
        myMarker = marker;
    }
    
    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int foodAmount;
    public void act() 
    {
      //foodAmount=0;  
      
      if(isTouching(fly.class))    
        {           
            foodAmount=foodAmount+1;
            bcount++;
            removeTouching(fly.class);
            myMarker.setText("Count: " + bcount);
            
            
        }
      
      if(Greenfoot.isKeyDown("S"))
            move(-4);
      if(Greenfoot.isKeyDown("W"))
            move(4);
      if(Greenfoot.isKeyDown("A")) 
            turn(-4);
      if(Greenfoot.isKeyDown("D")) 
            turn(4);
            
        
            
        
        
        
        
        
        
        // Add your action code here.
    }    
}
These are the two codes for my Frog and Snake.
danpost danpost

2019/2/22

#
MTelesca wrote...
<< Code Omitted >> << Code Omitted >> These are the two codes for my Frog and Snake.
You should be able to compare 'frog.acount' against 'snake.bcount' with no problem.
MTelesca MTelesca

2019/2/22

#
How do I compare them?
danpost danpost

2019/2/22

#
MTelesca wrote...
How do I compare them?
if (frog.acount == snake.bcount) ...
else if (frog.acount > snake.b count) ...
else ...
(obviously, the only other possibility , for the last else part, is if frog.acount is less than snake.bcount).
MTelesca MTelesca

2019/2/24

#
what is the code that I would use for the win screen?
MTelesca MTelesca

2019/2/24

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class paper here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class paper extends World
{
    private Frog frog;
    private Snake snake;
    /**
     * Constructor for objects of class paper.
     * 
     */
    public paper()
    {    
        // 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()
    {

        
        fly fly = new fly();
        addObject(fly,368,204);
        fly fly2 = new fly();
        addObject(fly2,473,123);
        fly fly3 = new fly();
        addObject(fly3,467,347);
        fly fly4 = new fly();
        addObject(fly4,170,101);
        fly fly5 = new fly();
        addObject(fly5,216,333);
        Label label = new Label("Frog: 0");
        Marker marker= new Marker("Snake: 0");
        Frog frog = new Frog(label);
        addObject(frog, 100, 100);
        Snake snake = new Snake(marker);
        addObject(snake,76,150);
        frog.setLocation(77,223);
        frog.setLocation(69,245);
        addObject(marker,571,21);
        marker.setLocation(563,23);
        marker.setLocation(576,14);
        marker.setLocation(528,23);
        marker.setLocation(573,69);
        marker.setLocation(555,18);
        marker.setLocation(569,15);
        marker.setLocation(559,15);
        marker.setLocation(541,20);
        addObject(label,51,36);
        label.setLocation(63,21);
        marker.setLocation(531,17);
        marker.setLocation(544,19);
        label.setLocation(26,13);
        label.setLocation(52,12);
        marker.setLocation(550,20);
        label.setLocation(96,21);
        marker.setLocation(562,17);
        marker.setLocation(562,26);
        fly.setLocation(328,193);
        snake.setLocation(48,57);
        frog.setLocation(38,359);
    }

    public void act()
    {
        if (getObjects(fly.class).isEmpty())
        {
            Greenfoot.stop();
        }
        if(frog.acount > snake.bcount) 
        {
            FrogWins frogwins= new FrogWins();
            addObject(new FrogWins(true), getWidth()/2, getHeight()/2);
            Greenfoot.stop();
            return;
        }
        if(frog.acount < snake.bcount) 
        {
            SnakeWins snakewins= new SnakeWins();
            addObject(new SnakeWins(true), getWidth()/2, getHeight()/2);
            Greenfoot.stop();
            return;
        }
    }
    
}
This is my current code in my world(paper). The code for the win screen is at the bottom of the code. I created new actors FrogWins and SnakeWins. But there is a red underline under the part of the code (new FrogWins(true) and (new SnakeWins(true). It says - required: no arguments, found: boolean, reason: actual and formal arguments different in length. How do I fix this? I current do not have any code in the SnakeWins and FrogWins subclass.
danpost danpost

2019/2/24

#
MTelesca wrote...
there is a red underline under the part of the code (new FrogWins(true) and (new SnakeWins(true). It says - required: no arguments, found: boolean, reason: actual and formal arguments different in length. How do I fix this?
Remove the word true from those lines. You have yet to:
danpost wrote...
remove the first word in both lines 47 and 49.
which now appear to be lines 46 and 48.
There are more replies on the next page.
1
2