Здравствуйте, продолжаем создавать игры на LibGDX – сегодня создадим простую игру про машинки: за основу возьмем игру, которую мы создавали раньше, вот в этих статьях:
Скачать исходники для статьи можно ниже
Android Studio и LibGDX: Создаем простую 2D игру (Часть 3: Анимация актера)
Android Studio и LibGDX: Создаем простую 2D игру (Часть 2)
Android Studio и LibGDX: Создаем простую 2D игру (Часть 1)
Выглядеть игра будет следующим образом:
Суть игры простая кликаем по экрану появляется новая машинка, которая едет слева направо.
Файлы игры:
Код файла ActorJugador:
package com.mygdx.game.actors; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Actor; public class ActorJugador extends Actor { private TextureAtlas textureAtlas; private Animation animation; private float stateTime; private boolean alive; public boolean isAlive() { return alive; } public void setAlive(boolean alive) { this.alive = alive; } public ActorJugador(TextureAtlas jugator){ this.alive=true; setSize(279,225 ); textureAtlas=new TextureAtlas(Gdx.files.internal("buldatlas.atlas")); animation=new Animation(1/7f,textureAtlas.getRegions()); } public void act (float delta){ super.act(delta); setX(getX()+15*delta); } public void draw (Batch batch, float parentAlpha){ super.draw(batch, parentAlpha); stateTime += Gdx.graphics.getDeltaTime(); batch.draw((TextureRegion) animation.getKeyFrame(stateTime, true), getX(), 10); } }
Код файла ActorMashina2:
package com.mygdx.game.actors; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Actor; public class ActorMashina2 extends Actor { private TextureAtlas textureAtlas; private Animation animation; private float stateTime; private boolean alive; public boolean isAlive() { return alive; } public void setAlive(boolean alive) { this.alive = alive; } public ActorMashina2(TextureAtlas jugator){ this.alive=true; setSize(279,225 ); textureAtlas=new TextureAtlas(Gdx.files.internal("mashatlas.atlas")); animation=new Animation(1/7f,textureAtlas.getRegions()); } public void act (float delta){ super.act(delta); setX(getX()+15*delta); } public void draw (Batch batch, float parentAlpha){ super.draw(batch, parentAlpha); stateTime += Gdx.graphics.getDeltaTime(); batch.draw((TextureRegion) animation.getKeyFrame(stateTime, true), getX(), 10); } }
Код файла ActorPinchos:
package com.mygdx.game.actors; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Actor; public class ActorPinchos extends Actor { private TextureRegion pinchos; public ActorPinchos (TextureRegion pinchos) { this.pinchos=pinchos; setSize(pinchos.getRegionWidth(), pinchos.getRegionHeight()); } public void act (float delta){ setX(getX()-50*delta); } @Override public void draw(Batch batch, float parentAlpha) { batch.draw(pinchos, getX(), getY()); } }
Код файла BaseScreen:
package com.mygdx.game; import com.badlogic.gdx.Screen; public abstract class BaseScreen implements Screen { public MainGame game; public BaseScreen(MainGame game) { this.game=game; } @Override public void show() { } @Override public void render(float delta) { } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } @Override public void hide() { } @Override public void dispose() { } }
Код файла GameOverScreen:
package com.mygdx.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.utils.viewport.FillViewport; public class GameOverScreen extends BaseScreen { private Stage stage; private Skin skin; private Image gameover; private TextButton retry, menu; public GameOverScreen(final MainGame game) { super(game); stage=new Stage(new FillViewport(640, 360)); skin=new Skin(Gdx.files.internal("skin/uiskin.json")); gameover = new Image(new Texture(Gdx.files.internal("gameover.png"))); retry=new TextButton("Retry",skin); menu=new TextButton("Menu", skin); retry.addCaptureListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { game.setScreen(game.gameScreen); } }); menu.addCaptureListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { game.setScreen(game.menuScreen); } }); gameover.setPosition(520-gameover.getWidth(), 320- gameover.getHeight()); retry.setSize(200,100); retry.setPosition(350,50); stage.addActor(retry); menu.setSize(200,100); menu.setPosition(100,50); stage.addActor(menu); stage.addActor(gameover); } @Override public void show() { Gdx.input.setInputProcessor(stage); } @Override public void hide() { Gdx.input.setInputProcessor(null); } @Override public void dispose() { super.dispose(); } @Override public void render(float delta) { Gdx.gl.glClearColor(0.4f, 0.5f, 0.8f, 1f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } }
Код файла MainGame:
package com.mygdx.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.Game; public class MainGame extends Game { public MainGameScreen gameScreen; public GameOverScreen gameOverScreen; public MenuScreen menuScreen; @Override public void create () { menuScreen=new MenuScreen(this); gameScreen=new MainGameScreen(this); gameOverScreen=new GameOverScreen(this); setScreen(menuScreen); } }
Код файла MainGameScreen:
package com.mygdx.game; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; import com.mygdx.game.actors.ActorJugador; import com.mygdx.game.actors.ActorMashina2; import com.mygdx.game.actors.ActorPinchos; public class MainGameScreen extends BaseScreen { public MainGameScreen (MainGame game) { super(game); texturePinchos=new Texture("pinchos.png"); regionPinchos=new TextureRegion(texturePinchos, 0, 64, 128, 64); } public static Texture backgroundTexture1; private Stage stage; private ActorJugador jugador; private ActorPinchos pinchos; private ActorMashina2 mashina2; private Texture texturePinchos; private TextureRegion regionPinchos; private TextureAtlas textureAtlas; float tim = 0; @Override public void show() { backgroundTexture1 = new Texture("bg.png"); stage = new Stage(); jugador = new ActorJugador(textureAtlas); mashina2 = new ActorMashina2(textureAtlas); pinchos=new ActorPinchos(regionPinchos); stage.addActor(jugador); stage.addActor(mashina2); stage.addActor(pinchos); jugador.setPosition(20,100); mashina2.setPosition(850,100); pinchos.setPosition(500,100); } @Override public void hide() { stage.dispose(); } @Override public void render(float delta) { Gdx.gl.glClearColor(0.4f, 0.5f, 0.8f, 1f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.getBatch().begin(); stage.getBatch().draw(backgroundTexture1, 0, 0, 800, 480); stage.getBatch().end(); comprobarColisiones(); stage.draw(); } private void comprobarColisiones(){ if (Gdx.input.isKeyPressed(Input.Keys.ANY_KEY) || Gdx.input.justTouched()) { jugador.remove(); tim += 1; if (tim == 1) { mashina2.setPosition(50,100); } //game.setScreen(game.gameOverScreen); if (tim == 2) { mashina2.remove(); } if (tim == 3) { game.setScreen(game.gameOverScreen); tim=0; } } if (jugador.isAlive()&&jugador.getX()>pinchos.getX()){ // if (jugador.isAlive()&&jugador.getX()+jugador.getWidth()>pinchos.getX()){ //System.out.println("Colision"); pinchos.setPosition(800,100); //jugador.setAlive(false); //game.setScreen(game.gameOverScreen); } } @Override public void dispose() { texturePinchos.dispose(); textureAtlas.dispose(); } }
Код файла MenuScreen:
package com.mygdx.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.Button; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.utils.viewport.FillViewport; public class MenuScreen extends BaseScreen { private Stage stage; private Skin skin; private Image gameover; //private TextButton retry; private Button retry; public MenuScreen(final MainGame game) { super(game); stage=new Stage(new FillViewport(640, 360)); skin=new Skin(Gdx.files.internal("skin/uiskin.json")); gameover = new Image(); //retry=new TextButton("Play",skin); retry=new Button(skin); retry.addCaptureListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { game.setScreen(game.gameScreen); } }); gameover.setPosition(320-gameover.getWidth(), 320- gameover.getHeight()); retry.setSize(200,100); retry.setPosition(220,50); stage.addActor(retry); stage.addActor(gameover); } @Override public void show() { Gdx.input.setInputProcessor(stage); } @Override public void hide() { Gdx.input.setInputProcessor(null); } @Override public void dispose() { super.dispose(); } @Override public void render(float delta) { Gdx.gl.glClearColor(0.3f, 0.4f, 0.7f, 1f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } }
Медиафайлы для игры:
Папку с медиафайлами можно загрузить – здесь.
PS:
Папку с игрой можно скачать – здесь.
APK файл игры можно скачать – здесь.