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

2014/4/21

Can't get powerup to appear on screen

1
2
3
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
Hi, i'm having trouble with my speed powerup not appearing on the screen, here is the code for my single player world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyGame here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyGame extends World
{
    Counter counter = new Counter();
 
    /**
     * Constructor for objects of class MyGame.
     * 
     */
    public MyGame()
    {    
        super(600, 600, 1); 
        prepare();
        Powerup pu = new Powerup(Greenfoot.getRandomNumber(Powerup.images.length));
    }
    
    public Counter getCounter()
    {
        return counter;
    }    
    
    public void prepare()
    {
        addObject(counter, 50, 50);
        addObject (new Starfighter (), 300, 520);
        addObject (new BugEnemy (), 50, 100);
        addObject (new BugEnemy (), 100,100);
        addObject (new BugEnemy (), 150,100);
        addObject (new BugEnemy (), 250,100);
        addObject (new BugEnemy (), 350,100);
        addObject (new BugEnemy (), 450,100);
        addObject (new BugEnemy (), 500,100);
        addObject (new BugEnemy (), 550,100);
        addObject (new BugEnemy (), 100,150);
        addObject (new BugEnemy (), 200,150);
        addObject (new BugEnemy (), 300,150);
        addObject (new BugEnemy (), 400,150);
        addObject (new BugEnemy (), 500,150);
        addObject (new InsectEnemy (), 100, 200);
        addObject (new InsectEnemy (), 200, 200);
        addObject (new InsectEnemy (), 300, 200);
        addObject (new InsectEnemy (), 400, 200);
        addObject (new InsectEnemy (), 500, 200);
        addObject (new Bee (), 140, 250);
        addObject (new Bee (), 240, 250);
        addObject (new Bee (), 340, 250);
        addObject (new Bee (), 440, 250);

    }     
}    
here is also the code for my universal powerup...
import greenfoot.*;  
  
public class Powerup extends Actor  
{  
    static final int SPEED_PU = 0; 
    static final String[] images = { "speedPU.png" }; 
  
    int puType; 
    int lifespan = 400; 
  

    public Powerup(int type)  
    {  
        puType = type;  
        setImage(images[puType]);  
    }  
  
    public void act()  
    {  
        lifespan--;  
        if (lifespan == 0) getWorld().removeObject(this);  
    }  
  
    public int getType()  
    {  
        return puType;  
    }  
}  
and the starfighter...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Starfighter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Starfighter extends Player
{
    private final int SPEED_BOOST_TIMER = 115;  
    private int SpeedBoostTimeLeft = SPEED_BOOST_TIMER;  
    private int Speed;  
    private boolean gotSpeedBoost = false; 
    private int x;
    private int y;

    public Starfighter()
    {
        Speed = 5;
    }    

    public void act() 
    {
        checkKeys();
        atWorldEdge(); 
        stopAtWalls(); 
        getSpeedBoost();  
        if (gotSpeedBoost)  
        {  
            speedBoostTimer();  
        }
    }

    public void checkKeys()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            move(-5);
        }
        if ( Greenfoot.isKeyDown("right") )
        {
            move(5);
        }   
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Fireball(getRotation()), getX(), getY());
            Greenfoot.playSound("fireball.wav");
        }    
    }

    public void stopAtWalls()
    {  
        if(getOneIntersectingObject(Wall.class)!=null)
        {
            setLocation(x,y);
        }
        else
        {
            x = getX();
            y = getY();
        }    
    }  

    public void getSpeedBoost()  
    {  
        Powerup pu = (Powerup)getOneObjectAtOffset(0, 0, Powerup.class);  
        if (pu != null)  
        {   int kind = pu.getType();  
            getWorld().removeObject(pu);  
            if (kind == Powerup.SPEED_PU) { Speed += 5; } 
        }
    }

    public void speedBoostTimer()  
    {  
        SpeedBoostTimeLeft--;  
        if (SpeedBoostTimeLeft <= 0)  
        {  
            gotSpeedBoost = false;  
            Speed -= 5;  
            SpeedBoostTimeLeft = SPEED_BOOST_TIMER;  
        }  
    }  
}    
i'm not sure if the powerup and starfighter would help but can someone help please
danpost danpost

2014/4/21

#
You are creating a Powerup object at line 21 in your MyGame world class. However, nowhere do I see where it is being added to the world. This is not something that just want to do right off. You need to regulate in a random fashion when it will appear in the world. You need an 'int' timer field set to a value within a range of values and an act method that will run the timer and spawn the Powerup object and reset the timer when it reaches zero. Remove line 21. Insert at line 12:
private int puTimerMin = 1000; // adjust as needed
private int puTimerMax = 1500; // adjust as needed
private int puRange = puTimerMax-puTimerMin;
private int puTimer;
Then add the following method within the class:
public void act()
{
    if (puTimer == 0) puTimer = puTimerMin+Greenfoot.getRandomNumber(puTimerRange);
    puTImer--;
    if (puTimer == 0) 
    {
        int puNum = Greenfoot.getRandomNumber(Powerup.images.length);
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new Powerup(puNum), x, y);
    }
}
The min and max given will produce a spawning between about 20 and 30 seconds. The x and y values can be altered to limit to area in which the spawning will occur.
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
danpost wrote...
You are creating a Powerup object at line 21 in your MyGame world class. However, nowhere do I see where it is being added to the world. This is not something that just want to do right off. You need to regulate in a random fashion when it will appear in the world. You need an 'int' timer field set to a value within a range of values and an act method that will run the timer and spawn the Powerup object and reset the timer when it reaches zero. Remove line 21. Insert at line 12:
private int puTimerMin = 1000; // adjust as needed
private int puTimerMax = 1500; // adjust as needed
private int puRange = puTimerMax-puTimerMin;
private int puTimer;
Then add the following method within the class:
public void act()
{
    if (puTimer == 0) puTimer = puTimerMin+Greenfoot.getRandomNumber(puTimerRange);
    puTImer--;
    if (puTimer == 0) 
    {
        int puNum = Greenfoot.getRandomNumber(Powerup.images.length);
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new Powerup(puNum), x, y);
    }
}
The min and max given will produce a spawning between about 20 and 30 seconds. The x and y values can be altered to limit to area in which the spawning will occur.
i did it but it still doesn't respawn for some reason, here is the new code...also i put the first part of the code private int puTimerMin...at the top cos when i put it in public MyGame() it states illegal start of expression
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyGame here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyGame extends World
{
    private int puTimerMin = 1000;   
    private int puTimerMax = 1500; 
    private int puTimerRange = puTimerMax-puTimerMin;  
    private int puTimer;
    Counter counter = new Counter();

    /**
     * Constructor for objects of class MyGame.
     * 
     */
    public MyGame()
    {    
        super(600, 600, 1); 
        prepare(); 
    }

    public void act()
    {
        if (puTimer == 0) puTimer = puTimerMin+Greenfoot.getRandomNumber(puTimerRange);  
        puTimer--;  
        if (puTimer == 0)   
        {  
            int puNum = Greenfoot.getRandomNumber(Powerup.images.length);  
            int x = Greenfoot.getRandomNumber(getWidth());  
            int y = Greenfoot.getRandomNumber(getHeight());  
            addObject(new Powerup(puNum), x, y);  
        }  
    }

    public Counter getCounter()
    {
        return counter;
    }    

    public void prepare()
    {
        addObject(counter, 50, 50);
        addObject (new Starfighter (), 300, 520);
        addObject (new BugEnemy (), 50, 100);
        addObject (new BugEnemy (), 100,100);
        addObject (new BugEnemy (), 150,100);
        addObject (new BugEnemy (), 250,100);
        addObject (new BugEnemy (), 350,100);
        addObject (new BugEnemy (), 450,100);
        addObject (new BugEnemy (), 500,100);
        addObject (new BugEnemy (), 550,100);
        addObject (new BugEnemy (), 100,150);
        addObject (new BugEnemy (), 200,150);
        addObject (new BugEnemy (), 300,150);
        addObject (new BugEnemy (), 400,150);
        addObject (new BugEnemy (), 500,150);
        addObject (new InsectEnemy (), 100, 200);
        addObject (new InsectEnemy (), 200, 200);
        addObject (new InsectEnemy (), 300, 200);
        addObject (new InsectEnemy (), 400, 200);
        addObject (new InsectEnemy (), 500, 200);
        addObject (new Bee (), 140, 250);
        addObject (new Bee (), 240, 250);
        addObject (new Bee (), 340, 250);
        addObject (new Bee (), 440, 250);
    }     
}    
danpost danpost

2014/4/21

#
GamesGrinder1998 wrote...
i did it but it still doesn't respawn for some reason, here is the new code...
Did you wait long enough? Change the values to: 'puTimerMin = 100' and 'puTimerMax = 150' and try again (you will have to re-adjust these values after testing). It should only take a few seconds between spawnings with these values.
also i put the first part of the code private int puTimerMin...at the top cos when i put it in public MyGame() it states illegal start of expression < Code Omitted >
Well, if you followed directions you would not have had that happen (I did say line 12 -- but 11, where you ended up putting it, is fine also).
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
danpost wrote...
GamesGrinder1998 wrote...
i did it but it still doesn't respawn for some reason, here is the new code...
Did you wait long enough? Change the values to: 'puTimerMin = 100' and 'puTimerMax = 150' and try again (you will have to re-adjust these values after testing). It should only take a few seconds between spawnings with these values.
also i put the first part of the code private int puTimerMin...at the top cos when i put it in public MyGame() it states illegal start of expression < Code Omitted >
Well, if you followed directions you would not have had that happen (I did say line 12 -- but 11, where you ended up putting it, is fine also).
thanks it works but it's my starfighter that collects it not the fireball (which just goes through the powerup). How can i fix that
danpost danpost

2014/4/21

#
I asked you about that two days ago in the other discussion. I wondered why you were putting the collision detection/remove of powerup/etc. in the Fireball class. You need to move everything related to the powerup from the Fireball class to the Starfighter class.
GamesGrinder1998 GamesGrinder1998

2014/4/21

#
danpost wrote...
I asked you about that two days ago in the other discussion. I wondered why you were putting the collision detection/remove of powerup/etc. in the Fireball class. You need to move everything related to the powerup from the Fireball class to the Starfighter class.
but i want the fireball to collect the powerup, not the starfighter. Though the starfighter to have the ability. So if i do what you told me to do "change everything related to the powerup..." will it do that?
danpost danpost

2014/4/21

#
Then the 'getSpeedBoost' method you have in the Starfighter class should be changed to only setting the 'gotSpeedBoost' value to 'true' and adding 5 to the speed; and it should do this unconditionally. The act method should not call 'getSpeedBoost'; but, the fireball that encounters the powerup should call it. The Fireball class will need to know what called the fireball into existence, so the fireball can inform the starfighter when the powerup is encountered. The Fireball class will need the detection and removal code for the powerup.
GamesGrinder1998 GamesGrinder1998

2014/4/22

#
danpost wrote...
Then the 'getSpeedBoost' method you have in the Starfighter class should be changed to only setting the 'gotSpeedBoost' value to 'true' and adding 5 to the speed; and it should do this unconditionally. The act method should not call 'getSpeedBoost'; but, the fireball that encounters the powerup should call it. The Fireball class will need to know what called the fireball into existence, so the fireball can inform the starfighter when the powerup is encountered. The Fireball class will need the detection and removal code for the powerup.
so what can i change and remove, here are the codes for both classes...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Starfighter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Starfighter extends Player
{
    private final int SPEED_BOOST_TIMER = 115;  
    private int SpeedBoostTimeLeft = SPEED_BOOST_TIMER;  
    private int Speed;  
    private boolean gotSpeedBoost = false; 
    private int x;
    private int y;

    public Starfighter()
    {
        Speed = 5;
    }    

    public void act() 
    {
        checkKeys();
        atWorldEdge(); 
        stopAtWalls(); 
        getSpeedBoost();  
        if (gotSpeedBoost)  
        {  
            speedBoostTimer();  
        }
    }

    public void checkKeys()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            move(-5);
        }
        if ( Greenfoot.isKeyDown("right") )
        {
            move(5);
        }   
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Fireball(getRotation()), getX(), getY());
            Greenfoot.playSound("fireball.wav");
        }    
    }

    public void stopAtWalls()
    {  
        if(getOneIntersectingObject(Wall.class)!=null)
        {
            setLocation(x,y);
        }
        else
        {
            x = getX();
            y = getY();
        }    
    }  

    public void getSpeedBoost()  
    {  
        Powerup pu = (Powerup)getOneObjectAtOffset(0, 0, Powerup.class);  
        if (pu != null)  
        {   int kind = pu.getType();  
            getWorld().removeObject(pu);  
            if (kind == Powerup.SPEED_PU) { Speed += 5; } 
        }
    }

    public void speedBoostTimer()  
    {  
        SpeedBoostTimeLeft--;  
        if (SpeedBoostTimeLeft <= 0)  
        {  
            gotSpeedBoost = false;  
            Speed -= 5;  
            SpeedBoostTimeLeft = SPEED_BOOST_TIMER;  
        }  
    }  
}    
Fireball....
import greenfoot.*;  

public class Fireball extends Actor
{
    private int direction, speed;  

    public Fireball(int dir)
    {
        direction = 270;
        speed = 7;
    }

    public void act() 
    {
        setRotation(direction);
        move(speed);
        disappear();
    }    

    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            int x = 100+Greenfoot.getRandomNumber(500);  
            int y = 100+Greenfoot.getRandomNumber(300);  
            BugEnemy.setLocation(x, y); 
            return;
        }    

        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            int x = 100+Greenfoot.getRandomNumber(500);  
            int y = 100+Greenfoot.getRandomNumber(300);  
            InsectEnemy.setLocation(x, y); 
            return;
        } 

        if (Bee != null)
        {
            World world;
            world = getWorld();
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            int x = 100+Greenfoot.getRandomNumber(500);  
            int y = 100+Greenfoot.getRandomNumber(300);  
            Bee.setLocation(x, y); 
            return;
        } 

        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        }  else {
            if (getY()<=0)
            {
                getWorld().removeObject(this);
                return;
            } 
        }    
    } 

     
}    
danpost danpost

2014/4/22

#
Change line 47 in the Starfighter class to:
getWorld().addObject(new Fireball(this), getX(), getY());
In the Fireball class: Back into the Starfighter class:
// change the 'getSpeedBoost' method to the following
public void getSpeedBoost()
{
    speed += 5;
    gotSpeedBoost = true;
}
Finally, remove line 28 from the Starfighter class (the call to 'getSpeedBoost' in the 'act' method)
GamesGrinder1998 GamesGrinder1998

2014/4/22

#
danpost wrote...
Change line 47 in the Starfighter class to:
getWorld().addObject(new Fireball(this), getX(), getY());
In the Fireball class: Back into the Starfighter class:
// change the 'getSpeedBoost' method to the following
public void getSpeedBoost()
{
    speed += 5;
    gotSpeedBoost = true;
}
Finally, remove line 28 from the Starfighter class (the call to 'getSpeedBoost' in the 'act' method)
so i dont add or change anything in the fireball?
danpost danpost

2014/4/22

#
Sorry, must have missed a 'code' tag. Let me quote it below (with the fix):
danpost wrote...
Change line 47 in the Starfighter class to:
getWorld().addObject(new Fireball(this), getX(), getY());
In the Fireball class:
// replace lines 5 through 18 with the following
private int direction = 270;
private int speed = 7;
private Starfighter starfighter;

public Fireball(Starfighter ship)
{
    starfighter = ship;
    setRotation(direction);
}

public void act()
{
    move(speed);
    checkSpeedPU();
    if (getWorld() == null) return;
    disappear();
}

private void checkSpeedPU()
{
    Powerup pu = getOneIntersectingObject(Powerup.class);
    if (pu == null) return;
    if (pu.getType() == Powerup.SpeedPU)
    {
        starfighter.getSpeedBoost();
        getWorld().removeObject(this);
    }
}
Back into the Starfighter class:
// change the 'getSpeedBoost' method to the following
public void getSpeedBoost()
{
    speed += 5;
    gotSpeedBoost = true;
}
Finally, remove line 28 from the Starfighter class (the call to 'getSpeedBoost' in the 'act' method)
GamesGrinder1998 GamesGrinder1998

2014/4/22

#
danpost wrote...
Sorry, must have missed a 'code' tag. Let me quote it below (with the fix):
danpost wrote...
Change line 47 in the Starfighter class to:
getWorld().addObject(new Fireball(this), getX(), getY());
In the Fireball class:
// replace lines 5 through 18 with the following
private int direction = 270;
private int speed = 7;
private Starfighter starfighter;

public Fireball(Starfighter ship)
{
    starfighter = ship;
    setRotation(direction);
}

public void act()
{
    move(speed);
    checkSpeedPU();
    if (getWorld() == null) return;
    disappear();
}

private void checkSpeedPU()
{
    Powerup pu = getOneIntersectingObject(Powerup.class);
    if (pu == null) return;
    if (pu.getType() == Powerup.SpeedPU)
    {
        starfighter.getSpeedBoost();
        getWorld().removeObject(this);
    }
}
Back into the Starfighter class:
// change the 'getSpeedBoost' method to the following
public void getSpeedBoost()
{
    speed += 5;
    gotSpeedBoost = true;
}
Finally, remove line 28 from the Starfighter class (the call to 'getSpeedBoost' in the 'act' method)
well in the fireball class, it highlights the "(Powerup.class)" and it states incompatible types, here is the edited code...
import greenfoot.*;  

public class Fireball extends Actor
{
    private int direction = 270;
    private int speed = 7;
    private Starfighter starfighter;
    
    public Fireball (Starfighter ship)
    {
        starfighter = ship;
        setRotation(direction);
    }    
    
    public void act()
    {
        move(speed);
        checkSpeedPU();
        if (getWorld() == null) return;
        disappear();
    }    
    
    private void checkSpeedPU()
    {
        Powerup pu = getOneIntersectingObject(Powerup.class); 
        if (pu == null) return;
        if (pu.getType() == Powerup.SpeedPU)
        {
            starfighter.getSpeedBoost();
            getWorld().removeObject(this);
        }    
    }    
    
    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            int x = 100+Greenfoot.getRandomNumber(500);  
            int y = 100+Greenfoot.getRandomNumber(300);  
            BugEnemy.setLocation(x, y); 
            return;
        }    

        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            int x = 100+Greenfoot.getRandomNumber(500);  
            int y = 100+Greenfoot.getRandomNumber(300);  
            InsectEnemy.setLocation(x, y); 
            return;
        } 

        if (Bee != null)
        {
            World world;
            world = getWorld();
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            int x = 100+Greenfoot.getRandomNumber(500);  
            int y = 100+Greenfoot.getRandomNumber(300);  
            Bee.setLocation(x, y); 
            return;
        } 

        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        }  else {
            if (getY()<=0)
            {
                getWorld().removeObject(this);
                return;
            } 
        }    
    } 

     
}    
danpost danpost

2014/4/22

#
Change line 25 in the Fireball class to this:
Powerup pu = (Powerup)getOneIntersectingObject(Powerup.class);
Also, I noticed something else, change line 27 (also in the Fireball class) to this:
if (pu.getType() == Powerup.SPEED_PU)
GamesGrinder1998 GamesGrinder1998

2014/4/22

#
danpost wrote...
Change line 25 in the Fireball class to this:
Powerup pu = (Powerup)getOneIntersectingObject(Powerup.class);
Also, I noticed something else, change line 27 (also in the Fireball class) to this:
if (pu.getType() == Powerup.SPEED_PU)
it compiled but when my fireball shoots it, it's like the powerup deflects it or "eats" it. It just blocks the fireball, do you know any problem with my code, here is some classes that might help...
import greenfoot.*;  
  
public class Powerup extends Actor  
{  
    static final int SPEED_PU = 0; 
    static final String[] images = { "speedPU.png" }; 
  
    int puType; 
    int lifespan = 400; 
  

    public Powerup(int type)  
    {  
        puType = type;  
        setImage(images[puType]);  
    }  
  
    public void act()  
    {  
        lifespan--;  
        if (lifespan == 0) getWorld().removeObject(this);  
    }  
  
    public int getType()  
    {  
        return puType;  
    }  
}  
import greenfoot.*; public class Fireball extends Actor { private int direction = 270; private int speed = 7; private Starfighter starfighter; public Fireball (Starfighter ship) { starfighter = ship; setRotation(direction); } public void act() { move(speed); checkSpeedPU(); if (getWorld() == null) return; disappear(); } private void checkSpeedPU() { Powerup pu = (Powerup)getOneIntersectingObject(Powerup.class); if (pu == null) return; if (pu.getType() == Powerup.SPEED_PU) { starfighter.getSpeedBoost(); getWorld().removeObject(this); } } public void disappear() { Actor BugEnemy; BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class); Actor InsectEnemy; InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class); Actor Bee; Bee = getOneObjectAtOffset(0, 0, Bee.class); Actor DarkPower; DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class); if (BugEnemy != null) { World world; world = getWorld(); MyGame mygame = (MyGame)world; Counter counter = mygame.getCounter(); counter.addScore(); world.removeObject(this); int x = 100+Greenfoot.getRandomNumber(500); int y = 100+Greenfoot.getRandomNumber(300); BugEnemy.setLocation(x, y); return; } if (InsectEnemy != null) { World world; world = getWorld(); MyGame mygame = (MyGame)world; Counter counter = mygame.getCounter(); counter.addScore(); world.removeObject(this); int x = 100+Greenfoot.getRandomNumber(500); int y = 100+Greenfoot.getRandomNumber(300); InsectEnemy.setLocation(x, y); return; } if (Bee != null) { World world; world = getWorld(); MyGame mygame = (MyGame)world; Counter counter = mygame.getCounter(); counter.addScore(); world.removeObject(this); int x = 100+Greenfoot.getRandomNumber(500); int y = 100+Greenfoot.getRandomNumber(300); Bee.setLocation(x, y); return; } if (DarkPower != null) { World world; world = getWorld(); world.removeObject(DarkPower); world.removeObject(this); return; } else { if (getY()<=0) { getWorld().removeObject(this); return; } } } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Starfighter here. * * @author (your name) * @version (a version number or a date) */ public class Starfighter extends Player { private final int SPEED_BOOST_TIMER = 115; private int SpeedBoostTimeLeft = SPEED_BOOST_TIMER; private int Speed; private boolean gotSpeedBoost = false; private int x; private int y; public Starfighter() { Speed = 5; } public void act() { checkKeys(); atWorldEdge(); stopAtWalls(); getSpeedBoost(); if (gotSpeedBoost) { speedBoostTimer(); } } public void checkKeys() { if ( Greenfoot.isKeyDown("left") ) { move(-5); } if ( Greenfoot.isKeyDown("right") ) { move(5); } if("space".equals(Greenfoot.getKey())) { getWorld().addObject(new Fireball(this), getX(), getY()); Greenfoot.playSound("fireball.wav"); } } public void stopAtWalls() { if(getOneIntersectingObject(Wall.class)!=null) { setLocation(x,y); } else { x = getX(); y = getY(); } } public void getSpeedBoost() { Speed += 5; gotSpeedBoost = true; } public void speedBoostTimer() { SpeedBoostTimeLeft--; if (SpeedBoostTimeLeft <= 0) { gotSpeedBoost = false; Speed -= 5; SpeedBoostTimeLeft = SPEED_BOOST_TIMER; } } } possibly the single player world...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyGame here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyGame extends World
{
    private int puTimerMin = 100;   
    private int puTimerMax = 150; 
    private int puTimerRange = puTimerMax-puTimerMin;  
    private int puTimer;
    Counter counter = new Counter();

    /**
     * Constructor for objects of class MyGame.
     * 
     */
    public MyGame()
    {    
        super(600, 600, 1); 
        prepare(); 
    }

    public void act()
    {
        if (puTimer == 0) puTimer = puTimerMin+Greenfoot.getRandomNumber(puTimerRange);  
        puTimer--;  
        if (puTimer == 0)   
        {  
            int puNum = Greenfoot.getRandomNumber(Powerup.images.length);  
            int x = Greenfoot.getRandomNumber(getWidth());  
            int y = Greenfoot.getRandomNumber(getHeight());  
            addObject(new Powerup(puNum), x, y);  
        }  
    }

    public Counter getCounter()
    {
        return counter;
    }    

    public void prepare()
    {
        addObject(counter, 50, 50);
        addObject (new Starfighter (), 300, 520);
        addObject (new BugEnemy (), 50, 100);
        addObject (new BugEnemy (), 100,100);
        addObject (new BugEnemy (), 150,100);
        addObject (new BugEnemy (), 250,100);
        addObject (new BugEnemy (), 350,100);
        addObject (new BugEnemy (), 450,100);
        addObject (new BugEnemy (), 500,100);
        addObject (new BugEnemy (), 550,100);
        addObject (new BugEnemy (), 100,150);
        addObject (new BugEnemy (), 200,150);
        addObject (new BugEnemy (), 300,150);
        addObject (new BugEnemy (), 400,150);
        addObject (new BugEnemy (), 500,150);
        addObject (new InsectEnemy (), 100, 200);
        addObject (new InsectEnemy (), 200, 200);
        addObject (new InsectEnemy (), 300, 200);
        addObject (new InsectEnemy (), 400, 200);
        addObject (new InsectEnemy (), 500, 200);
        addObject (new Bee (), 140, 250);
        addObject (new Bee (), 240, 250);
        addObject (new Bee (), 340, 250);
        addObject (new Bee (), 440, 250);
    }     
}    
There are more replies on the next page.
1
2
3