diff --git a/DEV3.2/TP05/04_Serpent/Fenetre.class b/DEV3.2/TP05/04_Serpent/Fenetre.class new file mode 100644 index 0000000..bc7ffcd Binary files /dev/null and b/DEV3.2/TP05/04_Serpent/Fenetre.class differ diff --git a/DEV3.2/TP05/04_Serpent/Fenetre.java b/DEV3.2/TP05/04_Serpent/Fenetre.java new file mode 100644 index 0000000..678a308 --- /dev/null +++ b/DEV3.2/TP05/04_Serpent/Fenetre.java @@ -0,0 +1,20 @@ +import java.awt.*; +import javax.swing.*; +import java.util.ArrayDeque; + +public class Fenetre extends JFrame { + + public Fenetre() { + this.setSize(500, 500); + this.setLocation(100, 100); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLayout(new GridLayout(1, 1)); + ArrayDeque coordSnake = new ArrayDeque<>(); + for (int i = 0; i != 5; i++) { + coordSnake.addLast(new Point(i+5, 5)); + } + this.add(new JGrilleDeJeu(coordSnake)); + + } + +} \ No newline at end of file diff --git a/DEV3.2/TP05/04_Serpent/JGrilleDeJeu.class b/DEV3.2/TP05/04_Serpent/JGrilleDeJeu.class new file mode 100644 index 0000000..27f642d Binary files /dev/null and b/DEV3.2/TP05/04_Serpent/JGrilleDeJeu.class differ diff --git a/DEV3.2/TP05/04_Serpent/JGrilleDeJeu.java b/DEV3.2/TP05/04_Serpent/JGrilleDeJeu.java new file mode 100644 index 0000000..47a24b6 --- /dev/null +++ b/DEV3.2/TP05/04_Serpent/JGrilleDeJeu.java @@ -0,0 +1,77 @@ +import java.awt.*; +import javax.swing.*; +import java.util.Random; +import java.util.ArrayDeque; +import java.util.Timer; + +public class JGrilleDeJeu extends JComponent { + + private static final int TAILLE_GRILLE = 25; + private char[][] grille; + private ArrayDeque coordSnake; + + public JGrilleDeJeu(ArrayDeque coordSnake) { + this.grille = new char[TAILLE_GRILLE][TAILLE_GRILLE]; + this.coordSnake = coordSnake; + + for (int i = 0; i != TAILLE_GRILLE; i++) { + for (int j = 0; j != TAILLE_GRILLE; j++) { + this.grille[i][j] = 'g'; + } + } + + for (Point point : coordSnake) { + this.grille[point.x][point.y] = 'o'; + } + + Random r = new Random(); + int coordXPomme = Math.abs(r.nextInt() % TAILLE_GRILLE); + int coordYPomme = Math.abs(r.nextInt() % TAILLE_GRILLE); + + this.grille[coordXPomme][coordYPomme] = 'r'; + + Timer timer = new Timer(); + + while (true) { + timer.wait(1000L); + this.avancerSnake(); + } + } + + @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( + this.getWidth()/TAILLE_GRILLE*i, + this.getHeight()/TAILLE_GRILLE*j, + this.getWidth()/TAILLE_GRILLE, + this.getHeight()/TAILLE_GRILLE + ); + } + } + } + + public void avancerSnake() { + this.coordSnake.removeFirst(); + this.coordSnake.addLast(new Point(this.coordSnake.getLast().x+1, this.coordSnake.getLast().y)); + } +} \ No newline at end of file diff --git a/DEV3.2/TP05/04_Serpent/Main.class b/DEV3.2/TP05/04_Serpent/Main.class new file mode 100644 index 0000000..0476717 Binary files /dev/null and b/DEV3.2/TP05/04_Serpent/Main.class differ diff --git a/DEV3.2/TP05/04_Serpent/Main.java b/DEV3.2/TP05/04_Serpent/Main.java new file mode 100644 index 0000000..2768c3d --- /dev/null +++ b/DEV3.2/TP05/04_Serpent/Main.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + Fenetre fenetre = new Fenetre(); + fenetre.setVisible(true); + } +} \ No newline at end of file