Block.docx
- 文档编号:16045045
- 上传时间:2023-07-10
- 格式:DOCX
- 页数:34
- 大小:20.68KB
Block.docx
《Block.docx》由会员分享,可在线阅读,更多相关《Block.docx(34页珍藏版)》请在冰点文库上搜索。
Block
packageday;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.Random;
importjavax.swing.*;
publicclassBlockextendsJPanelimplementsActionListener,KeyListener//继承动作监听器类和键盘监听器类
{
privatestaticfinallongserialVersionUID=1L;
staticButtonbut[]=newButton[6];
staticButtonnoStop=newButton("取消暂停");
staticLabelscoreLab=newLabel("分数:
");
staticLabelinfoLab=newLabel("提示:
");
staticLabelspeedLab=newLabel("级数:
");
staticLabelscoreTex=newLabel("0");
staticLabelinfoTex=newLabel("游戏还未开始!
");
staticLabelspeedTex=newLabel("1");
staticJFramejf=newJFrame();
staticMyTimertimer;
staticJMenumenu0=newJMenu("游戏");
staticJMenumenu1=newJMenu("帮助");
staticJMenuItemmi0=newJMenuItem("开始游戏");
staticJMenuItemmi0_1=newJMenuItem("退出游戏");
staticJMenuItemmi1_0=newJMenuItem("关于");
staticJMenuBarmb=newJMenuBar();
staticintstartSign=0;//游戏开始标志0未开始1开始2暂停
staticStringbutLab[]={"开始游戏","重新开始","降低级数","提高级数","游戏暂停","退出游戏"};
staticintgame_body[][]=newint[19][10];
staticintgame_sign_x[]=newint[4];//用于记录4个方格的水平位置
staticintgame_sign_y[]=newint[4];//用于记录4个方格的垂直位置
staticbooleandownSign=false;//是否落下
staticintblockNumber=1;//砖块的编号
staticintgameScore=0;//游戏分数
staticintspeedMark=1;
staticintline=0;//消去的行数
//主方法main设置主窗体,以及启动线程
publicstaticvoidmain(Stringargs[])
{
BlockmyBlock=newBlock();
mb.add(menu0);
mb.add(menu1);
menu0.add(mi0);
menu0.add(mi0_1);
menu1.add(mi1_0);
jf.setJMenuBar(mb);
jf.setBackground(Color.gray);
myBlock.init();
jf.add(myBlock);
jf.setSize(565,501);
jf.setResizable(false);
jf.setTitle("俄罗斯方块");
jf.setLocation(200,100);
jf.show();
timer=newMyTimer(myBlock);//启动线程
timer.setDaemon(true);
timer.start();//线程启动
timer.suspend();
}
//初始化方法,主要设置布局(按钮,文本框等),以及添加监听
publicvoidinit()
{
setLayout(null);
for(inti=0;i<6;i++)
{
but[i]=newButton(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);
mi0_1.addActionListener(this);
mi1_0.addActionListener(this);
//数组清零
num_csh_game();
//随机产生方块的方法
rand_block();
}
//监听方法
publicvoidactionPerformed(ActionEvente)
{
//如果按了开始游戏的键,需要重启线程
if(e.getSource()==but[0])
{
//按了开始后把startSign=1
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(startSign==0){//当游戏还未开始时才可以设置级数
if(e.getSource()==but[2])
{
infoTex.setText("降低级数!
");
speedMark--;
if(speedMark<=1)
{
speedMark=1;
infoTex.setText("已经是最低级数!
");
}
speedTex.setText(speedMark+"");
}
}
//如果按了提高级数的键
if(startSign==0){//当游戏还未开始时才可以设置级数
if(e.getSource()==but[3])
{
infoTex.setText("提高级数!
");
speedMark++;
if(speedMark>=10)
{
speedMark=10;
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()==mi0_1)
{
//确认对话框
intval=JOptionPane.showConfirmDialog(jf,"确认离开?
","离开系统",JOptionPane.YES_NO_OPTION);
if(val==JOptionPane.YES_OPTION){
System.exit(0);//离开系统
}
}
if(e.getSource()==mi1_0)//关于
{
JOptionPane.showMessageDialog(jf,"当玩家控制的下落图形填满横向一行所有空格时,本行自动消除,得五分!
");
}
}
//随机产生砖块的方法
publicvoidrand_block()
{
Randomr=newRandom();
intnum;
num=r.nextInt(7);//产生0~6之间的随机数
blockNumber=num;
switch(blockNumber)
{
case0:
block1();blockNumber=1;break;
case1:
block2();blockNumber=2;break;
case2:
block3();blockNumber=3;break;
case3:
block4();blockNumber=4;break;
case4:
block5();blockNumber=5;break;
case5:
block6();blockNumber=6;break;
case6:
block7();blockNumber=7;break;
}
}
publicvoidblock1()//长条
{
game_body[0][4]=1;
game_body[1][4]=1;
game_body[2][4]=1;
game_body[3][4]=1;
}
publicvoidblock2()//田形
{
game_body[3][4]=1;
game_body[2][4]=1;
game_body[3][5]=1;
game_body[2][5]=1;
}
publicvoidblock3()//L型
{
game_body[1][4]=1;
game_body[2][4]=1;
game_body[3][4]=1;
game_body[3][5]=1;
}
publicvoidblock4()//一竖加一横(一横在一竖的右边)
{
game_body[1][4]=1;
game_body[2][4]=1;
game_body[3][4]=1;
game_body[2][5]=1;
}
publicvoidblock5()//7关于Y轴对称后的图形
{
game_body[1][4]=1;
game_body[2][4]=1;
game_body[3][4]=1;
game_body[1][5]=1;
}
publicvoidblock6()//Z逆时针旋转90度后的图形
{
game_body[1][5]=1;
game_body[2][5]=1;
game_body[2][4]=1;
game_body[3][4]=1;
}
publicvoidblock7()//Z关于Y轴对称后的图形
{
game_body[1][4]=1;
game_body[2][4]=1;
game_body[2][5]=1;
game_body[3][5]=1;
}
//画方块的方法
publicvoidpaint(Graphicsg)
{
g.setColor(Color.black);
g.fill3DRect(0,0,300,450,true);
for(inti=0;i<19;i++)
{
for(intj=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);
}
}
}
}
//给已经落下的块换色
publicvoidchangeColor()
{
downSign=false;
for(intk=0;k<4;k++)
{
game_body[game_sign_y[k]][game_sign_x[k]]=2;
}
}
//确定四个方块的X,Y坐标位置
publicvoiddingwei()
{
intk=0;
//将定位数组初始化
cancelDW();
for(inti=0;i<19;i++)
{
for(intj=0;j<10;j++)
{
if(game_body[i][j]==1)
{
game_sign_x[k]=j;
game_sign_y[k]=i;
k++;
}
}
}
}
//将定位数组初始化
publicvoidcancelDW()
{
for(intk=0;k<4;k++)
{
game_sign_x[k]=0;
game_sign_y[k]=0;
}
}
//数组清零,下落后的数组不清除
publicvoidnum_csh_game()
{
for(inti=0;i<19;i++)
{
for(intj=0;j<10;j++)
{
if(game_body[i][j]==2)
{
game_body[i][j]=2;
}
else
{
game_body[i][j]=0;
}
}
}
}
//重新开始时数组清零
publicvoidnum_csh_restart()
{
for(inti=0;i<19;i++)
{
for(intj=0;j<10;j++)
{
game_body[i][j]=0;
}
}
}
//判断游戏是否结束的方法,如果顶层有下落固定后的方块即结束游戏(一个或是有多个都结束)
publicbooleangame_over()
{
intsign=0;
for(inti=0;i<10;i++)
{
if(game_body[4][i]==2)
{
sign=1;
}
}
if(sign==1)
{
infoTex.setText("游戏结束!
");
changeColor();
repaint();
startSign=0;
//暂停线程
timer.suspend();
returntrue;
}
else
returnfalse;
}
publicvoidxiaohang()//满行消除并设置分数的方法
{
for(inti=0;i<19;i++)
{
//判断一行中有几个已经下落的格子
intsign=0;
for(intj=0;j<10;j++)
{
//如果一行中已满10格子把sign++
if(game_body[i][j]==2)
{
sign++;
}
}
//如果一行中满了10个格子,即得5分,如果分数能被100整除,则提高级数
if(sign==10)
{
line++;
//满了10个后消行
for(intj=i;j>=1;j--)
{
for(intk=0;k<10;k++)
{
game_body[j][k]=game_body[j-1][k];
}
}
}
}
}
publicvoidgetScore(){
if(line>=1){
gameScore=gameScore+5*line;
scoreTex.setText(gameScore+"");
infoTex.setText("恭喜得分!
");
line=0;
//如果分数能被100整除,则提高级数,但至多到10级
if(gameScore%5==0){
if(speedMark<=9){
speedMark++;
speedTex.setText(speedMark+"");
num_csh_restart();
line=0;
startSign=1;
infoTex.setText("游戏已经开始!
");
but[0].setEnabled(false);
but[1].setEnabled(true);
but[4].setEnabled(true);
//重启线程
timer.resume();
}
}
}
}
publicvoidkeyTyped(KeyEvente){}
publicvoidkeyReleased(KeyEvente){}
publicvoidkeyPressed(KeyEvente)
{
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)//处理上键转换
{
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Block