📄 boxpanel.java
字号:
package box;
import java.awt.*;
import javax.swing.*;
public class BoxPanel extends JPanel implements Runnable{
private int x=250,y;
private Point point[]=new Point[1000];
private Point topPoint[]=new Point[25];
private int currentNumber;
Thread thread;
private BoxCell boxcell;
private boolean canGoOn=true;
private int score=0;
private int time=200;
public BoxPanel() {
for(int i=0;i<25;i++){
topPoint[i] = new Point(10+i*20,660);
}
this.setSize(500,680);
thread=new Thread(this);
thread.start();
boxcell=new BoxCell();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("Scores:"+String.valueOf(score),15,9);
g.fill3DRect(10,10,500,650,false);
for(int i=0;i<24;i++){
if (i%2==0) g.setColor(Color.gray);
else g.setColor(Color.DARK_GRAY);
g.drawLine(30 + 20 * i, 10, 30 + 20 * i, 660);
}
g.setColor(Color.red);
for(int i=0;i<4;i++){
for (int j = 0; j < 4; j++)
if(boxcell.boxcell[i][j]==1)
g.fill3DRect(x+20*i, y+20*j, 20, 20, true);
}
for(int i=0;i<currentNumber;i++){
g.fill3DRect(point[i].x,point[i].y,20,20,true);
}
}
public void run() {
while(true){
try{
canGoOn = true;
if (isTimeToStop()) {
canGoOn = false;
if(y<20){
JOptionPane.showMessageDialog(this, "You Lose!!!", "Game Over", 1);
return;
}
}
if (y < 640 && canGoOn == true) y += 20;
else {
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(boxcell.boxcell[i][j]==1){
point[currentNumber] = new Point(x+20*i, y+20*j);
currentNumber++;
if(y+20*j<topPoint[(x-10)/20+i].y)
topPoint[(x-10)/20+i].y=y+20*j;
}
x = 250;
y = 0;
boxCellDestroy();
boxcell=new BoxCell();
}
thread.sleep(time-score>100?time-score/2:100);
//thread.sleep(100);
repaint();
}catch(Exception e){}
}
}
private void boxCellDestroy() {
int tempScore=0;
Point tempPoint=new Point();
for(int i=0;i<currentNumber-1;i++)
for(int j=i+1;j<currentNumber;j++){
if(point[i].y<point[j].y){
tempPoint=point[i];
point[i]=point[j];
point[j]=tempPoint;
}
}
for(int i=0;i<currentNumber-25;i++){
if (point[i].y == point[i + 24].y) {
tempScore++;
for (int j = i + 25; j < currentNumber; j++) {
point[j].y+=20;
point[i++] = point[j];
}
currentNumber -= 25;
i=-1;
for(int m=0;m<25;m++){
//topPoint[m].y+=20;
for(int a=currentNumber-1;a>=0;a--){
if (point[a].x == topPoint[m].x) {
topPoint[m].y = point[a].y;
break;
}
topPoint[m].y = 660;
}
}
repaint();
}
}
switch(tempScore){
case 1:{score++;break;}
case 2:{score+=3;break;}
case 3:{score+=5;break;}
case 4:{score+=7;break;}
}
}
private boolean isTimeToStop(){
boolean isStop;
for(int i=0;i<4;i++){
for(int j=3;j>-1;j--)
if(boxcell.boxcell[i][j]==1&&topPoint[(x-10)/20+i].y<=(y+(j+1)*20))return true;
}
return false;
}
public void move(int key){
switch(key){
case 32:{boxcell=rotate();break;}
case 40:{while(isTimeToStop()==false)y+=20;break;}
case 37:{if(x>10&&isTimeToStop()==false)x-=20;break;}
case 39:{if(x+(boxcell.width-1)*20<480&&isTimeToStop()==false)x+=20;break;}
default:break;
}
repaint();
}
protected BoxCell rotate() {
int tempY=y;
int n=0;
y+=boxcell.width*20;
if(isTimeToStop()==true){
y=tempY;
return boxcell;
}
if(x>=400) x-=20*(boxcell.height-boxcell.width);
BoxCell temp=new BoxCell();
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
temp.boxcell[i][j]=0;
for(int j=boxcell.height-1;j>=0;j--){
for(int i=0;i<4;i++){
temp.boxcell[n][i]=boxcell.boxcell[i][j];
}
n++;
}
temp.height=boxcell.width;
temp.width=boxcell.height;
y=tempY;
return temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -