From b7f44474fca241ba0a7a0d4f122dd558b8952bb7 Mon Sep 17 00:00:00 2001 From: horville Date: Fri, 18 Nov 2022 10:05:14 +0100 Subject: [PATCH] =?UTF-8?q?Snake=20Termin=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DEV 3.2/TP05/Serpent/Main.java | 2 -- DEV 3.2/TP05/Serpent/Snake.java | 6 ++++- DEV 3.2/TP05/Serpent/SnakePanel.java | 34 ++++++++++++++++++++++------ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/DEV 3.2/TP05/Serpent/Main.java b/DEV 3.2/TP05/Serpent/Main.java index ecdae21..0d31792 100644 --- a/DEV 3.2/TP05/Serpent/Main.java +++ b/DEV 3.2/TP05/Serpent/Main.java @@ -1,6 +1,4 @@ import javax.swing.*; -import java.awt.*; - public class Main { diff --git a/DEV 3.2/TP05/Serpent/Snake.java b/DEV 3.2/TP05/Serpent/Snake.java index 6e3a4d9..8f45a62 100644 --- a/DEV 3.2/TP05/Serpent/Snake.java +++ b/DEV 3.2/TP05/Serpent/Snake.java @@ -55,15 +55,19 @@ public class Snake { break; } - if (headY >= parent.sizeY || headX >= parent.sizeX) { + if (headX < 0 || headY < 0 || headY >= parent.sizeY || headX >= parent.sizeX) { parent.gameOver(); return; } + Cell newCell = grid[headY][headX]; if (newCell == null || newCell.getType() == CellType.SNAKE) { parent.gameOver(); return; + } else if (newCell.getType() == CellType.FRUIT) { + length++; + parent.makeFruit(); } snakeCells.addFirst(newCell); diff --git a/DEV 3.2/TP05/Serpent/SnakePanel.java b/DEV 3.2/TP05/Serpent/SnakePanel.java index d55176b..c5b389e 100644 --- a/DEV 3.2/TP05/Serpent/SnakePanel.java +++ b/DEV 3.2/TP05/Serpent/SnakePanel.java @@ -1,12 +1,15 @@ import javax.swing.JPanel; import java.awt.*; +import java.util.Random; import java.util.Timer; -import java.util.TimerTask; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class SnakePanel extends JPanel { private Cell[][] grid; - private Timer moveTimer; + private ScheduledExecutorService gameRunner; private Snake snake; public final int sizeX = 25; @@ -25,12 +28,12 @@ public class SnakePanel extends JPanel { add(grid[x][y]); } } - int i = 0; + this.snake = new Snake(this); addKeyListener(new SnakeListener(this)); - moveTimer = new Timer(); - moveTimer.scheduleAtFixedRate(new TimerTask() { + gameRunner = Executors.newSingleThreadScheduledExecutor(); + gameRunner.scheduleAtFixedRate(new Runnable() { @Override public void run() { @@ -38,11 +41,28 @@ public class SnakePanel extends JPanel { snake.performMove(); } - }, 200, 200); + }, 0, 300, TimeUnit.MILLISECONDS); + + makeFruit(); + } + + public void makeFruit() { + Random r = new Random(); + while (true) { + int x, y; + x = r.nextInt(sizeX); + y = r.nextInt(sizeY); + + if (grid[x][y].getType() == CellType.EMPTY) { + grid[x][y].setType(CellType.FRUIT); + repaint(); + return; + } + } } public void gameOver() { - moveTimer.cancel(); + gameRunner.shutdown(); Timer gameOverTimer = new Timer(); gameOverTimer.scheduleAtFixedRate(new GameOverTask(this, gameOverTimer), 0, 60);