Malus mis a jour et fonctionnel
This commit is contained in:
@@ -63,6 +63,10 @@ public class Cercle extends ObjetGraphique{ // il s'agit plutôt d'arcs de cercl
|
|||||||
return rayon;
|
return rayon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setRayon(double rayon){
|
||||||
|
this.rayon = rayon;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
// Méthode qui note que up a été appuyée
|
// Méthode qui note que up a été appuyée
|
||||||
|
|||||||
@@ -25,16 +25,17 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
protected JLabel labScore;
|
protected JLabel labScore;
|
||||||
protected int score;
|
protected int score;
|
||||||
|
|
||||||
//base de données
|
// base de données
|
||||||
protected GestionnaireBDD bdd = new GestionnaireBDD();
|
protected GestionnaireBDD bdd = new GestionnaireBDD();
|
||||||
|
|
||||||
protected Random random = new Random();
|
protected Random random = new Random();
|
||||||
protected int prochainBonusScore;
|
protected int prochainBonusScore;
|
||||||
|
protected int prochainMalusScore;
|
||||||
|
|
||||||
protected int utilisateurIdConnecte = -1;
|
protected int utilisateurIdConnecte = -1;
|
||||||
protected String identifiantUtilisateurConnecte;
|
protected String identifiantUtilisateurConnecte;
|
||||||
public int idCampagneActive = 0; // Retient la campagne (1 = Autoroute, etc.)
|
public int idCampagneActive = 0;
|
||||||
public int difficulteActive = 0; // Retient la difficulté (1=Facile, 2=Moyen, 3=Difficile)
|
public int difficulteActive = 0;
|
||||||
|
|
||||||
public Jeu() {
|
public Jeu() {
|
||||||
score = 0;
|
score = 0;
|
||||||
@@ -60,7 +61,6 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
// Initialisation initiale
|
// Initialisation initiale
|
||||||
resetPartie(6, 20);
|
resetPartie(6, 20);
|
||||||
|
|
||||||
// On ajoute l'action au bouton "Retour" de la ZoneDessin (Game Over)
|
|
||||||
ecran.btnRetour.addActionListener(new ActionListener() {
|
ecran.btnRetour.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@@ -167,6 +167,7 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
labScore.setText("<html><h3>score : 0</h3></html>");
|
labScore.setText("<html><h3>score : 0</h3></html>");
|
||||||
|
|
||||||
this.prochainBonusScore = 300 + random.nextInt(201);
|
this.prochainBonusScore = 300 + random.nextInt(201);
|
||||||
|
this.prochainMalusScore = 400 + random.nextInt(201);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -190,6 +191,44 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (score == prochainMalusScore) {
|
||||||
|
double xSpawn = 810;
|
||||||
|
double yLigne = laligne.getHauteurLigneA(xSpawn);
|
||||||
|
|
||||||
|
if (yLigne != -1) {
|
||||||
|
double yOffset = random.nextBoolean() ? -30 : 30;
|
||||||
|
double yMalus = yLigne + yOffset;
|
||||||
|
|
||||||
|
Malus malus = new Malus(xSpawn, yMalus, laligne.vitesseDefilement);
|
||||||
|
ecran.ajouterObjet(malus);
|
||||||
|
|
||||||
|
this.prochainMalusScore = score + 400 + random.nextInt(201);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = ecran.listeObjets.size() - 1; i >= 0; i--) {
|
||||||
|
ObjetGraphique obj = ecran.listeObjets.get(i);
|
||||||
|
if (obj instanceof Bonus || obj instanceof Malus) {
|
||||||
|
double dx = obj.getX() - demiCercleAvant.getX();
|
||||||
|
double dy = obj.getY() - demiCercleAvant.getY();
|
||||||
|
double dist = Math.sqrt(dx * dx + dy * dy);
|
||||||
|
|
||||||
|
if (dist < demiCercleAvant.getRayon()) {
|
||||||
|
if (obj instanceof Bonus) {
|
||||||
|
double newRayon = demiCercleAvant.getRayon() + 3;
|
||||||
|
demiCercleAvant.setRayon(newRayon);
|
||||||
|
demiCercleArriere.setRayon(newRayon);
|
||||||
|
} else {
|
||||||
|
double newRayon = demiCercleAvant.getRayon() - 3;
|
||||||
|
if (newRayon < 10)
|
||||||
|
newRayon = 10;
|
||||||
|
demiCercleAvant.setRayon(newRayon);
|
||||||
|
demiCercleArriere.setRayon(newRayon);
|
||||||
|
}
|
||||||
|
ecran.listeObjets.remove(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
labScore.setText("<html><h3>score : " + score + "</h3></html>");
|
labScore.setText("<html><h3>score : " + score + "</h3></html>");
|
||||||
// Gestion collision simple
|
// Gestion collision simple
|
||||||
@@ -197,35 +236,29 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
if (hauteurLigne != -1) {
|
if (hauteurLigne != -1) {
|
||||||
// calcule de la distance entre le centre du cercle et la ligne
|
// calcule de la distance entre le centre du cercle et la ligne
|
||||||
double distance = Math.abs(hauteurLigne - demiCercleAvant.getY());
|
double distance = Math.abs(hauteurLigne - demiCercleAvant.getY());
|
||||||
|
|
||||||
// Si cette distance est strictement supérieure au rayon du bouclier,
|
|
||||||
// cela signifie que la ligne est sortie de la bulle !
|
|
||||||
if (distance > demiCercleAvant.getRayon()) {
|
if (distance > demiCercleAvant.getRayon()) {
|
||||||
gameOver();
|
gameOver();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Si on arrive au bout de la ligne
|
|
||||||
victoire();
|
victoire();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void victoire() {
|
public void victoire() {
|
||||||
horloge.stop();
|
horloge.stop();
|
||||||
ecran.setVictoire(true); // Affiche "Victoire" et le bouton Retour
|
ecran.setVictoire(true);
|
||||||
|
|
||||||
int dureePartie = score / 25; // Exemple : 25 frames par seconde
|
int dureePartie = score / 25;
|
||||||
|
|
||||||
bdd.enregistrerPartie(dureePartie, idCampagneActive, difficulteActive, score, utilisateurIdConnecte);
|
bdd.enregistrerPartie(dureePartie, idCampagneActive, difficulteActive, score, utilisateurIdConnecte);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void gameOver() {
|
public void gameOver() {
|
||||||
horloge.stop();
|
horloge.stop();
|
||||||
ecran.setGameOver(true); // Affiche "Game Over" et le bouton Retour
|
ecran.setGameOver(true);
|
||||||
|
|
||||||
int dureePartie = score / 25; // Exemple : 25 frames par seconde
|
int dureePartie = score / 25;
|
||||||
|
|
||||||
// On utilise nos deux variables pour la sauvegarde !
|
|
||||||
// L'appel est maintenant plus robuste grâce au nouveau GestionnaireBDD
|
|
||||||
bdd.enregistrerPartie(dureePartie, idCampagneActive, difficulteActive, score, utilisateurIdConnecte);
|
bdd.enregistrerPartie(dureePartie, idCampagneActive, difficulteActive, score, utilisateurIdConnecte);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
29
src/linea/Malus.java
Normal file
29
src/linea/Malus.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package linea;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
|
||||||
|
public class Malus extends ObjetGraphique {
|
||||||
|
|
||||||
|
private int largeur = 20;
|
||||||
|
private int hauteur = 20;
|
||||||
|
private double vitesseDefilement;
|
||||||
|
|
||||||
|
public Malus(double x, double y, double vitesse) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.vitesseDefilement = vitesse;
|
||||||
|
this.couleur = Color.RED;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void Afficher(Graphics g) {
|
||||||
|
g.setColor(this.couleur);
|
||||||
|
g.fillRect((int)x, (int)y, largeur, hauteur);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void Animer() {
|
||||||
|
this.x -= vitesseDefilement;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,31 +17,35 @@ public class ZoneDessin extends JPanel {
|
|||||||
// LE BOUTON DOIT ÊTRE PUBLIC POUR ÊTRE ACCESSIBLE DEPUIS JEU.JAVA
|
// LE BOUTON DOIT ÊTRE PUBLIC POUR ÊTRE ACCESSIBLE DEPUIS JEU.JAVA
|
||||||
public JButton btnRetour;
|
public JButton btnRetour;
|
||||||
|
|
||||||
public ZoneDessin(){
|
public ZoneDessin() {
|
||||||
// Layout null pour positionner le bouton pixel par pixel
|
// Layout null pour positionner le bouton pixel par pixel
|
||||||
setLayout(null);
|
setLayout(null);
|
||||||
setPreferredSize(new Dimension(800, 600));
|
setPreferredSize(new Dimension(800, 600));
|
||||||
setBackground(new Color(100,100,100));
|
setBackground(new Color(100, 100, 100));
|
||||||
|
|
||||||
// 2. Création et configuration du bouton
|
|
||||||
btnRetour = new JButton("Back to Menu");
|
btnRetour = new JButton("Back to Menu");
|
||||||
// x, y, largeur, hauteur (centré horizontalement pour une fenêtre de 800px)
|
// entré horizontalement pour une fenêtre de 800px
|
||||||
btnRetour.setBounds(300, 350, 200, 50);
|
btnRetour.setBounds(300, 350, 200, 50);
|
||||||
btnRetour.setFont(new Font("Arial", Font.BOLD, 16));
|
btnRetour.setFont(new Font("Arial", Font.BOLD, 16));
|
||||||
btnRetour.setFocusable(false); // Important pour ne pas gêner le clavier
|
btnRetour.setFocusable(false);
|
||||||
btnRetour.setVisible(false); // Caché au début
|
btnRetour.setVisible(false);
|
||||||
|
|
||||||
this.add(btnRetour);
|
this.add(btnRetour);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ... (méthodes ajouterObjet, viderObjets, arreter, demarrer restent pareilles) ...
|
public void ajouterObjet(ObjetGraphique unObjet) {
|
||||||
public void ajouterObjet(ObjetGraphique unObjet) { this.listeObjets.add(unObjet); }
|
this.listeObjets.add(unObjet);
|
||||||
public void viderObjets() { this.listeObjets.clear(); }
|
}
|
||||||
|
|
||||||
public void traiterBoucleAnimation(){
|
public void viderObjets() {
|
||||||
if (estArrete || isGameOver || isVictoire) return;
|
this.listeObjets.clear();
|
||||||
|
}
|
||||||
|
|
||||||
for (ObjetGraphique obj : listeObjets){
|
public void traiterBoucleAnimation() {
|
||||||
|
if (estArrete || isGameOver || isVictoire)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (ObjetGraphique obj : listeObjets) {
|
||||||
obj.Animer();
|
obj.Animer();
|
||||||
}
|
}
|
||||||
repaint();
|
repaint();
|
||||||
@@ -50,15 +54,15 @@ public class ZoneDessin extends JPanel {
|
|||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
Graphics2D g2D = (Graphics2D) g;
|
Graphics2D g2D = (Graphics2D) g;
|
||||||
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
|
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
|
||||||
for (ObjetGraphique obj : listeObjets){
|
for (ObjetGraphique obj : listeObjets) {
|
||||||
obj.Afficher(g);
|
obj.Afficher(g);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Affichage de l'écran Game Over
|
// Affichage de l'écran Game Over
|
||||||
if (isGameOver) {
|
if (isGameOver) {
|
||||||
g2D.setColor(new Color(0, 0, 0, 150)); // Fond sombre
|
g2D.setColor(new Color(0, 0, 0, 150));
|
||||||
g2D.fillRect(0, 0, getWidth(), getHeight());
|
g2D.fillRect(0, 0, getWidth(), getHeight());
|
||||||
|
|
||||||
g2D.setColor(Color.WHITE);
|
g2D.setColor(Color.WHITE);
|
||||||
@@ -67,7 +71,7 @@ public class ZoneDessin extends JPanel {
|
|||||||
int largeur = g2D.getFontMetrics().stringWidth(msg);
|
int largeur = g2D.getFontMetrics().stringWidth(msg);
|
||||||
g2D.drawString(msg, (getWidth() - largeur) / 2, getHeight() / 2 - 50);
|
g2D.drawString(msg, (getWidth() - largeur) / 2, getHeight() / 2 - 50);
|
||||||
} else if (isVictoire) {
|
} else if (isVictoire) {
|
||||||
g2D.setColor(new Color(0, 0, 0, 150)); // Fond sombre
|
g2D.setColor(new Color(0, 0, 0, 150));
|
||||||
g2D.fillRect(0, 0, getWidth(), getHeight());
|
g2D.fillRect(0, 0, getWidth(), getHeight());
|
||||||
|
|
||||||
g2D.setColor(Color.GREEN);
|
g2D.setColor(Color.GREEN);
|
||||||
@@ -81,10 +85,10 @@ public class ZoneDessin extends JPanel {
|
|||||||
|
|
||||||
public void setGameOver(boolean state) {
|
public void setGameOver(boolean state) {
|
||||||
this.isGameOver = state;
|
this.isGameOver = state;
|
||||||
// 3. C'est ici qu'on affiche ou cache le bouton
|
|
||||||
btnRetour.setVisible(state);
|
btnRetour.setVisible(state);
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVictoire(boolean state) {
|
public void setVictoire(boolean state) {
|
||||||
this.isVictoire = state;
|
this.isVictoire = state;
|
||||||
btnRetour.setVisible(state);
|
btnRetour.setVisible(state);
|
||||||
|
|||||||
Reference in New Issue
Block a user