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
PowerUps
Imune (the power up I am having trouble with)
FlappyWorld(level 1) I don't think you need the other levels because they are pretty much the same
Thank you for the help.
I tried other discussions but none of them answered my questions.
Thank you in advance!
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);
}
}
}
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);
}
}
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());
}
}
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);
}
}

