commit 0472daaf6cc67b214e3020c10ff9e1c238007e40 parent 07a4a634521dd39c93d61aef14f808586d89f667 Author: William Lindholm <William_Lindholm@outlook.com> Date: Mon, 20 Mar 2023 13:28:12 +0100 Created functioning grid, polys can now spawn and fall Diffstat:
14 files changed, 289 insertions(+), 42 deletions(-)
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Cell.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Cell.java @@ -1,22 +0,0 @@ -package blocks; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; - - -public class Cell { - Dimension size; - Graphics g; - boolean occupied = true; - - public Cell(Graphics g, int x, int y, Dimension size) { - this.g = g; - this.size = size; - g.drawRect(x, y, size.width, size.height); - } - - public void setOccupied(boolean occupied) { - this.occupied = occupied; - } -} diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/I_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/I_Block.java @@ -1,5 +1,32 @@ package blocks; -public class I_Block implements Tileable { +import java.awt.Color; +public class I_Block extends Poly { + + //holds the information that dictates what shape the object becomes on the board + public int[][] Shape = { + {1, 0}, + {1, 0}, + {1, 0}, + {1, 1}, + }; + + private Color color; + + public I_Block(int x, int y) { + super(x, y); + color = Color.green; + } + + @Override + public int[][] getShape() { + // TODO Auto-generated method stub + return Shape; + } + + @Override + public Color getColor() { + return color; + } } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/J_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/J_Block.java @@ -1,5 +1,5 @@ package blocks; -public class J_Block implements Tileable { - +public class J_Block { + } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/L_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/L_Block.java @@ -1,5 +1,5 @@ package blocks; -public class L_Block implements Tileable { +public class L_Block { } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/O_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/O_Block.java @@ -1,5 +1,5 @@ package blocks; -public class O_Block implements Tileable { +public class O_Block { } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Poly.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Poly.java @@ -0,0 +1,56 @@ +package blocks; + +import java.awt.Color; + +public class Poly { + private boolean falling; + + private int posX, posY; + + + public Poly(int x, int y) { + this.posX = x; + this.posY = y; + } + + public void rotateRight() { + + } + + public void rotateLeft() { + + } + + public boolean isFalling() { + return this.falling; + } + + public void setFailling(boolean falling) { + this.falling = falling; + } + + public void move(int x, int y) { + this.posX += x; + this.posY += y; + } + + + public boolean getFalling() { + return falling; + } + + public int[] getPos() { + int[] pos = {posX, posY}; + return pos; + } + + public int[][] getShape() { + return null; + } + + + public Color getColor() { + //if no other value is set, red will be the default color. + return Color.red; + } +} diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/S_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/S_Block.java @@ -1,5 +1,5 @@ package blocks; -public class S_Block implements Tileable { +public class S_Block { } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Tile.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Tile.java @@ -0,0 +1,35 @@ +package blocks; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; + + +public class Tile { + int posX; + int posY; + Color color; + + + + public Tile(int x, int y, Color color) { + this.color = color; + this.posX = x; + this.posY = y; + } + + public void setColor(Color color) { + this.color = color; + } + + public Color getColor() { + return color; + } + + public int[] getPos() { + int[] positions = {posX, posY}; + return positions; + } + + +} diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Tileable.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Tileable.java @@ -1,9 +1,11 @@ package blocks; +import java.awt.Color; + public interface Tileable { - //public Object generate(); + //public int[][] getShape(); - //public void rotateLeft(); + //public Color getColor(); //public void rotateRight(); diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Z_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Z_Block.java @@ -1,5 +1,5 @@ package blocks; -public class Z_Block implements Tileable { +public class Z_Block { } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/BlockFactory.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/BlockFactory.java @@ -1,5 +1,17 @@ package game; -public class BlockFactory { +import blocks.I_Block; +import blocks.*; +public class BlockFactory { + + private int[] spawnPos; + + public Poly generatePoly() { + return new I_Block(spawnPos[0], spawnPos[1]); + } + + public void setSpawn(int[] spawnPos) { + this.spawnPos = spawnPos; + } } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Board.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Board.java @@ -1,6 +1,7 @@ package game; import java.awt.Color; + import java.awt.Dimension; import java.awt.Graphics; import java.util.ArrayList; @@ -8,7 +9,9 @@ import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.JPanel; -import blocks.Cell; +import blocks.Tile; +import blocks.Tileable; +import blocks.Poly; public class Board extends JPanel { //size of window @@ -16,12 +19,18 @@ public class Board extends JPanel { //size of each individual tile private final int tileSize = 20; - private Cell[][] grid; + private Poly[][] grid; + + private ArrayList<Poly> polys = new ArrayList<Poly>(); + + + private Poly fallingPoly; + private boolean polyFalling; public Board() { - - grid = new Cell[Math.abs(size.width / tileSize)][Math.abs(size.height / tileSize)]; + grid = new Poly[Math.abs(size.width / tileSize)][Math.abs(size.height / tileSize)]; setBorder(BorderFactory.createLineBorder(Color.black)); + polyFalling = false; } public Dimension getSize() { @@ -31,17 +40,110 @@ public class Board extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.gray); + draw(g); + } + + + public void addPoly(Poly poly) { + polys.add(poly); + } + + public int[] getPrefferedSpawn() { + int[] spawn = {grid.length / 2, 2}; + return spawn; + } + + /* + public void drawPoly(Poly poly) { + for (int x = 0; x < poly.getShape().length; x++) { + for (int y = 0; y < poly.getShape()[0].length; y++) { + addTile(new Tile(grid.length / 2, 2, Color.red), x, y); + } + } + }*/ + + /* + public void drawPoly(Tileable poly) { + fallingPoly = (Poly) poly; + int[][] shape = poly.getShape(); + for (int x = 0; x < shape.length; x++) { + for (int y = 0; y < shape[0].length; y++) { + Tile tile = new Tile(grid.length / 2, 2, poly.getColor()); + grid[tile.getPos()[0] + y][tile.getPos()[1] + x] = tile; + addTile(new Tile(grid.length / 2, 2, Color.red), x, y); + } + } + }*/ + + /* + private void addTile(Tile tile, int x, int y) { + grid[tile.getPos()[0] + y][tile.getPos()[1] + x] = tile; + }*/ + + /** + * Draws the grid (this code needs fixing) + * @param g + */ + + /** + public void draw(Graphics g) { + for (int x = 0; x < grid.length; x++) { + for (int y = 0; y < grid[0].length; y++) { + + + int posX = x*tileSize; + int posY = y*tileSize; + + + if (grid[x][y] != null) { + int[][] shape = grid[x][y].getShape(); + for (int i = 0; i < shape.length; i++) { + for (int j = 0; j < shape[0].length; j++) { + if (shape[i][j] == 1) { + + System.out.println("drawing tile at position: " + (posX + (i * tileSize))+ " " + (posY + (j * tileSize))); + g.setColor(grid[x][y].getColor()); + g.fillRect(posX + (i * tileSize), posY + (j * tileSize), tileSize, tileSize); + } + } + } + } + else { + g.setColor(Color.gray); + //g.fillRect(posX, posY, tileSize, tileSize); + } + } + } + }**/ + + private void clearBoard(Graphics g) { + g.setColor(Color.DARK_GRAY); g.fillRect(0, 0, size.width, size.height); - generateGrid(g); - } + public void draw(Graphics g) { + clearBoard(g); + for (Poly poly : polys) { + g.setColor(poly.getColor()); + int[][] shape = poly.getShape(); + + for (int i = 0; i < shape.length; i++) { + for (int j = 0; j < shape[0].length; j++) { + if (shape[i][j] == 1) { + System.out.println("Drawing poly at" + poly.getPos()[0]+ " " + (i*tileSize) + " : " + poly.getPos()[1]+(j*tileSize)); + g.fillRect(poly.getPos()[0] * tileSize + (i * tileSize), poly.getPos()[1] * tileSize + (j * tileSize), tileSize, tileSize); + } + } + } + } + } + /** private void generateGrid(Graphics g) { for (int x = 0; x < Math.abs(size.width / tileSize); x++) { for (int y = 0; y < Math.abs(size.height / tileSize); y++) { grid[y][x] = new Cell(g, y, x, size); } } - } + }**/ } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Game.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Game.java @@ -1,5 +1,39 @@ package game; -public class Game { +import java.util.Timer; +import java.util.TimerTask; + +import blocks.Poly; +public class Game { + private Board board; + private Timer timer; + + BlockFactory factory; + + private Poly poly; + + + public Game(Board board) { + timer = new Timer(); + timer.scheduleAtFixedRate(gameLoop, (long) 500, (long) 500); + this.board = board; + factory = new BlockFactory(); + factory.setSpawn(board.getPrefferedSpawn()); + + poly = factory.generatePoly(); + board.addPoly(poly); + } + + private TimerTask gameLoop = new TimerTask() { + @Override + public void run() { + board.repaint(); + + + poly.move(0, 1); + + board.repaint(); + } + }; } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Main.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Main.java @@ -3,8 +3,9 @@ package game; public class Main { public static void main(String[] args) { - Board Board = new Board(); - GUI gui = new GUI(Board); + Board board = new Board(); + GUI gui = new GUI(board); + Game game = new Game(board); } }