Android Games: HelloWorld

De Aulas
Revisão de 20h00min de 8 de novembro de 2021 por Admin (discussão | contribs) (→‎Classe Principal)

Afluentes: Jogos Digitais

Assets

Classe Principal

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.ApplicationAdapter;
 4import com.badlogic.gdx.Gdx;
 5import com.badlogic.gdx.Input;
 6import com.badlogic.gdx.audio.Music;
 7import com.badlogic.gdx.audio.Sound;
 8import com.badlogic.gdx.graphics.Texture;
 9import com.badlogic.gdx.graphics.g2d.BitmapFont;
10import com.badlogic.gdx.graphics.g2d.Sprite;
11import com.badlogic.gdx.graphics.g2d.SpriteBatch;
12import com.badlogic.gdx.utils.ScreenUtils;
13
14public class MyGdxGame extends ApplicationAdapter {
15    SpriteBatch batch;
16    BitmapFont font;
17    Sprite sprite;
18    Music music;
19    Sound sndIn, sndOut;
20    static final int SPEED = 10;
21    int w, h;
22
23    @Override
24    public void create() {
25        w = Gdx.graphics.getWidth();
26        h = Gdx.graphics.getHeight();
27        batch = new SpriteBatch();
28        sprite = new Sprite(new Texture("sphere.png"));
29        font = new BitmapFont(
30                Gdx.files.internal("verdana.fnt"),
31                Gdx.files.internal("verdana.png"), false
32        );
33        sndIn = Gdx.audio.newSound(Gdx.files.internal("in.wav"));
34        sndOut = Gdx.audio.newSound(Gdx.files.internal("out.wav"));
35        music = Gdx.audio.newMusic(Gdx.files.internal("otomata_music.ogg"));
36        music.setLooping(true);
37        music.play();
38    }
39
40    public void execute() {
41        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
42            sprite.translateY(SPEED);
43        } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
44            sprite.translateY(-SPEED);
45        }
46        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
47            sprite.translateX(SPEED);
48        } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
49            sprite.translateX(-SPEED);
50        }
51        sprite.setX(clamp(sprite.getX(), 0, w - sprite.getWidth()));
52        sprite.setY(clamp(sprite.getY(), 0, h - sprite.getHeight()));
53    }
54
55    @Override
56    public void render() {
57        execute();
58        ScreenUtils.clear(0, 0.5f, 0, 1);
59        batch.begin();
60        sprite.draw(batch);
61        font.draw(batch, "X: " + sprite.getX() + " Y: " + sprite.getY(), 1, h);
62        batch.end();
63    }
64
65    public float clamp(float val, float min, float max) {
66        if (val < min) {
67        	sndOut.play();
68        	return min;
69		}
70        if (val > max) {
71        	sndOut.play();
72        	return max;
73		}
74        return val;
75    }
76
77    @Override
78    public void dispose() {
79        batch.dispose();
80        sprite.getTexture().dispose();
81        font.dispose();
82        music.dispose();
83        sndIn.dispose();
84        sndOut.dispose();
85    }
86}

Gerar pacote JAR para desktop

No Linux, na pasta do projeto executar:

./gradlew desktop:dist

No Windows, na pasta do projeto executar:

gradlew desktop:dist

Vai ser gerado um jar na pasta desktop/build/libs/ . Copie o arquivo ou execute lá:

java -jar desktop-1.0.jar

Você pode renomear o arquivo para o nome do projeto.

Tpl warning.png

Use o oracle java 8 ou o Open Java 8.