Android Games: Exemplo 01

De Aulas

Links relacionados: Jogos Digitais

Arquivos utilizados

Colocá-lo na pasta assept do projeto do android:

Main no projeto desktop

 1package com.mygdx.game.desktop;
 2
 3import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
 4import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
 5import com.mygdx.game.MyGdxGame;
 6
 7public class DesktopLauncher {
 8    public static void main(String[] arg) {
 9        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
10        config.title = "Primeiro";
11        config.width = 800;
12        config.height = 600;
13        new LwjglApplication(new MyGdxGame(), config);
14    }
15}

Classe Game do projeto principal

  1package com.mygdx.game;
  2
  3// claudius.png 224 x 256
  4// ground.png = 800 x 600
  5
  6import com.badlogic.gdx.ApplicationAdapter;
  7import com.badlogic.gdx.Gdx;
  8import com.badlogic.gdx.graphics.GL20;
  9import com.badlogic.gdx.Input.Keys;
 10import com.badlogic.gdx.graphics.Color;
 11import com.badlogic.gdx.graphics.Pixmap;
 12import com.badlogic.gdx.graphics.Texture;
 13import com.badlogic.gdx.graphics.g2d.Sprite;
 14import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 15import com.badlogic.gdx.graphics.g2d.TextureRegion;
 16
 17public class MyGdxGame extends ApplicationAdapter {
 18    private SpriteBatch batch;
 19    private int avatarWidth, avatarHeight;
 20    private int x, y;
 21    private int direction, step;
 22    private Texture avatarTexture;
 23    private Texture groundTexture;
 24    private Sprite avatarSprite;
 25    private Pixmap groundMask;
 26
 27    @Override
 28    public void create() {
 29        batch = new SpriteBatch();
 30        groundMask = new Pixmap(Gdx.files.internal("ground_mask.png"));
 31        groundTexture = new Texture("ground.png");
 32        avatarTexture = new Texture("claudius.png");
 33        avatarWidth = 224 / 7;
 34        avatarHeight = 256 / 4;
 35        x = 300;
 36        y = 240;
 37    }
 38
 39    public boolean path(int px, int py) {
 40        if ((px < 0) || (px >= 800) || (py <= 0) || (py >= 600)) {
 41            return false;
 42        }
 43        Color color = new Color();
 44        Color.rgb888ToColor(color, groundMask.getPixel(px, py));
 45        return ((color.r > 0.1) && (color.g > 0.1) && (color.b > 0.1));
 46    }
 47
 48    //@Override
 49    public void dispose() {
 50        batch.dispose();
 51        groundTexture.dispose();
 52        avatarTexture.dispose();
 53        groundMask.dispose();
 54    }
 55
 56    public void drawAvatar(SpriteBatch batch, int posX, int posY, int step, int dir) {
 57        int px = step * avatarWidth;
 58        int py = dir * avatarHeight;
 59        TextureRegion region = new TextureRegion(avatarTexture, px, py, avatarWidth, avatarHeight);
 60        if (avatarSprite == null) {
 61            avatarSprite = new Sprite(region);
 62        } else {
 63            avatarSprite.setRegion(region);
 64        }
 65        avatarSprite.setPosition(posX, posY);
 66        avatarSprite.draw(batch);
 67    }
 68
 69    private void processInput() {
 70        if (Gdx.input.isKeyPressed(Keys.UP)) {
 71            direction = 2;
 72            if (path(x + 16, y + avatarHeight)) {
 73                step++;
 74                y += 2;
 75            }
 76        }
 77        if (Gdx.input.isKeyPressed(Keys.DOWN)) {
 78            direction = 0;
 79            if (path(x + 16, y)) {
 80                step++;
 81                y -= 2;
 82            }
 83        }
 84        if (Gdx.input.isKeyPressed(Keys.LEFT)) {
 85            direction = 1;
 86            if (path(x, y + (avatarHeight / 2))) {
 87                x -= 2;
 88                step++;
 89            }
 90        }
 91        if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
 92            direction = 3;
 93            if (path(x + avatarWidth, y + (avatarHeight / 2))) {
 94                step++;
 95                x += 2;
 96            }
 97        }
 98        if (step >= 7) {
 99            step = 0;
100        }
101    }
102
103    @Override
104    public void render() {
105        processInput();
106        Gdx.gl.glClearColor(0, 0, 0, 1);
107        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
108        batch.begin();
109        batch.draw(groundTexture, 0, 0);
110        drawAvatar(batch, x, y, step, direction);
111        batch.end();
112    }
113}