From 58cd1d05f0b029f7e7ac1bb7b11f39487622135a Mon Sep 17 00:00:00 2001 From: zhe Date: Mon, 13 Dec 2021 13:18:15 +0100 Subject: [PATCH] steine stapeln --- src/Tetris/Board.java | 20 +++++---- src/Tetris/Stein.java | 100 +++++++++++++++++++++++++++--------------- 2 files changed, 77 insertions(+), 43 deletions(-) diff --git a/src/Tetris/Board.java b/src/Tetris/Board.java index b36eb6c..338c2a7 100644 --- a/src/Tetris/Board.java +++ b/src/Tetris/Board.java @@ -6,6 +6,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.util.Random; import javax.swing.JPanel; import javax.swing.Timer; @@ -15,7 +16,7 @@ public class Board extends JPanel implements KeyListener private static int FPS =60; 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 BLOCK_SIZE=30; @@ -23,6 +24,7 @@ public class Board extends JPanel implements KeyListener private Color[][] board = new Color[BOARD_HEIGHT][BOARD_WIDTH]; private Stein [] steine=new Stein[7]; private Stein currenStein; + private Random ran; private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.decode("#00ff80"),Color.decode("#ff8000"),Color.decode("#ffb3b3"), Color.decode("#8000ff"),Color.decode("#ff0040"),}; @@ -30,13 +32,16 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d public Board() { - + ran = new Random(); steine[0]= new Stein(new int[][]{ - {1,1,1,1} - }, this,colors[0]); - steine[1]= 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}, @@ -55,8 +60,7 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d {0,1,1} }, this,colors[5]); steine[6]= new Stein(new int[][]{ - {1,1}, - {1,1} + {1,1,1,1} }, this,colors[6]); currenStein= steine[0]; @@ -81,7 +85,7 @@ private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.d public void setCurrenStein() { - currenStein = steine[1]; + currenStein = steine[ran.nextInt(steine.length)]; currenStein.reset(); } diff --git a/src/Tetris/Stein.java b/src/Tetris/Stein.java index bebb8ce..ebe7bda 100644 --- a/src/Tetris/Stein.java +++ b/src/Tetris/Stein.java @@ -3,74 +3,104 @@ package Tetris; import java.awt.*; public class Stein { - private int x=4,y=0; - private int normal =850; + private int x = 4, y = 0; + private int normal = 850; private int fast = 50; private long beginTime; private int delayTime = normal; private int deltax = 0; private boolean collision = false; - public static final int BOARD_WIDTH=11; - public static final int BOARD_HEIGHT=20; - public static final int BLOCK_SIZE=30; + public static final int BOARD_WIDTH = 11; + public static final int BOARD_HEIGHT = 20; + public static final int BLOCK_SIZE = 30; - private int[][]coords; + private int[][] coords; private Board board; private Color color; - public Stein(int [][] coords, Board board, Color color){ + public Stein(int[][] coords, Board board, Color color) { this.coords = coords; this.board = board; - this.color=color; - } - public void setX(int x){ - this.x=x; + this.color = color; } - public void setY(int y){ - this.y=y; + public void setX(int x) { + this.x = x; } - public void reset(){ - this.x=4; - this.y=0; - collision= false; + public void setY(int y) { + this.y = y; + } + + public void reset() { + this.x = 4; + this.y = 0; + collision = false; } - public void update(){ - if(collision){ - for(int row =0;row < coords.length;row++){ - for(int col =0;col < coords[0].length;col++){ - if (coords[row][col]!=0){ - board.getBoard()[y+row][x+col]= color; + + public void update() { + if (collision) { + for (int row = 0; row < coords.length; row++) { + for (int col = 0; col < coords[0].length; col++) { + if (coords[row][col] != 0) { + board.getBoard()[y + row][x + col] = color; } } } board.setCurrenStein(); return; } - if(!(x + deltax + coords[0].length >11) && !(x + deltax<0)) - { - x +=deltax; - } - deltax= 0; - if(System.currentTimeMillis() -beginTime > delayTime){ - if(!(y+1+coords.length > BOARD_HEIGHT)){ - y++; - }else{ - collision=true; + boolean moveX=true; + if (!(x + deltax + coords[0].length > 11) && !(x + deltax < 0)) { + 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 + 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){ for(int row=0;row< coords.length;row++){ for(int col = 0;col< coords[0].length;col++){ 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); }