Java实现俄罗斯方块小游戏源码
程序员文章站
2022-06-16 20:35:28
本文实例为大家分享了java实现俄罗斯方块小游戏的具体代码,供大家参考,具体内容如下一、最终效果二、功能需求1、 在二维平面里面用各种随机产生的方块堆积木,每满一行消去一行,当达到顶部时,游戏结束。2...
本文实例为大家分享了java实现俄罗斯方块小游戏的具体代码,供大家参考,具体内容如下
一、最终效果
二、功能需求
1、 在二维平面里面用各种随机产生的方块堆积木,每满一行消去一行,当达到顶部时,游戏结束。
2、 通过方向键来控制方块转动,左移,右移和直落。
3、 方块下落统一设置蓝色,接触底部变粉色。
4、 计算分数,分数是由方块的类型决定的,每堆积一个方块就把分数累加到总分中。
5、 游戏有开始、重新开始、降低提高级数(速度)、暂停、退出
三、程序实现
这个是最基础的方块素材
package 俄罗斯方块; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; import java.lang.string.*; import java.lang.*; import java.io.*; public class block extends jpanel implements actionlistener,keylistener//应该是继承jpanel { static button but[] = new button[6]; static button nostop = new button("取 消 暂 停"); static label scorelab = new label("分数:"); static label infolab = new label("提示:"); static label speedlab = new label("级数:"); static label scoretex = new label("0"); static label infotex = new label(" "); static label speedtex = new label("1"); static jframe jf = new jframe(); static mytimer timer; static imageicon icon=new imageicon("resource/block.jpg"); static jmenubar mb = new jmenubar(); static jmenu menu0 = new jmenu("游 戏 "); static jmenu menu1 = new jmenu("帮 助 "); static jmenuitem mi0 = new jmenuitem("新 游 戏"); static jmenuitem mi1 = new jmenuitem("退 出"); static jmenuitem mi1_0 = new jmenuitem("关 于"); static jdialog dlg_1; static jtextarea dlg_1_text = new jtextarea(); static int startsign = 0;//游戏开始标志 0 未开始 1 开始 2 暂停 static string butlab[] = {"开 始 游 戏","重 新 开 始","降 低 级 数","提 高 级 数","游 戏 暂 停","退 出 游 戏"}; static int game_body[][] = new int[19][10]; static int game_sign_x[] = new int[4];//用于记录4个方格的水平位置 static int game_sign_y[] = new int[4];//用于记录4个方格的垂直位置 static boolean downsign = false;//是否落下 static int blocknumber = 1;//砖块的编号 static int gamescore = 0;//游戏分数 static int speedmark = 1; public static void main(string args[]) { block myblock = new block(); mb.add(menu0); mb.add(menu1); menu0.add(mi0); menu0.add(mi1); menu1.add(mi1_0); jf.setjmenubar(mb); myblock.init(); jf.add(myblock); jf.setsize(565,501); jf.setresizable(false); jf.settitle("俄罗斯方块"); jf.seticonimage(icon.getimage()); jf.setlocation(200,100); jf.show(); timer = new mytimer(myblock); //启动线程 timer.setdaemon(true); timer.start(); timer.suspend(); } public void init() { setlayout(null); for(int i = 0;i < 6;i++) { but[i] = new button(butlab[i]); add(but[i]); but[i].addactionlistener(this); but[i].addkeylistener(this); but[i].setbounds(360,(240 + 30 * i),160,25); } add(scorelab); add(scoretex); add(speedlab); add(speedtex); add(infolab); add(infotex); add(scorelab); scorelab.setbounds(320,15,30,20); scoretex.setbounds(360,15,160,20); scoretex.setbackground(color.white); speedlab.setbounds(320,45,30,20); speedtex.setbounds(360,45,160,20); speedtex.setbackground(color.white); but[1].setenabled(false); but[4].setenabled(false); infolab.setbounds(320,75,30,20); infotex.setbounds(360,75,160,20); infotex.setbackground(color.white); nostop.setbounds(360,360,160,25); nostop.addactionlistener(this); nostop.addkeylistener(this); mi0.addactionlistener(this); mi1.addactionlistener(this); mi1_0.addactionlistener(this); num_csh_game(); rand_block(); } public void actionperformed(actionevent e) { if(e.getsource() == but[0])//开始游戏 { startsign = 1; infotex.settext("游戏已经开始!"); but[0].setenabled(false); but[1].setenabled(true); but[4].setenabled(true); timer.resume(); } if(e.getsource() == but[1]||e.getsource() == mi0)//重新开始游戏 { startsign = 0; gamescore = 0; timer.suspend(); num_csh_restart(); repaint(); rand_block(); scoretex.settext("0"); infotex.settext("新游戏!"); but[0].setenabled(true); but[1].setenabled(false); but[4].setenabled(false); } if(e.getsource() == but[2])//降低级数 { infotex.settext("降低级数!"); speedmark--; if(speedmark <= 1) { speedmark = 1; infotex.settext("已经是最低级数!"); } speedtex.settext(speedmark + ""); } if(e.getsource() == but[3])//提高级数 { infotex.settext("提高级数!"); speedmark++; if(speedmark >= 9) { speedmark = 9; infotex.settext("已经是*数!"); } speedtex.settext(speedmark + ""); } if(e.getsource() == but[4])//游戏暂停 { this.add(nostop); this.remove(but[4]); infotex.settext("游戏暂停!"); timer.suspend(); } if(e.getsource() == nostop)//取消暂停 { this.remove(nostop); this.add(but[4]); infotex.settext("继续游戏!"); timer.resume(); } if(e.getsource() == but[5]||e.getsource() == mi1)//退出游戏 { jf.dispose(); } if(e.getsource() == mi1_0)//退出游戏 { dlg_1 = new jdialog(jf,"关 于"); try{ fileinputstream io = new fileinputstream("resource/guanyu.txt");//得到路径 byte a[] = new byte[io.available()]; io.read(a); io.close(); string str = new string(a); dlg_1_text.settext(str); } catch(exception g){} dlg_1_text.seteditable(false); dlg_1.add(dlg_1_text); dlg_1.pack(); dlg_1.setresizable(false); dlg_1.setsize(200, 120); dlg_1.setlocation(400, 240); dlg_1.show(); } } public void rand_block()//随机产生砖块 { int num; num = (int)(math.random() * 6) + 1;//产生0~6之间的随机数 blocknumber = num; switch(blocknumber) { case 1: block1(); blocknumber = 1; break; case 2: block2(); blocknumber = 2; break; case 3: block3(); blocknumber = 3; break; case 4: block4(); blocknumber = 4; break; case 5: block5(); blocknumber = 5; break; case 6: block6(); blocknumber = 6; break; case 7: block7(); blocknumber = 7; break; } } public void change_body(int blocknumber)//改变砖块状态 { dingwei(); if(blocknumber == 1&&downsign == false)//变换长条2种情况 { if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 16)//说明长条是横着的 { if(game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] != 2) { num_csh_game(); game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] + 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] >= 1&&game_sign_x[3] <= 7)//说明长条是竖着的 { if(game_body[game_sign_y[0] + 1][game_sign_x[0]-1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] != 2) { num_csh_game(); game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]]=1; game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] = 1; infotex.settext("游戏进行中!"); repaint(); } } } if(blocknumber == 3&&downsign == false)//变换转弯1有4种情况 { if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] >= 1) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2) { num_csh_game(); game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] <= 17) { if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2) { num_csh_game(); game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_x[1] == game_sign_x[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[3] <= 8) { if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2) { num_csh_game(); game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] == game_sign_x[3]) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2) { num_csh_game(); game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1; infotex.settext("游戏进行中!"); repaint(); } } } if(blocknumber == 4&&downsign == false)//变换转弯2有4种情况 { if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[3] <= 7) { if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2) { num_csh_game(); game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[1] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[2]) { if(game_body[game_sign_y[1]][game_sign_x[1] + 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2) { num_csh_game(); game_body[game_sign_y[0]][game_sign_x[0]] = 1; game_body[game_sign_y[1]][game_sign_x[1] + 2] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] >= 2) { if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2) { num_csh_game(); game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[0] == game_sign_y[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] <= 16) { if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] - 2] != 2) { num_csh_game(); game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1; game_body[game_sign_y[2]][game_sign_x[2] - 2] = 1; game_body[game_sign_y[3]][game_sign_x[3]] = 1; infotex.settext("游戏进行中!"); repaint(); } } } if(blocknumber == 5&&downsign == false)//变换转弯3有4种情况 { if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[2] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[1] >= 2) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] - 2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2) { num_csh_game(); game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[1]&&game_sign_y[0] <= 16) { if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2) { num_csh_game(); game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[2] == game_sign_y[3]) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] + 2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2) { num_csh_game(); game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2]][game_sign_x[2] + 2] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[2] == game_sign_x[3]) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2) { num_csh_game(); game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1; infotex.settext("游戏进行中!"); repaint(); } } } if(blocknumber == 6&&downsign == false)//变换两层砖块1的2种情况 { if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] >= 2) { if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] -1 ] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2) { num_csh_game(); game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 17) { if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] != 2) { num_csh_game(); game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] = 1; infotex.settext("游戏进行中!"); repaint(); } } } if(blocknumber == 7&&downsign == false)//变换两层砖块2的2种情况 { if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] <= 16) { if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2) { num_csh_game(); game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1; game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1; infotex.settext("游戏进行中!"); repaint(); } } if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[2] <= 17) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] - 2] != 2&&game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] != 2) { num_csh_game(); game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1; game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] = 1; game_body[game_sign_y[3]][game_sign_x[3]] = 1; infotex.settext("游戏进行中!"); repaint(); } } } } public void num_csh_game()//数组清零 { for(int i = 0;i < 19;i++) { for(int j = 0;j < 10;j++) { if(game_body[i][j] == 2) { game_body[i][j] = 2; } else { game_body[i][j] = 0; } } } } public void num_csh_restart()//重新开始时数组清零 { for(int i = 0;i < 19;i++) { for(int j = 0;j < 10;j++) { game_body[i][j] = 0; } } } public void keytyped(keyevent e){} public void keypressed(keyevent e) { if(e.getkeycode() == keyevent.vk_down&&startsign == 1)//处理下键 { this.down(); } if(e.getkeycode() == keyevent.vk_left&&startsign == 1)//处理左键 { this.left(); } if(e.getkeycode() == keyevent.vk_right&&startsign == 1)//处理右键 { this.right(); } if(e.getkeycode() == keyevent.vk_up&&startsign == 1)//处理上键转换 { this.change_body(blocknumber); } if(startsign == 0) { infotex.settext("游戏未开始或已结束!"); } } public void keyreleased(keyevent e){} public void paint(graphics g) { g.setcolor(color.black); g.fill3drect(0,0,300,450,true); for(int i = 0;i < 19;i++) { for(int j = 0;j < 10;j++) { if(game_body[i][j] == 1) { g.setcolor(color.blue); g.fill3drect(30*j,30*(i-4),30,30,true); } if(game_body[i][j] == 2) { g.setcolor(color.magenta); g.fill3drect(30*j,30*(i-4),30,30,true); } } } } public void left()//向左移动 { int sign = 0; dingwei(); for(int k = 0;k < 4;k++) { if(game_sign_x[k] == 0||game_body[game_sign_y[k]][game_sign_x[k] - 1] == 2) { sign = 1; } } if(sign == 0&&downsign == false) { num_csh_game(); for(int k = 0;k < 4;k++) { game_body[game_sign_y[k]][game_sign_x[k] - 1] = 1; } infotex.settext("向左移动!"); repaint(); } } public void right()//向右移动 { int sign = 0; dingwei(); for(int k = 0;k < 4;k++) { if(game_sign_x[k] == 9||game_body[game_sign_y[k]][game_sign_x[k] + 1] == 2) { sign = 1; } } if(sign == 0&&downsign == false) { num_csh_game(); for(int k = 0;k < 4;k++) { game_body[game_sign_y[k]][game_sign_x[k] + 1] = 1; } infotex.settext("向右移动!"); repaint(); } } public void down()//下落 { int sign = 0; dingwei(); for(int k = 0;k < 4;k++) { if(game_sign_y[k] == 18||game_body[game_sign_y[k] + 1][game_sign_x[k]] == 2) { sign = 1; downsign = true; changecolor(); canceldw(); getscore(); if(game_over() == false) { rand_block(); repaint(); } } } if(sign == 0) { num_csh_game(); for(int k = 0;k < 4;k++) { game_body[game_sign_y[k] + 1][game_sign_x[k]] = 1; } infotex.settext("游戏进行中!"); repaint(); } } public boolean game_over()//判断游戏是否结束 { int sign=0; for(int i = 0;i < 10;i++) { if(game_body[4][i] == 2) { sign = 1; } } if(sign == 1) { infotex.settext("游戏结束!"); changecolor(); repaint(); startsign = 0; timer.suspend(); return true; } else return false; } public void getscore()//满行消除方法 { for(int i = 0;i < 19;i++) { int sign = 0; for(int j = 0;j < 10;j++) { if(game_body[i][j] == 2) { sign++; } } if(sign == 10) { gamescore += 100; scoretex.settext(gamescore+""); infotex.settext("恭喜得分!"); for(int j = i;j >= 1;j--) { for(int k = 0;k < 10;k++) { game_body[j][k] = game_body[j - 1][k]; } } } } } public void changecolor()//给已经落下的块换色 { downsign = false; for(int k = 0;k < 4;k++) { game_body[game_sign_y[k]][game_sign_x[k]] = 2; } } public void dingwei()//确定其位置 { int k = 0; canceldw(); for(int i = 0;i < 19;i++) { for(int j = 0;j < 10;j++) { if(game_body[i][j] == 1) { game_sign_x[k] = j; game_sign_y[k] = i; k++; } } } } public void canceldw()//将定位数组初始化 { for(int k = 0;k < 4;k++) { game_sign_x[k] = 0; game_sign_y[k] = 0; } } public void block1()//长条 { game_body[0][4] = 1; game_body[1][4] = 1; game_body[2][4] = 1; game_body[3][4] = 1; } public void block2()//正方形 { game_body[3][4] = 1; game_body[2][4] = 1; game_body[3][5] = 1; game_body[2][5] = 1; } public void block3()//3加1(下) { game_body[1][4] = 1; game_body[2][4] = 1; game_body[3][4] = 1; game_body[3][5] = 1; } public void block4()//3加1(中) { game_body[1][4] = 1; game_body[2][4] = 1; game_body[3][4] = 1; game_body[2][5] = 1; } public void block5()//3加1(上) { game_body[1][4] = 1; game_body[2][4] = 1; game_body[3][4] = 1; game_body[1][5] = 1; } public void block6()//转折1 { game_body[1][5] = 1; game_body[2][5] = 1; game_body[2][4] = 1; game_body[3][4] = 1; } public void block7()//转折2 { game_body[1][4] = 1; game_body[2][4] = 1; game_body[2][5] = 1; game_body[3][5] = 1; } } //定时线程 class mytimer extends thread { block myblock; public mytimer(block myblock) { this.myblock = myblock; } public void run() { while(myblock.startsign == 1) { try{ sleep((10-myblock.speedmark + 1)*100); myblock.down(); } catch(interruptedexception e){} } } }
备注:上键变换方向
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。