Android Games: Exemplo 04
De Aulas
Links relacionados: Jogos Digitais
Recursos
Classe Main
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.Input.Peripheral;
9
10public class MyGdxGame extends ApplicationAdapter {
11 private SpriteBatch batch;
12 private Texture background;
13 private Texture sphere;
14 private int w, h, x, y;
15 private boolean accel;
16
17 @Override
18 public void create() {
19 w = Gdx.graphics.getWidth();
20 h = Gdx.graphics.getHeight();
21 batch = new SpriteBatch();
22 background = new Texture("background.png");
23 sphere = new Texture("sphere.png");
24 accel = Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer);
25 }
26
27 @Override
28 public void render() {
29 run();
30 Gdx.gl.glClearColor(1, 0, 0, 1);
31 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
32 batch.begin();
33 batch.draw(background, 0, 0, w, h);
34 batch.draw(sphere, x, y);
35 batch.end();
36 }
37
38 public void run() {
39 if (!accel) {
40 return;
41 }
42 y += (int) -Gdx.input.getAccelerometerX() * 5;
43 x += (int) Gdx.input.getAccelerometerY() * 5;
44 if (x < 0) {
45 x = 0;
46 }
47 if (y < 0) {
48 y = 0;
49 }
50 if (x + sphere.getWidth() > w) {
51 x = w - sphere.getWidth();
52 }
53 if (y + sphere.getHeight() > h) {
54 y = h - sphere.getHeight();
55 }
56 }
57
58 @Override
59 public void dispose() {
60 batch.dispose();
61 background.dispose();
62 sphere.dispose();
63 }
64}