From 9a37b8f8323e32eeb95da959023b4783a890e45d Mon Sep 17 00:00:00 2001 From: lcaohoanq Date: Tue, 3 Sep 2024 18:00:39 +0700 Subject: [PATCH] refactor: refactor Box --- .../fxsnakegame/views/game/IGameDrawer.java | 11 +++ .../fxsnakegame/views/game/IGameLogic.java | 6 ++ .../views/game/apartment/Apartment.java | 15 ++++ .../fxsnakegame/views/game/box/Box.java | 81 +++++++++++-------- .../views/game/campaign/Campaign.java | 15 ++++ .../fxsnakegame/views/game/mill/Mill.java | 15 ++++ .../fxsnakegame/views/game/nomaze/NoMaze.java | 15 ++++ .../fxsnakegame/views/game/rails/Rails.java | 15 ++++ .../fxsnakegame/views/game/tunnel/Tunnel.java | 15 ++++ 9 files changed, 156 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/lcaohoanq/fxsnakegame/views/game/IGameDrawer.java diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/IGameDrawer.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/IGameDrawer.java new file mode 100644 index 0000000..b605cb3 --- /dev/null +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/IGameDrawer.java @@ -0,0 +1,11 @@ +package com.lcaohoanq.fxsnakegame.views.game; + +import java.awt.Graphics; + +public interface IGameDrawer { + + void drawHorizontalWall(Graphics g, int startX, int startY, int length, int step); + + void drawVerticalWall(Graphics g, int startX, int startY, int length, int step); + +} diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/IGameLogic.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/IGameLogic.java index 52d26c4..3237174 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/IGameLogic.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/IGameLogic.java @@ -4,6 +4,12 @@ public interface IGameLogic { void checkCollision(); + void checkSelfCollision(); + + void checkBoundaryCollision(); + + void checkWallCollision(); + void locateApple(); void locateBigApple(); diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/apartment/Apartment.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/apartment/Apartment.java index 13d87ba..b983d4a 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/apartment/Apartment.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/apartment/Apartment.java @@ -130,6 +130,21 @@ public void checkCollision() { } } + @Override + public void checkSelfCollision() { + + } + + @Override + public void checkBoundaryCollision() { + + } + + @Override + public void checkWallCollision() { + + } + @Override public void locateApple() { if (apple_count % 5 == 0 && apple_count != 0) { diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/box/Box.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/box/Box.java index ba47a30..99dc579 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/box/Box.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/box/Box.java @@ -2,11 +2,12 @@ import com.lcaohoanq.fxsnakegame.constants.ResourcePaths; import com.lcaohoanq.fxsnakegame.styles.UISizes; +import com.lcaohoanq.fxsnakegame.views.game.IGameDrawer; import com.lcaohoanq.fxsnakegame.views.game.board.BoardView; import java.awt.Graphics; import java.io.InputStream; -public class Box extends BoardView { +public class Box extends BoardView implements IGameDrawer { protected int wallThickness = 20; public Box() { @@ -14,51 +15,69 @@ public Box() { } @Override - public void paintComponent(Graphics g) { - super.paintComponent(g); - for (int i = 0; i < UISizes.WIDTH_BOARD; i += 20) { + public void drawHorizontalWall(Graphics g, int startX, int startY, int length, int step) { + for (int i = startX; i < length; i += step) { g.drawImage(uiImages.getWall(), i, 0, this); - g.drawImage(uiImages.getWall(), i, UISizes.HEIGHT_BOARD - 70, this); + g.drawImage(uiImages.getWall(), i, startY, this); } - for (int i = 0; i < UISizes.HEIGHT_BOARD - 70; i += 20) { + } + + @Override + public void drawVerticalWall(Graphics g, int startX, int startY, int length, int step) { + for (int i = startX; i < length; i += step) { g.drawImage(uiImages.getWall(), 0, i, this); - g.drawImage(uiImages.getWall(), UISizes.WIDTH_BOARD - 20, i, this); + g.drawImage(uiImages.getWall(), startY, i, this); } } + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + drawHorizontalWall(g, 0, UISizes.HEIGHT_BOARD - 70, UISizes.WIDTH_BOARD, 20); + drawVerticalWall(g, 0, UISizes.HEIGHT_BOARD - 70, UISizes.HEIGHT_BOARD - 70, 20); + } + @Override public void checkCollision() { - for (int z = dots; z > 0; z--) { + checkSelfCollision(); + checkBoundaryCollision(); + checkWallCollision(); + if (!inGame) { + if (isOnSound()) { + InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_GAME_OVER); + audioUtils.playAudio(inputStream); + } + timer.stop(); + } + } + @Override + public void checkSelfCollision() { + for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; break; } } + } - if (y[0] >= UISizes.HEIGHT_BOARD - 70) { - inGame = false; - } - - if (y[0] < 20) { - inGame = false; - } - - if (x[0] >= UISizes.WIDTH_BOARD) { - inGame = false; - } + @Override + public void checkBoundaryCollision() { + //nothing + } - if (x[0] < 20) { + @Override + public void checkWallCollision(){ + //x-axis: north and bottom: wall with 20px thickness (but bottom have 50px score panel so + // plus 50= 70px) + //y-axis: left and right: wall with 20px thickness + if (y[0] >= UISizes.HEIGHT_BOARD - 70 || y[0] < 20 || x[0] >= UISizes.WIDTH_BOARD || x[0] < 20) { inGame = false; } + } - if (!inGame) { - if (isOnSound()) { - InputStream inputStream = getClass().getResourceAsStream(ResourcePaths.URL_GAME_OVER); - audioUtils.playAudio(inputStream); - } - timer.stop(); - } + private int getRandomPosition() { + return ((int) (Math.random() * (29 - 2 * wallThickness)) + wallThickness) * DOT_SIZE; } @Override @@ -67,12 +86,9 @@ public void locateApple() { locateBigApple(); } else { bigApple_x = -100; - int r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); - apple_x = ((r + wallThickness) * DOT_SIZE); - + apple_x = getRandomPosition(); bigApple_y = -100; - r = (int) (Math.random() * (RAND_POS - 2 * wallThickness)); - apple_y = ((r + wallThickness) * DOT_SIZE); + apple_y = getRandomPosition(); } } @@ -92,4 +108,5 @@ public void locateBigApple() { setBigAppleTime(); renderProgressBar(); } + } diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/campaign/Campaign.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/campaign/Campaign.java index adba1e8..e5c9a48 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/campaign/Campaign.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/campaign/Campaign.java @@ -8,6 +8,21 @@ public void checkCollision() { } + @Override + public void checkSelfCollision() { + + } + + @Override + public void checkBoundaryCollision() { + + } + + @Override + public void checkWallCollision() { + + } + @Override public void locateApple() { diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/mill/Mill.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/mill/Mill.java index 6186b3a..46aa895 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/mill/Mill.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/mill/Mill.java @@ -100,6 +100,21 @@ public void checkCollision() { } } + @Override + public void checkSelfCollision() { + + } + + @Override + public void checkBoundaryCollision() { + + } + + @Override + public void checkWallCollision() { + + } + @Override public void locateApple() { if (apple_count % 5 == 0 && apple_count != 0) { diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/nomaze/NoMaze.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/nomaze/NoMaze.java index c99f084..937c55a 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/nomaze/NoMaze.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/nomaze/NoMaze.java @@ -47,6 +47,21 @@ public void checkCollision() { } } + @Override + public void checkSelfCollision() { + + } + + @Override + public void checkBoundaryCollision() { + + } + + @Override + public void checkWallCollision() { + + } + @Override public void locateApple() { if (apple_count % 5 == 0 && apple_count != 0) { diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/rails/Rails.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/rails/Rails.java index 0617097..bd9bc53 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/rails/Rails.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/rails/Rails.java @@ -108,6 +108,21 @@ public void checkCollision() { } } + @Override + public void checkSelfCollision() { + + } + + @Override + public void checkBoundaryCollision() { + + } + + @Override + public void checkWallCollision() { + + } + @Override public void locateApple() { if (apple_count % 5 == 0 && apple_count != 0) { diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/tunnel/Tunnel.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/tunnel/Tunnel.java index 52addc8..a1fea0f 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/tunnel/Tunnel.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/tunnel/Tunnel.java @@ -142,6 +142,21 @@ public void checkCollision() { } } + @Override + public void checkSelfCollision() { + + } + + @Override + public void checkBoundaryCollision() { + + } + + @Override + public void checkWallCollision() { + + } + @Override public void locateApple() { if (apple_count % 5 == 0 && apple_count != 0) {