-
Notifications
You must be signed in to change notification settings - Fork 20
Platformer
Attention: you must complete all basic tutorial before starting this one!
Intro
What is a platformer? You must imagine Mario Bros games, Sonic or just a random games where there is someone jumping around from platform to platform.
MarteEngine and platformers
MarteEngine can offer you a basic class with basics physics calculation (motion, gravity and friction), most of the times, it's a good start point for this kind of game, but handle just math, not how a platformer entity should work. So MarteEngine provide PlatformerEntity an entity that extends PhysicsEntity and put some logic on top of it, in particular jump and movements.
We use this basic PlatformerEntity in this example let you to extend it in your game or copy logic into it:
public class PlatformerGameWorld extends World {
public PlatformerGameWorld(int id) {
super(id);
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
super.init(container, game);
PlatformerEntity player = new PlatformerEntity(80, 100, "player");
add(player);
// add some blocks to the level
// bottom
for (int i = 0; i < 18; i++) {
add(new Block(20+i*32, 400, 32, 32, 10));
}
// left platform
for (int i = 0; i < 5; i++) {
add(new Block(120+i*32, 304, 32, 32, 10));
}
// right platform
for (int i = 0; i < 5; i++) {
add(new Block(320+i*32, 304, 32, 32, 10));
}
// top platform
for (int i = 0; i < 6; i++) {
add(new Block(200+i*32, 200, 32, 32, 10));
}
// left wall
for (int i = 1; i < 13; i++) {
add(new Block(20, 400-i*32, 32, 32,10));
}
// right wall
for (int i = 1; i < 13; i++) {
add(new Block(564, 400-i*32, 32, 32,10));
}
// add a background image, from http://thetutorials.wordpress.com/2008/11/26/ps-cute-cartoon-clouds-the-simple-way/
add(new Background(0, 0));
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
super.render(container, game, g);
// render gui
g.drawString("Press WASD or ARROWS to move, X or UP to Jump", 65, 5);
}
}
Surprise! Code listed is not Entity logic, but World. Why this choice?
Because PlatformerEntity handle basic stuff: physics and player input, but more important, if there isn't into world to collide into, gravity move down PlatformerEntity. So we listed code of a world where we add Player as PlatformerEntity and more important blocks to build a world around it.
You can run this example and see how it works: player can move his sprite and fall from some platforms, hit walls and jump around, not bad, right?
You can play online using this link too.
References
Following games are platformer made with MarteEngine:
MarteEngine version 0.2