Android Games: Midori

De Aulas
Revisão de 09h24min de 29 de outubro de 2022 por Admin (discussão | contribs)
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)

Afluentes: Jogos Digitais

Midori game.jpg

Vídeoaula

Assets

Actor

  1package com.mygdx.game;
  2
  3import com.badlogic.gdx.Gdx;
  4import com.badlogic.gdx.Input;
  5import com.badlogic.gdx.graphics.g2d.Sprite;
  6import com.badlogic.gdx.graphics.g2d.TextureRegion;
  7
  8class Actor {
  9    private static final int w = 85;
 10    private static final int h = 100;
 11    private final int WALK = 0;
 12    private final int PAIN = 1;
 13    private final int HIT = 2;
 14    private final int JUMP = 3;
 15    private final int DIE = 4;
 16    private final int LEFT = 0;
 17    private final int RIGHT = 1;
 18    private final int SPEED = 5;
 19    private final float G = 0.99f;
 20    private final int stepMax = 8;
 21    private int step;
 22    private int action;
 23    private int direction;
 24    private boolean jumping = false;
 25    private MyGdxGame game;
 26    private Sprite sprite;
 27    private TextureRegion texture;
 28    private float vectorG = 0;
 29
 30    Actor(TextureRegion reg, int x, int y, MyGdxGame game) {
 31        this.game = game;
 32        texture = reg;
 33        vectorG = 0;
 34        direction = RIGHT;
 35        sprite = new Sprite(new TextureRegion(texture,
 36                w * step, (action * h * 2) + (direction * h), w, h));
 37        sprite.setPosition(x, y);
 38    }
 39
 40    void draw() {
 41        sprite.setRegion(texture, w * step, (action * h * 2) + (direction * h), w, h);
 42        sprite.draw(game.batch);
 43    }
 44
 45    void run() {
 46        gravity();
 47        if (step >= stepMax - 1) step = 0;
 48        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
 49            move(LEFT);
 50        } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
 51            move(RIGHT);
 52        }
 53        if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) {
 54            jump();
 55        }
 56        if (jumping) step = 3;
 57    }
 58
 59    private void move(int dir) {
 60        boolean moved = false;
 61        direction = dir;
 62        if (direction == LEFT) {
 63            if (game.isFree(sprite.getX(), sprite.getY())) {
 64                sprite.translateX(-SPEED);
 65                moved = true;
 66            }
 67        } else {
 68            if (game.isFree(sprite.getX() + sprite.getWidth(), sprite.getY())) {
 69                sprite.translateX(SPEED);
 70                moved = true;
 71            }
 72        }
 73        if (moved && !jumping) {
 74            step++;
 75            action = WALK;
 76        }
 77    }
 78
 79    private void jump() {
 80        if (jumping) return;
 81        game.sndJump.play();
 82        jumping = true;
 83        action = JUMP;
 84        sprite.translateY(10);
 85        vectorG = -15;
 86
 87    }
 88
 89    private void gravity() {
 90        boolean falling = false;
 91        int middle = w / 2;
 92        if (game.isFree(sprite.getX() + middle, sprite.getY() - 1)) {
 93            vectorG += G;
 94            sprite.translateY(-vectorG);
 95            falling = true;
 96        }
 97        if (!game.isFree(sprite.getX() + middle, sprite.getY())) {
 98            while (!game.isFree(sprite.getX() + middle, sprite.getY())) {
 99                sprite.translateY(1);
100            }
101            vectorG = 0;
102            jumping = false;
103            action = WALK;
104            falling = false;
105        }
106        if (!falling && jumping) jumping = false;
107    }
108}

MyGdxGame

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.ApplicationAdapter;
 4import com.badlogic.gdx.Gdx;
 5import com.badlogic.gdx.Input;
 6import com.badlogic.gdx.audio.Sound;
 7import com.badlogic.gdx.graphics.Color;
 8import com.badlogic.gdx.graphics.GL20;
 9import com.badlogic.gdx.graphics.Pixmap;
10import com.badlogic.gdx.graphics.Texture;
11import com.badlogic.gdx.graphics.g2d.SpriteBatch;
12import com.badlogic.gdx.graphics.g2d.TextureRegion;
13
14public class MyGdxGame extends ApplicationAdapter {
15	SpriteBatch batch;
16	Sound sndJump;
17	private Texture midori;
18	private Texture cenario;
19	private Pixmap mapa;
20	private Actor actor;
21	private int w, h;
22
23	@Override
24	public void create() {
25		w = Gdx.graphics.getWidth();
26		h = Gdx.graphics.getHeight();
27		batch = new SpriteBatch();
28		cenario = new Texture("cenario.png");
29		midori = new Texture("midori.png");
30		mapa = new Pixmap(Gdx.files.internal("mapa.png"));
31		sndJump = Gdx.audio.newSound(Gdx.files.internal("jump.wav"));
32		actor = new Actor(
33				new TextureRegion(midori, 0, 24, 680, 1000),
34				100, 200, this);
35	}
36
37	@Override
38	public void render() {
39		if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
40			Gdx.app.exit();
41		}
42		actor.run();
43
44		Gdx.gl.glClearColor(1, 0, 0, 1);
45		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
46		batch.begin();
47		batch.draw(cenario, 0, 0);
48		actor.draw();
49		batch.end();
50	}
51
52	boolean isFree(float px, float py) {
53		if (px < 0 || px > w || py < 0 || py > h) return false;
54		Color color = new Color();
55		Color.rgb888ToColor(color, mapa.getPixel((int) px, (int) (mapa.getHeight() - py)));
56		return color.r > 0.5f && color.g > 0.5f && color.b > 0.5f;
57	}
58
59	@Override
60	public void dispose() {
61		batch.dispose();
62		midori.dispose();
63		cenario.dispose();
64		mapa.dispose();
65		sndJump.dispose();
66	}
67}