Mudanças entre as edições de "Android Games: HelloWorld"

De Aulas
 
Linha 20: Linha 20:
 
import com.badlogic.gdx.audio.Music;
 
import com.badlogic.gdx.audio.Music;
 
import com.badlogic.gdx.audio.Sound;
 
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
 
 
import com.badlogic.gdx.graphics.Texture;
 
import com.badlogic.gdx.graphics.Texture;
 
import com.badlogic.gdx.graphics.g2d.BitmapFont;
 
import com.badlogic.gdx.graphics.g2d.BitmapFont;
 
import com.badlogic.gdx.graphics.g2d.Sprite;
 
import com.badlogic.gdx.graphics.g2d.Sprite;
 
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 +
import com.badlogic.gdx.utils.ScreenUtils;
  
 
public class MyGdxGame extends ApplicationAdapter {
 
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
+
    SpriteBatch batch;
BitmapFont font;
+
    BitmapFont font;
Sprite sprite;
+
    Sprite sprite;
Sound soundIn, soundOut;
+
    Music music;
Music music;
+
    Sound sndIn, sndOut;
int w, h;
+
    static final int SPEED = 10;
float x, y;
+
    int w, h;
static final int SPEED = 10;
 
 
@Override
 
public void create () {
 
batch = new SpriteBatch();
 
w = Gdx.graphics.getWidth();
 
h = Gdx.graphics.getHeight();
 
sprite = new Sprite(new Texture("sphere.png"));
 
sprite.setPosition(
 
w / 2f - sprite.getWidth() / 2f,
 
h / 2f - sprite.getHeight() / 2f);
 
font = new BitmapFont(
 
Gdx.files.internal("verdana.fnt"),
 
Gdx.files.internal("verdana.png"), false);
 
soundIn = Gdx.audio.newSound(Gdx.files.internal("in.wav"));
 
soundOut = Gdx.audio.newSound(Gdx.files.internal("out.wav"));
 
music = Gdx.audio.newMusic(Gdx.files.internal("otomata_music.ogg"));
 
soundIn.play();
 
music.setLooping(true);
 
music.play();
 
}
 
  
private void execute () {
+
    @Override
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
+
    public void create() {
y += SPEED;
+
        w = Gdx.graphics.getWidth();
} else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
+
        h = Gdx.graphics.getHeight();
y -= SPEED;
+
        batch = new SpriteBatch();
 +
        sprite = new Sprite(new Texture("sphere.png"));
 +
        font = new BitmapFont(
 +
                Gdx.files.internal("verdana.fnt"),
 +
                Gdx.files.internal("verdana.png"), false
 +
        );
 +
        sndIn = Gdx.audio.newSound(Gdx.files.internal("in.wav"));
 +
        sndOut = Gdx.audio.newSound(Gdx.files.internal("out.wav"));
 +
        music = Gdx.audio.newMusic(Gdx.files.internal("otomata_music.ogg"));
 +
        music.setLooping(true);
 +
        music.play();
 +
    }
 +
 
 +
    public void execute() {
 +
        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
 +
            sprite.translateY(SPEED);
 +
        } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
 +
            sprite.translateY(-SPEED);
 +
        }
 +
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
 +
            sprite.translateX(SPEED);
 +
        } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
 +
            sprite.translateX(-SPEED);
 +
        }
 +
        sprite.setX(clamp(sprite.getX(), 0, w - sprite.getWidth()));
 +
        sprite.setY(clamp(sprite.getY(), 0, h - sprite.getHeight()));
 +
    }
 +
 
 +
    @Override
 +
    public void render() {
 +
        execute();
 +
        ScreenUtils.clear(0, 0.5f, 0, 1);
 +
        batch.begin();
 +
        sprite.draw(batch);
 +
        font.draw(batch, "X: " + sprite.getX() + " Y: " + sprite.getY(), 1, h);
 +
        batch.end();
 +
    }
 +
 
 +
    public float clamp(float val, float min, float max) {
 +
        if (val < min) {
 +
        sndOut.play();
 +
        return min;
 
}
 
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
+
        if (val > max) {
x -= SPEED;
+
        sndOut.play();
} else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
+
        return max;
x += SPEED;
 
 
}
 
}
sprite.setPosition(
+
        return val;
clamp(x, 0, w - sprite.getWidth()),
+
    }
clamp(y, 0, h - sprite.getHeight()));
 
if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
 
Gdx.app.exit();
 
} else if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
 
if (music.isPlaying()) {
 
music.stop();
 
} else {
 
music.play();
 
}
 
soundOut.play();
 
}
 
}
 
 
 
@Override
 
public void render () {
 
execute();
 
Gdx.gl.glClearColor(0, 0.5f, 0, 1);
 
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
 
batch.begin();
 
font.draw(batch, "Hello android game world!", 1, h + 1);
 
sprite.draw(batch);
 
batch.end();
 
}
 
  
public static float clamp(float val, float min, float max) {
+
    @Override
if (val < 0) return min;
+
    public void dispose() {
else if (val > max) return max;
+
        batch.dispose();
else return val;
+
        sprite.getTexture().dispose();
}
+
        font.dispose();
+
        music.dispose();
@Override
+
        sndIn.dispose();
public void dispose () {
+
        sndOut.dispose();
batch.dispose();
+
    }
sprite.getTexture().dispose();
 
font.dispose();
 
soundIn.dispose();
 
soundOut.dispose();
 
music.dispose();
 
}
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Edição das 20h00min de 8 de novembro de 2021

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.