menu
This commit is contained in:
218
src/Jeu.java
218
src/Jeu.java
@@ -1,18 +1,29 @@
|
|||||||
package linea;
|
package linea;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.awt.Cursor;
|
||||||
|
import java.awt.Dimension;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.KeyListener;
|
import java.awt.event.KeyListener;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.awt.event.MouseListener;
|
||||||
|
|
||||||
|
import javax.swing.BorderFactory;
|
||||||
|
import javax.swing.Box;
|
||||||
|
import javax.swing.BoxLayout;
|
||||||
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JTextField;
|
||||||
import javax.swing.SwingConstants;
|
import javax.swing.SwingConstants;
|
||||||
import javax.swing.Timer;
|
import javax.swing.Timer;
|
||||||
|
|
||||||
public class Jeu implements KeyListener, ActionListener {
|
public class Jeu implements KeyListener, ActionListener, MouseListener {
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// PROPRIETES
|
// PROPRIETES
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
@@ -43,9 +54,14 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
protected boolean enCollision = false;
|
protected boolean enCollision = false;
|
||||||
protected boolean estGameOver = false;
|
protected boolean estGameOver = false;
|
||||||
protected JLabel labGameOver;
|
protected JLabel labGameOver;
|
||||||
protected int compteurGameOver = 0;
|
|
||||||
protected int compteurInvincible = 0;
|
protected int compteurInvincible = 0;
|
||||||
|
|
||||||
|
// menu et fenetre
|
||||||
|
protected JFrame fenetre;
|
||||||
|
protected JPanel panneauMenu;
|
||||||
|
protected JTextField champPseudo;
|
||||||
|
protected String pseudo = "";
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// METHODES
|
// METHODES
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
@@ -62,13 +78,16 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
labScore.setBounds(20, 0, 200, 50);
|
labScore.setBounds(20, 0, 200, 50);
|
||||||
ecran.add(labScore);
|
ecran.add(labScore);
|
||||||
|
|
||||||
labGameOver = new JLabel("GAME OVER", SwingConstants.CENTER);
|
labGameOver = new JLabel("GAME OVER - clic pour menu", SwingConstants.CENTER);
|
||||||
labGameOver.setForeground(Color.WHITE);
|
labGameOver.setForeground(Color.WHITE);
|
||||||
labGameOver.setFont(new Font("Arial", Font.BOLD, 60));
|
labGameOver.setFont(new Font("Arial", Font.BOLD, 40));
|
||||||
labGameOver.setBounds(0, 200, 800, 100);
|
labGameOver.setBounds(0, 200, 800, 100);
|
||||||
labGameOver.setVisible(false);
|
labGameOver.setVisible(false);
|
||||||
ecran.add(labGameOver);
|
ecran.add(labGameOver);
|
||||||
|
|
||||||
|
// ecoute les clics de souris (pour le game over)
|
||||||
|
ecran.addMouseListener(this);
|
||||||
|
|
||||||
ecran.traiterBoucleAnimation();
|
ecran.traiterBoucleAnimation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +145,123 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
public void demarrer() {
|
public void demarrer() {
|
||||||
// Création d'une fenêtre
|
// Création d'une fenêtre
|
||||||
JFrame fenetre = new JFrame();
|
fenetre = new JFrame("Linea");
|
||||||
|
|
||||||
|
// on affiche le menu en premier
|
||||||
|
afficherMenu();
|
||||||
|
|
||||||
|
fenetre.setSize(800, 600);
|
||||||
|
fenetre.setResizable(false);
|
||||||
|
fenetre.setLocationRelativeTo(null); // centre la fenetre sur l'ecran
|
||||||
|
fenetre.setVisible(true);
|
||||||
|
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void afficherMenu() {
|
||||||
|
panneauMenu = new JPanel();
|
||||||
|
panneauMenu.setLayout(new BoxLayout(panneauMenu, BoxLayout.Y_AXIS));
|
||||||
|
panneauMenu.setBackground(new Color(15, 15, 35));
|
||||||
|
panneauMenu.setPreferredSize(new Dimension(800, 600));
|
||||||
|
|
||||||
|
// espace en haut
|
||||||
|
panneauMenu.add(Box.createVerticalStrut(80));
|
||||||
|
|
||||||
|
// titre du jeu
|
||||||
|
JLabel titre = new JLabel("LINEA");
|
||||||
|
titre.setAlignmentX(JLabel.CENTER_ALIGNMENT);
|
||||||
|
titre.setForeground(new Color(0, 180, 255));
|
||||||
|
titre.setFont(new Font("Arial", Font.BOLD, 80));
|
||||||
|
panneauMenu.add(titre);
|
||||||
|
|
||||||
|
// petit sous-titre
|
||||||
|
JLabel sousTitre = new JLabel("esquive la ligne");
|
||||||
|
sousTitre.setAlignmentX(JLabel.CENTER_ALIGNMENT);
|
||||||
|
sousTitre.setForeground(new Color(120, 120, 160));
|
||||||
|
sousTitre.setFont(new Font("Arial", Font.PLAIN, 18));
|
||||||
|
panneauMenu.add(sousTitre);
|
||||||
|
|
||||||
|
panneauMenu.add(Box.createVerticalStrut(40));
|
||||||
|
|
||||||
|
// label pseudo
|
||||||
|
JLabel labPseudo = new JLabel("pseudo");
|
||||||
|
labPseudo.setAlignmentX(JLabel.CENTER_ALIGNMENT);
|
||||||
|
labPseudo.setForeground(new Color(180, 180, 200));
|
||||||
|
labPseudo.setFont(new Font("Arial", Font.PLAIN, 14));
|
||||||
|
panneauMenu.add(labPseudo);
|
||||||
|
|
||||||
|
panneauMenu.add(Box.createVerticalStrut(8));
|
||||||
|
|
||||||
|
// champ de texte pour le pseudo
|
||||||
|
champPseudo = new JTextField(15);
|
||||||
|
champPseudo.setText(pseudo); // garde le pseudo prérempli s'il y en a un
|
||||||
|
champPseudo.setMaximumSize(new Dimension(250, 40));
|
||||||
|
champPseudo.setFont(new Font("Arial", Font.PLAIN, 16));
|
||||||
|
champPseudo.setHorizontalAlignment(JTextField.CENTER);
|
||||||
|
champPseudo.setBackground(new Color(30, 30, 60));
|
||||||
|
champPseudo.setForeground(Color.WHITE);
|
||||||
|
champPseudo.setCaretColor(Color.WHITE);
|
||||||
|
champPseudo.setBorder(BorderFactory.createLineBorder(new Color(0, 180, 255), 2, true));
|
||||||
|
champPseudo.setAlignmentX(JTextField.CENTER_ALIGNMENT);
|
||||||
|
panneauMenu.add(champPseudo);
|
||||||
|
|
||||||
|
panneauMenu.add(Box.createVerticalStrut(40));
|
||||||
|
|
||||||
|
// bouton jouer
|
||||||
|
JButton boutonJouer = new JButton("JOUER");
|
||||||
|
styleBouton(boutonJouer, new Color(0, 130, 255));
|
||||||
|
boutonJouer.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
lancerPartie();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panneauMenu.add(boutonJouer);
|
||||||
|
|
||||||
|
panneauMenu.add(Box.createVerticalStrut(15));
|
||||||
|
|
||||||
|
// bouton classement
|
||||||
|
JButton boutonClassement = new JButton("CLASSEMENT");
|
||||||
|
styleBouton(boutonClassement, new Color(100, 100, 150));
|
||||||
|
boutonClassement.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JOptionPane.showMessageDialog(fenetre, "TOP SCORES", "Classement", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panneauMenu.add(boutonClassement);
|
||||||
|
|
||||||
|
panneauMenu.add(Box.createVerticalStrut(15));
|
||||||
|
|
||||||
|
// bouton quitter
|
||||||
|
JButton boutonQuitter = new JButton("QUITTER");
|
||||||
|
styleBouton(boutonQuitter, new Color(200, 50, 50));
|
||||||
|
boutonQuitter.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panneauMenu.add(boutonQuitter);
|
||||||
|
|
||||||
|
fenetre.setContentPane(panneauMenu);
|
||||||
|
fenetre.revalidate();
|
||||||
|
fenetre.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void styleBouton(JButton btn, Color fond) {
|
||||||
|
btn.setAlignmentX(JButton.CENTER_ALIGNMENT);
|
||||||
|
btn.setFont(new Font("Arial", Font.BOLD, 18));
|
||||||
|
btn.setForeground(Color.WHITE);
|
||||||
|
btn.setBackground(fond);
|
||||||
|
btn.setFocusPainted(false);
|
||||||
|
btn.setBorderPainted(false);
|
||||||
|
btn.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
||||||
|
btn.setMaximumSize(new Dimension(200, 45));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void lancerPartie() {
|
||||||
|
// on recupere le pseudo
|
||||||
|
pseudo = champPseudo.getText();
|
||||||
|
|
||||||
// A FAIRE :
|
// A FAIRE :
|
||||||
// placer dans l'instance de l'écran tous les objets graphiques nécessaires
|
// placer dans l'instance de l'écran tous les objets graphiques nécessaires
|
||||||
@@ -140,14 +275,26 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
// on indique que c'est le jeu qui traitera les appuis sur une touche
|
// on indique que c'est le jeu qui traitera les appuis sur une touche
|
||||||
ecran.addKeyListener(this);
|
ecran.addKeyListener(this);
|
||||||
ecran.setFocusable(true);
|
ecran.setFocusable(true);
|
||||||
|
|
||||||
|
// on remplace le menu par l'ecran de jeu
|
||||||
fenetre.setContentPane(ecran);
|
fenetre.setContentPane(ecran);
|
||||||
fenetre.pack();
|
fenetre.revalidate();
|
||||||
fenetre.setLocation(100, 100);
|
fenetre.repaint();
|
||||||
fenetre.setVisible(true);
|
ecran.requestFocusInWindow();
|
||||||
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
|
// on reinitialise les valeurs de jeu
|
||||||
|
score = 1;
|
||||||
|
compteurFrames = 0;
|
||||||
|
enCollision = false;
|
||||||
|
estGameOver = false;
|
||||||
|
compteurInvincible = 0;
|
||||||
|
labGameOver.setVisible(false);
|
||||||
|
ecran.demarrer();
|
||||||
|
|
||||||
// Démarrage du timer, qui rythmera l'animation
|
// Démarrage du timer, qui rythmera l'animation
|
||||||
horloge = new Timer(40, this);
|
if (horloge == null) {
|
||||||
|
horloge = new Timer(40, this);
|
||||||
|
}
|
||||||
horloge.start();
|
horloge.start();
|
||||||
|
|
||||||
// A FAIRE :
|
// A FAIRE :
|
||||||
@@ -161,11 +308,8 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
// si c'est game over, on arrete juste l'animation et on attend le clic
|
||||||
if (estGameOver == true) {
|
if (estGameOver == true) {
|
||||||
compteurGameOver = compteurGameOver + 1;
|
|
||||||
if (compteurGameOver >= 75) {
|
|
||||||
RecommencerPartie();
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,7 +347,13 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
demiCercleAvant.ResterDansLigne(lili);
|
demiCercleAvant.ResterDansLigne(lili);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RecommencerPartie() {
|
private void retourMenu() {
|
||||||
|
// on arrete le timer du jeu
|
||||||
|
if (horloge != null) {
|
||||||
|
horloge.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
// on recrée une nouvelle ligne propre pour la prochaine partie
|
||||||
lili = new Ligne();
|
lili = new Ligne();
|
||||||
|
|
||||||
demiCercleAvant.y = 200;
|
demiCercleAvant.y = 200;
|
||||||
@@ -212,18 +362,34 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
demiCercleArriere.vitesse = -1.0;
|
demiCercleArriere.vitesse = -1.0;
|
||||||
|
|
||||||
ecran.viderObjets();
|
ecran.viderObjets();
|
||||||
ecran.ajouterObjet(demiCercleArriere);
|
|
||||||
ecran.ajouterObjet(lili);
|
|
||||||
ecran.ajouterObjet(demiCercleAvant);
|
|
||||||
|
|
||||||
score = 1;
|
// on retourne au menu principal
|
||||||
compteurFrames = 0;
|
afficherMenu();
|
||||||
enCollision = false;
|
|
||||||
estGameOver = false;
|
|
||||||
compteurGameOver = 0;
|
|
||||||
compteurInvincible = 0;
|
|
||||||
labGameOver.setVisible(false);
|
|
||||||
ecran.demarrer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// événements souris (pour revenir au menu apres game over)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
if (estGameOver == true) {
|
||||||
|
retourMenu();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseEntered(MouseEvent e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package linea;
|
package linea;
|
||||||
|
|
||||||
public class LineaAppli {
|
public class LineaAppli {
|
||||||
//-------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// Classe de base de l'application, rien à modifier ici
|
// Classe de base de l'application, rien à modifier ici
|
||||||
//-------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
public static void main(String[] arg) {
|
public static void main(String[] arg) {
|
||||||
|
Jeu jeu = new Jeu();
|
||||||
new MenuPrincipal();
|
jeu.demarrer();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
package linea;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
public class MenuPrincipal extends JFrame {
|
|
||||||
|
|
||||||
public MenuPrincipal() {
|
|
||||||
// Configuration de la fenêtre du menu
|
|
||||||
setTitle("LINEA - Menu Principal");
|
|
||||||
setSize(400, 500);
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
setLocationRelativeTo(null); // Centre la fenêtre au milieu de l'écran
|
|
||||||
setResizable(false);
|
|
||||||
|
|
||||||
// Panneau de fond (Même bleu que le jeu pour le style)
|
|
||||||
JPanel panel = new JPanel();
|
|
||||||
panel.setLayout(null); // On place les éléments manuellement
|
|
||||||
panel.setBackground(new Color(0, 73, 220));
|
|
||||||
setContentPane(panel);
|
|
||||||
|
|
||||||
// --- TITRE ---
|
|
||||||
JLabel titre = new JLabel("LINEA", SwingConstants.CENTER);
|
|
||||||
titre.setFont(new Font("Arial", Font.BOLD, 40));
|
|
||||||
titre.setForeground(Color.WHITE);
|
|
||||||
titre.setBounds(0, 50, 400, 60);
|
|
||||||
panel.add(titre);
|
|
||||||
|
|
||||||
// --- BOUTON START ---
|
|
||||||
JButton btnStart = new JButton("COMMENCER");
|
|
||||||
btnStart.setBounds(100, 160, 200, 50);
|
|
||||||
styleBouton(btnStart);
|
|
||||||
btnStart.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
lancerJeu();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
panel.add(btnStart);
|
|
||||||
|
|
||||||
// --- BOUTON CLASSEMENT ---
|
|
||||||
JButton btnClassement = new JButton("CLASSEMENT");
|
|
||||||
btnClassement.setBounds(100, 230, 200, 50);
|
|
||||||
styleBouton(btnClassement);
|
|
||||||
btnClassement.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
afficherClassement();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
panel.add(btnClassement);
|
|
||||||
|
|
||||||
// --- BOUTON LOGOUT ---
|
|
||||||
JButton btnLogout = new JButton("LOGOUT");
|
|
||||||
btnLogout.setBounds(100, 300, 200, 50);
|
|
||||||
styleBouton(btnLogout);
|
|
||||||
btnLogout.setBackground(new Color(200, 50, 50)); // Rouge pour quitter
|
|
||||||
btnLogout.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
System.exit(0); // Ferme tout
|
|
||||||
}
|
|
||||||
});
|
|
||||||
panel.add(btnLogout);
|
|
||||||
|
|
||||||
// Afficher la fenêtre
|
|
||||||
setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Méthode pour lancer le jeu
|
|
||||||
private void lancerJeu() {
|
|
||||||
// On ferme le menu
|
|
||||||
this.dispose();
|
|
||||||
|
|
||||||
// On lance le jeu existant
|
|
||||||
Jeu jeu = new Jeu();
|
|
||||||
jeu.demarrer();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Méthode pour afficher les classements
|
|
||||||
private void afficherClassement() {
|
|
||||||
// Comme on a retiré la BDD, on affiche une liste simulée
|
|
||||||
String message = "TOP SCORES";
|
|
||||||
|
|
||||||
JOptionPane.showMessageDialog(this, message, "Classement", JOptionPane.INFORMATION_MESSAGE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Petite méthode pour rendre les boutons jolis
|
|
||||||
private void styleBouton(JButton btn) {
|
|
||||||
btn.setFont(new Font("Arial", Font.BOLD, 16));
|
|
||||||
btn.setBackground(Color.WHITE);
|
|
||||||
btn.setForeground(Color.BLACK);
|
|
||||||
btn.setFocusPainted(false);
|
|
||||||
btn.setBorder(BorderFactory.createRaisedBevelBorder());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user