Finally the last chapter, or maybe not 🙂 I may add more chapters after this but this is the regular game. Lets add the game over screen and get it over.
Nothing really new other than gameoverscreen, enjoy your finished game
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 |
import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; public class GameOverScreen extends AbstractScreen { TextureRegion intro; SpriteBatch batch; float time = 0; public GameOverScreen (Game game) { super(game); } @Override public void show () { intro = new TextureRegion(new Texture(Gdx.files.internal("data/gameover.png")), 0, 0, 480, 320); batch = new SpriteBatch(); batch.getProjectionMatrix().setToOrtho2D(0, 0, 480, 320); } @Override public void render (float delta) { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); batch.draw(intro, 0, 0); batch.end(); time += delta; if (time > 1) { if (Gdx.input.isKeyPressed(Keys.ANY_KEY) || Gdx.input.justTouched()) { game.setScreen(new MainMenu(game)); } } } @Override public void hide () { Gdx.app.debug("Platformer", "dispose intro"); batch.dispose(); intro.getTexture().dispose(); } } |
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 |
public void render (float deltaTime) { if (map.cube.state != Cube.CONTROLLED){ cam.position.lerp(lerpTarget.set(map.bob.pos.x, map.bob.pos.y, 0), 2f * deltaTime); }else{ cam.position.lerp(lerpTarget.set(map.cube.pos.x, map.cube.pos.y, 0), 2f * deltaTime); } cam.update(); cache.setProjectionMatrix(cam.combined); Gdx.gl.glDisable(GL20.GL_BLEND); cache.begin(); for (int blockY = 0; blockY < 4; blockY++) { for (int blockX = 0; blockX < 6; blockX++) { cache.draw(blocks[blockX][blockY]); } } cache.end(); stateTime += deltaTime; batch.setProjectionMatrix(cam.combined); batch.begin(); if (map.endDoor != null) batch.draw(endDoor, map.endDoor.bounds.x, map.endDoor.bounds.y, 1, 1); renderLasers(); renderBob(); renderCube(); renderDispensers(); renderRockets(); renderMovingSpikes(); batch.end(); renderLaserBeams(); stage.draw(); if(currentScore != map.getScore()){ scoreLabel.setText("Score: " + map.getScore()); currentScore = map.getScore(); } } |
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 |
import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.GL20; import com.metsblog.tutorial.entity.Map; import com.metsblog.tutorial.entity.MapRenderer; public class GameScreen extends AbstractScreen { Map map; MapRenderer mapRenderer; public GameScreen (Game game) { super(game); } @Override public void show () { map = new Map(); mapRenderer = new MapRenderer(map); } @Override public void render (float delta) { delta = Math.min(0.06f, Gdx.graphics.getDeltaTime()); map.update(delta); Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); mapRenderer.render(delta); if (map.bob.bounds.overlaps(map.endDoor.bounds)) { game.setScreen(new GameOverScreen(game)); } if (Gdx.input.isKeyPressed(Keys.ESCAPE)) { game.setScreen(new MainMenu(game)); } } @Override public void hide () { Gdx.app.debug("Platformer", "dispose game screen"); } } |