snake fini
This commit is contained in:
Binary file not shown.
@@ -5,17 +5,16 @@ import java.util.ArrayDeque;
|
||||
public class Fenetre extends JFrame {
|
||||
|
||||
public Fenetre() {
|
||||
this.setSize(500, 500);
|
||||
this.setSize(460, 483);
|
||||
this.setLocation(100, 100);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setLayout(new GridLayout(1, 1));
|
||||
ArrayDeque<Point> coordSnake = new ArrayDeque<>();
|
||||
for (int i = 0; i != 5; i++) {
|
||||
coordSnake.addLast(new Point(i+5, 5));
|
||||
}
|
||||
JGrilleDeJeu grille = new JGrilleDeJeu(this, coordSnake);
|
||||
grille.setSize(new Dimension(500, 500));
|
||||
this.add(grille);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -24,14 +24,13 @@ public class JGrilleDeJeu extends JComponent {
|
||||
this.controleur = new ControleurClavier();
|
||||
this.fenetre.addKeyListener(controleur);
|
||||
this.directionActuelle = "Right";
|
||||
this.intervalleTimer = 150;
|
||||
this.intervalleTimer = 100;
|
||||
|
||||
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);
|
||||
|
||||
this.grille[coordXPomme][coordYPomme] = 'r';
|
||||
|
||||
this.timer = new Timer();
|
||||
@@ -47,6 +46,8 @@ public class JGrilleDeJeu extends JComponent {
|
||||
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||||
}
|
||||
|
||||
int tailleCase = this.getWidth()/TAILLE_GRILLE;
|
||||
|
||||
for (int i = 0; i != TAILLE_GRILLE; i++) {
|
||||
for (int j = 0; j != TAILLE_GRILLE; j++) {
|
||||
switch (grille[i][j]) {
|
||||
@@ -60,11 +61,12 @@ public class JGrilleDeJeu extends JComponent {
|
||||
secondPinceau.setColor(Color.ORANGE);
|
||||
}
|
||||
|
||||
|
||||
secondPinceau.fillRect(
|
||||
this.getWidth()/TAILLE_GRILLE*i+1,
|
||||
this.getHeight()/TAILLE_GRILLE*j+1,
|
||||
this.getWidth()/TAILLE_GRILLE-2,
|
||||
this.getHeight()/TAILLE_GRILLE-2
|
||||
tailleCase*i+1,
|
||||
tailleCase*j+1,
|
||||
tailleCase-2,
|
||||
tailleCase-2
|
||||
);
|
||||
|
||||
}
|
||||
@@ -72,7 +74,29 @@ public class JGrilleDeJeu extends JComponent {
|
||||
}
|
||||
|
||||
public void avancerSnake() {
|
||||
this.coordSnake.removeFirst();
|
||||
|
||||
// ======== TON CODE REMPLACE ICI LA LOGIQUE DU DÉPLACEMENT ==========
|
||||
boolean mangePomme = this.coordSnake.getLast().equals(this.posPomme);
|
||||
if (!mangePomme) {
|
||||
this.coordSnake.removeFirst(); // avance normale
|
||||
}
|
||||
|
||||
Point tete = this.coordSnake.getLast();
|
||||
switch (directionActuelle) {
|
||||
case "Right": tete = new Point(tete.x + 1, tete.y); break;
|
||||
case "Left": tete = new Point(tete.x - 1, tete.y); break;
|
||||
case "Up": tete = new Point(tete.x, tete.y - 1); break;
|
||||
case "Down": tete = new Point(tete.x, tete.y + 1); break;
|
||||
}
|
||||
this.coordSnake.addLast(tete);
|
||||
|
||||
if (mangePomme) {
|
||||
Random r = new Random();
|
||||
posPomme = new Point(r.nextInt(TAILLE_GRILLE), r.nextInt(TAILLE_GRILLE));
|
||||
}
|
||||
// ==================================================================
|
||||
|
||||
// Gestion de la direction selon les touches pressées
|
||||
if (!this.controleur.getTouches().isEmpty()) {
|
||||
String toucheAModifier = this.controleur.getTouches().removeFirst();
|
||||
switch (this.directionActuelle) {
|
||||
@@ -97,56 +121,15 @@ public class JGrilleDeJeu extends JComponent {
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
// Efface la grille
|
||||
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) {
|
||||
@@ -161,7 +144,6 @@ public class JGrilleDeJeu extends JComponent {
|
||||
}
|
||||
|
||||
this.grille[posPomme.x][posPomme.y] = 'r';
|
||||
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user