Android Games: Garden
De Aulas
Links relacionados: Jogos Digitais
Vídeo Demonstrativo
Recursos
DesktopLauncher.java
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 = "Garden";
11 config.width = 800;
12 config.height = 600;
13 new LwjglApplication(new MyGdxGame(), config);
14 }
15}
Actor.java
1package com.mygdx.game;
2
3import com.badlogic.gdx.Gdx;
4import com.badlogic.gdx.Input;
5import com.badlogic.gdx.graphics.Texture;
6import com.badlogic.gdx.graphics.g2d.Sprite;
7import com.badlogic.gdx.graphics.g2d.TextureRegion;
8
9class Actor {
10 private final int DOWN = 0;
11 private final int LEFT = 1;
12 private final int UP = 2;
13 private final int RIGHT = 3;
14 private MyGdxGame game;
15 private float x, y;
16 private Texture texture;
17 private Sprite sprite;
18 private int direction = DOWN;
19 private int step = 0;
20 private int w, h;
21
22 Actor(float x, float y, Texture texture, MyGdxGame game) {
23 this.game = game;
24 this.texture = texture;
25 this.x = x;
26 this.y = y;
27 w = 224 / 7;
28 h = 256 / 4;
29 sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
30 }
31
32 void run() {
33 if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
34 move(UP);
35 } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
36 move(LEFT);
37 } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
38 move(DOWN);
39 } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
40 move(RIGHT);
41 }
42 }
43
44 private void move(int dir) {
45 int SPEED = 5;
46 direction = dir;
47 switch (dir) {
48 case DOWN:
49 if (game.isFree((int) x + (w / 2), (int) y - SPEED)) {
50 step++;
51 y -= SPEED;
52 }
53 break;
54 case UP:
55 if (game.isFree((int) x + (w / 2), (int) y + 20)) {
56 step++;
57 y += SPEED;
58 }
59 break;
60 case LEFT:
61 if (game.isFree((int) x - SPEED, (int) y)) {
62 x -= SPEED;
63 step++;
64 }
65 break;
66 case RIGHT:
67 if (game.isFree((int) x + w + SPEED, (int) y)) {
68 step++;
69 x += SPEED;
70 }
71 break;
72 }
73 if (step >= 7) step = 0;
74 }
75
76 void draw() {
77 sprite.setRegion(new TextureRegion(texture, step * w, direction * h, w, h));
78 sprite.setPosition(x, y);
79 sprite.draw(game.batch);
80 }
81}
MyGdxGame.java
1package com.mygdx.game;
2
3import com.badlogic.gdx.ApplicationAdapter;
4import com.badlogic.gdx.Gdx;
5import com.badlogic.gdx.Input;
6import com.badlogic.gdx.graphics.Color;
7import com.badlogic.gdx.graphics.GL20;
8import com.badlogic.gdx.graphics.Pixmap;
9import com.badlogic.gdx.graphics.Texture;
10import com.badlogic.gdx.graphics.g2d.SpriteBatch;
11
12public class MyGdxGame extends ApplicationAdapter {
13 SpriteBatch batch;
14 private Pixmap mask;
15 private Texture ground;
16 private Texture kaneda;
17 private Actor actor;
18
19 @Override
20 public void create() {
21 batch = new SpriteBatch();
22 mask = new Pixmap(Gdx.files.internal("mask.png"));
23 ground = new Texture("ground.png");
24 kaneda = new Texture("kaneda.png");
25 actor = new Actor(300, 240, kaneda, this);
26 }
27
28 @Override
29 public void render() {
30 if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
31 Gdx.app.exit();
32 }
33 actor.run();
34 Gdx.gl.glClearColor(1, 0, 0, 1);
35 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
36 batch.begin();
37 batch.draw(ground, 0, 0);
38 actor.draw();
39 batch.end();
40 sleep(40);
41 }
42
43 private void sleep(int timer) {
44 try {
45 Thread.sleep(timer);
46 } catch (InterruptedException e) {
47 e.printStackTrace();
48 }
49 }
50
51 boolean isFree(int px, int py) {
52 Color color = new Color();
53 Color.rgb888ToColor(color, mask.getPixel(px, mask.getHeight() - py));
54 return ((color.r > 0.1) && (color.g > 0.1) && (color.b > 0.1));
55 }
56
57 @Override
58 public void dispose() {
59 batch.dispose();
60 mask.dispose();
61 kaneda.dispose();
62 }
63}