效果如下图
虽然整体上比原版没有功能上的增加,甚至把一些不必要的组件给删掉了,但是从程序本身上讲,这和原版有质的飞跃,与原版相比
改变:
1:删除了自定义图片按钮,用R键来重新开始,用P键来暂停和开始
2:删除了分数展示框,使分数展示在窗口标题栏里
特点:
1:代码的可读性大大增强
2:代码的可学习性大大增强
3:代码被模块化,使之更容易分离用作他用
4:大幅改善代码,取代繁杂的代码,用最精简的代码完成
5:优化UI刷新,使之不会出现任何的刷新不协调
保留:
1:按下向上按钮来旋转方块
2:按下左/右/下按钮进行移动
3:按下空格键进行落到底部
扩展:
1:可自定义方块图片(格式:jpg,尺寸49*49,名称:red,yellow,green,blue,purple,empty)
2:代码移植可抠出类GameContent,Block,Units来分离游戏逻辑
总计累计大概10小时完成
可以通过以下进行代码比较
原版代码展示(极其繁杂,毫无可读性)1000+行
package UIDesign.UI2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class UI2 {
static boolean IsTest=true;
public static void main(String []args){
frame2 x=new frame2();
x.setTitle("俄罗斯方块");
}
}
class frame2 extends JFrame implements KeyListener, ActionListener, MouseListener ,MouseMotionListener{
static boolean enable=false;
static int Button_Restart =1;
static int Score =0;
static int drop=0;
static int color;
static int nowType;
static final int wid=10;
static final int hei=18;
static int[]ST=new int[6];
static int[][]Ready1=new int[4][4];
static int[][]Ready2=new int[4][4];
static int[][]Ready3=new int[4][4];
static int[][]A=new int[hei+4][wid];
static int[][][]M=new int[2][4][2];
static Timer timer;
frame2(){
setSize(310+50*wid,50+50*hei);
setDefaultCloseOperation(frame2.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
enable=true;
create(RandomTypeAndColor());
addInit();
RCopy(GetReady(ST[1]), GetReady(ST[3]), GetReady(ST[5]));
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
if(UI2.IsTest){setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\green.jpg"));}
else{setIconImage(Toolkit.getDefaultToolkit().getImage("green.jpg"));}
timer = new Timer(1000, this);
timer.start();
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.drawLine(50*wid+11,0,50*wid+11,40+50*hei);
Image empty,blue,yellow,purple,red,green,restart0,restart1;
if(UI2.IsTest) {
empty = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\empty.jpg");
blue = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\blue.jpg");
yellow = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\yellow.jpg");
purple = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\purple.jpg");
red = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\red.jpg");
green = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\green.jpg");
restart0 = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\restart0.jpg");
restart1 = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\restart1.jpg");
}
else{empty = getToolkit().getImage("empty.jpg");
blue = getToolkit().getImage("blue.jpg");
yellow = getToolkit().getImage("yellow.jpg");
purple = getToolkit().getImage("purple.jpg");
red = getToolkit().getImage("red.jpg");
green = getToolkit().getImage("green.jpg");
restart0 = getToolkit().getImage("restart0.jpg");
restart1 = getToolkit().getImage("restart1.jpg");
}
for(int y=4;y<A.length;y++){
for(int x=0;x<A[0].length;x++){
switch (A[y][x]){
case 0:g.drawImage(empty,10+x*50,y*50-160,this);break;
case 1:g.drawImage(blue,10+x*50,y*50-160,this);break;
case 2:g.drawImage(yellow,10+x*50,y*50-160,this);break;
case 3:g.drawImage(purple,10+x*50,y*50-160,this);break;
case 4:g.drawImage(red,10+x*50,y*50-160,this);break;
case 5:g.drawImage(green,10+x*50,y*50-160,this);break;
}
}
}
for(int y=0;y<4;y++){
for(int x=0;x<4;x++){
switch (Ready1[y][x]){
case 0:g.drawImage(empty,x*50+560,y*50+50,this);break;
case 1:g.drawImage(blue,x*50+560,y*50+50,this);break;
case 2:g.drawImage(yellow,x*50+560,y*50+50,this);break;
case 3:g.drawImage(purple,x*50+560,y*50+50,this);break;
case 4:g.drawImage(red,x*50+560,y*50+50,this);break;
case 5:g.drawImage(green,x*50+560,y*50+50,this);break;
}
}
}
for(int y=0;y<4;y++){
for(int x=0;x<4;x++){
switch (Ready2[y][x]){
case 0:g.drawImage(empty,x*50+560,y*50+300,this);break;
case 1:g.drawImage(blue,x*50+560,y*50+300,this);break;
case 2:g.drawImage(yellow,x*50+560,y*50+300,this);break;
case 3:g.drawImage(purple,x*50+560,y*50+300,this);break;
case 4:g.drawImage(red,x*50+560,y*50+300,this);break;
case 5:g.drawImage(green,x*50+560,y*50+300,this);break;
}
}
}
for(int y=0;y<4;y++){
for(int x=0;x<4;x++){
switch (Ready3[y][x]){
case 0:g.drawImage(empty,x*50+560,y*50+550,this);break;
case 1:g.drawImage(blue,x*50+560,y*50+550,this);break;
case 2:g.drawImage(yellow,x*50+560,y*50+550,this);break;
case 3:g.drawImage(purple,x*50+560,y*50+550,this);break;
case 4:g.drawImage(red,x*50+560,y*50+550,this);break;
case 5:g.drawImage(green,x*50+560,y*50+550,this);break;
}
}
}
g.clearRect(550,850,200,100);
g.drawString("得分:"+ Score,550,900);
if(Button_Restart ==1) {g.drawImage(restart1, 550, 800, this);}
else{g.drawImage(restart0, 550, 800, this);}
}
void addNext(){
Random A=new Random();
ST[0]=ST[2];
ST[1]=ST[3];
ST[2]=ST[4];
ST[3]=ST[5];
ST[4]=A.nextInt(5)+1;
ST[5]=A.nextInt(19)+1;
}
void addInit(){
Random A=new Random();
ST[0]=A.nextInt(5)+1;
ST[1]=A.nextInt(19)+1;
ST[2]=A.nextInt(5)+1;
ST[3]=A.nextInt(19)+1;
ST[4]=A.nextInt(5)+1;
ST[5]=A.nextInt(19)+1;
}
void checkIt(){
int i=21;
int t=0;
while(i>3){
int k=1;
for(int x=0;x<=9;x++){if(A[i][x]==0){k=0;break;}}
if(k==1){Destroy(i);t++;}
else{i--;}
}
if(t==1){
Score = Score +10;}
else if(t==2){
Score = Score +30;}
else if(t==3){
Score = Score +60;}
else if(t==4){
Score = Score +100;}
for(int x=0;x<10;x++){
if(A[3][x]!=0){
GameOver();
break;
}
}
}
void Convert(){
if(check(RConvert())){DConvert();repaint();
}
}
int [][]copy(int [][]S){
int [][]M=new int[S.length][];
for(int y=0;y<S.length;y++){
M[y]=S[y].clone();
}
return M;
}
boolean check_help(int []S){
for(int y=0;y<=3;y++){
if(M[1][y][0]==S[0]&&M[1][y][1]==S[1]){return false;}
}
return true;
}
boolean check(int [][]N){
for(int i=0;i<=3;i++){
if(N[i][1]>9||N[i][1]<0||N[i][0]>21||N[i][0]<0){return false;}
}
for(int i=0;i<=3;i++){
if(check_help(N[i])){if(A[N[i][0]][N[i][1]]!=0){return false;}}
}
return true;
}
void change(){
for (int i = 0; i <= 3; i++) {
A[M[0][i][0]][M[0][i][1]] = 0;
}
for(int i=0;i<=3;i++){
A[M[1][i][0]][M[1][i][1]]=color;
}
}
void create(int type){
drop=0;
nowType=type;
switch (type){
case 1:SCopy(TYPE.A1);M[1]=copy(TYPE.AI1);break;
case 2:SCopy(TYPE.A2);M[1]=copy(TYPE.AI2);break;
case 3:SCopy(TYPE.B1);M[1]=copy(TYPE.BI1);break;
case 4:SCopy(TYPE.B2);M[1]=copy(TYPE.BI2);break;
case 5:SCopy(TYPE.C1);M[1]=copy(TYPE.CI1);break;
case 6:SCopy(TYPE.C2);M[1]=copy(TYPE.CI2);break;
case 7:SCopy(TYPE.D1);M[1]=copy(TYPE.DI1);break;
case 8:SCopy(TYPE.D2);M[1]=copy(TYPE.DI2);break;
case 9:SCopy(TYPE.D3);M[1]=copy(TYPE.DI3);break;
case 10:SCopy(TYPE.D4);M[1]=copy(TYPE.DI4);break;
case 11:SCopy(TYPE.E1);M[1]=copy(TYPE.EI1);break;
case 12:SCopy(TYPE.E2);M[1]=copy(TYPE.EI2);break;
case 13:SCopy(TYPE.E3);M[1]=copy(TYPE.EI3);break;
case 14:SCopy(TYPE.E4);M[1]=copy(TYPE.EI4);break;
case 15:SCopy(TYPE.F);M[1]=copy(TYPE.FI);break;
case 16:SCopy(TYPE.G1);M[1]=copy(TYPE.GI1);break;
case 17:SCopy(TYPE.G2);M[1]=copy(TYPE.GI2);break;
case 18:SCopy(TYPE.G3);M[1]=copy(TYPE.GI3);break;
case 19:SCopy(TYPE.G4);M[1]=copy(TYPE.GI4);break;
default:
}
}
boolean checkInRestart(int x,int y){
return x >= 550 && x <= 650 && y >= 800 && y < 840;
}
int ConvertType(int Type){
switch (Type){
case 1:return 2;
case 2:return 1;
case 3:return 4;
case 4:return 3;
case 5:return 6;
case 6:return 5;
case 7:return 8;
case 8:return 9;
case 9:return 10;
case 10:return 7;
case 11:return 12;
case 12:return 13;
case 13:return 14;
case 14:return 11;
case 15:return 15;
case 16:return 17;
case 17:return 18;
case 18:return 19;
case 19:return 16;
}
return 0;
}
void Drop(){
if(check(RDrop())){
DDrop();
drop++;
change();}
else{checkIt();
color=ST[0];
create(ST[1]);
addNext();
RCopy(GetReady(ST[1]), GetReady(ST[3]), GetReady(ST[5]));}
repaint();
}
void Down(){
if(enable) {
Drop();
}
}
void DropDown(){
if(enable) {
while (check(RDrop())) {
DDrop();
drop++;
change();
}
repaint();
}
}
void DConvert(){
int [][]K=copy(M[1]);
switch (nowType){
case 1:
K[0][0]+=2;
K[0][1]-=2;
K[1][0]+=1;
K[1][1]-=1;
K[3][0]-=1;
K[3][1]+=1;
break;
case 2:
K[0][0]-=2;
K[0][1]+=2;
K[1][0]-=1;
K[1][1]+=1;
K[3][0]+=1;
K[3][1]-=1;
break;
case 3:
K[0][0]+=1;
K[0][1]-=1;
K[1][1]+=1;
K[2][0]+=1;
K[3][1]+=2;
break;
case 4:
K[0][0]-=1;
K[0][1]+=1;
K[1][1]-=1;
K[2][0]-=1;
K[3][1]-=2;
break;
case 5:
K[0][0]+=1;
K[1][1]+=1;
K[2][0]+=1;
K[2][1]-=2;
K[3][1]-=1;
break;
case 6:
K[0][0]-=1;
K[1][1]-=1;
K[2][0]-=1;
K[2][1]+=2;
K[3][1]+=1;
break;
case 7:
K[0][0]+=1;
K[0][1]+=-1;
K[1][0]+=1;
K[1][1]+=-1;
K[2][1]+=1;
K[3][1]+=1;
break;
case 8:
K[1][0]+=-1;
K[1][1]+=1;
K[2][1]+=-1;
K[3][0]+=1;
K[3][1]+=-2;
break;
case 9:
K[0][1]+=-1;
K[1][1]+=-1;
K[2][0]+=-1;
K[2][1]+=1;
K[3][0]+=-1;
K[3][1]+=1;
break;
case 10:
K[0][0]+=-1;
K[0][1]+=2;
K[1][1]+=1;
K[2][0]+=1;
K[2][1]+=-1;
break;
case 11:
K[0][0]+=1;
K[0][1]+=1;
K[1][0]+=1;
K[1][1]+=-1;
break;
case 12:
K[0][1]-=1;
K[1][0]-=1;
K[1][1]+=2;
K[2][1]+=1;
K[3][0]+=1;
break;
case 13:
K[2][0]+=-1;
K[2][1]+=1;
K[3][0]+=-1;
K[3][1]+=-1;
break;
case 14:
K[0][0]+=-1;
K[1][1]+=-1;
K[2][0]+=1;
K[2][1]+=-2;
K[3][1]+=1;
break;
case 15:
break;
case 16:
K[1][1]+=1;
K[2][1]+=1;
K[3][0]+=1;
K[3][1]-=1;
break;
case 17:
K[0][0]+=1;
K[0][1]-=1;
break;
case 18:
K[0][0]-=1;
K[0][1]+=1;
K[1][1]-=1;
K[2][1]-=1;
break;
case 19:
K[3][0]-=1;
K[3][1]+=1;
break;
}
M[0]=copy(M[1]);
M[1]=K.clone();
nowType=ConvertType(nowType);
change();
}
void DDrop(){
M[0]=copy(M[1]);
for(int i=0;i<=3;i++){
M[1][i][0]++;
}
change();
}
void DLeft(){
M[0]=copy(M[1]);
for(int i=0;i<=3;i++){
M[1][i][1]--;
}
change();
}
void DRight(){
M[0]=copy(M[1]);
for(int i=0;i<=3;i++){
M[1][i][1]++;
}
change();
}
void Destroy(int Line){
for(int i=0;i<10;i++){
A[Line][i]=0;
}
for(int y=Line;y>3;y--){
System.arraycopy(A[y - 1], 0, A[y], 0, 10);
}
repaint();
}
int[][] GetReady(int M){
switch (M){
case 1:return TYPE.A1;
case 2:return TYPE.A2;
case 3:return TYPE.B1;
case 4:return TYPE.B2;
case 5:return TYPE.C1;
case 6:return TYPE.C2;
case 7:return TYPE.D1;
case 8:return TYPE.D2;
case 9:return TYPE.D3;
case 10:return TYPE.D4;
case 11:return TYPE.E1;
case 12:return TYPE.E2;
case 13:return TYPE.E3;
case 14:return TYPE.E4;
case 15:return TYPE.F;
case 16:return TYPE.G1;
case 17:return TYPE.G2;
case 18:return TYPE.G3;
case 19:return TYPE.G4;
}
return TYPE.AI1;
}
void GameOver(){
timer.stop();
enable=false;
int n = JOptionPane.showConfirmDialog(null, "游戏结束,是否重新开始游戏?", "提示",JOptionPane.YES_NO_OPTION);
if(n==0){restart();}
}
void init(){
Score =0;
for(int y=0;y<A.length;y++){
for(int x=0;x<A[0].length;x++){
A[y][x]=0;
}
}
create(RandomTypeAndColor());
addInit();
RCopy(GetReady(ST[1]), GetReady(ST[3]), GetReady(ST[5]));
}
void Left(){
if(enable) {
if (check(RLeft())) {
DLeft();
repaint();
}
}
}
void Right(){
if(enable) {
if (check(RRight())) {
DRight();
repaint();
}
}
}
void RCopy(int [][]E,int [][]W,int [][]K){
for(int y=0;y<4;y++){
for(int x=0;x<4;x++){
if(E[y][x+3]==1){Ready1[y][x]=ST[0];}
else if(E[y][x+3]==0){Ready1[y][x]=0;}
if(W[y][x+3]==1){Ready2[y][x]=ST[2];}
else if(W[y][x+3]==0){Ready2[y][x]=0;}
if(K[y][x+3]==1){Ready3[y][x]=ST[4];}
else if(K[y][x+3]==0){Ready3[y][x]=0;}
}
}
}
int[][] RConvert(){
int [][]K=copy(M[1]);
switch (nowType){
case 1:
K[0][0]+=2;
K[0][1]-=2;
K[1][0]+=1;
K[1][1]-=1;
K[3][0]-=1;
K[3][1]+=1;
break;
case 2:
K[0][0]-=2;
K[0][1]+=2;
K[1][0]-=1;
K[1][1]+=1;
K[3][0]+=1;
K[3][1]-=1;
break;
case 3:
K[0][0]+=1;
K[0][1]-=1;
K[1][1]+=1;
K[2][0]+=1;
K[3][1]+=2;
break;
case 4:
K[0][0]-=1;
K[0][1]+=1;
K[1][1]-=1;
K[2][0]-=1;
K[3][1]-=2;
break;
case 5:
K[0][0]+=1;
K[1][1]+=1;
K[2][0]+=1;
K[2][1]-=2;
K[3][1]-=1;
break;
case 6:
K[0][0]-=1;
K[1][1]-=1;
K[2][0]-=1;
K[2][1]+=2;
K[3][1]+=1;
break;
case 7:
K[0][0]+=1;
K[0][1]+=-1;
K[1][0]+=1;
K[1][1]+=-1;
K[2][1]+=1;
K[3][1]+=1;
break;
case 8:
K[1][0]+=-1;
K[1][1]+=1;
K[2][1]+=-1;
K[3][0]+=1;
K[3][1]+=-2;
break;
case 9:
K[0][1]+=-1;
K[1][1]+=-1;
K[2][0]+=-1;
K[2][1]+=1;
K[3][0]+=-1;
K[3][1]+=1;
break;
case 10:
K[0][0]+=-1;
K[0][1]+=2;
K[1][1]+=1;
K[2][0]+=1;
K[2][1]+=-1;
break;
case 11:
K[0][0]+=1;
K[0][1]+=1;
K[1][0]+=1;
K[1][1]+=-1;
break;
case 12:
K[0][1]+=-1;
K[1][0]+=-1;
K[1][1]+=2;
K[2][1]+=1;
K[3][0]+=1;
break;
case 13:
K[2][0]+=-1;
K[2][1]+=1;
K[3][0]+=-1;
K[3][1]+=-1;
break;
case 14:
K[0][0]+=-1;
K[1][1]+=-1;
K[2][0]+=1;
K[2][1]+=-2;
K[3][1]+=1;
break;
case 15:
break;
case 16:
K[1][1]+=1;
K[2][1]+=1;
K[3][0]+=1;
K[3][1]-=1;
break;
case 17:
K[0][0]+=1;
K[0][1]-=1;
break;
case 18:
K[0][0]-=1;
K[0][1]+=1;
K[1][1]-=1;
K[2][1]-=1;
break;
case 19:
K[3][0]-=1;
K[3][1]+=1;
break;
}
return K;
}
int[][] RDrop(){
int [][]K=copy(M[1]);
for(int i=0;i<=3;i++){
K[i][0]++;
}
return K;
}
int[][] RLeft(){
int [][]K=copy(M[1]);
for(int i=0;i<=3;i++){
K[i][1]--;
}
return K;
}
int[][] RRight(){
int [][]K=copy(M[1]);
for(int i=0;i<=3;i++){
K[i][1]++;
}
return K;
}
int RandomTypeAndColor(){
Random A=new Random();
color=A.nextInt(5)+1;
return A.nextInt(19)+1;
}
void restart(){
init();
repaint();
enable=true;
timer.restart();
}
void SCopy(int [][]E){
for(int i=0;i<=3;i++){
A[i]=E[i].clone();
}
}
void Up(){
if(enable) {
Convert();
}
}
@Override
public void actionPerformed(ActionEvent e) {
Drop();
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton()== MouseEvent.BUTTON1 && Button_Restart ==0){
timer.stop();
enable=false;
int n = JOptionPane.showConfirmDialog(null, "确定重新开始游戏?", "提示",JOptionPane.YES_NO_OPTION);
if(n==0){restart();}
else {
timer.restart();
enable=true;
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int s=e.getKeyCode();
switch (s){
case 37:Left();break;
case 38:Up();break;
case 39:Right();break;
case 40:Down();break;
case 32:DropDown();break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
int x=e.getX();
int y=e.getY();
if(checkInRestart(x,y)){
Button_Restart =0;}
else {
Button_Restart =1;}
repaint(550,800,100,40);
}
}
class TYPE{
static int [][]A1={
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0}};
static int [][]AI1={
{0,5},
{1,5},
{2,5},
{3,5}};//T1
static int [][]A2={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,0,0,0}};
static int [][]AI2={
{3,3},
{3,4},
{3,5},
{3,6}};//T2
static int [][]B1={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0}};
static int [][]BI1={
{1,5},
{2,4},
{2,5},
{3,4}};//T3
static int [][]B2={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0}};
static int [][]BI2={
{2,3},
{2,4},
{3,4},
{3,5}};//T4
static int [][]C1={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0}};
static int [][]CI1={
{1,4},
{2,4},
{2,5},
{3,5}};//T5
static int [][]C2={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,0,0,0},
{0,0,0,0,1,1,0,0,0,0}};
static int [][]CI2={
{2,5},
{2,6},
{3,4},
{3,5}};//T6
static int [][]D1={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0}};
static int [][]DI1={
{1,5},
{2,5},
{3,4},
{3,5}};//T7
static int [][]D2={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0}};
static int [][]DI2={
{2,4},
{3,4},
{3,5},
{3,6}};//T8
static int [][]D3={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0}};
static int [][]DI3={
{1,4},
{1,5},
{2,4},
{3,4}};//T9
static int [][]D4={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,0,0,1,0,0,0}};
static int [][]DI4={
{2,4},
{2,5},
{2,6},
{3,6}};//T10
static int [][]E1={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0}};
static int [][]EI1={
{1,4},
{2,4},
{3,4},
{3,5}};//T11
static int [][]E2={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,1,1,1,0,0,0}};
static int [][]EI2={
{2,6},
{3,4},
{3,5},
{3,6}};//T12
static int [][]E3={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0}};
static int [][]EI3={
{1,4},
{1,5},
{2,5},
{3,5}};//T13
static int [][]E4={
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,1,0,0,0,0,0}};
static int [][]EI4={
{2,4},
{2,5},
{2,6},
{3,4}};//T14
static int [][]F= {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0}};
static int [][]FI={
{2,4},
{2,5},
{3,4},
{3,5}};//T15
static int [][]G1= {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0}};
static int [][]GI1={
{2,5},
{3,4},
{3,5},
{3,6}};//T16
static int [][]G2= {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0}};
static int [][]GI2={
{1,4},
{2,4},
{2,5},
{3,4}};//T17
static int [][]G3= {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,0,1,0,0,0,0}};
static int [][]GI3={
{2,4},
{2,5},
{2,6},
{3,5}};//T18
static int [][]G4= {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0}};
static int [][]GI4={
{1,5},
{2,4},
{2,5},
{3,5}};//T19
}
重制版(代码全部重写,简单明了) <800行
package UIDesign.UI2A;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
class Settings{
public static boolean IsDebug=false;//设置版本为发行版本(false)还是开发版本(true),主要区别在于寻找图片路径不同
}
public class Game {
public static void main(String []args){
new GameFrame("俄罗斯方块-得分:0");
}
}
class GameFrame extends JFrame implements KeyListener{
public GamePanel gp;
public GameFrame(String title){
if(Settings.IsDebug){
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\green.jpg"));
}
else{
setIconImage(Toolkit.getDefaultToolkit().getImage("green.jpg"));
}
setTitle(title);//设置标题
setSize(800,950);//设置尺寸
setLocationRelativeTo(null);//使之默认显示在屏幕中心
setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE);//设置当窗体关闭时发生的事情(退出程序)
setResizable(false);//使窗口大小不会被改变
gp=new GamePanel(this);//添加游戏面板类
add(gp);
addKeyListener(this);//添加KeyListener接口
setVisible(true);//设置窗体可见性true
//game start:
gp.GameStart();//------游戏开始入口------
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int s=e.getKeyCode();
switch (s){
case 37:
gp.gc.BlockMoveLeft();//左移方块
break;
case 39:
gp.gc.BlockMoveRight();//右移方块
break;
case 38:
gp.gc.BlockChange();//旋转方块
break;
case 40:
gp.gc.BlockMoveDown();//下移方块
break;
case 32:
gp.gc.BlockDropDown();//坠落方块
break;
case 80:
gp.gc.parse();//暂停/开始
break;
case 82:
gp.gc.restart();//重新开始
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
class GamePanel extends JPanel implements Runnable,ActionListener{
public GameContent gc;
public GameFrame gf;
public Thread t;
private Image IB;
private Graphics GB;
Image empty,blue,yellow,purple,red,green;//声明所需要的图片资源
public GamePanel(GameFrame gf){
//init
this.gf=gf;
if(Settings.IsDebug) {//设置图片资源
empty = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\empty.jpg");
blue = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\blue.jpg");
yellow = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\yellow.jpg");
purple = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\purple.jpg");
red = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\red.jpg");
green = getToolkit().getImage("C:\\Users\\86158\\Desktop\\use\\n02\\green.jpg");
}
else{empty = getToolkit().getImage("empty.jpg");
blue = getToolkit().getImage("blue.jpg");
yellow = getToolkit().getImage("yellow.jpg");
purple = getToolkit().getImage("purple.jpg");
red = getToolkit().getImage("red.jpg");
green = getToolkit().getImage("green.jpg");
}
//UI
setLayout(null);//设置空布局
//GameStart
gc=new GameContent();//初始化游戏逻辑类GameContent
}
public void GameStart(){//游戏界面绘制开始
t=new Thread(this);
t.start();
gc.GameStart();//游戏逻辑开始
}
public void paint(Graphics g){
if(IB ==null){
IB =createImage(this.getSize().width,this.getSize().height);
GB = IB.getGraphics();
}
GB.setColor(getBackground());
GB.fillRect(0,0,this.getSize().width,this.getSize().height);
//绘制主要游戏图形
for(int y=4;y<GameContent.Height+GameContent.ReservedSpaceHeight;y++){
for(int x=0;x<GameContent.Width;x++){
if(gc.game[y][x]!=null){
GB.drawImage(GetImg(gc.game[y][x].color),50*x,50*y-200,this);
}
else{
GB.drawImage(GetImg(0),50*x,50*y-200,this);
}
}
}
//移动中的方块将会覆盖上面的固定方块
for(int i=0;i<4;i++){
GB.drawImage(GetImg(gc.BlockStatus[i].color),gc.BlockStatus[i].x*50,gc.BlockStatus[i].y*50-200,this);
}
//绘制队列1
for(int y=0;y<=3;y++){
for(int x=0;x<=3;x++){
GB.drawImage(GetImg(0),x*50+550,y*50,this);
}
}
int [][]S1=gc.GetBlock(gc.BlockQueue[0][0]);
for(int i=0;i<=3;i++){
GB.drawImage(GetImg(gc.BlockQueue[0][1]),(S1[i][1]-3)*50+550,S1[i][0]*50,this);
}
//绘制队列2
for(int y=0;y<=3;y++){
for(int x=0;x<=3;x++){
GB.drawImage(GetImg(0),x*50+550,250+y*50,this);
}
}
int [][]S2=gc.GetBlock(gc.BlockQueue[1][0]);
for(int i=0;i<=3;i++){
GB.drawImage(GetImg(gc.BlockQueue[1][1]),(S2[i][1]-3)*50+550,250+S2[i][0]*50,this);
}
//绘制队列3
for(int y=0;y<=3;y++){
for(int x=0;x<=3;x++){
GB.drawImage(GetImg(0),x*50+550,500+y*50,this);
}
}
int [][]S3=gc.GetBlock(gc.BlockQueue[2][0]);
for(int i=0;i<=3;i++){
GB.drawImage(GetImg(gc.BlockQueue[2][1]),(S3[i][1]-3)*50+550,500+S3[i][0]*50,this);
}
//显示更新
g.drawImage(IB,0,0,this);
}
private Image GetImg(int color){//返回相应图片资源
switch (color){
case 1:
return red;
case 2:
return yellow;
case 3:
return green;
case 4:
return blue;
case 5:
return purple;
default:
return empty;
}
}
@Override
public void run() {//UI更新
while(true){
repaint();
if(!gc.IsGameOver) {
gf.setTitle("俄罗斯方块-得分:" + gc.Score);
}
else{
gf.setTitle("俄罗斯方块-游戏结束-最终得分:" + gc.Score+"-"+"按下R键以重新开始");
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
class GameContent implements ActionListener{
//Game Parameters:
public static final int Width= 10;//游戏宽度,如不懂其影响,不要更改!
public static final int Height= 18;//游戏高度,如不懂其影响,不要更改!
public static final int ReservedSpaceHeight=4;//预留高度,用来产生方块,如不懂其影响,不要更改!
//Game Setting:
//Basic Properties:
public Block [][]game;//22*10方块数组
public Timer timer;//定时器
//Run Dependencies:
public Block [] BlockStatus;//正在移动中的方块的属性
public int [][] BlockQueue=new int[3][2];//队列方块属性
public int Score;//得分
public boolean IsParse;//是否暂停
public boolean IsGameOver;
public int BlockType;//正在移动中的方块属性
public GameContent(){
}
public void GameStart(){//开始游戏
init();
timer =new Timer(1000,this);
timer.start();
IsParse=false;
}
public void restart(){//重新开始游戏
timer.stop();
init();
timer.restart();
IsParse=false;
}
public void parse(){//暂停/开始游戏
if(!IsParse){
timer.stop();
}else{
timer.restart();
}
IsParse=!IsParse;
}
public void BlockMoveLeft(){
if(IsParse){
return;
}
Block []ReadyBlockStatus=CalculateSum(BlockStatus,GetMoveChange(20));//计算移动后的方块的变化,CalculateSum(目前的位置,位移)
if(CheckOccupancy(ReadyBlockStatus)){//检测边界碰撞和方块碰撞
BlockStatus=ReadyBlockStatus;//如果无碰撞那么更新移动中的方块状态
}
}
public void BlockMoveRight(){
if(IsParse){
return;
}
Block []ReadyBlockStatus=CalculateSum(BlockStatus,GetMoveChange(21));//同上
if(CheckOccupancy(ReadyBlockStatus)){
BlockStatus=ReadyBlockStatus;
}
}
public void BlockMoveDown(){
if(IsParse){
return;
}
Block []ReadyBlockStatus=CalculateSum(BlockStatus,GetMoveChange(22));//同上
if(CheckOccupancy(ReadyBlockStatus)){
BlockStatus=ReadyBlockStatus;
}
else{//如果检测到碰撞
for (Block blockStatus : BlockStatus) {
game[blockStatus.y][blockStatus.x] = blockStatus;//将移动中的方块固定
}
Destroy();//检测并摧毁可以消除的行,并计分
CheckGameStatus();//检测游戏状态,是否游戏结束?
CreateNewBlock();//产生新的方块
}
}
public void BlockChange(){
if(IsParse){
return;
}
Block []ReadyBlockStatus=CalculateSum(BlockStatus,GetMoveChange(BlockType));//同上
if(CheckOccupancy(ReadyBlockStatus)){
BlockStatus=ReadyBlockStatus;
BlockType= TypeChange(BlockType);//更新旋转后的形状的编号
}
}
public void BlockDropDown(){
if(IsParse){
return;
}
while(true){//循环下落,直到检测到碰撞
Block []ReadyBlockStatus=CalculateSum(BlockStatus,GetMoveChange(22));//同上
if(CheckOccupancy(ReadyBlockStatus)){
BlockStatus=ReadyBlockStatus;
}
else{
for (Block blockStatus : BlockStatus) {
game[blockStatus.y][blockStatus.x] = blockStatus;
}
Destroy();//同上
CreateNewBlock();
break;
}
}
}
public int [][] GetBlock(int type){//返回随机产生的方块数据
switch (type) {
case 1:
return Units.BA1;
case 2:
return Units.BA2;
case 3:
return Units.BB1;
case 4:
return Units.BB2;
case 5:
return Units.BB3;
case 6:
return Units.BB4;
case 7:
return Units.BC1;
case 8:
return Units.BC2;
case 9:
return Units.BC3;
case 10:
return Units.BC4;
case 11:
return Units.BD1;
case 12:
return Units.BD2;
case 13:
return Units.BD3;
case 14:
return Units.BD4;
case 15:
return Units.BE1;
case 16:
return Units.BE2;
case 17:
return Units.BF1;
case 18:
return Units.BF2;
default:
return Units.BG;
}
}
private void CreateNewBlock(){
Random a =new Random();//随机方块形状,颜色数据
int i=a.nextInt(19)+1;
int e=a.nextInt(5)+1;//Red,yellow,green,blue,purple
int [][]New=GetBlock(BlockQueue[0][0]);//将队列1方块取出
for(int ii=0;ii<New.length;ii++){
BlockStatus[ii]=new Block(New[ii][1],New[ii][0],BlockQueue[0][1]);
}
BlockType=BlockQueue[0][0];
for(int u=0;u<=1;u++){//队列前进
System.arraycopy(BlockQueue[u + 1], 0, BlockQueue[u], 0, 2);
}
//刷新队列尾部
BlockQueue[2][0]=i;
BlockQueue[2][1]=e;
}
private Block[] CalculateSum(Block [] main,int [][] change){
Block []cp=new Block[4];//声明新的方块
for(int i=0;i<=3;i++){
cp[i]=new Block(main[i]);//复制方块
}
for(int i=0;i<=3;i++){//将方块位置数据和位移数据计算和,得出移动后的位置
cp[i].y+=change[i][0];
cp[i].x+=change[i][1];
}
return cp;//返回移动后的状态
}
private int [][] GetMoveChange(int type){//获取相应的方块变化状况
switch (type){
case 1:
return Units.SBA1;//方块1变化
case 2:
return Units.SBA2;//方块2变化
case 3:
return Units.SBB1;//以此类推
case 4:
return Units.SBB2;//...
case 5:
return Units.SBB3;
case 6:
return Units.SBB4;
case 7:
return Units.SBC1;
case 8:
return Units.SBC2;
case 9:
return Units.SBC3;
case 10:
return Units.SBC4;
case 11:
return Units.SBD1;
case 12:
return Units.SBD2;
case 13:
return Units.SBD3;
case 14:
return Units.SBD4;
case 15:
return Units.SBE1;
case 16:
return Units.SBE2;
case 17:
return Units.SBF1;
case 18:
return Units.SBF2;
case 19:
return Units.SBG;
case 20:
return Units.SML;//左移
case 21:
return Units.SMR;//右移
case 22:
return Units.SMD;//下移
default:
return null;
}
}
private boolean CheckOccupancy(Block [] bl){
for(Block a:bl){
if(!(a.x>=0&&a.x<10&&a.y>=0&&a.y<22)){//检测碰撞,如果超出边界,那么返回false
return false;
}
if(game[a.y][a.x]!=null){//检测碰撞,如果位置和已经固定的方块冲突,返回false
return false;
}
}
return true;//如果没有碰撞,返回true
}
private int TypeChange(int blockType) {//更新方块变化之后的类型编号
switch (blockType){
case 1:return 2;
case 2:return 1;
case 3:return 4;
case 4:return 5;
case 5:return 6;
case 6:return 3;
case 7:return 8;
case 8:return 9;
case 9:return 10;
case 10:return 7;
case 11:return 12;
case 12:return 13;
case 13:return 14;
case 14:return 11;
case 15:return 16;
case 16:return 15;
case 17:return 18;
case 18:return 17;
case 19:return 19;
default:return 0;
}
}
private void Destroy(){//摧毁可以消除的行
int line=0;
for(int i=Height+ReservedSpaceHeight-1;i>ReservedSpaceHeight;i--){//从下往上检测
for(int f=0;f<Width;f++){
if(game[i][f]==null){
break;//如果这行有空格,那么结束循环,继续向上寻找
}
if(f==Width-1){//如果这行没有空格
for(int h=0;h<Width;h++){
game[i][h]=null;//消除行
}
for(int h=i;h>ReservedSpaceHeight;h--){
for(int v=0;v<Width;v++){
game[h][v]=game[h-1][v];//平移方块
game[h-1][v]=null;//消除平移源方块
}
}
i++;//如果此行被消除,那么将会使i加一,从而重新检测此行的可摧毁情况
line++;//本次消除的行数
}
}
}
switch (line){
case 1: Score+=10;//一行+10分
break;
case 2: Score+=30;//二行+30分
break;
case 3: Score+=60;//..........
break;
case 4: Score+=100;
break;
}
}
private void CheckGameStatus(){
for(int y=0;y<=3;y++){
for(int x=0;x<=9;x++){
if(game[y][x]!=null){
parse();
IsGameOver=true;
}
}
}
}
private void init(){//初始化游戏
game=new Block[Height+ReservedSpaceHeight][Width];//清空方块
BlockStatus= new Block[4];//初始化移动中的方块
Random R=new Random();
for(int i=0;i<3;i++){//填充方块队列
BlockQueue[i][0]=R.nextInt(19)+1;
BlockQueue[i][1]=R.nextInt(5)+1;
}
CreateNewBlock();//产生方块,移动队列
Score=0;//刷新分数
IsGameOver=false;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==timer){
BlockMoveDown();//计时器
}
}
}
class Block{
//记录单个方块的数据
public int color;
public int x;
public int y;
public Block(int x,int y,int color){//构造1,用全部数据进行构造
this.x=x;
this.y=y;
this.color=color;
}
public Block(Block bl){//构造2,用一个已经存在的方块进行构造,用来复制方块
this.x=bl.x;
this.y=bl.y;
this.color=bl.color;
}
}
class Units{
//此为方块1的产生位置数据
public static int [][]BA1={// I-0
{0,4},
{1,4},
{2,4},
{3,4}
};
//此为该方块1的旋转的数据
public static int [][]SBA1={// I-0
{2,-1},
{1,0},
{0,1},
{-1,2}
};
//>1
//方块2开始,以此类推
public static int [][]BA2={// I-90
{3,3},
{3,4},
{3,5},
{3,6}
};
public static int [][]SBA2={// I-90
{-2,1},
{-1,0},
{0,-1},
{1,-2}
};
//>2
public static int [][]BB1={// L-0
{1,4},
{2,4},
{3,4},
{3,5}
};
public static int [][]SBB1={// L-0
{2,-1},
{1,0},
{0,1},
{-1,0}
};
//>3
public static int [][]BB2={// L-90
{3,3},
{3,4},
{3,5},
{2,5}
};
public static int [][]SBB2={// L-90
{1,2},
{0,1},
{-1,0},
{0,-1}
};
//>4
public static int [][]BB3={//L-180
{3,5},
{2,5},
{1,5},
{1,4}
};
public static int [][]SBB3={//L-180
{-2,1},
{-1,0},
{0,-1},
{1,0}
};
//>5
public static int [][]BB4={ //L-270
{2,6},
{2,5},
{2,4},
{3,4}
};
public static int [][]SBB4={ //L-270
{-1,-2},
{0,-1},
{1,0},
{0,1}
};
//>6
public static int [][]BC1={//RL-0
{1,5},
{2,5},
{3,5},
{3,4}
};
public static int [][]SBC1={//RL-0
{1,-2},
{0,-1},
{-1,0},
{0,1}
};
//>7
public static int [][]BC2={//RL-90
{2,3},
{2,4},
{2,5},
{3,5}
};
public static int [][]SBC2={//RL-90
{2,1},
{1,0},
{0,-1},
{-1,0}
};
//>8
public static int [][]BC3={//RL-180
{3,4},
{2,4},
{1,4},
{1,5}
};
public static int [][]SBC3={//RL-180
{-1,2},
{0,1},
{1,0},
{0,-1}
};
//>9
public static int [][]BC4={//RL-270
{3,6},
{3,5},
{3,4},
{2,4}
};
public static int [][]SBC4={//RL-270
{-2,-1},
{-1,0},
{0,1},
{1,0}
};
//>10
public static int [][]BD1={//E-0
{1,4},
{2,4},
{3,4},
{2,5}
};
public static int [][]SBD1={//E-0
{1,-1},
{0,0},
{-1,1},
{-1,-1}
};
//>11
public static int [][]BD2={//E-90
{3,3},
{3,4},
{3,5},
{2,4}
};
public static int [][]SBD2={//E-90
{1,1},
{0,0},
{-1,-1},
{1,-1}
};
//>12
public static int [][]BD3={//E-180
{3,4},
{2,4},
{1,4},
{2,3}
};
public static int [][]SBD3={//E-180
{-1,1},
{0,0},
{1,-1},
{1,1}
};
//>13
public static int [][]BD4={//E-270
{2,5},
{2,4},
{2,3},
{3,4}
};
public static int [][]SBD4={//E-270
{-1,-1},
{0,0},
{1,1},
{-1,1}
};
//>14
public static int [][]BE1={//Z-0
{2,3},
{2,4},
{3,4},
{3,5}
};
public static int [][]SBE1={//Z-0
{0,2},
{1,1},
{0,0},
{1,-1}
};
//>15
public static int [][]BE2={//Z-90
{1,5},
{2,5},
{2,4},
{3,4}
};
public static int [][]SBE2={//Z-90
{0,-2},
{-1,-1},
{0,0},
{-1,1}
};
//>16
public static int [][]BF1={//RZ-0
{2,5},
{2,4},
{3,4},
{3,3}
};
public static int [][]SBF1={//RZ-0
{0,-2},
{1,-1},
{0,0},
{1,1}
};
//>17
public static int [][]BF2={//RZ-90
{1,3},
{2,3},
{2,4},
{3,4}
};
public static int [][]SBF2={//RZ-90
{0,2},
{-1,1},
{0,0},
{-1,-1}
};
//>18
public static int [][]BG={//O-0
{1,4},
{1,5},
{2,4},
{2,5}
};
public static int [][]SBG={//O-0
{0,0},
{0,0},
{0,0},
{0,0}
};
//>19
public static int [][]SML={//左移的位移数据
{0,-1},
{0,-1},
{0,-1},
{0,-1}
};
public static int [][]SMR={//右移的位移数据
{0, 1},
{0, 1},
{0, 1},
{0, 1}
};
public static int [][]SMD={//下移的位移数据
{1,0},
{1,0},
{1,0},
{1,0}
};
}
这是已经能正常运行的代码,但是编写以后发现还有一些细节需要改动,如果喜欢,可以点赞,收藏,也可以下载我的资源获取完整的资源(jar+图片,可以直接运行,java程序+全注释,class字节码),下载所需积分非常少,仅仅5积分,算作支持一下作者吧.如有疑问,请私信联系
下载链接为https://download.csdn.net/download/A3010367369/85751265
如需免费获取原版请参考文章https://blog.csdn.net/A3010367369/article/details/122118187
此代码仅供学习参考用,请勿作他用,如需要转载请声明来源