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

2019/7/16

Power Ups

AnimeRules AnimeRules

2019/7/16

#
Hi so I'm making a game and I want to add power ups that when eaten you can go through pipes instead of having to go through them. I only want it to last for a certain amount of time. I have managed to make the power up appear and random times and disappear once eaten but I'm not sure how to override the hit Pipes methods. I tried null but couldn't figure out how. FlappyBird
public class FlappyBird extends Actor
{
    /**
     * Act - do whatever the FlappyBird wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    // sets a variable
    int count = 0;
    int gravity = 0;
    boolean jumpPressed = false;
    public void act() 
    {
        //adds one to gravity
        gravity++;
        //adds one to count
        count++;
        //calls the methods
        hitEdge();
        Fall();
        Jump();
        hitBottomPipe();
        hitTopPipe();
        stayUpright();
        collectPowerUp();
    }   

    public void stayUpright()
    {
        //makes the bird rotate
        setRotation(getRotation() + 3);
    }

    public void Fall()
    {
        //sets what happens when the bird falls
        if(count % 3 == 0)
        {
            setLocation(getX(), getY() + gravity);
        }
    }

    public void Jump()
    {
        if (Greenfoot.isKeyDown("space") && !jumpPressed)
        {
            //if space is down the gravity will set to -24
            gravity = -20; 
            setRotation(-75);
            jumpPressed = true;
        }

        if (!Greenfoot.isKeyDown("space"))
        {
            jumpPressed = false;
        }
    }

    public void hitEdge()
    {
        //stops the game if the bird touches the top or the bottom
        if(isTouching(Ground.class) || getY() <= 1)
        {
            gravity = 0;
            Greenfoot.stop();
            GameOver gameOver = new GameOver();
            Greenfoot.setWorld(gameOver);
            return;

        }
    }

    public void hitBottomPipe()
    {
        //stops the game if the bird touches the tbottom pipes
        if(isTouching(BottomPipe.class))
        {
            gravity = 0;
            Greenfoot.stop();
            GameOver gameOver = new GameOver();
            Greenfoot.setWorld(gameOver);
            return;
        }
    }

    public void hitTopPipe()
    {
        //stops the game if the bird touches the top pipes 
        if(isTouching(TopPipe.class))
        {
            gravity = 0;
            Greenfoot.stop();
            GameOver gameOver = new GameOver();
            Greenfoot.setWorld(gameOver);
            return;
        }
    }
    
    public void collectPowerUp()
    {
        if (isTouching(PowerUps.class))
        {
            removeTouching(PowerUps.class); 
        }
    }
}
PowerUps
public class PowerUps extends Actor
{
    //sets variables
    int lineScaleDown;
    int  acrossScaleDown;
    /**
     * Act - do whatever the PowerUps wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

    }  

    public void scaleDownImage(int x, int y)
    {
        //makes the method to resize the pipes
        lineScaleDown = x;
        acrossScaleDown = y;
        getImage().scale(getImage().getWidth()/lineScaleDown, getImage().getHeight()/acrossScaleDown);

    }
}
Imune (the power up I am having trouble with)
public class Imune extends PowerUps
{
    /**
     * Act - do whatever the Imune wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //sets speed
    int speed = 5;
    public Imune()
    {
        //resizes the worm
        scaleDownImage(11,10);
    }

    public void act() 
    {
        moveAround();
    }   

    public void moveAround()
    {
        //moves the pipe across the screen
        setLocation(getX() - speed, getY());
    }
}
FlappyWorld(level 1) I don't think you need the other levels because they are pretty much the same
public class FlappyWorld extends World
{
    /**
     * Constructor for objects of class FlappyWorld.
     * 
     */
    // the beginning count 
    int count = 0;
    private static Counter cnt;
    public FlappyWorld()
    {    
        // resizes the world
        super(800, 600, 1, false); 
        cnt= new Counter();
        addObject(cnt, 100, 30);
        prepare();
    }

    public void act()
    {
        //adds one to the count
        count = count + 1;
        //calls the methods created
        addPipes();
        addPowerUps();
    }

    public void addPipes()
    {
        if(count % 100 == 0)
        {
            //adds a new pipe at the edge of the screen
            int randomNum = Greenfoot.getRandomNumber(6);
            addObject(new BottomPipe(), getWidth()-1, 350 + 50 * randomNum);
            addObject(new TopPipe(), getWidth()-1, -200 + 50 * randomNum);
        }
    }

    public void addPowerUps()
    {   //adds power ups
        if(count % 369 == 0)
        {
            int randomN = Greenfoot.getRandomNumber(6);
            addObject(new Imune(), getWidth()-1, 200 + 50 * randomN);
        }
    }
    
    public static Counter getCount(){
        return cnt;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        //makes what you go through
        MiddleInvisible mi15 = new MiddleInvisible();
        addObject(mi15, 75,570);
        //draw the ground
        Ground ground = new Ground();
        addObject(ground,296,575);
        Ground ground2 = new Ground();
        addObject(ground2,702,575);
        //draws the flappybird
        FlappyBird flappyBird = new FlappyBird();
        addObject(flappyBird,76,169);
    }
}
Thank you for the help. I tried other discussions but none of them answered my questions. Thank you in advance!
danpost danpost

2019/7/16

#
AnimeRules wrote...
I want to add power ups that when eaten you can go through pipes instead of having to go through them. I only want it to last for a certain amount of time. I have managed to make the power up appear and random times and disappear once eaten but I'm not sure how to override the hit Pipes methods. I tried null but couldn't figure out how.
So your flappy bird needs another state -- that of being immune. States are defined by field values. For immunity, there are only two possible states -- immune or not immune. Normally, a boolean field would be sufficient for a true/false state like this; however, being one of the two states is time-limited and being time is ever-changing. an int field would be more appropriate:
// field in FlappyBird class
private int immune = 0; // for remaining immune time
You can then put lines 21 and 22 inside an if block as illustrated below:
if (immune == 0)
{
    // lines 21 and 22 here
} else immune--;
Now, all you need to do is set the immune field to some positive value (probably around 60 times the number of seconds you want the immunity to last) when an Imune powerup is eaten.
AnimeRules AnimeRules

2019/7/16

#
Thanks1 it works!
AnimeRules AnimeRules

2019/7/16

#
One more question. how would I make a timer to show how much time is left. my code is the same plus what you said to add.
danpost danpost

2019/7/17

#
AnimeRules wrote...
ow would I make a timer to show how much time is left.
Please refer to my Value Display Tutorial scenario.
AnimeRules AnimeRules

2019/7/17

#
ok thank you
You need to login to post a reply.