Mudanças entre as edições de "Android Games: Walkers"
De Aulas
(5 revisões intermediárias pelo mesmo usuário não estão sendo mostradas) | |||
Linha 11: | Linha 11: | ||
= Actor.class = | = Actor.class = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game; | package com.mygdx.game; | ||
Linha 24: | Linha 24: | ||
static final int UP = 2; | static final int UP = 2; | ||
static final int LEFT = 3; | static final int LEFT = 3; | ||
− | |||
private static final int SPEED = 2; | private static final int SPEED = 2; | ||
int direction = DOWN; | int direction = DOWN; | ||
MyGdxGame game; | MyGdxGame game; | ||
Boolean collided = false; | Boolean collided = false; | ||
− | private Sprite sprite; | + | private final Sprite sprite; |
public int w = 64; | public int w = 64; | ||
public int h = 64; | public int h = 64; | ||
private int step = 0; | private int step = 0; | ||
private int counter; | private int counter; | ||
+ | private static final int counterMax = 10; | ||
private boolean moved = false; | private boolean moved = false; | ||
Linha 70: | Linha 70: | ||
moved = true; | moved = true; | ||
} | } | ||
− | sprite. | + | sprite.setPosition( |
− | + | clamp(sprite.getX(), 0, game.w - w - 1), | |
+ | clamp(sprite.getY(), 0, game.h - h - 1)); | ||
} | } | ||
public float clamp(float val, float min, float max) { | public float clamp(float val, float min, float max) { | ||
− | if (val < min) return min | + | if (val < min) return min; |
if (val > max) return max; | if (val > max) return max; | ||
return val; | return val; | ||
Linha 81: | Linha 82: | ||
Boolean collision(Actor other) { | Boolean collision(Actor other) { | ||
− | return | + | return this != other && |
Intersector.overlaps( | Intersector.overlaps( | ||
sprite.getBoundingRectangle(), | sprite.getBoundingRectangle(), | ||
Linha 96: | Linha 97: | ||
= Avatar.class = | = Avatar.class = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game; | package com.mygdx.game; | ||
Linha 142: | Linha 143: | ||
Monster(Texture texture, MyGdxGame game) { | Monster(Texture texture, MyGdxGame game) { | ||
− | super(rand.nextInt(game.w - | + | super(game.rand.nextInt(game.w - 64), |
+ | game.rand.nextInt(game.h - 64), texture, game); | ||
+ | new_goal(); | ||
+ | } | ||
+ | |||
+ | private void new_goal() { | ||
direction = game.rand.nextInt(4); | direction = game.rand.nextInt(4); | ||
+ | count = 0; | ||
max = game.rand.nextInt(20) + 1; | max = game.rand.nextInt(20) + 1; | ||
} | } | ||
Linha 150: | Linha 157: | ||
public void execute() { | public void execute() { | ||
count++; | count++; | ||
− | if (count > max) | + | if (count > max) new_goal(); |
− | + | move(direction); | |
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
Linha 172: | Linha 174: | ||
import com.badlogic.gdx.graphics.g2d.BitmapFont; | import com.badlogic.gdx.graphics.g2d.BitmapFont; | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | import com.badlogic.gdx.graphics.g2d.SpriteBatch; | ||
+ | import com.badlogic.gdx.utils.ScreenUtils; | ||
import com.badlogic.gdx.utils.TimeUtils; | import com.badlogic.gdx.utils.TimeUtils; | ||
Linha 179: | Linha 182: | ||
public class MyGdxGame extends ApplicationAdapter { | public class MyGdxGame extends ApplicationAdapter { | ||
− | + | int w, h; | |
− | + | SpriteBatch batch; | |
− | + | Random rand = new Random(); | |
− | + | List<Actor> actors = new ArrayList<Actor>(); | |
− | + | private Texture avatarTexture; | |
− | + | private Texture monsterTexture; | |
− | + | private BitmapFont font; | |
− | + | private Avatar avatar; | |
− | |||
− | |||
− | |||
− | |||
− | + | @Override | |
− | + | public void create() { | |
− | + | w = Gdx.graphics.getWidth(); | |
− | + | h = Gdx.graphics.getHeight(); | |
− | + | batch = new SpriteBatch(); | |
− | + | avatarTexture = new Texture("avatar.png"); | |
− | + | monsterTexture = new Texture("monster.png"); | |
− | + | for (int i = 0; i < 10; i++) { | |
− | + | actors.add(new Monster(monsterTexture, this)); | |
− | + | } | |
− | + | actors.add(avatar = new Avatar(100, 100, avatarTexture, this)); | |
− | + | font = new BitmapFont( | |
− | + | Gdx.files.internal("verdana.fnt"), | |
− | + | Gdx.files.internal("verdana.png"), false); | |
− | + | font.getData().setScale(0.75f, 0.75f); | |
− | + | font.setColor(1, 1, 0, 1); | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | + | private void execute() { | |
− | + | if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) { | |
− | + | Gdx.app.exit(); | |
− | + | } | |
− | + | for (Actor a : actors) a.run(); | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | @Override | |
− | + | public void render() { | |
− | + | execute(); | |
− | + | ScreenUtils.clear(0, 0.5f, 0, 1); | |
− | + | float delta = Gdx.graphics.getDeltaTime(); | |
− | + | float frameRate = Gdx.graphics.getFramesPerSecond(); | |
− | + | batch.begin(); | |
− | + | for (Actor a : actors) a.draw(); | |
− | + | String text = avatar.collided ? "Collided! " : "I'm free... "; | |
− | + | font.draw(batch, text + (int) frameRate + " fps" + " delta: " + delta, 1, h + 1); | |
− | + | batch.end(); | |
− | + | } | |
− | |||
− | + | @Override | |
− | + | public void dispose() { | |
− | + | batch.dispose(); | |
− | + | avatarTexture.dispose(); | |
− | + | monsterTexture.dispose(); | |
− | + | } | |
− | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Edição atual tal como às 10h11min de 29 de outubro de 2022
Links relacionados: Jogos Digitais
Vídeo Demonstrativo
Recursos
Actor.class
package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Intersector;
class Actor {
static final int DOWN = 0;
static final int RIGHT = 1;
static final int UP = 2;
static final int LEFT = 3;
private static final int SPEED = 2;
int direction = DOWN;
MyGdxGame game;
Boolean collided = false;
private final Sprite sprite;
public int w = 64;
public int h = 64;
private int step = 0;
private int counter;
private static final int counterMax = 10;
private boolean moved = false;
Actor(float x, float y, Texture texture, MyGdxGame game) {
this.game = game;
this.sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
sprite.setPosition(x, y);
}
void execute() {
}
void run() {
execute();
counter++;
if (counter > counterMax) {
counter = 0;
if (moved) step = (step == 1) ? 0 : 1;
}
moved = false;
}
void move(int dir) {
direction = dir;
if (direction == UP) {
sprite.translateY(SPEED);
moved = true;
} else if (direction == DOWN) {
sprite.translateY(-SPEED);
moved = true;
} else if (direction == RIGHT) {
sprite.translateX(SPEED);
moved = true;
} else if (direction == LEFT) {
sprite.translateX(-SPEED);
moved = true;
}
sprite.setPosition(
clamp(sprite.getX(), 0, game.w - w - 1),
clamp(sprite.getY(), 0, game.h - h - 1));
}
public float clamp(float val, float min, float max) {
if (val < min) return min;
if (val > max) return max;
return val;
}
Boolean collision(Actor other) {
return this != other &&
Intersector.overlaps(
sprite.getBoundingRectangle(),
other.sprite.getBoundingRectangle());
}
void draw() {
sprite.setRegion(step * w, direction * h, w, h);
sprite.draw(game.batch);
}
}
Avatar.class
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
class Avatar extends Actor {
Avatar(float x, float y, Texture texture, MyGdxGame game) {
super(x, y, texture, game);
}
@Override
public void execute() {
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
move(UP);
} else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
move(DOWN);
} else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
move(RIGHT);
} else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
move(LEFT);
}
collided = false;
for (Actor a : game.actors) {
if (collision(a)) {
collided = true;
break;
}
}
}
}
Monster.class
package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
class Monster extends Actor {
private int count = 0;
private int max;
Monster(Texture texture, MyGdxGame game) {
super(game.rand.nextInt(game.w - 64),
game.rand.nextInt(game.h - 64), texture, game);
new_goal();
}
private void new_goal() {
direction = game.rand.nextInt(4);
count = 0;
max = game.rand.nextInt(20) + 1;
}
@Override
public void execute() {
count++;
if (count > max) new_goal();
move(direction);
}
}
MyGdxGame.class
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.TimeUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MyGdxGame extends ApplicationAdapter {
int w, h;
SpriteBatch batch;
Random rand = new Random();
List<Actor> actors = new ArrayList<Actor>();
private Texture avatarTexture;
private Texture monsterTexture;
private BitmapFont font;
private Avatar avatar;
@Override
public void create() {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
batch = new SpriteBatch();
avatarTexture = new Texture("avatar.png");
monsterTexture = new Texture("monster.png");
for (int i = 0; i < 10; i++) {
actors.add(new Monster(monsterTexture, this));
}
actors.add(avatar = new Avatar(100, 100, avatarTexture, this));
font = new BitmapFont(
Gdx.files.internal("verdana.fnt"),
Gdx.files.internal("verdana.png"), false);
font.getData().setScale(0.75f, 0.75f);
font.setColor(1, 1, 0, 1);
}
private void execute() {
if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
Gdx.app.exit();
}
for (Actor a : actors) a.run();
}
@Override
public void render() {
execute();
ScreenUtils.clear(0, 0.5f, 0, 1);
float delta = Gdx.graphics.getDeltaTime();
float frameRate = Gdx.graphics.getFramesPerSecond();
batch.begin();
for (Actor a : actors) a.draw();
String text = avatar.collided ? "Collided! " : "I'm free... ";
font.draw(batch, text + (int) frameRate + " fps" + " delta: " + delta, 1, h + 1);
batch.end();
}
@Override
public void dispose() {
batch.dispose();
avatarTexture.dispose();
monsterTexture.dispose();
}
}