I want to make a game where lava chase the actor and the actor stay at the center of the background ( the background scrolling horizontally)
how to make something like that ?


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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import greenfoot.*; /** * CLASS: Scroller extend Object * AUTHOR: danpost (greenfoot.org username) * DATE: November 11, 2016 * * DESCRIPTION: This is a support class for a scrolling world. It contains two constructors; * one for unlimited scrolling and one for limited scrolling. Both constructors have an 'image' * parameter. Because image manipulation can hog up CPU time, it is important to remember that * it is better not to have a scrolling background image (having an Actor for the background is * probably worse than having the background scroll). For unlimited scrolling using a background * image, the smaller that background image to be tiled, the better. Making the viewport (the * size of the world that is visible) smaller can help in CPU expense, also. Scrolling worlds * should be unbounded, allowing actors to move beyond the visible area. Ensuring that actors * are removed from the world if no longer needed when out of view will help to prevent lag, * as well. * * It is the responsibility of the World object that creates a Scroller object to determine when * to scroll and by how much. */ public class Scroller { private World world; // view window world private GreenfootImage scrollImage; // scrolling image private boolean limited; private int scrolledX, scrolledY; /** * This constructor is for an unlimited scrolling world; * If 'image' is null, the background will not change; else the given image is wrapped * * @param viewWorld the world that scrolling will be performed on * @param image the background image that will be tiled, if needed, and wrap with scrolling */ public Scroller(World viewWorld, GreenfootImage image) { world = viewWorld; scrollImage = image; scroll( 0 , 0 ); // sets initial background image } /** * This constructor is for a limited scrolling world; * If 'image' is smaller than the given total scrolling area, it will be tiled * If 'image' is null, the background will not change * * @param viewWorld the world that scrolling will be performed on * @param image the background image that will be tiled, if needed, to fill the scrolling area * @param wide the width of the visible area encompassed through scrolling; * the given value must be at least equal to the width of 'viewWorld' and * is given in world cells (not in pixels) * @param high the height of the visible area encompassed through scrolling; * the given value must be at least equal to the height of 'viewWorld' and * is given in world cells (not in pixels) */ public Scroller(World viewWorld, GreenfootImage image, int wide, int high) { limited = true ; world = viewWorld; if (image != null ) { // create an image as large as scrolling area; tiled, if needeed scrollImage = new GreenfootImage(wide*world.getCellSize(), high*world.getCellSize()); for ( int x= 0 ; x<wide*world.getCellSize(); x+= image.getWidth()) for ( int y= 0 ; y<high*world.getCellSize(); y+=image.getHeight()) scrollImage.drawImage(image, x, y); // set initial background image scroll( 0 , 0 ); } } /** * performs scrolling on 'world' by the given distances along the horizontal and vertical; * if 'limited' is false, requested distances are actual scrolling distances; * if 'limited' is true, the distances may be adjusted due to the limits of scrolling * * @param dsx the requested distance to shift everything horizontally * @param dsy the requested distance to shift everything vertically */ public void scroll( int dsx, int dsy) { if (scrollImage != null ) { if (limited) { // calculate limits of scrolling int maxX = scrollImage.getWidth()/world.getCellSize()-world.getWidth(); int maxY = scrollImage.getHeight()/world.getCellSize()-world.getHeight(); // apply limits to distances to scroll if (scrolledX+dsx < 0 ) dsx = -scrolledX; if (scrolledX+dsx >= maxX) dsx = maxX-scrolledX; if (scrolledY+dsy < 0 ) dsy = -scrolledY; if (scrolledY+dsy >= maxY) dsy = maxY-scrolledY; // update scroll positions scrolledX += dsx; scrolledY += dsy; // adjust background image world.getBackground().drawImage ( scrollImage, -scrolledX*world.getCellSize(), -scrolledY*world.getCellSize() ); } else { // update scroll positions scrolledX += dsx; scrolledY += dsy; // create working variables of scroll positions int imageX = scrolledX*world.getCellSize(); int imageY = scrolledY*world.getCellSize(); // find a similar positive value for scroll positions while (imageX < 0 ) imageX += 100 *scrollImage.getWidth(); while (imageY < 0 ) imageY += 100 *scrollImage.getHeight(); // get new starting positions for drawing 'scrollImage' imageX = imageX%scrollImage.getWidth(); imageY = imageY%scrollImage.getHeight(); // create image of appropriate size and tile fill 'scrollImage' onto it GreenfootImage hold = new GreenfootImage(scrollImage); hold.drawImage(scrollImage, -imageX, -imageY); if (imageX > 0 ) hold.drawImage(scrollImage, scrollImage.getWidth()-imageX, -imageY); if (imageY > 0 ) hold.drawImage(scrollImage, -imageX, scrollImage.getHeight()-imageY); if (imageX > 0 && imageY > 0 ) hold.drawImage(scrollImage, scrollImage.getWidth()-imageX, scrollImage.getHeight()-imageY); // set image to background of 'world' world.setBackground(hold); } } // adjust position of all actors (that can move with 'setLocation') for (Object obj : world.getObjects( null )) { Actor actor = (Actor) obj; actor.setLocation(actor.getX()-dsx, actor.getY()-dsy); } } /** * getter method for the current total scrolled distance horizontally * * @return the current total offset of horizontal scrolling */ public int getScrolledX() { return scrolledX; } /** * getter method for the current total scrolled distance vertically * * @return the current total offset of vertical scrolling */ public int getScrolledY() { return scrolledY; } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class ScrollingWorld here. * * @author (your name) * @version (a version number or a date) */ public class ScrollingWorld extends World { private Scroller scroller; public ScrollingWorld() { scroller = new Scroller( this , new GreenfootImage( "background.png" ), 1366 , 613 ); } public void act() { scroll(); } private void scroll() { scroller.scroll(- 5 , 0 ); } } |
1 2 3 4 5 | // starting at line 14 public ScrollingWorld() { super ( 600 , 400 , 1 ); // this line is missing (adjust view window size as needed) scroller = ... |
1 | public void setLocation( int x, int y) { } |
1 2 3 4 | public void setLocation( int x, int y) { //what should i put here ? } |
1 2 3 4 | public void setLocation( int x, int y) { //what should i put here ? } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class ScrollWorld here. * * @author (your name) * @version (a version number or a date) */ public class ScrollWorld extends World { /** * Constructor for objects of class ScrollWorld. * */ Ground ground = new Ground(); Lava lava = new Lava(); Time time = new Time(); private int lavaAnimation = 0 ; private Scroller scroller; public ScrollWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 1366 , 613 , 1 , false ); scroller = new Scroller( this , new GreenfootImage( "Background.png" ), 5466 , 613 ); prepare(); } public void prepare() { addObject(ground,getWidth()/ 2 , 539 ); addObject(lava, 31 , 295 ); addObject(time, 88 , 23 ); } public void act() { lavaAnimation++; LavaAnimation(); scroll(); } private void LavaAnimation() { if ( lavaAnimation == 10 ) { lava.setImage( new GreenfootImage( "lava 1.png" )); } else if ( lavaAnimation == 20 ) { lava.setImage( new GreenfootImage( "lava 2.png" )); } else if ( lavaAnimation == 30 ) { lava.setImage( new GreenfootImage( "Lava 3.png" )); } else if ( lavaAnimation == 40 ) { lava.setImage( new GreenfootImage( "lava 4.png" )); } else if ( lavaAnimation == 50 ) { lava.setImage( new GreenfootImage( "lava 5.png" )); } else if ( lavaAnimation == 60 ) { lava.setImage( new GreenfootImage( "lava 6.png" )); } else if ( lavaAnimation == 70 ) { lava.setImage( new GreenfootImage( "lava 7.png" )); } else if ( lavaAnimation == 80 ) { lava.setImage( new GreenfootImage( "lava 8.png" )); } else if ( lavaAnimation == 90 ) { lava.setImage( new GreenfootImage( "lava 9.png" )); lavaAnimation = 0 ; } } private void scroll() { scroller.scroll( 5 , 0 ); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class Time extends Actor { int time = 0 ; int counter = 0 ; public Time() { updateImage(); } public void act() { counter++; if ( counter >= 60 ) { time += 1 ; counter = 0 ; } updateImage(); Hold(); } private void Hold() { setLocation(getX()+ 5 ,getY()); } public void updateImage() { setImage( new GreenfootImage( "Time Elapsed : " +time, 24 ,Color.black, new Color( 0 , 0 , 0 , 0 ))); } } |
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Rex here. * * @author (your name) * @version (a version number or a date) */ public class Rex extends Actor { private boolean walking; private boolean facingLeft; private boolean isKeyPressed; private int frame = 0 ; private int vSpeed = 0 ; private int jumpPower = 25 ; private int acceleration = 2 ; private int apexTimer; private int xSpeed; private int animationCounter = 0 ; public boolean onTheGround; public void Rex() { walking = false ; facingLeft = true ; } public void act() { checkFall(); animationCounter++; checkIdle(); move(); checkHit(); } private void move () { move( 5 ); if (Greenfoot.isKeyDown( "space" ) && onGround()) { jump(); } if (Greenfoot.isKeyDown( "down" )) { setImage( new GreenfootImage( "Crouch 1.png" )); } onTheGround = false ; while (getOneObjectAtOffset( 0 , getImage().getHeight()/ 2 + 1 , null )!= null ) { setLocation(getX(), getY()- 1 ); onTheGround= true ; vSpeed= 0 ; } //check above the actor while (getOneObjectAtOffset( 0 , -getImage().getHeight()/ 2 - 1 , null )!= null ) { setLocation(getX(), getY()+ 1 ); vSpeed = 0 ; } // check to right of actor while (getOneObjectAtOffset(getImage().getWidth()/ 2 + 1 , 0 , null )!= null ) { setLocation(getX()- 1 , getY()); xSpeed = 0 ; } // check to left of actor while (getOneObjectAtOffset(-getImage().getWidth()/ 2 - 1 , 0 , null )!= null ) { setLocation(getX()+ 1 , getY()); xSpeed = 0 ; } } private void walkRight() { int delay = 4 ; walking = true ; facingLeft = false ; frame++; if (frame== 1 * delay ) { setImage( new GreenfootImage( "Rex 1 Right.png" )); } else if (frame== 2 * delay ) { setImage( new GreenfootImage( "Rex 2 Right.png" )); } else if (frame== 3 * delay ) { setImage( new GreenfootImage( "Rex 3 Right.png" )); } else if (frame== 4 * delay ) { setImage( new GreenfootImage( "Rex 4 Right.png" )); frame = 1 ; } } private void walkLeft() { int delay = 4 ; walking = true ; facingLeft = true ; frame++; if (frame== 1 * delay) { setImage( new GreenfootImage( "Rex 1.png" )); } else if (frame== 2 * delay) { setImage( new GreenfootImage( "Rex 2.png" )); } else if (frame== 3 * delay) { setImage( new GreenfootImage( "Rex 3.png" )); } else if (frame== 4 * delay ) { setImage( new GreenfootImage( "Rex 4.png" )); frame = 1 ; return ; } } public void checkFall() { if (onGround() ) { vSpeed = 0 ; } else { fall(); } } public void jump() { setImage( new GreenfootImage( "Jump Up.png" )); vSpeed = -jumpPower; setLocation(getX(),getY()+vSpeed); } public void fall() { setImage( new GreenfootImage( "Jump Down.png" )); setLocation(getX(),getY() + vSpeed); vSpeed = vSpeed + acceleration; } public boolean onGround() { int rexHeight = getImage().getHeight(); int lookForGround = ( int )(rexHeight/ 2 ) + 5 ; Actor tanah = getOneObjectAtOffset( 0 ,lookForGround,Ground. class ); if (tanah == null ) { return false ; } else { // moveToGround(tanah); return true ; } } // public void moveToGround(Actor tanah) // { // int tanahHeight = tanah.getImage().getHeight(); // int newY = tanah.getY() - (tanahHeight + getImage().getHeight())/2; // setLocation(getX(),newY); // } public void checkIdle() { if ( isKeyPressed == false && facingLeft == false && onGround()) { setImage( new GreenfootImage( "Rex Idle Right.png" )); } if ( isKeyPressed == false && facingLeft == true && onGround()) { setImage( new GreenfootImage( "Rex Idle.png" )); } } public void checkHit() { Actor hit = getOneIntersectingObject(Lava. class ); if ( hit != null ) { Greenfoot.playSound( "GameOver.wav" ); Greenfoot.stop(); } } } |
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Rex here. * * @author (your name) * @version (a version number or a date) */ public class Rex extends Actor { private boolean walking; private boolean facingLeft; private boolean isKeyPressed; private int frame = 0 ; private int vSpeed = 0 ; private int jumpPower = 25 ; private int acceleration = 2 ; private int apexTimer; private int xSpeed; private int animationCounter = 0 ; public boolean onTheGround; public void Rex() { walking = false ; facingLeft = true ; } public void act() { checkFall(); move(); checkHit(); Animation(); } private void move () { move( 5 ); if (Greenfoot.isKeyDown( "space" ) && onGround()) { jump(); } if (Greenfoot.isKeyDown( "down" )) { setImage( new GreenfootImage( "Crouch 1.png" )); } //check above the actor while (getOneObjectAtOffset( 0 , -getImage().getHeight()/ 2 - 1 , null )!= null ) { setLocation(getX(), getY()+ 1 ); vSpeed = 0 ; } // check to right of actor while (getOneObjectAtOffset(getImage().getWidth()/ 2 + 1 , 0 , null )!= null ) { setLocation(getX()- 1 , getY()); xSpeed = 0 ; } // check to left of actor // while(getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, null)!=null) // { // setLocation(getX()+1, getY()); // xSpeed = 0; // } } private void Animation() { int delay = 8 ; walking = true ; facingLeft = false ; frame++; if (frame== 1 * delay && vSpeed == 0 ) { setImage( new GreenfootImage( "Rex 1 Right.png" )); } else if (frame== 2 * delay && vSpeed == 0 ) { setImage( new GreenfootImage( "Rex 2 Right.png" )); } else if (frame== 3 * delay && vSpeed == 0 ) { setImage( new GreenfootImage( "Rex 3 Right.png" )); } else if (frame== 4 * delay && vSpeed == 0 ) { setImage( new GreenfootImage( "Rex 4 Right.png" )); frame = 1 ; } if (vSpeed > 0 && !onGround() ) { setImage( new GreenfootImage( "Jump Down.png" )); } else if (vSpeed < 0 ) { setImage( new GreenfootImage( "Jump Up.png" )); } } public void checkFall() { if (onGround() ) { vSpeed = 0 ; } else { fall(); } } public void jump() { vSpeed = -jumpPower; setLocation(getX(),getY()+vSpeed); } public void fall() { setLocation(getX(),getY() + vSpeed); vSpeed = vSpeed + acceleration; } public boolean onGround() { int rexHeight = getImage().getHeight(); int lookForGround = ( int )(rexHeight/ 2 ) + 5 ; Actor tanah = getOneObjectAtOffset( 0 ,lookForGround,Ground. class ); if (tanah == null ) { return false ; } else { // moveToGround(tanah); return true ; } } // public void moveToGround(Actor tanah) // { // int tanahHeight = tanah.getImage().getHeight(); // int newY = tanah.getY() - (tanahHeight + getImage().getHeight())/2; // setLocation(getX(),newY); // } public void checkIdle() { if ( isKeyPressed == false && facingLeft == false && onGround()) { setImage( new GreenfootImage( "Rex Idle Right.png" )); } if ( isKeyPressed == false && facingLeft == true && onGround()) { setImage( new GreenfootImage( "Rex Idle.png" )); } } public void checkHit() { Actor hit = getOneIntersectingObject(Lava. class ); if ( hit != null ) { Greenfoot.playSound( "GameOver.wav" ); Greenfoot.stop(); } } } |