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

2012/6/14

Problem with Counter and Text

Prudene Prudene

2012/6/14

#
After putting a lot of effort in it, I finally managed to create a little game for two players with two controllable rockets that can shoot and two lifebars which star with 100 points and lose 5 points, each time a rocket gets shot. I want the game to show a "PLAYER1 WINS" message, if the lifebar of Player2 is at 0 and "PLAYER 2 WINS" if the other lifebar is at 0. I tried a lot now, but I just can't figure out how to do this and the deadline for the school project is tomorrow, so a quick response would be nice :) Here are the codes I made up: Player1 (GoodAgent):
import greenfoot.*;
public class GoodAgent extends Actor
{
    private Ex myWorld;
    private ShotOne gun;
    private int timesShot = 0;//the number of times shot 
    public void act() 
    {
        if(Greenfoot.isKeyDown("up"))move(8);
        if(Greenfoot.isKeyDown("down"))move(-8);
        if(Greenfoot.isKeyDown("left"))turn(-8);
        if(Greenfoot.isKeyDown("right"))turn(8);
        if(Greenfoot.isKeyDown("."))
        {
            //you only want the gun to fire on a specific shot (when you shoot once)
            timesShot++;//After act is called twice timesShot = 2 
            //and the action of shooting will not happen if you are holding space
            //(the gun will fire once when pressing space instead of firing multiple times and dragging)
            if(timesShot == 1)
            {
                gun  = new ShotOne();
                gun.setRotation(getRotation());
                myWorld.addObject(gun,this.getX(), this.getY());
                gun.move(55);
            }
        }
        //to get it to shoot again if you repress the space bar, a else statment resets timesShot
        //whenever the space is not pressed
        else {timesShot = 0;}
    }    
    public void addedToWorld(World world)
    {
        myWorld = (Ex)world;
    }
   
}
His lifebar (GoodCounter):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


public class GoodCounter  extends Actor
{
    private int totalCount = 100;

    public GoodCounter()
    {
        setImage(new GreenfootImage("100", 50, Color.ORANGE, Color.BLACK));
    }
public void bumpCount(int amount)
{
{
    totalCount += amount;
    setImage(new GreenfootImage("" + totalCount, 50, Color.ORANGE, Color.BLACK));
}
}
}
His bullet (ShotOne):
import greenfoot.*;  
public class ShotOne extends Actor
{
    private Ex myWorld;
    public void act() 
    {
        move(15);
    {
        Actor evilagent;
       evilagent = getOneObjectAtOffset(0, 0, EvilAgent.class);
        if (evilagent != null)
        {
            hitEvilAgent();
            World world;
            world = getWorld();
            world.removeObject(this);
        }
        else if(this.getX() < 1 || this.getX() == 599 || this.getY() < 1 || this.getY() == 399)
        //if this is at the worlds end then remove it
        {
            myWorld.removeObject(this);
        }
    }
    }    
    public void addedToWorld(World world)
    {
        myWorld = (Ex)world;
    } 
   private void hitEvilAgent()
   {
       Ex exWorld = (Ex) getWorld();
       EvilCounter evilcounter = exWorld.getEvilCounter();
       evilcounter.bumpCount(-5);
    }
}

I hope my question is understandable and I didn't mess up the code completely^^
Prudene Prudene

2012/6/14

#
Oh and if it's not clear: EvilAgent, EvilCounter and ShotTwo are the classes for the 2nd player and have the same codes, only reversed.
danpost danpost

2012/6/14

#
Create new sub-classes of Actor and name then 'GoodMessage' and 'EvilMessage'. In itheir constructors, create GreenfootImages, either with text or draw text on it. You could also use another program to create the images showing the text an import the images (pre-setting them to the actor classes). You can now create and add the message objects to the screen when a counter reaches 0.
You need to login to post a reply.