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

#
I removed the first words in the two lines you mentioned, and true from the other part of the code, but now it says ( or 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 = new Frog(label); addObject(frog, 100, 100); 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, getWidth()/2, getHeight()/2); Greenfoot.stop(); return; } if(frog.acount < snake.bcount) { SnakeWins snakewins= new SnakeWins(); addObject(new SnakeWins, getWidth()/2, getHeight()/2); Greenfoot.stop(); return; } } }
danpost danpost

2019/2/24

#
MTelesca wrote...
I removed the first words in the two lines you mentioned, and true from the other part of the code, but now it says ( or <open square bracket > expected next to the comma after SnakeWins and FrogWins, but when I put a bracket or parenthesis there it does not get fixed? What should I do instead?
You need to keep the parentheses -- just not with the true in them. What error do you then get?
MTelesca MTelesca

2019/2/24

#
I did what you said and it fixed the code, but now whenever one actor touches a fly, the game ends and the greenfoot logo shows up in the center of the screen.
danpost danpost

2019/2/24

#
You need to give your FrogWins and SnakeWins classes default images or create images for them in their constructor blocks. Also, you need to place a condition on your count comparisons, in that no Fly object can be in the world.
danpost danpost

2019/2/24

#
Currently, this is your act method:
public void act()
{
    if (getObjects(fly.class).isEmpty())
    {
        Greenfoot.stop();
    }
    if(frog.acount > snake.bcount) 
    {
        FrogWins frogwins= new FrogWins();
        addObject(new FrogWins, getWidth()/2, getHeight()/2);
        Greenfoot.stop();
        return;
    }
    if(frog.acount < snake.bcount) 
    {
        SnakeWins snakewins= new SnakeWins();
        addObject(new SnakeWins, getWidth()/2, getHeight()/2);
        Greenfoot.stop();
        return;
    }
}
Change it to:
public void act()
{
    if (getObjects(fly.class).isEmpty())
    {
        Actor winObject = null;
        if (frog.acount > snake.bcount)
        {
            winObject = new FrogWins();
        }
        else
        {
            winObject = new SnakeWins();
        }
        addObject(winObject, getWidth()/2, getHeight()/2);
        Greenfoot.stop();
    }
}
MTelesca MTelesca

2019/2/24

#
This worked. Thank you.
You need to login to post a reply.
1
2