steine stapeln

This commit is contained in:
zhe 2021-12-13 13:18:15 +01:00
parent ce85efaf76
commit 58cd1d05f0
2 changed files with 77 additions and 43 deletions

View File

@ -6,6 +6,7 @@ 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;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.Timer; import javax.swing.Timer;
@ -15,7 +16,7 @@ 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;
public static final int BOARD_WIDTH=11; public static final int BOARD_WIDTH=10;
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;
@ -23,6 +24,7 @@ public class Board extends JPanel implements KeyListener
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 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"),};
@ -30,13 +32,16 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d
public Board() { public Board() {
ran = new Random();
steine[0]= new Stein(new int[][]{ steine[0]= new Stein(new int[][]{
{1,1,1,1}
}, this,colors[0]);
steine[1]= new Stein(new int[][]{
{1,1,1}, {1,1,1},
{0,1,0} {0,1,0}
}, this,colors[0]);
steine[1]= new Stein(new int[][]{
{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},
@ -55,8 +60,7 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d
{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];
@ -81,7 +85,7 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d
public void setCurrenStein() { public void setCurrenStein() {
currenStein = steine[1]; currenStein = steine[ran.nextInt(steine.length)];
currenStein.reset(); currenStein.reset();
} }

View File

@ -3,74 +3,104 @@ package Tetris;
import java.awt.*; import java.awt.*;
public class Stein { public class Stein {
private int x=4,y=0; private int x = 4, y = 0;
private int normal =850; private int normal = 850;
private int fast = 50; private int fast = 50;
private long beginTime; private long beginTime;
private int delayTime = normal; private int delayTime = normal;
private int deltax = 0; private int deltax = 0;
private boolean collision = false; private boolean collision = false;
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 int[][]coords; private int[][] coords;
private Board board; private Board board;
private Color color; private Color color;
public Stein(int [][] coords, Board board, Color color){ public Stein(int[][] coords, Board board, Color color) {
this.coords = coords; this.coords = coords;
this.board = board; this.board = board;
this.color=color; this.color = color;
}
public void setX(int x){
this.x=x;
} }
public void setY(int y){ public void setX(int x) {
this.y=y; this.x = x;
} }
public void reset(){ public void setY(int y) {
this.x=4; this.y = y;
this.y=0; }
collision= false;
public void reset() {
this.x = 4;
this.y = 0;
collision = false;
} }
public void update(){
if(collision){ public void update() {
for(int row =0;row < coords.length;row++){ if (collision) {
for(int col =0;col < coords[0].length;col++){ for (int row = 0; row < coords.length; row++) {
if (coords[row][col]!=0){ for (int col = 0; col < coords[0].length; col++) {
board.getBoard()[y+row][x+col]= color; if (coords[row][col] != 0) {
board.getBoard()[y + row][x + col] = color;
} }
} }
} }
board.setCurrenStein(); board.setCurrenStein();
return; return;
} }
if(!(x + deltax + coords[0].length >11) && !(x + deltax<0))
{
x +=deltax;
}
deltax= 0; boolean moveX=true;
if(System.currentTimeMillis() -beginTime > delayTime){ if (!(x + deltax + coords[0].length > 11) && !(x + deltax < 0)) {
if(!(y+1+coords.length > BOARD_HEIGHT)){ for (int row = 0; row < coords.length; row++) {
y++; for (int col = 0; col < coords[row].length; col++) {
}else{ if(coords[row][col] !=0) {
collision=true; if (board.getBoard()[y + row][x + deltax + col] != null) {
moveX = true;
}
}
}
}
if(moveX){
x += deltax;
} }
beginTime=System.currentTimeMillis();
} }
deltax = 0;
if (System.currentTimeMillis() - beginTime > delayTime) {
if (!(y + 1+coords.length > BOARD_HEIGHT)) {
for (int row = 0; row < coords.length; row++) {
for (int col = 0; col < coords[row].length; col++) {
if (coords[row][col] != 0){
if (board.getBoard()[y +1 + row][x - deltax + col] != null) {
collision = true;
}
}
}
}
if (!collision) {
y++;
}
} else {
collision = true;
}
beginTime = System.currentTimeMillis();
} }
}
public void render(Graphics g){ public void render(Graphics g){
for(int row=0;row< coords.length;row++){ for(int row=0;row< coords.length;row++){
for(int col = 0;col< coords[0].length;col++){ for(int col = 0;col< coords[0].length;col++){
if(coords[row][col] !=0){ if(coords[row][col] !=0){
g.setColor(Color.yellow); g.setColor(color);
// //
g.fillRect(col*BLOCK_SIZE+x*BLOCK_SIZE,row*BLOCK_SIZE+y*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE); g.fillRect(col*BLOCK_SIZE+x*BLOCK_SIZE,row*BLOCK_SIZE+y*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
} }