Tetris/src/Tetris/Board.java

182 lines
5.1 KiB
Java
Raw Normal View History

2021-11-11 14:23:58 +01:00
package Tetris;
2022-02-07 19:43:14 +01:00
import java.awt.*;
2021-11-11 14:23:58 +01:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2021-11-17 19:31:18 +01:00
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
2021-12-13 13:18:15 +01:00
import java.util.Random;
2021-11-11 14:23:58 +01:00
import javax.swing.JPanel;
import javax.swing.Timer;
2021-11-11 14:48:14 +01:00
2021-11-17 19:31:18 +01:00
//Ewen Kerbs,Zhe Wang-Holkenbrink
2022-02-07 19:43:14 +01:00
public class Board extends JPanel implements KeyListener {
private static int FPS = 60;
private static int delay = FPS / 1000;
//Spielfeld Breite und Länge
public static final int BOARD_WIDTH = 11;
public static final int BOARD_HEIGHT = 20;
public static final int BLOCK_SIZE = 30;
private Timer loop;
private Color[][] board = new Color[BOARD_HEIGHT][BOARD_WIDTH];
private Stein[] steine = new Stein[7];
private Stein currenStein;
private Random ran;
private boolean gameOver = false;
// Steinfarben
private Color[] colors = {Color.decode("#ff00bf"), Color.decode("#0000ff"), Color.decode("#00ff80"), Color.decode("#ff8000"), Color.decode("#ffb3b3"),
Color.decode("#8000ff"), Color.decode("#ff0040"),};
// Steinform
public Board() {
ran = new Random();
steine[0] = new Stein(new int[][]{
{1, 1, 1},
{0, 1, 0},
}, this, colors[0]);
steine[1] = new Stein(new int[][]{
{1, 1},
{1, 1}
}, this, colors[1]);
steine[2] = new Stein(new int[][]{
{1, 1, 1},
{1, 0, 0}
}, this, colors[2]);
steine[3] = new Stein(new int[][]{
{1, 1, 1},
{0, 0, 1}
}, this, colors[3]);
steine[4] = new Stein(new int[][]{
{0, 1, 1},
{1, 1, 0}
}, this, colors[4]);
steine[5] = new Stein(new int[][]{
{1, 1, 0},
{0, 1, 1}
}, this, colors[5]);
steine[6] = new Stein(new int[][]{
{1, 1, 1, 1}
}, this, colors[6]);
currenStein = steine[0];
loop = new Timer(delay, new ActionListener() {
int n = 0;
@Override
public void actionPerformed(ActionEvent e) {
update();
repaint();
//System.out.println(n++);
}
});
loop.start();
}
private void update() {
if (spielbeenden()) {
loop.stop();
return;
2021-12-11 11:59:36 +01:00
}
2022-02-07 19:43:14 +01:00
currenStein.update();
2021-11-12 17:32:14 +01:00
}
2022-02-07 19:43:14 +01:00
private boolean spielbeenden() {
if (!gameOver) {
for (int i = 0; i < currenStein.getCoords().length; i++) {
for (int j = 0; j < currenStein.getCoords()[0].length; j++) {
if (currenStein.getCoords()[i][j] != 0) {
if (board[currenStein.getY() + i][currenStein.getX() + j] != null) {
gameOver = true;
}
}
}
}
}
return false;
}
2021-12-11 11:59:36 +01:00
2022-02-07 19:43:14 +01:00
public void setCurrenStein() {
currenStein = steine[ran.nextInt(steine.length)];
currenStein.reset();
2021-11-11 14:44:57 +01:00
}
2022-02-07 19:43:14 +01:00
public Stein getCurrenStein() {
return currenStein;
2021-11-11 14:44:57 +01:00
}
2021-11-12 17:32:14 +01:00
2022-02-07 19:43:14 +01:00
public Color[][] getBoard() {
return board;
2021-11-22 12:35:26 +01:00
}
2022-02-07 19:43:14 +01:00
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.lightGray);
currenStein.render(g);
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] != null) {
g.setColor(board[row][col]);
//
g.fillRect(col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
2022-02-11 10:52:51 +01:00
//linien mit 2-Forschleife
2022-02-07 19:43:14 +01:00
for (int row = 0; row < BOARD_HEIGHT + 1; row++) {
g.drawLine(0, BLOCK_SIZE * row, BLOCK_SIZE * BOARD_WIDTH, BLOCK_SIZE * row);
}
for (int col = 0; col < BOARD_WIDTH + 1; col++) {
g.drawLine(col * BLOCK_SIZE, 0, col * BLOCK_SIZE, BLOCK_SIZE * BOARD_HEIGHT);}
if (gameOver) {
String gameOverString = "GAME OVER";
g.setColor(Color.GREEN);
g.setFont(new Font("SANS_SERIF", Font.BOLD, 30));
g.drawString(gameOverString, 50, GameGui.height / 2);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
2021-11-17 19:31:18 +01:00
2022-02-07 19:43:14 +01:00
//Tastatur eingabe zur steinbewegung
//wang/kerbs
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
currenStein.speedup();
} else if (e.getKeyCode() == KeyEvent.VK_D) {
currenStein.moveRight();
} else if (e.getKeyCode() == KeyEvent.VK_A) {
currenStein.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_W) {
currenStein.steinRotiert();
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
currenStein.speedDown();
}
2021-11-17 19:31:18 +01:00
}
2021-11-11 14:23:58 +01:00
}