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

Tpl warning.png

Use o oracle java 8 ou o Open Java 8.