Android Games: HelloWorld
De Aulas
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.GL20;
9import com.badlogic.gdx.graphics.Texture;
10import com.badlogic.gdx.graphics.g2d.BitmapFont;
11import com.badlogic.gdx.graphics.g2d.Sprite;
12import com.badlogic.gdx.graphics.g2d.SpriteBatch;
13
14public class MyGdxGame extends ApplicationAdapter {
15 SpriteBatch batch;
16 BitmapFont font;
17 Sprite sprite;
18 Sound soundIn, soundOut;
19 Music music;
20 int w, h;
21 float x, y;
22 static final int SPEED = 10;
23
24 @Override
25 public void create () {
26 batch = new SpriteBatch();
27 w = Gdx.graphics.getWidth();
28 h = Gdx.graphics.getHeight();
29 sprite = new Sprite(new Texture("sphere.png"));
30 sprite.setPosition(
31 w / 2f - sprite.getWidth() / 2f,
32 h / 2f - sprite.getHeight() / 2f);
33 font = new BitmapFont(
34 Gdx.files.internal("verdana.fnt"),
35 Gdx.files.internal("verdana.png"), false);
36 soundIn = Gdx.audio.newSound(Gdx.files.internal("in.wav"));
37 soundOut = Gdx.audio.newSound(Gdx.files.internal("out.wav"));
38 music = Gdx.audio.newMusic(Gdx.files.internal("otomata_music.ogg"));
39 soundIn.play();
40 music.setLooping(true);
41 music.play();
42 }
43
44 private void execute () {
45 if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
46 y += SPEED;
47 } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
48 y -= SPEED;
49 }
50 if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
51 x -= SPEED;
52 } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
53 x += SPEED;
54 }
55 sprite.setPosition(
56 clamp(x, 0, w - sprite.getWidth()),
57 clamp(y, 0, h - sprite.getHeight()));
58 if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
59 Gdx.app.exit();
60 } else if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
61 if (music.isPlaying()) {
62 music.stop();
63 } else {
64 music.play();
65 }
66 soundOut.play();
67 }
68 }
69
70 @Override
71 public void render () {
72 execute();
73 Gdx.gl.glClearColor(0, 0.5f, 0, 1);
74 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
75 batch.begin();
76 font.draw(batch, "Hello android game world!", 1, h + 1);
77 sprite.draw(batch);
78 batch.end();
79 }
80
81 public static float clamp(float val, float min, float max) {
82 if (val < 0) return min;
83 else if (val > max) return max;
84 else return val;
85 }
86
87 @Override
88 public void dispose () {
89 batch.dispose();
90 sprite.getTexture().dispose();
91 font.dispose();
92 soundIn.dispose();
93 soundOut.dispose();
94 music.dispose();
95 }
96}
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.
|