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.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 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.
|