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

2019/6/3

Powerup!

Joyfu1 Joyfu1

2019/6/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Ship extends Actor
{
    // True or false statement for touching other actors
    boolean touchingAlien = false;
    boolean touchingFinalBoss = false;
    boolean touchingBossLaser = false;

    private ScoreBoard score;

    public Ship(ScoreBoard score)
    {
        this.score = score;
    }

    public void act() 
    {
        movement();
        Boundry();
        PowerUpMovement();
        hitAlien();
        hitFinalBoss();
    }  

    public void movement() {
        // User input for navigating ship
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY() - 5);
        }

        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + 5);
        }

        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX() + 5, getY());
        }

        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX() - 5, getY());
        }
        // User input for shooting lasers (works when key is lifted)
        if ("space".equals(Greenfoot.getKey())) {
            shoot();
        }
    }

    public void PowerUpMovement() {
        World testShop = getWorld();
        TestShop testshop = (TestShop)testShop;
        if (getWorld() != null) {
            if (testshop.OptionSpeed() == true) {
                if (Greenfoot.isKeyDown("up")) {
                    setLocation(getX(), getY() - 5);
                }

                if (Greenfoot.isKeyDown("down")) {
                    setLocation(getX(), getY() + 5);
                }

                if (Greenfoot.isKeyDown("right")) {
                    setLocation(getX() + 5, getY());
                }

                if (Greenfoot.isKeyDown("left")) {
                    setLocation(getX() - 5, getY());
                }
                // User input for shooting lasers (works when key is lifted)
                if ("space".equals(Greenfoot.getKey())) {
                    shoot();
                }

            }
        }
    }

    public void shoot() {    
        getWorld().addObject(new Laser(score), getX(), getY());
    }

    public void hitAlien() {
        // Define the actor as the ship touching an alien
        Actor alien = getOneIntersectingObject(Alien.class);
        if (alien != null) {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            HealthBar healthbar = myworld.getHealthBar();
            if (touchingAlien == false) {
                healthbar.loseHealth();
                touchingAlien = true;   
                if (healthbar.health <= 0) {
                    // Game over since health has hit 0
                    myWorld.removeObject(this);
                    Greenfoot.setWorld(new GameOver());
                }
            }

            else {
                // Continue game 
                touchingAlien = false;
            }
        }
    }

    public void hitFinalBoss() {
        if (getWorld() != null) {
            Actor finalboss = getOneIntersectingObject(FinalBoss.class);
            Actor bosslaser = getOneIntersectingObject(BossLaser.class);
            if (finalboss != null || bosslaser != null) {
                World finalWorld = getWorld();
                FinalWorld finalworld = (FinalWorld)finalWorld;
                HealthBar healthbar = finalworld.getHealthBar();
                if (touchingFinalBoss == false || touchingBossLaser == false) {
                    healthbar.loseHealth();
                    touchingFinalBoss = true;   
                    touchingBossLaser = true;

                    if (healthbar.health <= 0) {
                        // Game over since health has hit 0
                        finalWorld.removeObject(this);
                        Greenfoot.setWorld(new GameOver());
                    }
                }

                else {
                    // Continue game 
                    touchingFinalBoss = false;
                    touchingBossLaser = false;
                }
            }
        }
    }

    public void Boundry() {
        if (getX() > 980) {
            setLocation(980, getY());
        }
        if (getX() < 0) {
            setLocation(0, getY());
        }
        if (getY() > 575) {
            setLocation(getX(), 575);
        }
        if (getY() < 0) {
            setLocation(getX(), 0);
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class TestShop extends World
{
    public TestShop()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(980, 575, 1); 
        prepare(); 
        OptionHealth(); 
        OptionSpeed(); 
    }

    public void act() 
    {
        
    }   
    public boolean OptionHealth() 
    {
        if ("a".equals(Greenfoot.getKey())) {
            return true; 

        }
        return false; 
    }
    public boolean OptionSpeed() 
    {
        if ("s".equals(Greenfoot.getKey())) {
            return true; 

        }
        return false; 
    }

    public void prepare ()
    {
       
    }
}
We would like to change the speed of our ship but we are getting the error of not being able to cast our TestShop to myWorld. Thank yoU!
danpost danpost

2019/6/3

#
If you are going to make the speed of the ship variable, then you will need a variable field for the speed.
danpost danpost

2019/6/3

#
I noticed at least 3 thing wrong with the codes given. First, as you mentioned you cannot cast MyWorld to TestShop. Remove lines 50 thru 52 and line 74 in the Ship class. You do not need to check if in world there as nothing previous to this method removes it at any time. Next, OptionHealth and OptionSpeed need to be called continuously if the keys are to be detected. Move lines 11 and 12 in the TestShop class to line 16. Finally, you cannot use getKey effectively more than once during the same act cycle. Get the key once and compare that singly returned value to all the possible actionable values.
Joyfu1 Joyfu1

2019/6/4

#
Thank you! Here is our updated code for the LevelUp world and our Ship actor.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class LevelUp extends World
{
    public LevelUp()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(980, 575, 1); 
        prepare(); 
    } 
    
    public void act() {
        OptionHealth();
        OptionSpeed();
    }

    public boolean OptionHealth() {
        if (Greenfoot.isKeyDown("a")) {
            return true;
        }
        return false;
    }

    public boolean OptionSpeed() {
        if (Greenfoot.isKeyDown("s")) {
            return true;
        }
        return false;
    }

    public void prepare()
    {
    }
}
I want to implement something like this into ship which allows it to move faster
if (OptionSpeed == true) {
x = 8;
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

I'm confused on how to call OptionSpeed() within ship so it will move faster. Thank you!

public class Ship extends Actor
{
    // True or false statement for touching other actors
    boolean touchingAlien = false;
    boolean touchingFinalBoss = false;
    boolean touchingBossLaser = false;

    private ScoreBoard score;
    
    private int x = 5;

    public Ship(ScoreBoard score)
    {
        this.score = score;
    }

    public void act() 
    {
        movement();
        Boundry();
        hitAlien();
        hitFinalBoss();
    }  

    public void movement() {
        // User input for navigating ship
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY() - x);
        }

        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + x);
        }

        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX() + x, getY());
        }

        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX() - x, getY());
        }
        // User input for shooting lasers (works when key is lifted)
        if ("space".equals(Greenfoot.getKey())) {
            shoot();
        }
    }

    public void shoot() {    
        Laser laser = new Laser(score);
        getWorld().addObject(laser, getX(), getY());
    }

    public void hitAlien() {
        // Define the actor as the ship touching an alien
        Actor alien = getOneIntersectingObject(Alien.class);
        if (alien != null) {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            HealthBar healthbar = myworld.getHealthBar();
            if (touchingAlien == false) {
                healthbar.loseHealth();
                touchingAlien = true;   
                if (healthbar.health <= 0) {
                    // Game over since health has hit 0
                    myWorld.removeObject(this);
                    Greenfoot.setWorld(new GameOver());
                }
            }

            else {
                // Continue game 
                touchingAlien = false;
            }
        }
    }

    public void hitFinalBoss() {
        if (getWorld() != null) {
            Actor finalboss = getOneIntersectingObject(FinalBoss.class);
            Actor bosslaser = getOneIntersectingObject(BossLaser.class);
            if (finalboss != null || bosslaser != null) {
                World finalWorld = getWorld();
                FinalWorld finalworld = (FinalWorld)finalWorld;
                HealthBar healthbar = finalworld.getHealthBar();
                if (touchingFinalBoss == false || touchingBossLaser == false) {
                    healthbar.loseHealth();

                    touchingFinalBoss = true;   
                    touchingBossLaser = true;
                    if (healthbar.health <= 0) {
                        // Game over since health has hit 0
                        finalWorld.removeObject(this);
                        Greenfoot.setWorld(new GameOver());
                    }
                }

                else {
                    // Continue game 
                    touchingFinalBoss = false;
                    touchingBossLaser = false;
                }
            }
        }
    }

    public void Boundry() {
        if (getX() > 980) {
            setLocation(980, getY());
        }
        if (getX() < 0) {
            setLocation(0, getY());
        }
        if (getY() > 575) {
            setLocation(getX(), 575);
        }
        if (getY() < 0) {
            setLocation(getX(), 0);
        }
    }

}
Joyfu1 Joyfu1

2019/6/4

#
Sorry this wasn't meant to be in the code: I'm confused on how to call OptionSpeed() within ship so it will move faster. Thank you!
danpost danpost

2019/6/4

#
At a cursory glance, I am guessing it would be something like:
if (isTouching(PowerUp.class)
{
    removeTouching(PowerUp.class)
    OptionSpeed();
}
Joyfu1 Joyfu1

2019/6/4

#
It's more like the user gets an option to either increase their speed or health before they battle the final boss. So, if they press s then they would have selected the option to increase their speed.
Super_Hippo Super_Hippo

2019/6/5

#
So the LevelUp world is only there to decide which PowerUp is used? Then instead of those "return true/false" methods, you could change the world and pass a parameter (speed/health, so true/false or 0/1 or whatever) and use that parameter in the next world and pass it to the Ship.
Joyfu1 Joyfu1

2019/6/5

#
Yes, the LevelUp world was used only for PowerUp, but we now moved it into our scoreboard class instead. So, LevelUp is now an empty world and that code was added into scoreboard. Isn't that what we are doing? The user enters s, which makes the speed parameter true which then passes it down onto the ship to change the speed.
Super_Hippo Super_Hippo

2019/6/5

#
You are not passing anything. The code you use in the LevelUp world does not do anything.
danpost danpost

2019/6/6

#
Joyfu1 wrote...
It's more like the user gets an option to either increase their speed or health before they battle the final boss. So, if they press s then they would have selected the option to increase their speed.
Where is your code to proceed to the final (boss) battle in your TestShop (or LevelUp) class?
You need to login to post a reply.