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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 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 ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 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 ); } } |