diff --git a/linea_scores.db b/linea_bdd.db similarity index 72% rename from linea_scores.db rename to linea_bdd.db index 856f043..936f99e 100644 Binary files a/linea_scores.db and b/linea_bdd.db differ diff --git a/src/GestionnaireFinDePartie.java b/src/GestionnaireFinDePartie.java new file mode 100644 index 0000000..9c5a168 --- /dev/null +++ b/src/GestionnaireFinDePartie.java @@ -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); + } +} diff --git a/src/GestionnaireNiveau.java b/src/GestionnaireNiveau.java index 7046ef0..41e47a8 100644 --- a/src/GestionnaireNiveau.java +++ b/src/GestionnaireNiveau.java @@ -1,6 +1,7 @@ package linea; import java.awt.Color; +import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -15,32 +16,48 @@ public class GestionnaireNiveau { private static final int DUREE_FADE = 25 * 3; // 3 secondes + private static final String URL_BDD = "jdbc:sqlite:linea_bdd.db"; + 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 - niveaux.add(new Niveau(1, - new Color(112, 158, 251), - 8, 1 / 30.0, 60, 0, 600)); + private void chargerNiveaux() { + String sql = "SELECT * FROM niveaux ORDER BY numero ASC"; - // Niveau 2 - niveaux.add(new Niveau(2, - new Color(187, 138, 255), - 10, 1 / 25.0, 55, 0, 600)); + try (Connection conn = DriverManager.getConnection(URL_BDD); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql)) { - // Niveau 3 - niveaux.add(new Niveau(3, - new Color(255, 106, 132), - 11, 1 / 20.0, 50, 0, 600)); + while (rs.next()) { + int numero = rs.getInt("numero"); + int r = rs.getInt("couleur_r"); + 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(4, - new Color(191, 255, 207), - 12, 1 / 17.0, 47, 0, 600)); + niveaux.add(new Niveau(numero, new Color(r, g, b), vitesse, freq, rayon, haut, bas)); + } - // Niveau 5 - niveaux.add(new Niveau(5, - new Color(251, 233, 144), - 13, 1 / 15.0, 45, 100, 600)); + } catch (SQLException e) { + System.err.println("[BDD] Erreur lors du chargement des niveaux."); + e.printStackTrace(); + } + + // 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() { diff --git a/src/GestionnaireScore.java b/src/GestionnaireScore.java index 7abd497..e4ec140 100644 --- a/src/GestionnaireScore.java +++ b/src/GestionnaireScore.java @@ -4,47 +4,19 @@ import java.sql.*; 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() { - // 1) Charger explicitement le driver SQLite JDBC try { Class.forName("org.sqlite.JDBC"); - System.out.println("[BDD] Driver SQLite JDBC chargé correctement (org.sqlite.JDBC)."); } catch (ClassNotFoundException e) { - System.err.println("[BDD] ERREUR : impossible de charger le driver SQLite JDBC (org.sqlite.JDBC)."); - 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()); + System.err.println("[BDD] Impossible de charger le driver SQLite."); e.printStackTrace(); } } public void sauvegarderScore(String nom, int points) { if (nom == null || nom.trim().isEmpty()) { - System.err.println("[BDD] Tentative d'enregistrer un score avec un nom vide. Opération annulée."); return; } @@ -55,13 +27,10 @@ public class GestionnaireScore { pstmt.setString(1, nom.trim()); 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) { - System.err.println("[BDD] ERREUR SQL lors de l'enregistrement du score."); - System.err.println("Message SQL : " + e.getMessage()); + System.err.println("[BDD] Erreur lors de l'enregistrement du score."); e.printStackTrace(); } } @@ -89,13 +58,9 @@ public class GestionnaireScore { } } catch (SQLException e) { - System.err.println("[BDD] ERREUR SQL lors de la lecture des scores."); - System.err.println("Message SQL : " + e.getMessage()); + System.err.println("[BDD] Erreur lors de la lecture des scores."); e.printStackTrace(); - - 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 "Erreur lors de la lecture des scores."; } return sb.toString(); diff --git a/src/Jeu.java b/src/Jeu.java index b42f07d..2bfb181 100644 --- a/src/Jeu.java +++ b/src/Jeu.java @@ -32,6 +32,8 @@ public class Jeu implements KeyListener, ActionListener, MouseListener { protected GestionnaireScore gestionnaireBDD = new GestionnaireScore(); + protected GestionnaireFinDePartie gestionnaireFinDePartie; + protected int compteurFrames = 0; protected boolean enCollision = false; protected boolean estGameOver = false; @@ -57,6 +59,8 @@ public class Jeu implements KeyListener, ActionListener, MouseListener { labGameOver.setVisible(false); ecran.add(labGameOver); + gestionnaireFinDePartie = new GestionnaireFinDePartie(labGameOver, gestionnaireBDD); + ecran.addMouseListener(this); ecran.traiterBoucleAnimation(); } @@ -134,7 +138,7 @@ public class Jeu implements KeyListener, ActionListener, MouseListener { estGameOver = false; compteurInvincible = 0; gestionnaireNiveau.reinitialiser(); - labGameOver.setVisible(false); + gestionnaireFinDePartie.reinitialiser(); ecran.demarrer(); if (horloge == null) { @@ -185,12 +189,8 @@ public class Jeu implements KeyListener, ActionListener, MouseListener { if (vies <= 0) { vies = 0; estGameOver = true; - ecran.arreter(); - labGameOver.setVisible(true); - - if (pseudo != null && !pseudo.isEmpty()) { - gestionnaireBDD.sauvegarderScore(pseudo, (int) score); - } + // Le gestionnaire s'occupe d'arrêter l'écran, afficher le label et sauvegarder + gestionnaireFinDePartie.declencherGameOver(ecran, pseudo, (int) score); } labScore.setText("

Score : " + (int) score + " | Vies : " + vies + " | Niv. " + gestionnaireNiveau.getNumeroNiveau() + "

"); @@ -238,6 +238,7 @@ public class Jeu implements KeyListener, ActionListener, MouseListener { @Override public void mouseExited(MouseEvent e) { } + public GestionnaireScore getGestionnaireBDD() { return gestionnaireBDD; }