steine mit Form
This commit is contained in:
parent
9194320d79
commit
b607886184
2
.idea/.name
generated
2
.idea/.name
generated
@ -1 +1 @@
|
|||||||
GamePanel.java
|
GameGui.java
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -20,45 +20,53 @@ public class Board extends JPanel implements KeyListener
|
|||||||
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_WIDTH][BOARD_HEIGHT];
|
private Color[][] board = new Color[BOARD_HEIGHT][BOARD_WIDTH];
|
||||||
private Color[] [] shape ={
|
private Stein [] steine=new Stein[7];
|
||||||
{Color.YELLOW,Color.YELLOW,Color.YELLOW},
|
private Stein currenStein;
|
||||||
{null,Color.YELLOW,null}
|
|
||||||
};
|
private Color[] colors ={Color.decode("#ff00bf"),Color.decode("#0000ff"),Color.decode("#00ff80"),Color.decode("#ff8000"),Color.decode("#ffb3b3"),
|
||||||
private int x=4,y=0;
|
Color.decode("#8000ff"),Color.decode("#ff0040"),};
|
||||||
private int normal =650;
|
|
||||||
private int fast = 50;
|
|
||||||
private long beginTime;
|
|
||||||
private int delayTime = normal;
|
|
||||||
private int deltax = 0;
|
|
||||||
private boolean collision = false;
|
|
||||||
|
|
||||||
|
|
||||||
public Board() {
|
public Board() {
|
||||||
|
|
||||||
|
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[1]);
|
||||||
|
steine[2]= new Stein(new int[][]{
|
||||||
|
{1,1,1},
|
||||||
|
{1,0,0}
|
||||||
|
}, this,colors[2]);
|
||||||
|
steine[3]= new Stein(new int[][]{
|
||||||
|
{1,1,1},
|
||||||
|
{0,0,1}
|
||||||
|
}, this,colors[3]);
|
||||||
|
steine[4]= new Stein(new int[][]{
|
||||||
|
{1,1,1},
|
||||||
|
{1,1,0}
|
||||||
|
}, this,colors[4]);
|
||||||
|
steine[5]= new Stein(new int[][]{
|
||||||
|
{1,1,0},
|
||||||
|
{0,1,1}
|
||||||
|
}, this,colors[5]);
|
||||||
|
steine[6]= new Stein(new int[][]{
|
||||||
|
{1,1},
|
||||||
|
{1,1}
|
||||||
|
}, this,colors[6]);
|
||||||
|
|
||||||
|
currenStein= steine[0];
|
||||||
|
|
||||||
loop = new Timer(delay, new ActionListener() {
|
loop = new Timer(delay, new ActionListener() {
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if(collision){
|
update();
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(!(x + deltax + shape[0].length >11) && !(x + deltax<0))
|
|
||||||
{
|
|
||||||
x +=deltax;
|
|
||||||
}
|
|
||||||
|
|
||||||
deltax= 0;
|
|
||||||
if(System.currentTimeMillis() -beginTime > delayTime){
|
|
||||||
if(!(y+1+shape.length > BOARD_HEIGHT)){
|
|
||||||
y++;
|
|
||||||
}else{
|
|
||||||
collision=true;
|
|
||||||
}
|
|
||||||
|
|
||||||
beginTime=System.currentTimeMillis();
|
|
||||||
}
|
|
||||||
|
|
||||||
repaint();
|
repaint();
|
||||||
//System.out.println(n++);
|
//System.out.println(n++);
|
||||||
@ -67,24 +75,41 @@ public class Board extends JPanel implements KeyListener
|
|||||||
loop.start();
|
loop.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void update(){
|
||||||
|
currenStein.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setCurrenStein() {
|
||||||
|
currenStein = steine[1];
|
||||||
|
currenStein.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color[][] getBoard(){
|
||||||
|
return board;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
g.fillRect(0, 0, getWidth(), getHeight());
|
g.fillRect(0, 0, getWidth(), getHeight());
|
||||||
g.setColor(Color.lightGray);
|
g.setColor(Color.lightGray);
|
||||||
|
|
||||||
|
currenStein.render(g);
|
||||||
|
|
||||||
//shape mit 2-Forschleife
|
for(int row=0;row< board.length;row++){
|
||||||
for(int row=0;row< shape.length;row++){
|
for(int col = 0;col< board[row].length;col++){
|
||||||
for(int col = 0;col< shape[0].length;col++){
|
if(board[row][col] !=null){
|
||||||
if(shape[row][col] !=null){
|
g.setColor(board[row][col]);
|
||||||
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);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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);
|
||||||
}
|
}
|
||||||
@ -102,19 +127,19 @@ public class Board extends JPanel implements KeyListener
|
|||||||
@Override
|
@Override
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
if(e.getKeyChar() == KeyEvent.VK_SPACE){
|
if(e.getKeyChar() == KeyEvent.VK_SPACE){
|
||||||
delayTime=fast;
|
currenStein.speedup();
|
||||||
}else if(e.getKeyChar() == KeyEvent.VK_ENTER){
|
}else if(e.getKeyChar() == KeyEvent.VK_ENTER){
|
||||||
deltax = 2;
|
currenStein.moveRigth();
|
||||||
}
|
}
|
||||||
else if(e.getKeyChar() == KeyEvent.VK_ESCAPE){
|
else if(e.getKeyChar() == KeyEvent.VK_ESCAPE){
|
||||||
deltax = -2;
|
currenStein.moveLeft();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void keyReleased(KeyEvent e) {
|
public void keyReleased(KeyEvent e) {
|
||||||
if(e.getKeyChar() == KeyEvent.VK_SPACE){
|
if(e.getKeyChar() == KeyEvent.VK_SPACE){
|
||||||
delayTime=normal;
|
currenStein.speedDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
93
src/Tetris/Stein.java
Normal file
93
src/Tetris/Stein.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package Tetris;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class Stein {
|
||||||
|
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;
|
||||||
|
|
||||||
|
private int[][]coords;
|
||||||
|
private Board board;
|
||||||
|
private 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.fillRect(col*BLOCK_SIZE+x*BLOCK_SIZE,row*BLOCK_SIZE+y*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void speedup(){
|
||||||
|
delayTime=fast;
|
||||||
|
}
|
||||||
|
public void speedDown(){
|
||||||
|
delayTime=normal;
|
||||||
|
}
|
||||||
|
public void moveRigth(){
|
||||||
|
deltax = 2;
|
||||||
|
}
|
||||||
|
public void moveLeft(){
|
||||||
|
deltax = -2;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user