Android Games: Blocks

De Aulas
Revisão de 14h59min de 26 de abril de 2018 por Admin (discussão | contribs) (→‎MyGdxGame)
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)

Links relacionados: Jogos Digitais

Vídeo Demonstrativo

Recursos

DesktopLauncher

 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 = "Blocks";
11		config.width = 800;
12		config.height = 600;
13		new LwjglApplication(new MyGdxGame(), config);
14	}
15}

Actor

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.graphics.g2d.Sprite;
 4import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 5import com.badlogic.gdx.graphics.g2d.TextureRegion;
 6
 7class Actor {
 8	final static int VOID = 9;
 9	final static int BLOCK = 0;
10	final static int MONSTER = 1;
11	final static int AVATAR = 2;
12	final static int DOWN = 0;
13	final static int RIGHT = 1;
14	final static int UP = 2;
15	final static int LEFT = 3;
16	final static int NONE = 4;
17	MyGdxGame game;
18	Actor next = null;
19	int x, y;
20	int type;
21	int direction = DOWN;
22	int pushTo = NONE;
23	private int step = 0;
24	private int w = 64;
25	private int h = 64;
26	private TextureRegion texture;
27	private Sprite sprite;
28	private int turn = 0;
29
30	Actor(MyGdxGame game, int type, int x, int y, TextureRegion texture) {
31		this.game = game;
32		this.type = type;
33		this.x = x;
34		this.y = y;
35		this.texture = texture;
36		sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
37	}
38
39	void execute() {
40	}
41
42	void run() {
43		if (turn == game.turn) return;
44		if (game.step == 1) {
45			step = 0;
46			return;
47		}
48		execute();
49		turn = game.turn;
50	}
51
52	boolean move(int dir) {
53		direction = dir;
54		next = null;
55		int nextX = x;
56		int nextY = y;
57		if (direction == UP) nextY++;
58		else if (direction == DOWN) nextY--;
59		else if (direction == LEFT) nextX--;
60		else if (direction == RIGHT) nextX++;
61		if ((nextX < 0) || (nextX >= game.col) ||
62				(nextY < 0) || (nextY >= game.row)) {
63			return false;
64		}
65		next = game.get(nextX, nextY);
66		if (next == null) {
67			game.jump(this, nextX, nextY);
68			step = 1;
69			return true;
70		}
71		return false;
72	}
73
74	void push(int dir) {
75		pushTo = dir;
76		game.sound.play();
77	}
78
79	void draw(SpriteBatch batch) {
80		final int squareSize = 21;
81		int px = (x * 43) - squareSize + 50;
82		int py = (y * 43) + 50;
83		if (step == 1) {
84			if (direction == UP) py -= 21;
85			else if (direction == DOWN) py += squareSize;
86			else if (direction == LEFT) px += squareSize;
87			else if (direction == RIGHT) px -= squareSize;
88		}
89		if (type == BLOCK) {
90			sprite.setRegion(texture, 0, 0, w, h);
91		} else {
92			sprite.setRegion(texture, w * step, h * direction, w, h);
93		}
94		sprite.setPosition(px, py);
95		sprite.draw(batch);
96	}
97}

Block

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.graphics.g2d.TextureRegion;
 4
 5class Block extends Actor {
 6	Block(MyGdxGame game, int x, int y, TextureRegion texture) {
 7		super(game, BLOCK, x, y, texture);
 8	}
 9
10	@Override
11	void execute() {
12		if (pushTo != NONE) {
13			if (!move(pushTo)) {
14				if (next != null) {
15					if (next.type == BLOCK) {
16						next.push(pushTo);
17					}
18				}
19			}
20		}
21		pushTo = NONE;
22	}
23}

Monster

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.graphics.g2d.TextureRegion;
 4
 5class Monster extends Actor {
 6	private int counter;
 7	private int max;
 8
 9	Monster(MyGdxGame game, int x, int y, TextureRegion texture) {
10		super(game, MONSTER, x, y, texture);
11		direction = game.rand.nextInt(4);
12		counter = 0;
13		max = game.rand.nextInt(10) + 1;
14	}
15
16	@Override
17	void execute() {
18		int dir = direction;
19		counter++;
20		if (counter > max) {
21			counter = 0;
22			dir = game.rand.nextInt(4);
23			max = game.rand.nextInt(10) + 1;
24		}
25		if (!move(dir)) {
26			if (next != null) {
27				if (next.type == BLOCK) {
28					next.push(direction);
29				} else {
30					direction = game.rand.nextInt(4);
31				}
32			}
33		}
34	}
35}

Avatar

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.Gdx;
 4import com.badlogic.gdx.Input;
 5import com.badlogic.gdx.graphics.g2d.TextureRegion;
 6
 7class Avatar extends Actor {
 8	Avatar(MyGdxGame game, int x, int y, TextureRegion texture) {
 9		super(game, AVATAR, x, y, texture);
10	}
11
12	@Override
13	void execute() {
14		float accelY = 0;
15		float accelX = 0;
16		if (game.accel) {
17			accelY = Gdx.input.getAccelerometerX() * 10;
18			accelX = Gdx.input.getAccelerometerY() * 10;
19		}
20		boolean moved = true;
21		if ((accelY > 20) || (Gdx.input.isKeyPressed(Input.Keys.DOWN))) {
22			moved = move(DOWN);
23		} else if ((accelY < -20) || (Gdx.input.isKeyPressed(Input.Keys.UP))) {
24			moved = move(UP);
25		} else if ((accelX > 20) || (Gdx.input.isKeyPressed(Input.Keys.RIGHT))) {
26			moved = move(RIGHT);
27		} else if ((accelX < -20) || (Gdx.input.isKeyPressed(Input.Keys.LEFT))) {
28			moved = move(LEFT);
29		}
30		if (!moved && next != null) {
31			if (next.type == BLOCK) {
32				next.push(direction);
33			}
34		}
35	}
36}

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.Input.Peripheral;
  7import com.badlogic.gdx.audio.Sound;
  8import com.badlogic.gdx.files.FileHandle;
  9import com.badlogic.gdx.graphics.GL20;
 10import com.badlogic.gdx.graphics.Texture;
 11import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 12import com.badlogic.gdx.graphics.g2d.TextureRegion;
 13
 14import java.util.Random;
 15
 16public class MyGdxGame extends ApplicationAdapter {
 17    private static final int animationMax = 4;
 18    final int col = 16;
 19    final int row = 11;
 20    Sound sound;
 21    Random rand = new Random();
 22    boolean accel;
 23    int turn = 0;
 24    int step = 0;
 25    SpriteBatch batch;
 26    private Actor gameBoard[][];
 27    private Texture ground;
 28    private Texture industrial;
 29    private TextureRegion avatarTexture;
 30    private TextureRegion monsterTexture;
 31    private TextureRegion blockTexture;
 32    private int animation;
 33
 34    @Override
 35    public void create() {
 36        batch = new SpriteBatch();
 37        accel = Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer);
 38        gameBoard = new Actor[col][row];
 39        load();
 40    }
 41
 42    private void execute() {
 43        animation++;
 44        if (animation < animationMax) return;
 45        animation = 0;
 46        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
 47            Gdx.app.exit();
 48        }
 49        for (int x = 0; x < col; x++) {
 50            for (int y = 0; y < row; y++) {
 51                if (gameBoard[x][y] != null) {
 52                    gameBoard[x][y].run();
 53                }
 54            }
 55        }
 56        step = (step == 0) ? 1 : 0;
 57        turn++;
 58    }
 59
 60    @Override
 61    public void render() {
 62        execute();
 63        Gdx.gl.glClearColor(0, 0, 0, 1);
 64        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
 65        batch.begin();
 66        batch.draw(industrial, 0, 0);
 67        batch.draw(ground, 50, 50);
 68        for (int y = row - 1; y >= 0; y--) {
 69            for (int x = 0; x < col; x++) {
 70                if (gameBoard[x][y] != null) {
 71                    gameBoard[x][y].draw();
 72                }
 73            }
 74        }
 75        batch.end();
 76    }
 77
 78    private void load() {
 79        Texture texture = new Texture("actors.png");
 80        avatarTexture = new TextureRegion(texture, 0, 0, 128, 256);
 81        monsterTexture = new TextureRegion(texture, 128, 0, 128, 256);
 82        blockTexture = new TextureRegion(new Texture("block.png"), 0, 0, 64, 64);
 83        ground = new Texture("background.png");
 84        industrial = new Texture("industrial.png");
 85        sound = Gdx.audio.newSound(Gdx.files.internal("grass.wav"));
 86
 87        FileHandle file = Gdx.files.internal("map.txt");
 88        String map = file.readString();
 89        int idx = 0;
 90        for (int y = row - 1; y > 0; y--) {
 91            for (int x = 0; x < col; x++) {
 92                String chr = "9";
 93                if (idx < map.length()) {
 94                    chr = "" + map.charAt(idx);
 95                    if (chr.compareTo(".") == 0) chr = "9";
 96                }
 97                switch (Integer.valueOf(chr)) {
 98                    case Actor.VOID:
 99                        gameBoard[x][y] = null;
100                        break;
101                    case Actor.BLOCK:
102                        gameBoard[x][y] = new Block(this, x, y, blockTexture);
103                        break;
104                    case Actor.MONSTER:
105                        gameBoard[x][y] = new Monster(this, x, y, monsterTexture);
106                        break;
107                    case Actor.AVATAR:
108                        gameBoard[x][y] = new Avatar(this, x, y, avatarTexture);
109                        break;
110                }
111                idx++;
112                if (map.charAt(idx) == '\n') idx++;
113            }
114        }
115
116    }
117
118    void jump(Actor actor, int x, int y) {
119        if (gameBoard[x][y] != null) return;
120        gameBoard[x][y] = actor;
121        gameBoard[actor.x][actor.y] = null;
122        actor.x = x;
123        actor.y = y;
124    }
125
126    Actor get(int x, int y) {
127        return gameBoard[x][y];
128    }
129
130    @Override
131    public void dispose() {
132        batch.dispose();
133        avatarTexture.getTexture().dispose();
134        blockTexture.getTexture().dispose();
135        monsterTexture.getTexture().dispose();
136        sound.dispose();
137        ground.dispose();
138    }
139}