OOP-Projects-Java

Log | Files | Refs | README

commit a5b70527fa17be6df9730ee909190eb25e34cdc1
parent 5adb26fea238560907d7b70367850b73b79c7507
Author: William Lindholm <William_Lindholm@outlook.com>
Date:   Tue, 21 Mar 2023 21:18:30 +0100

Added instafall function and guidance for falling blocks

Diffstat:
MOOP2023_a22willi_a21liltr_assignment4/src/blocks/J_Block.java | 7+------
MOOP2023_a22willi_a21liltr_assignment4/src/game/BlockFactory.java | 2+-
MOOP2023_a22willi_a21liltr_assignment4/src/game/Board.java | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
MOOP2023_a22willi_a21liltr_assignment4/src/game/GUI.java | 10++++++++--
MOOP2023_a22willi_a21liltr_assignment4/src/game/Game.java | 11+++++------
MOOP2023_a22willi_a21liltr_assignment4/src/game/Main.java | 5++++-
6 files changed, 74 insertions(+), 30 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/J_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/J_Block.java @@ -5,7 +5,7 @@ import java.awt.Color; public class J_Block extends Poly { // holds the information that dictates what shape the object becomes on the // board - public int[][] Shape = { { 0, 1 }, { 0, 1 }, { 1, 1 }, }; + public int[][] Shape = { { 0, 1 }, { 0, 1 }, { 1, 1 } }; public J_Block(int x, int y) { super(x, y); @@ -22,9 +22,4 @@ public class J_Block extends Poly { public void rotate() { Shape = super.rotateRight(Shape); } - - public int[][] rotateRight(int[][] shape) { - - return shape; - } } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/BlockFactory.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/BlockFactory.java @@ -41,6 +41,6 @@ public class BlockFactory { } public Poly generateRandomPoly() { - return generatePoly(random.nextInt(0, 6)); + return generatePoly(random.nextInt(0, 7)); } } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Board.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Board.java @@ -16,7 +16,7 @@ import blocks.Poly; public class Board extends JPanel { // size of window - private final Dimension size = new Dimension(290, 790); + private final Dimension size = new Dimension(495, 790); private final Color backgroundColor = Color.DARK_GRAY; @@ -52,25 +52,27 @@ public class Board extends JPanel { for (int col = 0; col < grid.length; col++) { if (!(grid[col][row].isOccupied())) { break; - // System.out.println("Found occupied tile at: " + col + ":" + row); } if (col == grid.length - 1) { score += 10; clearRow(row); - tileFall(row); + fall(row); } } } } + public int getScore() { + return score; + } + private void clearRow(int rowNumber) { - System.out.println("Clearing row: " + rowNumber); for (int col = 0; col < grid.length; col++) { grid[col][rowNumber].setOccupied(false); grid[col][rowNumber].setColor(backgroundColor); } } - + public void givePoints(int amount) { score += amount; } @@ -86,14 +88,14 @@ public class Board extends JPanel { } public boolean move(Poly poly, int x, int y) { - if (clear(poly, x, y)) { + if (isLegal(poly, x, y)) { poly.move(x, y); return true; } return false; } - private boolean clear(Poly poly, int x, int y) { + private boolean isLegal(Poly poly, int x, int y) { int[][] shape = poly.getShape(); int[] position = poly.getPos(); @@ -163,15 +165,23 @@ public class Board extends JPanel { } } - public void tileFall(int row) { + public void fall(int row) { for (int j = row; j > 0; j--) { - System.out.println("moving row " + j + " to " + (j - 1)); for (int i = grid.length - 1; i >= 0; i--) { grid[i][j] = grid[i][j - 1]; } } } + public void instaFall(Poly poly) { + polyFalling = true; + boolean result = true; + do { + result = move(poly, 0, 1); + } while (result); + freeze(poly); + } + public boolean hasLost() { for (int i = 0; i < grid.length; i++) { if (grid[i][0].isOccupied()) { @@ -200,11 +210,19 @@ public class Board extends JPanel { clearBoard(g); // show score - g.setColor(Color.white); - String scoreText = "Score: " + String.valueOf(score); - g.setFont(new Font("Helvetica", Font.PLAIN, tileSize)); - g.drawString(scoreText, tileSize, g.getFontMetrics().getHeight()); + drawScore(g); + + // draw polys + drawPoly(g); + + // draw guidelines + drawPolyGuide(g); + + // draw tiles + drawTiles(g); + } + private void drawPoly(Graphics g) { // draw polys if (polys.size() > 0) { for (Poly poly : polys) { @@ -216,13 +234,15 @@ public class Board extends JPanel { if (shape[i][j] == 1) { g.fillRect(poly.getPos()[0] * tileSize + (j * tileSize), poly.getPos()[1] * tileSize + (i * tileSize), tileSize, tileSize); + } } } } } + } - // draw tiles + private void drawTiles(Graphics g) { for (int x = 0; x < grid[0].length; x++) { for (int y = 0; y < grid.length; y++) { if (grid[y][x].isOccupied()) { @@ -232,4 +252,25 @@ public class Board extends JPanel { } } } + + private void drawPolyGuide(Graphics g) { + g.setColor(Color.white); + if (getFallingPoly() != null) { + Poly poly = getFallingPoly(); + int[][] shape = poly.getShape(); + g.drawLine(poly.getPos()[0] * tileSize + (shape[0].length * tileSize), + poly.getPos()[1] * tileSize + shape.length * tileSize, + poly.getPos()[0] * tileSize + (shape[0].length * tileSize), size.height); + g.drawLine(poly.getPos()[0] * tileSize, poly.getPos()[1] * tileSize + shape.length * tileSize, + poly.getPos()[0] * tileSize, size.height); + } + } + + private void drawScore(Graphics g) { + // show score + g.setColor(Color.white); + String scoreText = "Score: " + String.valueOf(score); + g.setFont(new Font("Helvetica", Font.PLAIN, tileSize)); + g.drawString(scoreText, tileSize, g.getFontMetrics().getHeight()); + } } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/GUI.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/GUI.java @@ -33,12 +33,14 @@ public class GUI extends JFrame implements KeyListener { } @Override - + public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { // spaceBar case 32: - movePoly(0, 1); + if (board.getFallingPoly() != null) { + board.instaFall(board.getFallingPoly()); + } break; // left case 37: @@ -53,6 +55,10 @@ public class GUI extends JFrame implements KeyListener { if (board.getFallingPoly() != null) { board.getFallingPoly().rotate(); } + // slow fall + case 40: + movePoly(0, 1); + break; } board.repaint(); diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Game.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Game.java @@ -3,6 +3,8 @@ package game; import java.util.Timer; import java.util.TimerTask; +import javax.swing.JOptionPane; + import blocks.Poly; public class Game { @@ -38,13 +40,10 @@ public class Game { board.repaint(); } else { - resetGame(); + System.out.println("You lost"); + JOptionPane.showMessageDialog(null, "You scored: " + board.getScore() + " points"); + System.exit(0); } } }; - - private void resetGame() { - System.out.println("Restarting game"); - this.board = new Board(); - } } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Main.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Main.java @@ -1,11 +1,14 @@ package game; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + public class Main { public static void main(String[] args) { Board board = new Board(); GUI gui = new GUI(board); Game game = new Game(board); + } - }