bausteine fallen

This commit is contained in:
zhe 2021-11-17 19:31:18 +01:00
parent 5e7f03746e
commit c4a4708ab1
3 changed files with 48 additions and 20 deletions

View File

@ -4,11 +4,17 @@ import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel
//Ewen Kerbs,Zhe Wang-Holkenbrink
public class Board extends JPanel implements KeyListener
{
private static int FPS =60;
private static int delay =FPS/1000;
public static final int BOARD_WIDTH=10;
public static final int BOARD_HEIGHT=20;
public static final int BLOCK_SIZE=30;
@ -19,14 +25,25 @@ public class Board extends JPanel
{Color.YELLOW,Color.YELLOW,Color.YELLOW},
{null,Color.YELLOW,null}
};
private int x=4,y=0;
private int normal =650;
private int fast = 50;
private long beginTime;
private int delayTime = normal;
public Board() {
loop = new Timer(500, new ActionListener() {
loop = new Timer(delay, new ActionListener() {
int n = 0;
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(n++);
if(System.currentTimeMillis() -beginTime > delayTime){
y++;
beginTime=System.currentTimeMillis();
}
repaint();
//System.out.println(n++);
}
});
loop.start();
@ -44,7 +61,7 @@ public class Board extends JPanel
if(shape[row][col] !=null){
g.setColor(shape[row][col]);
//
g.fillRect(col*BLOCK_SIZE,row*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
g.fillRect(col*BLOCK_SIZE+x*BLOCK_SIZE,row*BLOCK_SIZE+y*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
}
}
@ -59,4 +76,22 @@ public class Board extends JPanel
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_SPACE){
delayTime=fast;
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_SPACE){
delayTime=normal;
}
}
}

View File

@ -7,24 +7,26 @@ public class GameGui {
private Board board;
private JFrame frame;
//Zhe Wang-Holkenbrink
public GameGui(){
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createMatteBorder(400, 10, 20, 10,Color.black));
panel.setBorder(BorderFactory.createMatteBorder(400, 10, 10, 10,Color.black));
/*
//Label
JLabel label = new JLabel(" Ihr Name:");
panel.add(label);
//Textfeld
JTextField tfName = new JTextField("Bitte Name eingeben");
tfName.setForeground(Color.white);
//JTextField tfName = new JTextField("Bitte Name eingeben");
//tfName.setForeground(Color.white);
// Hintergrundfarbe wird gesetzt
tfName.setBackground(Color.GRAY);
//tfName.setBackground(Color.GRAY);
// Textfeld wird unserem Panel hinzugefügt
panel.add(tfName);
//panel.add(tfName);
//Button
JButton button1 = new JButton("Start");
button1.setPreferredSize( new Dimension(150,150));
button1.setPreferredSize( new Dimension(150,50));
JButton button2 = new JButton("Pause");
JButton button3 = new JButton("Score");
@ -33,16 +35,6 @@ public class GameGui {
panel.add(button1);
panel.add(button2);
panel.add(button3);
/*
window.add(panel, BorderLayout.EAST);
frame.add(window,BorderLayout.WEST);
window.setSize(width,height);
//window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
window.setResizable(false);
window.setVisible(true);
board = new Board();
window.add(board);
*/
frame = new JFrame("Tetris");
frame.add(panel,BorderLayout.EAST);
@ -51,6 +43,7 @@ public class GameGui {
frame.setResizable(false);
frame.setVisible(true);
board = new Board();
frame.addKeyListener(board);
frame.add(board,BorderLayout.CENTER);
}
public static void main(String [] args)