Setup

  1. Descarga gdx-setup.jar

    Genera proyecto

  2. IntelliJ: Open...

    Java 8: modifica los ficheros desktop/build.gradle y core/build.gradle. sourceCompatibility = 1.8

    Sync Gradle

  3. Run 'DesktopLauncher.main()'

Pantallas

Haz que la clase MyGdxGame extienda de Game, y que navegue automáticamente a una PantallaInicial.

public class MyGdxGame extends Game { @Override public void create() {setScreen(new PantallaIncial(this));} @Override public void render () { super.render(); } }

La classe PantallaInicial la crearemos a continación

Crea la clase BaseScreen con el siguiente código:

import com.badlogic.gdx.Screen; public class BaseScreen implements Screen { public final MyGdxGame game; public BaseScreen(MyGdxGame game){ this.game = game; } public void setScreen(Screen screen){ game.setScreen(screen); } @Override public void show() {} @Override public void render(float delta) {} @Override public void resize(int width, int height) {} @Override public void pause() {} @Override public void resume() {} @Override public void hide() {} @Override public void dispose() {} }

Crea cada pantalla en una clase nueva. Haz que esta clase extienda de BaseScreen

public class PantallaIncial extends BaseScreen { public PantallaIncial(MyGdxGame game) { super(game); } @Override public void show() { } @Override public void render(float delta) { } }

Utiliza el método setScreen() para navegar a otras pantallas. Por ejemplo:

public class PantallaIncial extends BaseScreen { public PantallaIncial(MyGdxGame game) { super(game); } @Override public void show() { } @Override public void render(float delta) { setScreen(new Pantalla2(game)); } }

Botones

Copia las imagenes a la carpeta core/assets

  1. Establece el "estilo" del botón
  2. Crea el ImageButton, y establece su posicion, tamaño y el click listener.
  3. Añade el boton al Stage (el stage le pasa los clicks al botón)
  4. Llama a stage.act() y stage.draw()
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.ImageButton; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; public class PantallaIncial extends BaseScreen { PantallaIncial(MyGdxGame game) { super(game); } Stage stage; @Override public void show() { // Boton START ImageButton.ImageButtonStyle buttonStartStyle = new ImageButton.ImageButtonStyle(); buttonStartStyle.up = new TextureRegionDrawable(new TextureRegion(new Texture("button_start_up.png"))); buttonStartStyle.over = new TextureRegionDrawable(new TextureRegion(new Texture("button_start_over.png"))); ImageButton buttonStart = new ImageButton(buttonStartStyle); buttonStart.setPosition(280, 200); buttonStart.setSize(24*3, 10*3); buttonStart.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { setScreen(new Pantalla2(game)); return true; } }); // Boton QUIT ImageButton.ImageButtonStyle buttonQuitStyle = new ImageButton.ImageButtonStyle(); buttonQuitStyle.up = new TextureRegionDrawable(new TextureRegion(new Texture("button_quit_up.png"))); buttonQuitStyle.over = new TextureRegionDrawable(new TextureRegion(new Texture("button_quit_over.png"))); ImageButton buttonQuit = new ImageButton(buttonQuitStyle); buttonQuit.setPosition(280, 160); buttonQuit.setSize(24*3, 10*3); buttonQuit.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { Gdx.app.exit(); System.exit(0); return true; } }); Gdx.input.setInputProcessor(stage = new Stage()); stage.addActor(buttonStart); stage.addActor(buttonQuit); } @Override public void render(float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } }

SpriteBatch

Texturas

  1. Carga las texturas, e instancia el SpriteBatch en el método show()
  2. Llama a batch.begin() antes de dibujar, y a batch.end() después
  3. Utiliza el método draw para dibujar texturas
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class PantallaJuego extends BaseScreen { PantallaJuego(MyGdxGame game) { super(game); } SpriteBatch spriteBatch; Texture background, ballonRed, ballonGreen, ballonBlue; @Override public void show() { spriteBatch = new SpriteBatch(); background = new Texture("background.png"); ballonRed = new Texture("ballon_red.png"); ballonGreen = new Texture("ballon_green.png"); ballonBlue = new Texture("ballon_blue.png"); } @Override public void render(float delta) { spriteBatch.begin(); spriteBatch.draw(background, 0, 0, 640, 480); spriteBatch.draw(ballonRed, 60, 60); spriteBatch.draw(ballonGreen, 120, 160); spriteBatch.draw(ballonBlue, 160, 260); spriteBatch.draw(ballonRed, 360, 60, 100, 100); spriteBatch.draw(ballonGreen, 460, 160, 80, 80); spriteBatch.draw(ballonBlue, 260, 260, 140, 140); spriteBatch.end(); } }

Texto

  1. Instancia las clases BitmapFont y SpriteBatch
  2. Utiliza el método BitmapFont.draw() para dibujar texto en pantalla
  3. Opcionalmente: establece el color y la escala del texto
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class BitmapFontScreen extends BaseScreen { BitmapFontScreen(MyGdxGame game) { super(game); } SpriteBatch spriteBatch; BitmapFont bitmapFont; @Override public void show() { spriteBatch = new SpriteBatch(); bitmapFont = new BitmapFont(); } @Override public void render(float delta) { Gdx.gl.glClearColor(0.5f, 0.7f, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); spriteBatch.begin(); bitmapFont.getData().setScale(1); bitmapFont.setColor(new Color(1f, 0.2f, 0.2f, 1)); bitmapFont.draw(spriteBatch, "lib", 160, 300); bitmapFont.getData().setScale(2); bitmapFont.setColor(new Color(0.2f, 1f, 0.2f, 1)); bitmapFont.draw(spriteBatch, "GDX", 200f, 300f); bitmapFont.setColor(new Color(0.2f, 0.2f, 1f, 1)); bitmapFont.getData().setScale(3); bitmapFont.draw(spriteBatch, String.valueOf(2021), 300f, 300f); spriteBatch.end(); } }

Objects

import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class Globo { enum ColorGlobo {ROJO, VERDE, AZUL} static Texture globoRed = new Texture("ballon_red.png"); static Texture globoGreen = new Texture("ballon_green.png"); static Texture globoBlue = new Texture("ballon_blue.png"); float x, y, size; float speed; ColorGlobo color; Globo(float x, float y, float size, float speed, ColorGlobo color) { this.x = x; this.y = y; this.size = size; this.speed = speed; this.color = color; } public void update(float delta) { x += speed * delta; y += speed * delta; } public void render(SpriteBatch spriteBatch) { Texture texture; switch (color) { case ROJO: default: texture = globoRed; break; case VERDE: texture = globoGreen; break; case AZUL: texture = globoBlue; break; } spriteBatch.draw(texture, x, y, size, size); } } import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import java.util.ArrayList; import java.util.List; public class ObjectsScreen extends BaseScreen { ObjectsScreen(MyGdxGame game) { super(game); } SpriteBatch spriteBatch; List<Globo> globoList; @Override public void show() { spriteBatch = new SpriteBatch(); globoList = new ArrayList<>(); globoList.add(new Globo(10, 10, 30, 20, Globo.ColorGlobo.ROJO)); globoList.add(new Globo(50, 50, 50, 12, Globo.ColorGlobo.VERDE)); globoList.add(new Globo(100, 100, 70, 8, Globo.ColorGlobo.AZUL)); } @Override public void render(float delta) { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); for(Globo globo: globoList) globo.update(delta); spriteBatch.begin(); for(Globo globo: globoList) globo.render(spriteBatch); spriteBatch.end(); } }

Input

Gdx.input

La coordenada vertical del puntero está invertida respecto a la coordenada de dibujo.

int mouseY = Gdx.graphics.getHeight() - Gdx.input.getY();
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; public class InputScreen extends BaseScreen { InputScreen(MyGdxGame game) { super(game); } @Override public void render(float delta) { if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)){ System.out.println("Mouse-left just touched at: " + Gdx.input.getX() + ":" + (Gdx.graphics.getHeight()-Gdx.input.getY())); } if(Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){ System.out.println("Mouse-right is touched at: " + Gdx.input.getX() + ":" + (Gdx.graphics.getHeight()-Gdx.input.getY())); } if(Gdx.input.isKeyJustPressed(Input.Keys.A)){ System.out.println("Key A just pressed"); } if(Gdx.input.isKeyPressed(Input.Keys.S)){ System.out.println("Key S is pressed"); } } }

Alarmas

public class TimerScreen extends BaseScreen { float gameTime; float alarmTime; float alarmDuration = 1; // 1 segundo TimerScreen(MyGdxGame game) { super(game); } @Override public void render(float delta) { gameTime += delta; if(gameTime > alarmTime){ alarmTime = gameTime + alarmDuration; System.out.println("Alarm"); } } }

Colisiones

Intersector