Ajout d'un game over stylisé

This commit is contained in:
2024-11-24 00:57:22 +01:00
parent 3f06f179a5
commit 3e26d5cf33
2 changed files with 80 additions and 20 deletions

View File

@@ -54,7 +54,7 @@ public class Options {
public static boolean AUTO_FOCUS = true; public static boolean AUTO_FOCUS = true;
public static final int MAX_TILE_NUMBER = 50; public static final int MAX_TILE_NUMBER = 5;
public static boolean FULL_SCREEN = false; public static boolean FULL_SCREEN = false;

View File

@@ -2,6 +2,7 @@ package fr.monkhanny.dorfromantik.game;
import fr.monkhanny.dorfromantik.utils.Database; import fr.monkhanny.dorfromantik.utils.Database;
import fr.monkhanny.dorfromantik.Options; import fr.monkhanny.dorfromantik.Options;
import fr.monkhanny.dorfromantik.enums.Fonts;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@@ -20,45 +21,104 @@ public class GameOver extends JPanel {
setLayout(new BorderLayout()); setLayout(new BorderLayout());
// Message de fin de jeu // Background image setup
JLabel messageLabel = new JLabel("Partie terminée ! Votre score est : " + finalScore, JLabel.CENTER); JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg"));
messageLabel.setFont(new Font("Arial", Font.BOLD, 24)); background.setLayout(new GridBagLayout());
add(messageLabel, BorderLayout.CENTER); this.add(background);
// Panneau pour entrer le pseudo // Main content panel
JPanel inputPanel = new JPanel(); JPanel mainPanel = createMainPanel();
inputPanel.setLayout(new FlowLayout()); background.add(mainPanel);
JLabel nameLabel = new JLabel("Entrez votre pseudo :");
JTextField nameField = new JTextField(20);
JButton submitButton = new JButton("Soumettre");
// Title for the Game Over message
JLabel titleLabel = new JLabel("Partie terminée !");
titleLabel.setFont(Fonts.TITLE.getFont(48)); // Using the TITLE font
titleLabel.setForeground(Color.WHITE);
mainPanel.add(titleLabel, createGridBagConstraints(0, 0, 2));
// Display final score
JLabel scoreLabel = new JLabel("Votre score est de : " + finalScore);
scoreLabel.setFont(Fonts.SCORE.getFont(36)); // Using the SCORE font
scoreLabel.setForeground(Color.WHITE);
mainPanel.add(scoreLabel, createGridBagConstraints(0, 1, 2));
// Vertical spacer
mainPanel.add(Box.createVerticalStrut(20), createGridBagConstraints(0, 2, 1));
// Input panel for username and submission
JPanel inputPanel = createInputPanel();
mainPanel.add(inputPanel, createGridBagConstraints(0, 3, 2));
// Vertical spacer
mainPanel.add(Box.createVerticalStrut(20), createGridBagConstraints(0, 4, 1));
}
private JPanel createMainPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setOpaque(false); // Transparent background
return mainPanel;
}
private JPanel createInputPanel() {
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
inputPanel.setOpaque(false);
// Username label and text field
JLabel nameLabel = new JLabel("Entrez votre pseudo (facultatif) :");
nameLabel.setForeground(Color.WHITE);
nameLabel.setFont(Fonts.SCORE.getFont(24));
inputPanel.add(nameLabel); inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(submitButton);
add(inputPanel, BorderLayout.SOUTH);
// Action pour soumettre le pseudo et enregistrer dans la base de données JTextField nameField = new JTextField(20);
nameField.setFont(Fonts.SCORE.getFont(18));
nameField.setPreferredSize(new Dimension(250, 40));
nameField.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
nameField.setText("Anonyme");
inputPanel.add(nameField);
// Submit button
JButton submitButton = new JButton("Soumettre");
submitButton.setFont(Fonts.BUTTON.getFont(24)); // Using the BUTTON font
submitButton.setBackground(new Color(0, 255, 0));
submitButton.setForeground(Color.WHITE);
submitButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
submitButton.setPreferredSize(new Dimension(150, 50));
inputPanel.add(submitButton);
// Action to handle score submission
submitButton.addActionListener(new ActionListener() { submitButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String username = nameField.getText().trim(); String username = nameField.getText().trim();
if (username.isEmpty()) { if (username.isEmpty()) {
username = "Anonyme"; // Si aucun pseudo n'est donné, on utilise "Anonyme" username = "Anonyme"; // Default to "Anonyme" if no name is given
} }
// Enregistrer le score dans la base de données // Save the score to the database
try { try {
long seriesId = Options.SEED; // Remplacer par l'ID de la série ou du mode de jeu long seriesId = Options.SEED; // Replace with the appropriate series ID
database.addScore(username, seriesId, finalScore); database.addScore(username, seriesId, finalScore);
JOptionPane.showMessageDialog(gameFrame, "Score enregistré avec succès !"); JOptionPane.showMessageDialog(gameFrame, "Score enregistré avec succès !");
} catch (Exception ex) { } catch (Exception ex) {
JOptionPane.showMessageDialog(gameFrame, "Erreur lors de l'enregistrement du score : " + ex.getMessage()); JOptionPane.showMessageDialog(gameFrame, "Erreur lors de l'enregistrement du score : " + ex.getMessage());
} }
// Retourner au menu principal ou à une autre vue après la fin du jeu // Close game over screen and return to the main menu
gameFrame.setVisible(false); gameFrame.setVisible(false);
// Ajoutez ici le code pour retourner à l'écran principal, ou démarrer une nouvelle partie. // Add code here to return to the main menu or start a new game.
} }
}); });
return inputPanel;
}
private GridBagConstraints createGridBagConstraints(int x, int y, int gridWidth) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = gridWidth;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10, 20, 10, 20); // Adjust spacing
return gbc;
} }
} }