Skip to content

Commit

Permalink
refactor: refactor Box
Browse files Browse the repository at this point in the history
  • Loading branch information
lcaohoanq committed Sep 3, 2024
1 parent 8cdd1ee commit 9a37b8f
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ public interface IGameLogic {

void checkCollision();

void checkSelfCollision();

void checkBoundaryCollision();

void checkWallCollision();

void locateApple();

void locateBigApple();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
81 changes: 49 additions & 32 deletions src/main/java/com/lcaohoanq/fxsnakegame/views/game/box/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,82 @@

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() {
super();
}

@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
Expand All @@ -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();
}
}

Expand All @@ -92,4 +108,5 @@ public void locateBigApple() {
setBigAppleTime();
renderProgressBar();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ public void checkCollision() {

}

@Override
public void checkSelfCollision() {

}

@Override
public void checkBoundaryCollision() {

}

@Override
public void checkWallCollision() {

}

@Override
public void locateApple() {

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/lcaohoanq/fxsnakegame/views/game/mill/Mill.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9a37b8f

Please sign in to comment.