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):
His lifebar (GoodCounter):
His bullet (ShotOne):
I hope my question is understandable and I didn't mess up the code completely^^
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;
}
}
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));
}
}
}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);
}
}
