Hi, does anyone know how to make a side-scrolling platform game? Here is my code so you can help me in context. I have 2 worlds, CharSelect and Game, I also have a projectile, a ground class with two subclasses, and 1 hero and globals class. Here is the code fo CharSelect
Here is the code for Game
And here is the code for Hero
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 | public class CharSelect extends World { /** * Constructor for objects of class CharSelect. * */ public CharSelect() { super ( 600 , 400 , 1 ); addObject(Globals.h, 300 , 255 ); prepare(); } public void act() { if (Greenfoot.isKeyDown( "1" )) { Globals.h.changeImage( 1 ); } else if (Greenfoot.isKeyDown( "2" )) { Globals.h.changeImage( 2 ); } else if (Greenfoot.isKeyDown( "3" )) { Globals.h.changeImage( 3 ); } else if (Greenfoot.isKeyDown( "4" )) { Globals.h.changeImage( 4 ); } else if (Greenfoot.isKeyDown( "space" )) { Greenfoot.setWorld( new Game()); } } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { } } |
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 Game extends World { private int speed = 7 ; private int vSpeed = 0 ; private int acceleration = 1 ; private int jumpStrenght = - 6 ; /** * Constructor for objects of class Game. * */ public Game() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 1000 , 700 , 1 , false ); addObject(Globals.h, 50 , 400 ); addObject( new Long(), 190 , 654 ); addObject( new Hump(), 618 , 575 ); } } |
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 | public class Hero extends Actor Thanks in advance! { GreenfootImage i1 = new GreenfootImage( "wizard_1.png" ); GreenfootImage i2 = new GreenfootImage( "princess.png" ); GreenfootImage i3 = new GreenfootImage( "goblin_2.png" ); GreenfootImage i4 = new GreenfootImage( "barbarian_1.png" ); int vSpeed = 0 ; int acceleration = 1 ; int speed = 7 ; int jumpStrenght = - 6 ; /** * Act - do whatever the Hero wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act () { if (getWorld() instanceof CharSelect) return ; checkKeys(); checkFall(); landOnTop(); fireProjectile(); } public void fireProjectile() { if (Greenfoot.isKeyDown( "down" )) { getWorld(). addObject( new Projectile(), getX(), getY()); Projectile projectile = new Projectile(); } } private void checkKeys() { if (Greenfoot.isKeyDown( "left" )) { moveLeft(); } if (Greenfoot.isKeyDown( "right" )) { moveRight(); } if (Greenfoot.isKeyDown( "up" )) { jump(); } } public void checkFall() { if (onGround()) { vSpeed = 0 ; } else { fall(); } } public void jump() { vSpeed= jumpStrenght; fall(); } public boolean onGround() { Actor under = getOneObjectAtOffset( 0 ,getImage().getHeight()/ 4 ,Ground. class ); return under != null ; } public void landOnTop() { if (isTouching(Ground. class )) { setLocation(getX(), getY() - 1 ); } } public void moveRight() { setLocation (getX() + speed,getY() ); } public void moveLeft() { setLocation (getX() - speed,getY()); } public void fall() { setLocation (getX(),getY()+vSpeed); vSpeed = vSpeed + acceleration; } public Hero() { setImage(i1); } public void changeImage ( int x) { if (x == 1 ) { setImage(i1); } if (x == 2 ) { setImage(i2); } if (x == 3 ) { setImage(i3); } if (x == 4 ) { setImage(i4); } } } |