2021-11-11 14:23:58 +01:00
|
|
|
package Tetris;
|
|
|
|
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Graphics;
|
|
|
|
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-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
|
|
|
|
public class Board extends JPanel implements KeyListener
|
2021-11-11 14:23:58 +01:00
|
|
|
{
|
2021-11-17 19:31:18 +01:00
|
|
|
private static int FPS =60;
|
|
|
|
private static int delay =FPS/1000;
|
|
|
|
|
2021-11-11 14:23:58 +01:00
|
|
|
public static final int BOARD_WIDTH=10;
|
2021-11-11 14:44:57 +01:00
|
|
|
public static final int BOARD_HEIGHT=20;
|
|
|
|
public static final int BLOCK_SIZE=30;
|
2021-11-12 17:32:14 +01:00
|
|
|
|
2021-11-11 14:23:58 +01:00
|
|
|
private Timer loop;
|
2021-11-11 14:26:01 +01:00
|
|
|
private Color[][] board = new Color[BOARD_WIDTH][BOARD_HEIGHT];
|
2021-11-12 17:32:14 +01:00
|
|
|
private Color[] [] shape ={
|
|
|
|
{Color.YELLOW,Color.YELLOW,Color.YELLOW},
|
|
|
|
{null,Color.YELLOW,null}
|
|
|
|
};
|
2021-11-17 19:31:18 +01:00
|
|
|
private int x=4,y=0;
|
|
|
|
private int normal =650;
|
|
|
|
private int fast = 50;
|
|
|
|
private long beginTime;
|
|
|
|
private int delayTime = normal;
|
2021-11-11 14:25:40 +01:00
|
|
|
|
2021-11-11 14:23:58 +01:00
|
|
|
public Board() {
|
2021-11-19 12:37:20 +01:00
|
|
|
|
2021-11-17 19:31:18 +01:00
|
|
|
loop = new Timer(delay, new ActionListener() {
|
2021-11-11 14:23:58 +01:00
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2021-11-17 19:31:18 +01:00
|
|
|
if(System.currentTimeMillis() -beginTime > delayTime){
|
|
|
|
y++;
|
|
|
|
beginTime=System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
repaint();
|
|
|
|
//System.out.println(n++);
|
2021-11-11 14:23:58 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
loop.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void paintComponent(Graphics g) {
|
|
|
|
super.paintComponent(g);
|
2021-11-12 17:32:14 +01:00
|
|
|
g.fillRect(0, 0, getWidth(), getHeight());
|
2021-11-16 11:18:51 +01:00
|
|
|
g.setColor(Color.lightGray);
|
2021-11-11 14:44:57 +01:00
|
|
|
|
2021-11-12 17:32:14 +01:00
|
|
|
//shape mit 2-Forschleife
|
2021-11-12 17:40:58 +01:00
|
|
|
for(int row=0;row< shape.length;row++){
|
|
|
|
for(int col = 0;col< shape[0].length;col++){
|
|
|
|
if(shape[row][col] !=null){
|
|
|
|
g.setColor(shape[row][col]);
|
|
|
|
//
|
2021-11-17 19:31:18 +01:00
|
|
|
g.fillRect(col*BLOCK_SIZE+x*BLOCK_SIZE,row*BLOCK_SIZE+y*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
|
2021-11-12 17:32:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int row = 0; row < BOARD_HEIGHT+1; row++) {
|
2021-11-11 14:44:57 +01:00
|
|
|
g.drawLine(0, BLOCK_SIZE * row, BLOCK_SIZE * BOARD_WIDTH, BLOCK_SIZE * row);
|
|
|
|
}
|
2021-11-12 17:32:14 +01:00
|
|
|
for (int col = 0; col < BOARD_WIDTH+1; col++) {
|
2021-11-11 14:44:57 +01:00
|
|
|
g.drawLine(col * BLOCK_SIZE, 0, col * BLOCK_SIZE, BLOCK_SIZE * BOARD_HEIGHT);
|
|
|
|
}
|
2021-11-12 17:32:14 +01:00
|
|
|
|
2021-11-11 14:23:58 +01:00
|
|
|
}
|
2021-11-12 17:32:14 +01:00
|
|
|
|
2021-11-17 19:31:18 +01:00
|
|
|
@Override
|
|
|
|
public void keyTyped(KeyEvent e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void keyPressed(KeyEvent e) {
|
2021-11-19 12:37:20 +01:00
|
|
|
if(e.getKeyChar() == KeyEvent.VK_SPACE){
|
|
|
|
delayTime=fast;
|
|
|
|
}
|
2021-11-17 19:31:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void keyReleased(KeyEvent e) {
|
|
|
|
if(e.getKeyChar() == KeyEvent.VK_SPACE){
|
|
|
|
delayTime=normal;
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 14:23:58 +01:00
|
|
|
}
|