Aller au contenu

Exemples de projets

Création d'un jeu vidéo avec libGDX

Description du projet

Développement d'un jeu le framework libGDX. Le jeu doit inclure les fonctionnalités suivantes :

  • Ecran d'accueil avec menu de navigation (1 points)
  • Proposition d'un gameplay simple (plateforme, puzzle, combat au tour par tour, etc.) (3 points)
  • Ecran de fin (1 point)
  • Système de score (1 point) et sauvegarde des meilleurs scores (1 point)
  • Musique et effets sonores (2 points)
  • Sauvegarde et chargement de la progression du joueur (2 points)
  • Beaux graphismes (1.5 points) et belles animations (1.5 points)
  • Code de bonne qualité (commenté, structuré, respectant les bonnes pratiques) (2 points)
  • Réponse à deux questions de code (2 points par question)

Documentation

Exemples de codes

Logo qui se déplace
package org.cours.libgdx;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;

/**
 * {@link com.badlogic.gdx.ApplicationListener} implementation shared by all
 * platforms.
 */
public class Main extends ApplicationAdapter {
    private SpriteBatch batch;
    private Texture image;

    private float imagePosX = 140;
    private float imagePosY = 210;

    @Override
    public void create() {
        batch = new SpriteBatch();
        image = new Texture("libgdx.png");
    }

    @Override
    public void render() {
        imagePosX += 0.1;
        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
            imagePosY += 0.5;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
            imagePosY -= 0.5;
        }

        ScreenUtils.clear(0.15f, 0.15f, 0.2f, 1f);
        batch.begin();
        batch.draw(image, imagePosX, imagePosY);
        batch.end();
    }

    @Override
    public void dispose() {
        batch.dispose();
        image.dispose();
    }
}
Changement d'écran
  • Récupérer un fichier font.fnt et son font.png associé depuis cette liste. Puis, les placer dans le dossier assets/ui du projet libGDX.
package org.cours.libgdx;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;

enum GameScreen {
    MAIN_MENU,
    GAMEPLAY,
    GAME_OVER
}

/**
 * {@link com.badlogic.gdx.ApplicationListener} implementation shared by all
 * platforms.
 */
public class Main extends ApplicationAdapter {
    private SpriteBatch batch;
    private Texture image;

    private float imagePosX = 140;
    private float imagePosY = 210;
    private GameScreen currentGameScreen = GameScreen.MAIN_MENU;
    BitmapFont mainFont;

    @Override
    public void create() {
        batch = new SpriteBatch();
        image = new Texture("libgdx.png");
        mainFont = new BitmapFont(Gdx.files.internal("ui/font.fnt"));

    }

    @Override
    public void render() {
        // etat du jeu
        switch (currentGameScreen) {
            case MAIN_MENU:
                if (Gdx.input.isKeyPressed(Input.Keys.S)) {
                    currentGameScreen = GameScreen.GAMEPLAY;
                }
                break;
            case GAMEPLAY:
                imagePosX += 0.1;
                if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
                    imagePosY += 0.5;
                }
                if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
                    imagePosY -= 0.5;
                }
                if (Gdx.input.isKeyPressed(Input.Keys.X)) {
                    currentGameScreen = GameScreen.GAME_OVER;
                }
                break;
            case GAME_OVER:
                break;
        }

        // Rendu
        ScreenUtils.clear(0.15f, 0.15f, 0.2f, 1f);
        batch.begin();
        switch (currentGameScreen) {
            case MAIN_MENU:
                mainFont.draw(batch, "Press 'S' to start", 140, 210);
                break;
            case GAMEPLAY:
                batch.draw(image, imagePosX, imagePosY);
                break;
            case GAME_OVER:
                mainFont.draw(batch, "GAME OVER", 140, 210);
                break;
        }
        batch.end();
    }

    @Override
    public void dispose() {
        batch.dispose();
        image.dispose();
    }
}