俄罗斯方块 Java程序源代码 在eclipse上运行

2024-12-20 00:44:12
推荐回答(1个)
回答1:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Els extends JFrame implements KeyListener,Runnable
{
public Els()
{
setSize(240,320);
setUndecorated(true);
int H=(int) (this.getToolkit().getScreenSize().height);
int W=(int) (this.getToolkit().getScreenSize().width);
setLocation(W/2-120,H/2-160);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.darkGray.brighter().brighter());

//绘制小框格
for(int m=192;m<=232;m+=10)
{
g2.drawLine(m, 52, m,92);
}
for(int m=52;m<=92;m+=10)
{
g2.drawLine(192, m, 232, m);
}
//绘制大框格
for(int m=0;m<=180;m+=15)
{
g2.drawLine(m, 0, m, 300);
}
for(int m=0;m<=300;m+=15)
{
g2.drawLine(0, m, 180, m);
}

//将值不非0的格子涂黑
for(int i=1;i<13;i++)
{
for(int j=0;j<20;j++)
{
g2.setColor(Color.DARK_GRAY);
if (o[i][j]!=0) g2.fillRect(15*i-13, 15*j+2, 13, 13);
} }
//在小框格中绘制下一个的模型
switch(nextMODE)
{
case 0: g2.fillRect(204, 54, 8, 8);
g2.fillRect(204, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(204, 84, 8, 8);
break;
case 1: g2.fillRect(194, 64, 8, 8);
g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(224, 64, 8, 8);
break;
case 2: g2.fillRect(214, 64, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(214, 84, 8, 8);
g2.fillRect(204, 84, 8, 8);
break;
case 3: g2.fillRect(204, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(224, 74, 8, 8);
break;
case 4: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(204, 84, 8, 8);
break;
case 5: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(224, 64, 8, 8);
g2.fillRect(224, 74, 8, 8);
break;
case 6: g2.fillRect(204, 54, 8, 8);
g2.fillRect(204, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
break;
case 7: g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(224, 74, 8, 8);
g2.fillRect(224, 64, 8, 8);
break;
case 8: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(214, 84, 8, 8);
break;
case 9: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(224, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
break;
case 10: g2.fillRect(204, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(214, 84, 8, 8);
break;
case 11: g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(224, 64, 8, 8);
break;
case 12: g2.fillRect(214, 64, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(204, 84, 8, 8);
break;
case 13: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(224, 74, 8, 8);
break;
case 14: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
break;
} g2.setColor(Color.black);
g2.drawLine(180, 0, 180, 300);
g2.setColor(Color.DARK_GRAY);
g2.drawString("LEVEL", 194, 10);
g2.drawString(""+LEVEL, 210, 28);
g2.drawString("NEXT", 196, 45);
g2.drawString("SCORE",193, 110);
g2.drawString(""+SCORE,205, 130);
g2.drawString("Xiong", 205, 160);
g2.drawString("Xuan", 205, 180);
}
//方块下落的方法
public void down()
{
Y++;
switch(MODE)
{
case 0: o[X+2][Y]=1;
o[X+2][Y+1]=1;
o[X+2][Y+2]=1;
o[X+2][Y+3]++;
o[X+2][Y-1]=0;
break;
case 1: o[X][Y+1]++;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X+3][Y+1]++;
o[X][Y]=0;
o[X+1][Y]=0;
o[X+2][Y]=0;
o[X+3][Y]=0;
break;
case 2: o[X+2][Y]=1;
o[X+2][Y+1]=1;
o[X+2][Y+2]++;
o[X+1][Y+2]++;
o[X+2][Y-1]=0;
o[X+1][Y+1]=0;
break;
case 3: o[X][Y]=1;
o[X][Y+1]++;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X][Y-1]=0;
//o[X][Y]=0;
o[X+1][Y]=0;
o[X+2][Y]=0;
break;
case 4: o[X+1][Y]=1;
o[X+2][Y]++;
o[X+1][Y+1]=1;
o[X+1][Y+2]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
break;
case 5: o[X][Y]++;
o[X+1][Y]++;
o[X+2][Y]=1;
o[X+2][Y+1]++;
o[X][Y-1]=0;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
break;
case 6: o[X+1][Y]=1;
o[X+1][Y+1]=1;
o[X+1][Y+2]++;
o[X+2][Y+2]++;
o[X+1][Y-1]=0;
//o[X+1][Y]=0;
//o[X+1][Y+1]=0;
o[X+2][Y+1]=0;
break;
case 7: o[X][Y+1]++;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X+2][Y]=1;
o[X][Y]=0;
o[X+1][Y]=0;
//o[X+2][Y]=0;
o[X+2][Y-1]=0;
break;
case 8: o[X+1][Y]++;
o[X+2][Y]=1;
o[X+2][Y+1]=1;
o[X+2][Y+2]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
//o[X+2][Y]=0;
//o[X+2][Y+1]=0;
break;
case 9: o[X+1][Y]=1;
o[X+2][Y]++;
o[X+3][Y]++;
o[X+1][Y+1]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
o[X+3][Y-1]=0;
//o[X+1][Y]=0;
break;
case 10:o[X+1][Y]=1;
o[X+1][Y+1]++;
o[X+2][Y+1]=1;
o[X+2][Y+2]++;
o[X+1][Y-1]=0;
//o[X+1][Y]=0;
o[X+2][Y]=0;
//o[X+2][Y+1]=0;
break;
case 11:o[X+2][Y]=1;
o[X+3][Y]++;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X+2][Y-1]=0;
o[X+3][Y-1]=0;
o[X+1][Y]=0;
//o[X+2][Y]=0;
break;
case 12:o[X+1][Y+1]=1;
o[X+2][Y+1]++;
o[X+1][Y+2]++;
o[X+2][Y]=1;
o[X+1][Y]=0;
//o[X+2][Y]=0;
//o[X+1][Y+1]=0;
o[X+2][Y-1]=0;
break;
case 13:o[X+1][Y]++;
o[X+2][Y]=1;
o[X+2][Y+1]++;
o[X+3][Y+1]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
//o[X+2][Y]=0;
o[X+3][Y]=0;
break;
case 14:o[X+1][Y]=1;
o[X+2][Y]=1;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
//o[X+1][Y]=0;
//o[X+2][Y]=0;
break;
}
//如果碰撞,则自动上升一格
for(int i=0;i<13;i++)
{
for(int j=0;j<21;j++)
{
if (o[i][j]==2) up();
}
}

}