This commit is contained in:
Newe666 2022-02-07 19:43:14 +01:00
parent 8949a699ef
commit 744c17517b
2 changed files with 161 additions and 135 deletions

View File

@ -1,7 +1,6 @@
package Tetris; package Tetris;
import java.awt.Color; import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@ -11,61 +10,61 @@ import javax.swing.JPanel;
import javax.swing.Timer; import javax.swing.Timer;
//Ewen Kerbs,Zhe Wang-Holkenbrink //Ewen Kerbs,Zhe Wang-Holkenbrink
public class Board extends JPanel implements KeyListener public class Board extends JPanel implements KeyListener {
{ private static int FPS = 60;
private static int FPS =60; private static int delay = FPS / 1000;
private static int delay =FPS/1000;
//Spielfeld Breite und Länge //Spielfeld Breite und Länge
public static final int BOARD_WIDTH=11; public static final int BOARD_WIDTH = 11;
public static final int BOARD_HEIGHT=20; public static final int BOARD_HEIGHT = 20;
public static final int BLOCK_SIZE=30; public static final int BLOCK_SIZE = 30;
private Timer loop; private Timer loop;
private Color[][] board = new Color[BOARD_HEIGHT][BOARD_WIDTH]; private Color[][] board = new Color[BOARD_HEIGHT][BOARD_WIDTH];
private Stein [] steine=new Stein[7]; private Stein[] steine = new Stein[7];
private Stein currenStein; private Stein currenStein;
private Random ran; private Random ran;
private boolean gameOver = false;
// Steinfarben // Steinfarben
private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.decode("#00ff80"),Color.decode("#ff8000"),Color.decode("#ffb3b3"), private Color[] colors = {Color.decode("#ff00bf"), Color.decode("#0000ff"), Color.decode("#00ff80"), Color.decode("#ff8000"), Color.decode("#ffb3b3"),
Color.decode("#8000ff"),Color.decode("#ff0040"),}; Color.decode("#8000ff"), Color.decode("#ff0040"),};
// Steinform // Steinform
public Board() { public Board() {
ran = new Random(); ran = new Random();
steine[0]= new Stein(new int[][]{ steine[0] = new Stein(new int[][]{
{1,1,1}, {1, 1, 1},
{0,1,0}, {0, 1, 0},
}, this,colors[0]); }, this, colors[0]);
steine[1]= new Stein(new int[][]{ steine[1] = new Stein(new int[][]{
{1,1}, {1, 1},
{1,1} {1, 1}
}, this,colors[1]); }, this, colors[1]);
steine[2]= new Stein(new int[][]{ steine[2] = new Stein(new int[][]{
{1,1,1}, {1, 1, 1},
{1,0,0} {1, 0, 0}
}, this,colors[2]); }, this, colors[2]);
steine[3]= new Stein(new int[][]{ steine[3] = new Stein(new int[][]{
{1,1,1}, {1, 1, 1},
{0,0,1} {0, 0, 1}
}, this,colors[3]); }, this, colors[3]);
steine[4]= new Stein(new int[][]{ steine[4] = new Stein(new int[][]{
{0,1,1}, {0, 1, 1},
{1,1,0} {1, 1, 0}
}, this,colors[4]); }, this, colors[4]);
steine[5]= new Stein(new int[][]{ steine[5] = new Stein(new int[][]{
{1,1,0}, {1, 1, 0},
{0,1,1} {0, 1, 1}
}, this,colors[5]); }, this, colors[5]);
steine[6]= new Stein(new int[][]{ steine[6] = new Stein(new int[][]{
{1,1,1,1} {1, 1, 1, 1}
}, this,colors[6]); }, this, colors[6]);
currenStein= steine[0]; currenStein = steine[0];
loop = new Timer(delay, new ActionListener() { loop = new Timer(delay, new ActionListener() {
int n = 0; int n = 0;
@ -80,10 +79,29 @@ public class Board extends JPanel implements KeyListener
loop.start(); loop.start();
} }
private void update(){ private void update() {
if (spielbeenden()) {
loop.stop();
return;
}
currenStein.update(); currenStein.update();
} }
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;
}
public void setCurrenStein() { public void setCurrenStein() {
currenStein = steine[ran.nextInt(steine.length)]; currenStein = steine[ran.nextInt(steine.length)];
@ -94,7 +112,7 @@ public class Board extends JPanel implements KeyListener
return currenStein; return currenStein;
} }
public Color[][] getBoard(){ public Color[][] getBoard() {
return board; return board;
} }
@ -106,12 +124,12 @@ public class Board extends JPanel implements KeyListener
currenStein.render(g); currenStein.render(g);
for(int row=0;row< board.length;row++){ for (int row = 0; row < board.length; row++) {
for(int col = 0;col< board[row].length;col++){ for (int col = 0; col < board[row].length; col++) {
if(board[row][col] !=null){ if (board[row][col] != null) {
g.setColor(board[row][col]); g.setColor(board[row][col]);
// //
g.fillRect(col*BLOCK_SIZE,row*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE); g.fillRect(col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
} }
} }
} }
@ -119,48 +137,44 @@ public class Board extends JPanel implements KeyListener
//shape mit 2-Forschleife //shape mit 2-Forschleife
for (int row = 0; row < BOARD_HEIGHT+1; row++) { for (int row = 0; row < BOARD_HEIGHT + 1; row++) {
g.drawLine(0, BLOCK_SIZE * row, BLOCK_SIZE * BOARD_WIDTH, BLOCK_SIZE * row); g.drawLine(0, BLOCK_SIZE * row, BLOCK_SIZE * BOARD_WIDTH, BLOCK_SIZE * row);
} }
for (int col = 0; col < BOARD_WIDTH+1; col++) { for (int col = 0; col < BOARD_WIDTH + 1; col++) {
g.drawLine(col * BLOCK_SIZE, 0, col * BLOCK_SIZE, BLOCK_SIZE * BOARD_HEIGHT); 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);
}
} }
}
//Bewegen die Steine mit Taste, Taste-Space : nach unten, Taste d : nach rechts, Taste a : nach links, leider klappt nicht.
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
// switch(e.getKeyChar()) {
// case KeyEvent.VK_SPACE:
// currenStein.speedup();
// break;
// case KeyEvent.VK_A:
// currenStein.moveLeft();
// break;
// case KeyEvent.VK_D:
// currenStein.moveRight();
// break;
// }
} }
//Tastatur eingabe zur steinbewegung
//Bewegen die Steine mit Taste, Taste-Space : nach unten, Taste Enter : nach rechts, Taste Escape : nach links //wang/kerbs
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_SPACE){ if (e.getKeyCode() == KeyEvent.VK_SPACE) {
currenStein.speedup(); currenStein.speedup();
}else if(e.getKeyChar() == KeyEvent.VK_ENTER){ } else if (e.getKeyCode() == KeyEvent.VK_D) {
currenStein.moveRight(); currenStein.moveRight();
} } else if (e.getKeyCode() == KeyEvent.VK_A) {
else if(e.getKeyChar() == KeyEvent.VK_ESCAPE){
currenStein.moveLeft(); currenStein.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_W) {
currenStein.steinRotiert();
} }
} }
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_SPACE){ if (e.getKeyCode() == KeyEvent.VK_SPACE) {
currenStein.speedDown(); currenStein.speedDown();
} }
} }

View File

@ -6,6 +6,8 @@ import java.io.FileNotFoundException;
import java.util.Scanner; import java.util.Scanner;
public class Stein { public class Stein {
//SteineTyp Block; //SteineTyp Block;
private int x = 4, y = 0; private int x = 4, y = 0;
private int normal = 850; private int normal = 850;
@ -201,4 +203,14 @@ public void moveRight(){
public void moveLeft(){ public void moveLeft(){
deltax = -1; deltax = -1;
} }
public int[][] getCoords() {
return coords;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
} }