Mudanças entre as edições de "Android Games: Blocks"
De Aulas
Linha 16: | Linha 16: | ||
= DesktopLauncher = | = DesktopLauncher = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game.desktop; | package com.mygdx.game.desktop; | ||
Linha 36: | Linha 36: | ||
= Actor = | = Actor = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game; | package com.mygdx.game; | ||
Linha 138: | Linha 138: | ||
= Block = | = Block = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game; | package com.mygdx.game; | ||
Linha 166: | Linha 166: | ||
= Monster = | = Monster = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game; | package com.mygdx.game; | ||
Linha 206: | Linha 206: | ||
= Avatar = | = Avatar = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game; | package com.mygdx.game; | ||
Linha 247: | Linha 247: | ||
= MyGdxGame = | = MyGdxGame = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game; | package com.mygdx.game; | ||
Edição atual tal como às 18h30min de 27 de outubro de 2022
Links relacionados: Jogos Digitais
Vídeo Demonstrativo
Recursos
DesktopLauncher
package com.mygdx.game.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.MyGdxGame;
public class DesktopLauncher {
public static void main(String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "Blocks";
config.width = 800;
config.height = 600;
new LwjglApplication(new MyGdxGame(), config);
}
}
Actor
package com.mygdx.game;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
class Actor {
final static int VOID = 9;
final static int BLOCK = 0;
final static int MONSTER = 1;
final static int AVATAR = 2;
final static int DOWN = 0;
final static int RIGHT = 1;
final static int UP = 2;
final static int LEFT = 3;
final static int NONE = 4;
MyGdxGame game;
Actor next = null;
int x, y;
int type;
int direction = DOWN;
int pushTo = NONE;
private int step = 0;
private int w = 64;
private int h = 64;
private TextureRegion texture;
private Sprite sprite;
private int turn = 0;
Actor(MyGdxGame game, int type, int x, int y, TextureRegion texture) {
this.game = game;
this.type = type;
this.x = x;
this.y = y;
this.texture = texture;
sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
}
void execute() {
}
void run() {
if (turn == game.turn) return;
if (game.step == 1) {
step = 0;
return;
}
execute();
turn = game.turn;
}
boolean move(int dir) {
direction = dir;
next = null;
int nextX = x;
int nextY = y;
if (direction == UP) nextY++;
else if (direction == DOWN) nextY--;
else if (direction == LEFT) nextX--;
else if (direction == RIGHT) nextX++;
if ((nextX < 0) || (nextX >= game.col) ||
(nextY < 0) || (nextY >= game.row)) {
return false;
}
next = game.get(nextX, nextY);
if (next == null) {
game.jump(this, nextX, nextY);
step = 1;
return true;
}
return false;
}
void push(int dir) {
pushTo = dir;
game.sound.play();
}
void draw(SpriteBatch batch) {
final int squareSize = 21;
int px = (x * 43) - squareSize + 50;
int py = (y * 43) + 50;
if (step == 1) {
if (direction == UP) py -= 21;
else if (direction == DOWN) py += squareSize;
else if (direction == LEFT) px += squareSize;
else if (direction == RIGHT) px -= squareSize;
}
if (type == BLOCK) {
sprite.setRegion(texture, 0, 0, w, h);
} else {
sprite.setRegion(texture, w * step, h * direction, w, h);
}
sprite.setPosition(px, py);
sprite.draw(batch);
}
}
Block
package com.mygdx.game;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
class Block extends Actor {
Block(MyGdxGame game, int x, int y, TextureRegion texture) {
super(game, BLOCK, x, y, texture);
}
@Override
void execute() {
if (pushTo != NONE) {
if (!move(pushTo)) {
if (next != null) {
if (next.type == BLOCK) {
next.push(pushTo);
}
}
}
}
pushTo = NONE;
}
}
Monster
package com.mygdx.game;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
class Monster extends Actor {
private int counter;
private int max;
Monster(MyGdxGame game, int x, int y, TextureRegion texture) {
super(game, MONSTER, x, y, texture);
direction = game.rand.nextInt(4);
counter = 0;
max = game.rand.nextInt(10) + 1;
}
@Override
void execute() {
int dir = direction;
counter++;
if (counter > max) {
counter = 0;
dir = game.rand.nextInt(4);
max = game.rand.nextInt(10) + 1;
}
if (!move(dir)) {
if (next != null) {
if (next.type == BLOCK) {
next.push(direction);
} else {
direction = game.rand.nextInt(4);
}
}
}
}
}
Avatar
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
class Avatar extends Actor {
Avatar(MyGdxGame game, int x, int y, TextureRegion texture) {
super(game, AVATAR, x, y, texture);
}
@Override
void execute() {
float accelY = 0;
float accelX = 0;
if (game.accel) {
accelY = Gdx.input.getAccelerometerX() * 10;
accelX = Gdx.input.getAccelerometerY() * 10;
}
boolean moved = true;
if ((accelY > 20) || (Gdx.input.isKeyPressed(Input.Keys.DOWN))) {
moved = move(DOWN);
} else if ((accelY < -20) || (Gdx.input.isKeyPressed(Input.Keys.UP))) {
moved = move(UP);
} else if ((accelX > 20) || (Gdx.input.isKeyPressed(Input.Keys.RIGHT))) {
moved = move(RIGHT);
} else if ((accelX < -20) || (Gdx.input.isKeyPressed(Input.Keys.LEFT))) {
moved = move(LEFT);
}
if (!moved && next != null) {
if (next.type == BLOCK) {
next.push(direction);
}
}
}
}
MyGdxGame
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
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;
import java.util.Random;
public class MyGdxGame extends ApplicationAdapter {
private static final int animationMax = 4;
final int col = 16;
final int row = 11;
Sound sound;
Random rand = new Random();
boolean accel;
int turn = 0;
int step = 0;
SpriteBatch batch;
private Actor gameBoard[][];
private Texture ground;
private Texture industrial;
private TextureRegion avatarTexture;
private TextureRegion monsterTexture;
private TextureRegion blockTexture;
private int animation;
@Override
public void create() {
batch = new SpriteBatch();
accel = Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer);
gameBoard = new Actor[col][row];
load();
}
private void execute() {
animation++;
if (animation < animationMax) return;
animation = 0;
if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
Gdx.app.exit();
}
for (int x = 0; x < col; x++) {
for (int y = 0; y < row; y++) {
if (gameBoard[x][y] != null) {
gameBoard[x][y].run();
}
}
}
step = (step == 0) ? 1 : 0;
turn++;
}
@Override
public void render() {
execute();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(industrial, 0, 0);
batch.draw(ground, 50, 50);
for (int y = row - 1; y >= 0; y--) {
for (int x = 0; x < col; x++) {
if (gameBoard[x][y] != null) {
gameBoard[x][y].draw();
}
}
}
batch.end();
}
private void load() {
Texture texture = new Texture("actors.png");
avatarTexture = new TextureRegion(texture, 0, 0, 128, 256);
monsterTexture = new TextureRegion(texture, 128, 0, 128, 256);
blockTexture = new TextureRegion(new Texture("block.png"), 0, 0, 64, 64);
ground = new Texture("background.png");
industrial = new Texture("industrial.png");
sound = Gdx.audio.newSound(Gdx.files.internal("grass.wav"));
FileHandle file = Gdx.files.internal("map.txt");
String map = file.readString();
int idx = 0;
for (int y = row - 1; y > 0; y--) {
for (int x = 0; x < col; x++) {
String chr = "9";
if (idx < map.length()) {
chr = "" + map.charAt(idx);
if (chr.compareTo(".") == 0) chr = "9";
}
switch (Integer.valueOf(chr)) {
case Actor.VOID:
gameBoard[x][y] = null;
break;
case Actor.BLOCK:
gameBoard[x][y] = new Block(this, x, y, blockTexture);
break;
case Actor.MONSTER:
gameBoard[x][y] = new Monster(this, x, y, monsterTexture);
break;
case Actor.AVATAR:
gameBoard[x][y] = new Avatar(this, x, y, avatarTexture);
break;
}
idx++;
if (map.charAt(idx) == '\n') idx++;
}
}
}
void jump(Actor actor, int x, int y) {
if (gameBoard[x][y] != null) return;
gameBoard[x][y] = actor;
gameBoard[actor.x][actor.y] = null;
actor.x = x;
actor.y = y;
}
Actor get(int x, int y) {
return gameBoard[x][y];
}
@Override
public void dispose() {
batch.dispose();
avatarTexture.getTexture().dispose();
blockTexture.getTexture().dispose();
monsterTexture.getTexture().dispose();
sound.dispose();
ground.dispose();
}
}