ajout de toutes les données dans la bdd + Modularisation de l'écran fin de partie

This commit is contained in:
tit-exe
2026-03-17 14:11:55 +01:00
parent dbafa23b4d
commit 9ec6f0ebfa
5 changed files with 85 additions and 68 deletions

Binary file not shown.

View File

@@ -0,0 +1,34 @@
package linea;
import javax.swing.JLabel;
public class GestionnaireFinDePartie {
private boolean estGameOver = false;
private JLabel labGameOver;
private GestionnaireScore gestionnaireBDD;
public GestionnaireFinDePartie(JLabel labGameOver, GestionnaireScore gestionnaireBDD) {
this.labGameOver = labGameOver;
this.gestionnaireBDD = gestionnaireBDD;
}
public void declencherGameOver(ZoneDessin ecran, String pseudo, int score) {
estGameOver = true;
ecran.arreter();
labGameOver.setVisible(true);
if (pseudo != null && !pseudo.isEmpty()) {
gestionnaireBDD.sauvegarderScore(pseudo, score);
}
}
public boolean estGameOver() {
return estGameOver;
}
public void reinitialiser() {
estGameOver = false;
labGameOver.setVisible(false);
}
}

View File

@@ -1,6 +1,7 @@
package linea; package linea;
import java.awt.Color; import java.awt.Color;
import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -15,32 +16,48 @@ public class GestionnaireNiveau {
private static final int DUREE_FADE = 25 * 3; // 3 secondes private static final int DUREE_FADE = 25 * 3; // 3 secondes
private static final String URL_BDD = "jdbc:sqlite:linea_bdd.db";
public GestionnaireNiveau() { public GestionnaireNiveau() {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
System.err.println("[BDD] Impossible de charger le driver SQLite.");
e.printStackTrace();
}
chargerNiveaux();
}
// Niveau 1 private void chargerNiveaux() {
niveaux.add(new Niveau(1, String sql = "SELECT * FROM niveaux ORDER BY numero ASC";
new Color(112, 158, 251),
8, 1 / 30.0, 60, 0, 600));
// Niveau 2 try (Connection conn = DriverManager.getConnection(URL_BDD);
niveaux.add(new Niveau(2, Statement stmt = conn.createStatement();
new Color(187, 138, 255), ResultSet rs = stmt.executeQuery(sql)) {
10, 1 / 25.0, 55, 0, 600));
// Niveau 3 while (rs.next()) {
niveaux.add(new Niveau(3, int numero = rs.getInt("numero");
new Color(255, 106, 132), int r = rs.getInt("couleur_r");
11, 1 / 20.0, 50, 0, 600)); int g = rs.getInt("couleur_g");
int b = rs.getInt("couleur_b");
double vitesse = rs.getDouble("vitesse_scroll");
double freq = rs.getDouble("noise_frequence");
double rayon = rs.getDouble("rayon_cercle");
double haut = rs.getDouble("limite_haut");
double bas = rs.getDouble("limite_bas");
// Niveau 4 niveaux.add(new Niveau(numero, new Color(r, g, b), vitesse, freq, rayon, haut, bas));
niveaux.add(new Niveau(4, }
new Color(191, 255, 207),
12, 1 / 17.0, 47, 0, 600));
// Niveau 5 } catch (SQLException e) {
niveaux.add(new Niveau(5, System.err.println("[BDD] Erreur lors du chargement des niveaux.");
new Color(251, 233, 144), e.printStackTrace();
13, 1 / 15.0, 45, 100, 600)); }
// Si aucun niveau chargé, on en met un par défaut pour éviter un crash
if (niveaux.isEmpty()) {
niveaux.add(new Niveau(1, new Color(112, 158, 251), 8, 1.0/80.0, 70, 100, 500));
}
} }
public void mettreAJour() { public void mettreAJour() {

View File

@@ -4,47 +4,19 @@ import java.sql.*;
public class GestionnaireScore { public class GestionnaireScore {
private static final String URL = "jdbc:sqlite:linea_scores.db"; private static final String URL = "jdbc:sqlite:linea_bdd.db";
public GestionnaireScore() { public GestionnaireScore() {
// 1) Charger explicitement le driver SQLite JDBC
try { try {
Class.forName("org.sqlite.JDBC"); Class.forName("org.sqlite.JDBC");
System.out.println("[BDD] Driver SQLite JDBC chargé correctement (org.sqlite.JDBC).");
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
System.err.println("[BDD] ERREUR : impossible de charger le driver SQLite JDBC (org.sqlite.JDBC)."); System.err.println("[BDD] Impossible de charger le driver SQLite.");
System.err.println("Vérifie que le fichier sqlite-jdbc-xxx.jar est bien présent dans le classpath.");
e.printStackTrace();
}
// 2) Créer / vérifier la table au démarrage
try (Connection conn = DriverManager.getConnection(URL)) {
if (conn == null) {
System.err.println("[BDD] ERREUR : connexion JDBC nulle lors de l'initialisation.");
return;
}
String sql = "CREATE TABLE IF NOT EXISTS scores (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"nom TEXT NOT NULL," +
"points INTEGER NOT NULL" +
");";
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
}
System.out.println("[BDD] Table 'scores' vérifiée/créée avec succès.");
} catch (SQLException e) {
System.err.println("[BDD] ERREUR SQL lors de la création ou vérification de la table 'scores'.");
System.err.println("Message SQL : " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
public void sauvegarderScore(String nom, int points) { public void sauvegarderScore(String nom, int points) {
if (nom == null || nom.trim().isEmpty()) { if (nom == null || nom.trim().isEmpty()) {
System.err.println("[BDD] Tentative d'enregistrer un score avec un nom vide. Opération annulée.");
return; return;
} }
@@ -55,13 +27,10 @@ public class GestionnaireScore {
pstmt.setString(1, nom.trim()); pstmt.setString(1, nom.trim());
pstmt.setInt(2, points); pstmt.setInt(2, points);
int lignes = pstmt.executeUpdate(); pstmt.executeUpdate();
System.out.println("[BDD] Score enregistré pour '" + nom + "' (" + points +
" points). Lignes affectées : " + lignes);
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("[BDD] ERREUR SQL lors de l'enregistrement du score."); System.err.println("[BDD] Erreur lors de l'enregistrement du score.");
System.err.println("Message SQL : " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
@@ -89,13 +58,9 @@ public class GestionnaireScore {
} }
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("[BDD] ERREUR SQL lors de la lecture des scores."); System.err.println("[BDD] Erreur lors de la lecture des scores.");
System.err.println("Message SQL : " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
return "Erreur lors de la lecture des scores.";
return "Erreur lors de la lecture des scores.\n" +
"Détail : " + e.getMessage() + "\n" +
"Consulte la console pour la pile complète (stack trace).";
} }
return sb.toString(); return sb.toString();

View File

@@ -32,6 +32,8 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
protected GestionnaireScore gestionnaireBDD = new GestionnaireScore(); protected GestionnaireScore gestionnaireBDD = new GestionnaireScore();
protected GestionnaireFinDePartie gestionnaireFinDePartie;
protected int compteurFrames = 0; protected int compteurFrames = 0;
protected boolean enCollision = false; protected boolean enCollision = false;
protected boolean estGameOver = false; protected boolean estGameOver = false;
@@ -57,6 +59,8 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
labGameOver.setVisible(false); labGameOver.setVisible(false);
ecran.add(labGameOver); ecran.add(labGameOver);
gestionnaireFinDePartie = new GestionnaireFinDePartie(labGameOver, gestionnaireBDD);
ecran.addMouseListener(this); ecran.addMouseListener(this);
ecran.traiterBoucleAnimation(); ecran.traiterBoucleAnimation();
} }
@@ -134,7 +138,7 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
estGameOver = false; estGameOver = false;
compteurInvincible = 0; compteurInvincible = 0;
gestionnaireNiveau.reinitialiser(); gestionnaireNiveau.reinitialiser();
labGameOver.setVisible(false); gestionnaireFinDePartie.reinitialiser();
ecran.demarrer(); ecran.demarrer();
if (horloge == null) { if (horloge == null) {
@@ -185,12 +189,8 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
if (vies <= 0) { if (vies <= 0) {
vies = 0; vies = 0;
estGameOver = true; estGameOver = true;
ecran.arreter(); // Le gestionnaire s'occupe d'arrêter l'écran, afficher le label et sauvegarder
labGameOver.setVisible(true); gestionnaireFinDePartie.declencherGameOver(ecran, pseudo, (int) score);
if (pseudo != null && !pseudo.isEmpty()) {
gestionnaireBDD.sauvegarderScore(pseudo, (int) score);
}
} }
labScore.setText("<html><h3>Score : " + (int) score + " | Vies : " + vies + " | Niv. " + gestionnaireNiveau.getNumeroNiveau() + "</h3></html>"); labScore.setText("<html><h3>Score : " + (int) score + " | Vies : " + vies + " | Niv. " + gestionnaireNiveau.getNumeroNiveau() + "</h3></html>");
@@ -238,6 +238,7 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
@Override @Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
} }
public GestionnaireScore getGestionnaireBDD() { public GestionnaireScore getGestionnaireBDD() {
return gestionnaireBDD; return gestionnaireBDD;
} }