2025-11-03 12:30:52 +01:00
|
|
|
import java.awt.*;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.ArrayDeque;
|
|
|
|
|
import java.util.Timer;
|
2025-11-06 11:57:30 +01:00
|
|
|
import java.util.TimerTask;
|
2025-11-03 12:30:52 +01:00
|
|
|
|
|
|
|
|
public class JGrilleDeJeu extends JComponent {
|
|
|
|
|
|
|
|
|
|
private static final int TAILLE_GRILLE = 25;
|
|
|
|
|
private char[][] grille;
|
|
|
|
|
private ArrayDeque<Point> coordSnake;
|
2025-11-06 11:57:30 +01:00
|
|
|
private Point posPomme;
|
|
|
|
|
private ControleurClavier controleur;
|
|
|
|
|
private Fenetre fenetre;
|
|
|
|
|
private String directionActuelle;
|
|
|
|
|
private Timer timer;
|
|
|
|
|
private int intervalleTimer;
|
2025-11-03 12:30:52 +01:00
|
|
|
|
2025-11-06 11:57:30 +01:00
|
|
|
public JGrilleDeJeu(Fenetre fenetre, ArrayDeque<Point> coordSnake) {
|
|
|
|
|
this.fenetre = fenetre;
|
2025-11-03 12:30:52 +01:00
|
|
|
this.grille = new char[TAILLE_GRILLE][TAILLE_GRILLE];
|
|
|
|
|
this.coordSnake = coordSnake;
|
2025-11-06 11:57:30 +01:00
|
|
|
this.controleur = new ControleurClavier();
|
|
|
|
|
this.fenetre.addKeyListener(controleur);
|
|
|
|
|
this.directionActuelle = "Right";
|
2025-11-06 15:20:51 +01:00
|
|
|
this.intervalleTimer = 150;
|
2025-11-03 12:30:52 +01:00
|
|
|
|
|
|
|
|
Random r = new Random();
|
|
|
|
|
int coordXPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
|
|
|
|
int coordYPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
|
|
|
|
|
2025-11-06 11:57:30 +01:00
|
|
|
this.posPomme = new Point(coordXPomme, coordYPomme);
|
2025-11-03 12:30:52 +01:00
|
|
|
|
2025-11-06 11:57:30 +01:00
|
|
|
this.grille[coordXPomme][coordYPomme] = 'r';
|
2025-11-03 12:30:52 +01:00
|
|
|
|
2025-11-06 11:57:30 +01:00
|
|
|
this.timer = new Timer();
|
|
|
|
|
timer.scheduleAtFixedRate(new TacheTimer(this), 0, intervalleTimer);
|
2025-11-03 12:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void paintComponent(Graphics pinceau) {
|
|
|
|
|
Graphics secondPinceau = pinceau.create();
|
|
|
|
|
|
|
|
|
|
if (this.isOpaque()) {
|
|
|
|
|
secondPinceau.setColor(this.getBackground());
|
|
|
|
|
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i != TAILLE_GRILLE; i++) {
|
|
|
|
|
for (int j = 0; j != TAILLE_GRILLE; j++) {
|
|
|
|
|
switch (grille[i][j]) {
|
|
|
|
|
case 'g':
|
|
|
|
|
secondPinceau.setColor(Color.GREEN);
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
secondPinceau.setColor(Color.RED);
|
|
|
|
|
break;
|
|
|
|
|
case 'o':
|
|
|
|
|
secondPinceau.setColor(Color.ORANGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
secondPinceau.fillRect(
|
2025-11-06 11:57:30 +01:00
|
|
|
this.getWidth()/TAILLE_GRILLE*i+1,
|
|
|
|
|
this.getHeight()/TAILLE_GRILLE*j+1,
|
|
|
|
|
this.getWidth()/TAILLE_GRILLE-2,
|
|
|
|
|
this.getHeight()/TAILLE_GRILLE-2
|
2025-11-03 12:30:52 +01:00
|
|
|
);
|
2025-11-06 11:57:30 +01:00
|
|
|
|
2025-11-03 12:30:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void avancerSnake() {
|
|
|
|
|
this.coordSnake.removeFirst();
|
2025-11-06 11:57:30 +01:00
|
|
|
if (!this.controleur.getTouches().isEmpty()) {
|
|
|
|
|
String toucheAModifier = this.controleur.getTouches().removeFirst();
|
|
|
|
|
switch (this.directionActuelle) {
|
|
|
|
|
case "Right":
|
|
|
|
|
if (!toucheAModifier.equals("Left")) {
|
|
|
|
|
this.directionActuelle = toucheAModifier;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "Left":
|
|
|
|
|
if (!toucheAModifier.equals("Right")) {
|
|
|
|
|
this.directionActuelle = toucheAModifier;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "Up":
|
|
|
|
|
if (!toucheAModifier.equals("Down")) {
|
|
|
|
|
this.directionActuelle = toucheAModifier;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "Down":
|
|
|
|
|
if (!toucheAModifier.equals("Up")) {
|
|
|
|
|
this.directionActuelle = toucheAModifier;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
switch (this.directionActuelle) {
|
|
|
|
|
case "Right":
|
|
|
|
|
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x+1, this.coordSnake.getLast().y));
|
|
|
|
|
break;
|
|
|
|
|
case "Left":
|
|
|
|
|
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x-1, this.coordSnake.getLast().y));
|
|
|
|
|
break;
|
|
|
|
|
case "Up":
|
|
|
|
|
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x, this.coordSnake.getLast().y-1));
|
|
|
|
|
break;
|
|
|
|
|
case "Down":
|
|
|
|
|
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x, this.coordSnake.getLast().y+1));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i != TAILLE_GRILLE; i++) {
|
|
|
|
|
for (int j = 0; j != TAILLE_GRILLE; j++) {
|
|
|
|
|
this.grille[i][j] = 'g';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.coordSnake.getLast().equals(this.posPomme)) {
|
|
|
|
|
Random r = new Random();
|
|
|
|
|
int coordXPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
|
|
|
|
int coordYPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
|
|
|
|
|
|
|
|
|
this.posPomme = new Point(coordXPomme, coordYPomme);
|
|
|
|
|
|
|
|
|
|
Point coordQueue = this.coordSnake.removeFirst();
|
|
|
|
|
Point coordAvantQueue = this.coordSnake.getFirst();
|
|
|
|
|
Point aAjouter;
|
|
|
|
|
|
|
|
|
|
if (coordAvantQueue.equals(new Point(coordQueue.x+1, coordQueue.y))) {
|
|
|
|
|
aAjouter = new Point(coordQueue.x-1, coordQueue.y);
|
|
|
|
|
}
|
|
|
|
|
else if (coordAvantQueue.equals(new Point(coordQueue.x-1, coordQueue.y))) {
|
|
|
|
|
aAjouter = new Point(coordQueue.x+1, coordQueue.y);
|
|
|
|
|
}
|
|
|
|
|
else if (coordAvantQueue.equals(new Point(coordQueue.x, coordQueue.y+1))) {
|
|
|
|
|
aAjouter = new Point(coordQueue.x, coordQueue.y-1);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
aAjouter = new Point(coordQueue.x, coordQueue.y+1);
|
|
|
|
|
}
|
|
|
|
|
this.coordSnake.addFirst(aAjouter);
|
|
|
|
|
this.coordSnake.addFirst(coordQueue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
int compteur = this.coordSnake.size();
|
|
|
|
|
for (Point point : this.coordSnake) {
|
|
|
|
|
if ((point.equals(this.coordSnake.getLast()) && compteur > 1)) {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
this.grille[point.x][point.y] = 'o';
|
|
|
|
|
compteur--;
|
|
|
|
|
}
|
|
|
|
|
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.grille[posPomme.x][posPomme.y] = 'r';
|
|
|
|
|
|
|
|
|
|
this.repaint();
|
2025-11-03 12:30:52 +01:00
|
|
|
}
|
|
|
|
|
}
|