import java.awt.*; import javax.swing.*; import java.util.Random; import java.util.ArrayDeque; import java.util.Timer; import java.util.TimerTask; public class JGrilleDeJeu extends JComponent { private static final int TAILLE_GRILLE = 25; private char[][] grille; private ArrayDeque coordSnake; private Point posPomme; private ControleurClavier controleur; private Fenetre fenetre; private String directionActuelle; private Timer timer; private int intervalleTimer; public JGrilleDeJeu(Fenetre fenetre, ArrayDeque coordSnake) { this.fenetre = fenetre; this.grille = new char[TAILLE_GRILLE][TAILLE_GRILLE]; this.coordSnake = coordSnake; this.controleur = new ControleurClavier(); this.fenetre.addKeyListener(controleur); this.directionActuelle = "Right"; 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(); timer.scheduleAtFixedRate(new TacheTimer(this), 0, intervalleTimer); } @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()); } 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]) { case 'g': secondPinceau.setColor(Color.GREEN); break; case 'r': secondPinceau.setColor(Color.RED); break; case 'o': secondPinceau.setColor(Color.ORANGE); } secondPinceau.fillRect( tailleCase*i+1, tailleCase*j+1, tailleCase-2, tailleCase-2 ); } } } public void avancerSnake() { // ======== 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) { 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; } } // Efface la grille for (int i = 0; i != TAILLE_GRILLE; i++) { for (int j = 0; j != TAILLE_GRILLE; j++) { this.grille[i][j] = 'g'; } } 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(); } }