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

2018/3/12

How to make a you win and you lose in a two player game(can someone help me!!!)

DawnLight DawnLight

2018/3/12

#
// (this is for ground)
public class ground extends World
{

    /**
     * Constructor for objects of class ground.
     * 
     */
    public ground()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
        drawScore();
    }
    
    private Red theRedScore;
    private Blue theBlueScore;
    public void drawScore()
     {
        theRedScore = new Red();
        addObject(theRedScore, 58,24);
        
        
        theBlueScore = new Blue();
        addObject(theBlueScore, 27,24);
    }
    
    public Red getRedScore()
    {
        return theRedScore;
    }
    
    public Blue getBlueScore()
    {
        return theBlueScore;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        cars cars = new cars();
        addObject(cars,1,84);
        Car car = new Car();
        addObject(car,2,323);
        Item item = new Item();
        addObject(item,106,220);
        Item item2 = new Item();
        addObject(item2,324,44);
        Item item3 = new Item();
        addObject(item3,346,373);
        Item item4 = new Item();
        addObject(item4,436,212);
        Item item5 = new Item();
        addObject(item5,574,36);
        Item item6 = new Item();
        addObject(item6,577,387);
        item3.setLocation(341,357);
        item3.setLocation(332,354);
        item6.setLocation(568,380);
        item4.setLocation(361,207);
        Item item7 = new Item();
        addObject(item7,464,127);
        item4.setLocation(465,276);
        item7.setLocation(451,125);
        item4.setLocation(457,277);
    }
}

//(This is the actor for this game)
public class cars extends Actor
{
    /**
     * Constructor for Car - nothing to do.
     */
    public void cars()
    {
        act();
        eat();
    }
    
    /**
     * Drive and allow steering.
     */
    public void act()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            turn(-2);
        }
        if ( Greenfoot.isKeyDown("right") )
        {
            turn(2);
        }
        if ( Greenfoot.isKeyDown("up") )
        {
            move(5);
      
        }
        if ( Greenfoot.isKeyDown("down") )
        {
            move(-4);
      
        }
        eat();
    }
     public void eat()
    {
        Actor item;
        item = getOneObjectAtOffset(0, 0, Item.class);
        if (item!= null)
        {
            ground ground;
            ground = (ground) getWorld();
            Blue bluecounter = ground.getBlueScore();
            ground.removeObject(item);
            bluecounter.bumpCount(1);
        }
    }
}
danpost danpost

2018/3/12

#
In a two player game, you do not have a "You Lose" -- it is either "Red Wins" or "Blue Wins" (or "Tied"). I will presume your condition for game over will be when all Item objects are gone from the world. So, add an act method to your 'ground' class and have it check to see if a list of Item objects is empty. If so, determine winner by comparing scores, display game over message and stop the scenario. You could also add a 'started' method to the class which checks for the message. If found, initiate a new ground world and set it active; if not, do nothing so that the already initialized game, or one that was paused is started, or restarted.
DawnLight DawnLight

2018/3/13

#
but how am i going to check if red or blue win??? i've been trying for a few days for now but i can't solve this problem.
danpost danpost

2018/3/13

#
DawnLight wrote...
but how am i going to check if red or blue win??? i've been trying for a few days for now but i can't solve this problem.
danpost wrote...
determine winner by comparing scores
You need to login to post a reply.