Android e LibGDX - Spy Where

De Aulas

Links relacionados: Jogos Digitais

Spy Where

Exemplo de código de Android com LibGDX com a utilização de imagens, animação e sons.

Para compilar o jogo, é necessário baixar os recursos e descompacta-los na pasta assets do projeto referente ao android.

  1package com.mygdx.game;
  2
  3import com.badlogic.gdx.ApplicationAdapter;
  4import com.badlogic.gdx.Gdx;
  5import com.badlogic.gdx.graphics.GL20;
  6import com.badlogic.gdx.graphics.Texture;
  7import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8import com.badlogic.gdx.audio.Sound;
  9import com.badlogic.gdx.graphics.OrthographicCamera;
 10import com.badlogic.gdx.graphics.Texture.TextureFilter;
 11import com.badlogic.gdx.graphics.g2d.Sprite;
 12import com.badlogic.gdx.graphics.g2d.TextureRegion;
 13import com.badlogic.gdx.math.Vector3;
 14
 15public class MyGdxGame extends ApplicationAdapter {
 16    private OrthographicCamera camera;
 17    private SpriteBatch batch;
 18    private Texture texture;
 19    private Sprite sky;
 20    private Sprite city;
 21    private Sprite road;
 22    private Sprite lamppost;
 23    private Sprite iconLeft;
 24    private Sprite iconRight;
 25    private Sprite iconUp;
 26    private Sprite iconDown;
 27    private Sprite iconShoot;
 28    private Sprite spy[][] = new Sprite[2][3];
 29    private Sound soundStep[] = new Sound[2];
 30    private Sound shootSound;
 31    private Sound music;
 32    private int x_skyA;
 33    private int x_skyB;
 34    private int x_cityA;
 35    private int x_cityB;
 36    private int x_roadA;
 37    private int x_roadB;
 38    private int x_lamppost;
 39    private int y;
 40    private int step;
 41    private int action;
 42
 43    public final int RIGHT = 0;
 44    public final int LEFT = 1;
 45    public final int SHOT = 2;
 46    public final int UP = 3;
 47    public final int DOWN = 4;
 48    public final int WIDTH = 800;
 49    public final int HEIGHT = 480;
 50    public final float PROPORTION = 1.6f;
 51
 52    @Override
 53    public void create() {
 54        camera = new OrthographicCamera();
 55        camera.setToOrtho(false, WIDTH, HEIGHT);
 56
 57        batch = new SpriteBatch();
 58
 59        soundStep[0] = Gdx.audio.newSound(Gdx.files.internal("step_0.wav"));
 60        soundStep[1] = Gdx.audio.newSound(Gdx.files.internal("step_1.wav"));
 61        shootSound = Gdx.audio.newSound(Gdx.files.internal("shoot.wav"));
 62        music = Gdx.audio.newSound(Gdx.files
 63                .internal("fernando_strezelecki.mp3"));
 64        music.play();
 65
 66        texture = new Texture(Gdx.files.internal("imagens.png"));
 67        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
 68
 69        spy[0][0] = getSprite(texture, 0, 0, 25, 65, PROPORTION);
 70        spy[0][1] = getSprite(texture, 25, 0, 26, 65, PROPORTION);
 71        spy[0][2] = getSprite(texture, 51, 0, 28, 65, PROPORTION);
 72        spy[1][0] = getSprite(texture, 0, 65, 25, 65, PROPORTION);
 73        spy[1][1] = getSprite(texture, 25, 65, 26, 65, PROPORTION);
 74        spy[1][2] = getSprite(texture, 51, 65, 28, 65, PROPORTION);
 75
 76        sky = getSprite(texture, 0, 500, 798, 300, PROPORTION);
 77        city = getSprite(texture, 0, 800, 1024, 202, PROPORTION);
 78        road = getSprite(texture, 0, 300, 752, 126, PROPORTION);
 79        lamppost = getSprite(texture, 0, 150, 69, 122, PROPORTION);
 80
 81        iconRight = getSprite(texture, 0, 450, 50, 50, PROPORTION);
 82        iconRight.setPosition(WIDTH - iconRight.getWidth(), 0);
 83        iconLeft = getSprite(texture, 50, 450, 50, 50, PROPORTION);
 84        iconLeft.setPosition((WIDTH - (iconLeft.getWidth() * 2)), 0);
 85        iconShoot = getSprite(texture, 100, 450, 50, 50, PROPORTION);
 86        iconUp = getSprite(texture, 150, 450, 50, 50, PROPORTION);
 87        iconUp.setPosition(WIDTH - iconUp.getWidth(), iconUp.getHeight() * 2);
 88        iconDown = getSprite(texture, 200, 450, 50, 50, PROPORTION);
 89        iconDown.setPosition(WIDTH - iconDown.getWidth(), iconDown.getHeight());
 90        iconShoot.setPosition(0, 0);
 91
 92        x_skyA = 0;
 93        x_skyB = (int) sky.getWidth() + 1;
 94        x_cityA = 0;
 95        x_cityB = (int) city.getWidth() + 1;
 96        x_roadA = 0;
 97        x_roadB = (int) road.getWidth() + 1;
 98        x_lamppost = 0;
 99        y = HEIGHT / 8;
100        step = 0;
101        action = RIGHT;
102    }
103
104    public boolean touched(Sprite s, int tx, int ty) {
105        return ((s.getX() < tx) && (s.getX() + s.getWidth() > tx)
106                && (s.getY() < ty) && (s.getY() + s.getHeight() > ty));
107    }
108
109    public Sprite getSprite(Texture t, int x, int y, int w, int h,
110                            float proporcao) {
111        TextureRegion region = new TextureRegion(t, x, y, w, h);
112        Sprite sprite = new Sprite(region);
113        sprite.setSize(w * proporcao, h * proporcao);
114        sprite.setPosition(0, 0);
115        return sprite;
116    }
117
118    @Override
119    public void dispose() {
120        batch.dispose();
121        texture.dispose();
122        soundStep[0].dispose();
123        soundStep[1].dispose();
124        music.dispose();
125    }
126
127    public void simulateGameWorld() {
128        int tx;
129        int ty;
130        if (Gdx.input.isTouched()) {
131            Vector3 touchPos = new Vector3();
132            touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
133            camera.unproject(touchPos);
134            tx = (int) touchPos.x;
135            ty = (int) touchPos.y;
136
137            if (touched(iconShoot, tx, ty)) {
138                runAction(SHOT);
139            }
140            if (touched(iconLeft, tx, ty)) {
141                runAction(LEFT);
142            }
143            if (touched(iconRight, tx, ty)) {
144                runAction(RIGHT);
145            }
146            if (touched(iconUp, tx, ty)) {
147                runAction(UP);
148            }
149            if (touched(iconDown, tx, ty)) {
150                runAction(DOWN);
151            }
152        }
153    }
154
155    void runAction(int a) {
156        boolean acted = false;
157        step = (step > 0) ? 0 : 1;
158        switch (a) {
159            case RIGHT:
160                x_skyA -= 2;
161                x_skyB -= 2;
162                x_cityA -= 5;
163                x_cityB -= 5;
164                x_roadA -= 10;
165                x_roadB -= 10;
166                x_lamppost -= 15;
167                action = RIGHT;
168                acted = true;
169                break;
170            case LEFT:
171                x_skyA += 2;
172                x_skyB += 2;
173                x_cityA += 5;
174                x_cityB += 5;
175                x_roadA += 10;
176                x_roadB += 10;
177                x_lamppost += 15;
178                action = LEFT;
179                acted = true;
180                break;
181            case UP:
182                if (y < HEIGHT / 5) {
183                    y += 10;
184                    acted = true;
185                }
186                break;
187            case DOWN:
188                if (y > HEIGHT / 10) {
189                    y -= 10;
190                    acted = true;
191                }
192                break;
193            case SHOT:
194                step = SHOT;
195                shootSound.play();
196                acted = true;
197                break;
198        }
199        if ((step != SHOT) && acted) {
200            soundStep[step].play();
201        }
202        try {
203            Thread.sleep(50);
204        } catch (InterruptedException e) {
205            e.printStackTrace();
206        }
207    }
208
209    private int drawBackground(Sprite sprite, int x, int y) {
210        if ((int) (x + sprite.getWidth()) < 0) {
211            x += (int) (sprite.getWidth() * 2);
212        }
213        if (x > (int) sprite.getWidth()) {
214            x -= (int) (sprite.getWidth() * 2);
215        }
216        sprite.setPosition(x, y);
217        sprite.draw(batch);
218        return x;
219    }
220
221    @Override
222    public void render() {
223        simulateGameWorld();
224
225        Gdx.gl.glClearColor(1, 1, 1, 1);
226        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
227
228        if (x_lamppost < (int) (-lamppost.getWidth() - 300)) {
229            x_lamppost = WIDTH;
230        }
231        if (x_lamppost > WIDTH) {
232            x_lamppost = (int) (-lamppost.getWidth() - 300);
233        }
234        lamppost.setPosition(x_lamppost, 0);
235        spy[action][step].setPosition(
236                (int) ((WIDTH / 2) - (spy[action][step].getWidth() / 2)), y);
237
238        batch.setProjectionMatrix(camera.combined);
239        batch.begin();
240        x_skyA = drawBackground(sky, x_skyA, 0);
241        x_skyB = drawBackground(sky, x_skyB, 0);
242        x_cityA = drawBackground(city, x_cityA, WIDTH / 5);
243        x_cityB = drawBackground(city, x_cityB, WIDTH / 5);
244        x_roadA = drawBackground(road, x_roadA, 0);
245        x_roadB = drawBackground(road, x_roadB, 0);
246
247        // light.draw(batch);
248        spy[action][step].draw(batch);
249        lamppost.draw(batch);
250
251        iconShoot.draw(batch);
252        iconLeft.draw(batch);
253        iconRight.draw(batch);
254        iconUp.draw(batch);
255        iconDown.draw(batch);
256
257        batch.end();
258    }
259}