Mudanças entre as edições de "Android Games: HelloWorld"
De Aulas
(Uma revisão intermediária pelo mesmo usuário não está sendo mostrada) | |||
Linha 12: | Linha 12: | ||
= Classe Principal = | = Classe Principal = | ||
− | <syntaxhighlight lang=java | + | <syntaxhighlight lang=java> |
package com.mygdx.game; | package com.mygdx.game; | ||
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.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; | |
− | + | BitmapFont font; | |
− | + | Sprite sprite; | |
− | + | Music music; | |
− | + | Sound sndIn, sndOut; | |
− | + | static final int SPEED = 10; | |
− | + | int w, h; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | @Override | |
− | + | public void create() { | |
− | + | w = Gdx.graphics.getWidth(); | |
− | + | h = Gdx.graphics.getHeight(); | |
− | + | 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 (val > max) { | |
− | + | sndOut.play(); | |
− | + | return max; | |
− | |||
} | } | ||
− | + | return val; | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | @Override | |
− | + | public void dispose() { | |
− | + | batch.dispose(); | |
− | + | sprite.getTexture().dispose(); | |
− | + | font.dispose(); | |
− | + | music.dispose(); | |
− | + | sndIn.dispose(); | |
− | + | sndOut.dispose(); | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Edição atual tal como às 18h04min de 27 de outubro de 2022
Afluentes: Jogos Digitais
Assets
Classe Principal
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
BitmapFont font;
Sprite sprite;
Music music;
Sound sndIn, sndOut;
static final int SPEED = 10;
int w, h;
@Override
public void create() {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
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 (val > max) {
sndOut.play();
return max;
}
return val;
}
@Override
public void dispose() {
batch.dispose();
sprite.getTexture().dispose();
font.dispose();
music.dispose();
sndIn.dispose();
sndOut.dispose();
}
}
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.
|