Merge remote-tracking branch 'origin/Gwendal' into Thibaut
This commit is contained in:
BIN
app/bin/main/TetrisMusic.mp3
Normal file
BIN
app/bin/main/TetrisMusic.mp3
Normal file
Binary file not shown.
@@ -23,6 +23,9 @@ dependencies {
|
||||
|
||||
// This dependency is used by the application.
|
||||
implementation(libs.guava)
|
||||
|
||||
// JLayer pour lire les fichiers MP3
|
||||
implementation("javazoom:jlayer:1.0.1")
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
|
||||
@@ -6,25 +6,25 @@ package org;
|
||||
import org.Controllers.IO;
|
||||
import org.Models.Grille;
|
||||
import org.Models.Jeu;
|
||||
import org.Views.VueGrille;
|
||||
import org.Views.VueTetris;
|
||||
|
||||
public class App {
|
||||
|
||||
public String getGreeting() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new App().getGreeting());
|
||||
|
||||
// Models
|
||||
Grille grille = new Grille(20, 10);
|
||||
Jeu jeu = new Jeu(grille);
|
||||
|
||||
|
||||
// Views
|
||||
VueGrille vueGrille = new VueGrille(grille, jeu);
|
||||
VueTetris vueTetris = new VueTetris(grille, jeu);
|
||||
|
||||
// Controllers
|
||||
IO io = new IO(jeu);
|
||||
vueGrille.addKeyListener(io);
|
||||
vueTetris.addKeyListener(io);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.Controllers;
|
||||
|
||||
import org.Models.Musique;
|
||||
import org.Views.VueBandeauControle;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class TetrisBandeauControleur {
|
||||
private boolean partieEnPause = false;
|
||||
private boolean partieTerminee = false;
|
||||
private Musique musique;
|
||||
private VueBandeauControle vueControle;
|
||||
|
||||
public TetrisBandeauControleur(VueBandeauControle vueControle) {
|
||||
this.vueControle = vueControle;
|
||||
this.musique = new Musique();
|
||||
musique.start();// demarer musique
|
||||
// action play/pause
|
||||
this.vueControle.getPauseButton().addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
switchPlayPause();
|
||||
}
|
||||
});
|
||||
vueControle.getQuitterButton().addActionListener(e -> {
|
||||
System.out.println("Fermeture de l'application...");
|
||||
System.exit(0);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void switchPlayPause() {
|
||||
if (partieTerminee) {
|
||||
return;
|
||||
}
|
||||
partieEnPause = !partieEnPause;
|
||||
musique.basculePlayPause();
|
||||
vueControle.getPauseButton().setText(partieEnPause ? "PLAY" : "PAUSE");
|
||||
System.out.println(partieEnPause ? "Partie en pause" : "Partie en cours");
|
||||
}
|
||||
|
||||
public void setPartieTerminee() {
|
||||
partieTerminee = true;
|
||||
musique.arreterMusique();
|
||||
vueControle.getPauseButton().setEnabled(false);
|
||||
System.out.println("Partie terminée");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,10 @@ public class Grille extends Observable { // TODO: ?? implements Runnable {
|
||||
this.nbLignes = nbLignes;
|
||||
this.nbColonnes = nbColonnes;
|
||||
this.grille = new boolean[nbLignes][nbColonnes];
|
||||
initGrille();
|
||||
}
|
||||
|
||||
public void initGrille() {
|
||||
for (int i = 0; i < nbLignes; i++) {
|
||||
for (int j = 0; j < nbColonnes; j++) {
|
||||
this.grille[i][j] = false;
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.Models;
|
||||
import java.awt.Point;
|
||||
import java.util.Observable;
|
||||
|
||||
import org.Models.Pieces.PieceCarre;
|
||||
import org.Models.Pieces.PieceL;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
|
||||
51
app/src/main/java/org/Models/Musique.java
Normal file
51
app/src/main/java/org/Models/Musique.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package org.Models;
|
||||
|
||||
import javazoom.jl.player.Player;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class Musique extends Thread {
|
||||
private Player player;
|
||||
private boolean stop = false;
|
||||
private boolean enPause = false;
|
||||
|
||||
public void run() {
|
||||
while (!stop) {
|
||||
try {
|
||||
if (!enPause) {
|
||||
InputStream musique = getClass().getResourceAsStream("/TetrisMusic.mp3");
|
||||
if (musique == null) {
|
||||
System.err.println("Erreur : le fichier musique.mp3 est introuvable.");
|
||||
return;
|
||||
}
|
||||
BufferedInputStream buffer = new BufferedInputStream(musique);
|
||||
player = new Player(buffer);
|
||||
player.play();
|
||||
} else {
|
||||
Thread.sleep(1000); // Attendre 1 seconde avant de vérifier à nouveau
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Erreur lors de la lecture de la musique : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void basculePlayPause() {
|
||||
if (enPause) {
|
||||
enPause = false;
|
||||
} else {
|
||||
enPause = true;
|
||||
if (player != null) {
|
||||
player.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void arreterMusique() {
|
||||
stop = true;
|
||||
if (player != null) {
|
||||
player.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ public class PieceCarre implements PieceCourante {
|
||||
motif[2][2] = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[][] getMotif() {
|
||||
return motif;
|
||||
}
|
||||
|
||||
18
app/src/main/java/org/Models/Pieces/PieceI.java
Normal file
18
app/src/main/java/org/Models/Pieces/PieceI.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.Models.Pieces;
|
||||
|
||||
import org.Models.PieceCourante;
|
||||
|
||||
public class PieceI implements PieceCourante {
|
||||
|
||||
public PieceI() {
|
||||
motif[0][1] = true;
|
||||
motif[1][1] = true;
|
||||
motif[2][1] = true;
|
||||
motif[3][1] = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[][] getMotif() {
|
||||
return motif;
|
||||
}
|
||||
}
|
||||
17
app/src/main/java/org/Models/Pieces/PieceJ.java
Normal file
17
app/src/main/java/org/Models/Pieces/PieceJ.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package org.Models.Pieces;
|
||||
|
||||
import org.Models.PieceCourante;
|
||||
|
||||
public class PieceJ implements PieceCourante {
|
||||
|
||||
public PieceJ() {
|
||||
motif[0][2] = true;
|
||||
motif[1][2] = true;
|
||||
motif[2][2] = true;
|
||||
motif[2][1] = true;
|
||||
}
|
||||
|
||||
public boolean[][] getMotif() {
|
||||
return motif;
|
||||
}
|
||||
}
|
||||
17
app/src/main/java/org/Models/Pieces/PieceO.java
Normal file
17
app/src/main/java/org/Models/Pieces/PieceO.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package org.Models.Pieces;
|
||||
|
||||
import org.Models.PieceCourante;
|
||||
|
||||
public class PieceO implements PieceCourante {
|
||||
public PieceO() {
|
||||
motif[0][1] = true;
|
||||
motif[0][2] = true;
|
||||
motif[1][1] = true;
|
||||
motif[1][2] = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[][] getMotif() {
|
||||
return motif;
|
||||
}
|
||||
}
|
||||
18
app/src/main/java/org/Models/Pieces/PieceS.java
Normal file
18
app/src/main/java/org/Models/Pieces/PieceS.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.Models.Pieces;
|
||||
|
||||
import org.Models.PieceCourante;
|
||||
|
||||
public class PieceS implements PieceCourante {
|
||||
|
||||
public PieceS() {
|
||||
motif[0][1] = true;
|
||||
motif[1][1] = true;
|
||||
motif[1][2] = true;
|
||||
motif[2][2] = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[][] getMotif() {
|
||||
return motif;
|
||||
}
|
||||
}
|
||||
18
app/src/main/java/org/Models/Pieces/PieceT.java
Normal file
18
app/src/main/java/org/Models/Pieces/PieceT.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.Models.Pieces;
|
||||
|
||||
import org.Models.PieceCourante;
|
||||
|
||||
public class PieceT implements PieceCourante {
|
||||
|
||||
public PieceT() {
|
||||
motif[0][1] = true;
|
||||
motif[1][1] = true;
|
||||
motif[2][1] = true;
|
||||
motif[1][2] = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[][] getMotif() {
|
||||
return motif;
|
||||
}
|
||||
}
|
||||
18
app/src/main/java/org/Models/Pieces/PieceZ.java
Normal file
18
app/src/main/java/org/Models/Pieces/PieceZ.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.Models.Pieces;
|
||||
|
||||
import org.Models.PieceCourante;
|
||||
|
||||
public class PieceZ implements PieceCourante {
|
||||
|
||||
public PieceZ() {
|
||||
motif[0][2] = true;
|
||||
motif[1][2] = true;
|
||||
motif[1][1] = true;
|
||||
motif[2][1] = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[][] getMotif() {
|
||||
return motif;
|
||||
}
|
||||
}
|
||||
116
app/src/main/java/org/Views/VueBandeauControle.java
Normal file
116
app/src/main/java/org/Views/VueBandeauControle.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package org.Views;
|
||||
|
||||
import org.Models.PieceCourante;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class VueBandeauControle extends JPanel {
|
||||
private JLabel scoreLabel;
|
||||
private JPanel nextPiecePanel;
|
||||
private JButton pauseButton;
|
||||
private JPanel[][] caseNextPiece = new JPanel[4][4];
|
||||
private JLabel nbLigneLabel;
|
||||
private JButton quitterButton;
|
||||
|
||||
public VueBandeauControle() {
|
||||
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||
setBackground(Color.gray);
|
||||
// setPreferredSize();
|
||||
|
||||
// SCORE
|
||||
scoreLabel = new JLabel("SCORE : 0");
|
||||
scoreLabel.setForeground(Color.white);
|
||||
scoreLabel.setFont(new Font("Arial", Font.PLAIN, 16));
|
||||
scoreLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
// NB LIGNE
|
||||
nbLigneLabel = new JLabel("LIGNES : 0");
|
||||
nbLigneLabel.setForeground(Color.white);
|
||||
nbLigneLabel.setFont(new Font("Arial", Font.PLAIN, 16));
|
||||
nbLigneLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
// NEXT PIECE
|
||||
nextPiecePanel = new JPanel();
|
||||
// nextPiecePanel.setPreferredSize(new Dimension(100, 100));
|
||||
// nextPiecePanel.setMaximumSize(new Dimension(100, 100));
|
||||
nextPiecePanel.setBackground(Color.LIGHT_GRAY);
|
||||
nextPiecePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
initierNextPiecePanel();
|
||||
|
||||
// PAUSE BUTTON & QUIT BUTTON
|
||||
pauseButton = new JButton("PAUSE");
|
||||
quitterButton = new JButton("QUITTER");
|
||||
Dimension buttonSize = new Dimension(85, 30);
|
||||
Insets margeBoutton = new Insets(2, 2, 2, 2);
|
||||
// pauseButton.setPreferredSize(buttonSize);
|
||||
pauseButton.setMargin(margeBoutton);
|
||||
// quitterButton.setPreferredSize(buttonSize);
|
||||
quitterButton.setMargin(margeBoutton);
|
||||
JPanel boutonsPanel = new JPanel();
|
||||
boutonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
|
||||
boutonsPanel.setOpaque(false);
|
||||
boutonsPanel.add(pauseButton);
|
||||
boutonsPanel.add(quitterButton);
|
||||
pauseButton.setFocusable(false);
|
||||
quitterButton.setFocusable(false);
|
||||
|
||||
add(Box.createVerticalStrut(20));
|
||||
add(scoreLabel);
|
||||
add(Box.createVerticalStrut(20));
|
||||
add(nbLigneLabel);
|
||||
add(Box.createVerticalStrut(20));
|
||||
add(nextPiecePanel);
|
||||
add(Box.createVerticalStrut(20));
|
||||
add(boutonsPanel);
|
||||
|
||||
// setVisible(true);
|
||||
}
|
||||
|
||||
public void setScore(int score) {
|
||||
scoreLabel.setText("Score: " + score);
|
||||
}
|
||||
|
||||
public JButton getPauseButton() {
|
||||
return pauseButton;
|
||||
}
|
||||
|
||||
public JButton getQuitterButton() {
|
||||
return quitterButton;
|
||||
}
|
||||
|
||||
public JPanel getNextPiecePanel() {
|
||||
return nextPiecePanel;
|
||||
}
|
||||
|
||||
private void initierNextPiecePanel() {
|
||||
nextPiecePanel.removeAll();
|
||||
nextPiecePanel.setLayout(new GridLayout(4, 4));
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
JPanel caseG = new JPanel();
|
||||
caseG.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
caseG.setBackground(Color.WHITE);
|
||||
caseNextPiece[i][j] = caseG;
|
||||
nextPiecePanel.add(caseG);
|
||||
}
|
||||
}
|
||||
nextPiecePanel.revalidate();
|
||||
nextPiecePanel.repaint();
|
||||
}
|
||||
|
||||
public void afficherPieceSuivante(PieceCourante piece) {
|
||||
boolean[][] motif = piece.getMotif();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
if (motif[i][j]) {
|
||||
caseNextPiece[i][j].setBackground(Color.RED);
|
||||
} else {
|
||||
caseNextPiece[i][j].setBackground(Color.WHITE);
|
||||
}
|
||||
}
|
||||
}
|
||||
nextPiecePanel.revalidate();
|
||||
nextPiecePanel.repaint();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,5 @@
|
||||
package org.Views;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import org.Models.Grille;
|
||||
@@ -15,18 +10,14 @@ import org.Models.PieceCourante;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.Toolkit;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class VueGrille extends JFrame implements Observer, Runnable {
|
||||
public class VueGrille extends JPanel implements Observer, Runnable {
|
||||
private JPanel grillePanel;
|
||||
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
private double tailleJPanelX = screenSize.getHeight() / 4;
|
||||
private double tailleJPanelY = screenSize.getHeight() / 2;
|
||||
|
||||
private Grille grille;
|
||||
private Jeu jeu;
|
||||
@@ -34,12 +25,18 @@ public class VueGrille extends JFrame implements Observer, Runnable {
|
||||
|
||||
private boolean afficherFenetreFinPartie = false;
|
||||
|
||||
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
private double tailleJFrameX = screenSize.getHeight() / 2;
|
||||
private double tailleJFrameY = screenSize.getHeight();
|
||||
|
||||
public VueGrille(Grille grille, Jeu jeu) {
|
||||
this.grille = grille;
|
||||
this.jeu = jeu;
|
||||
setLayout(new BorderLayout());
|
||||
grillePanel = new JPanel(new GridLayout(grille.getNbLignes(), grille.getNbColonnes()));
|
||||
setSize((int) tailleJPanelX, (int) tailleJPanelY);
|
||||
setContentPane(grillePanel);
|
||||
setSize((int) tailleJFrameX, (int) tailleJFrameY);
|
||||
System.err.println("taille " + tailleJFrameX + " " + tailleJFrameY);
|
||||
add(this.grillePanel, BorderLayout.CENTER);
|
||||
initialiserVueGrille();
|
||||
|
||||
grille.addObserver(this);
|
||||
@@ -57,10 +54,6 @@ public class VueGrille extends JFrame implements Observer, Runnable {
|
||||
grillePanel.add(caseG);
|
||||
}
|
||||
}
|
||||
setVisible(true);
|
||||
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setTitle("TETRIS");
|
||||
}
|
||||
|
||||
public synchronized void updateGrille() {
|
||||
@@ -104,32 +97,8 @@ public class VueGrille extends JFrame implements Observer, Runnable {
|
||||
repaint();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void afficherFinPartie() {
|
||||
JDialog gameOverDialog = new JDialog(this, "Partie terminée", true);
|
||||
gameOverDialog.setLayout(new BorderLayout());
|
||||
|
||||
gameOverDialog.setUndecorated(true);
|
||||
|
||||
gameOverDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
|
||||
|
||||
JLabel gameOverLabel = new JLabel("GAME OVER", JLabel.CENTER);
|
||||
gameOverLabel.setFont(new Font("Arial", Font.BOLD, 50));
|
||||
gameOverLabel.setForeground(Color.RED);
|
||||
gameOverLabel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
||||
|
||||
gameOverDialog.add(gameOverLabel, BorderLayout.CENTER);
|
||||
|
||||
JButton quitButton = new JButton("Quitter");
|
||||
quitButton.addActionListener(e -> System.exit(0));
|
||||
|
||||
JPanel buttonPanel = new JPanel();
|
||||
buttonPanel.add(quitButton);
|
||||
gameOverDialog.add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
gameOverDialog.pack();
|
||||
gameOverDialog.setLocationRelativeTo(this);
|
||||
gameOverDialog.setVisible(true);
|
||||
System.err.println("FIN PARTIE"); // TODO : gerer affichage ?
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
34
app/src/main/java/org/Views/VueTetris.java
Normal file
34
app/src/main/java/org/Views/VueTetris.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package org.Views;
|
||||
|
||||
import org.Controllers.TetrisBandeauControleur;
|
||||
import org.Models.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class VueTetris extends JFrame {
|
||||
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
public static double tailleJFrameX = screenSize.getHeight() / 2;
|
||||
public static double tailleJFrameY = screenSize.getHeight() / 2;
|
||||
|
||||
public VueTetris(Grille grille, Jeu jeu) {
|
||||
super("Tetris");
|
||||
|
||||
VueGrille vueGrille = new VueGrille(grille, jeu);
|
||||
VueBandeauControle vueControle = new VueBandeauControle();
|
||||
TetrisBandeauControleur controleur = new TetrisBandeauControleur(vueControle);
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
add(vueGrille, BorderLayout.CENTER);
|
||||
add(vueControle, BorderLayout.EAST);
|
||||
|
||||
setSize((int) tailleJFrameX, (int) tailleJFrameY);
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
|
||||
// Utilisation de la pièce L
|
||||
vueControle.afficherPieceSuivante(jeu.getPieceSuivante());
|
||||
}
|
||||
}
|
||||
BIN
app/src/main/resources/TetrisMusic.mp3
Normal file
BIN
app/src/main/resources/TetrisMusic.mp3
Normal file
Binary file not shown.
Reference in New Issue
Block a user