俄罗斯方块源码.docx
- 文档编号:2196269
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:46
- 大小:21.32KB
俄罗斯方块源码.docx
《俄罗斯方块源码.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块源码.docx(46页珍藏版)》请在冰点文库上搜索。
俄罗斯方块源码
俄罗斯方块源码
packagewqn;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.util.Timer;
importjava.util.TimerTask;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
publicclassMainextendsJFrameimplementsKeyListener{
privateintrowCount=20;
privateintcolumnCount=15;
privateintwidth=20;
privateintheight=20;
privatebooleanstart=true;
privatebooleanend=false;
JButtonButton1,Button2,Button3,Button4,Button5,Button6;
JPanelpanel1,panel2;
JLabellabel1,label2,label3;
Graphicsg;
Squaresqu;
ShapeBlockblock=null;
ShapeBlockblock2=null;
SquaregameFieldSquares[][]=newSquare[rowCount][columnCount];
TimergameTimer;
privatelongtimerPeriod=500;
intgrade=0;
privateJLabellabel4;
intxy[][]=newint[4][2];
GameTimerTaskgameTimerTasknew=newGameTimerTask();
publicclassGameTimerTaskextendsTimerTask{//线程,实现方块开始后自行下落
publicvoidrun()
{
if(canStepDown()){block.downBlock();}
else{//在背景二维数组里定义方块
for(inti=0;i { Squares=block.squares[i]; introwIndex=s.getLocationY()/height; intcolumnIndex=s.getLocationX()/width; if (rowIndex>=0&&rowIndex gameFieldSquares[rowIndex][columnIndex]=s; } clearFullRows();//方块满行消除 over(); block=block2; block2.clear2Block(xy); block2=ShapeBlock.returnaobj(); returnxy(block2); block2.setGraphics(panel2.getGraphics()); block2.draw2Block(xy); //block2.clear2Block(xy); block.setGraphics(panel1.getGraphics()); block.drawBlock(); } } } publicMain(){ squ=newSquare(); block=newShapeBlock(); block2=newShapeBlock(); panel1=newJPanel(); panel1.setBounds(20,20,300,400); panel1.setBackground(Color.black); add(panel1); this.setResizable(false); panel2=newJPanel(); panel2.setBounds(350,20,130,130); panel2.setBackground(Color.black); add(panel2); label2=newJLabel("Score: "); //label2.setBounds(450,80,100,50); Fontfont2=newFont("宋体",Font.BOLD,19);//设置个人信息字体 label2.setFont(font2);//为登陆标签添加字体 label2.setForeground(Color.black);//为登陆标签添加字体颜色 label2.setBounds(350,140,100,50); add(label2); label3=newJLabel("0"); label3.setBounds(420,140,100,50); Fontfont3=newFont("宋体",Font.BOLD,20);//设置个人信息字体 label3.setFont(font3);//为登陆标签添加字体 label3.setForeground(Color.red);//为登陆标签添加字体颜色 add(label3); label1=newJLabel("游戏结束! "); label1.setBounds(110,140,300,200); label4=newJLabel("作者: 2012__级计算机系__wqn"); label4.setBounds(13,410,1000,50); Fontfont4=newFont("宋体",Font.BOLD,15);//设置个人信息字体 label4.setFont(font4);//为登陆标签添加字体 label4.setForeground(Color.black);//为登陆标签添加字体颜色 add(label4); Button1=newJButton("开始"); Button1.setBounds(400,190,60,30); Button1.addKeyListener(this); Button1.addMouseListener(newMouseAdapter(){ publicvoidmouseClicked(MouseEvente){ if(start){ block=ShapeBlock.returnaobj(); block.setGraphics(panel1.getGraphics()); block.drawBlock(); block2=ShapeBlock.returnaobj(); returnxy(block2); block2.setGraphics(panel2.getGraphics()); block2.draw2Block(xy); //block2.clear2Block(xy); //Button1.setEnabled(false); gameTimer=newTimer(); gameTimer.schedule(newGameTimerTask(),0,timerPeriod); start=false; Button1.setText("结束"); } else { System.exit(0); } } }); add(Button1); Button2=newJButton("暂停"); Button2.setBounds(400,240,60,30); Button2.addKeyListener(this); Button2.addMouseListener(newMouseAdapter(){ publicvoidmouseClicked(MouseEvente){ if(! end){ if(Button2.getText()=="暂停") { gameTimer.cancel(); Button2.setText("恢复"); } elseif(Button2.getText()=="恢复") { gameTimer=newTimer(); gameTimer.schedule(newGameTimerTask(),0,timerPeriod); Button2.setText("暂停"); } } } }); add(Button2); Button3=newJButton("旋转"); Button3.setBounds(400,290,60,30); Button3.addKeyListener(this); Button3.addMouseListener(newMouseAdapter(){ publicvoidmouseClicked(MouseEvente){ if(! end){ if(Main.this.canrorate()) { block.clearBlock(); block.rotateBlock(); block.drawBlock(); } } } }); add(Button3); Button4=newJButton("左移"); Button4.setBounds(350,340,60,30); Button4.addKeyListener(this); Button4.addMouseListener(newMouseAdapter(){ publicvoidmouseClicked(MouseEvente){ if(! end){ if(canStepLeft()) block.leftBlock(); } } }); add(Button4); Button5=newJButton("右移"); Button5.setBounds(450,340,60,30); Button5.addKeyListener(this); Button5.addMouseListener(newMouseAdapter(){ publicvoidmouseClicked(MouseEvente){ if(! end){ if(canStepRight()) block.rigthtBlock(); } } }); add(Button5); Button6=newJButton("下移"); Button6.setBounds(400,390,60,30); Button6.addKeyListener(this); Button6.addMouseListener(newMouseAdapter(){ publicvoidmouseClicked(MouseEvente){ if(! end){ if(canStepDown()) block.downBlock(); } } }); add(Button6); } publicvoidreturnxy(ShapeBlockblock) { for(inti=0;i<4;i++) { xy[i][0]=block2.squares[i].getLocationX(); xy[i][1]=block2.squares[i].getLocationY(); } } privatebooleancanStepDown() { booleancandown=true; for(inti=0;i { Squares; s=block.squares[i];/*不超下界*/ if(s.getLocationY()>=(rowCount-1)*height) { candown=false; break; } introwIndex=s.getLocationY()/height; intcolumnIndex=s.getLocationX()/width;/*不往下穿越*/ if(rowIndex>=0&&rowIndex if(gameFieldSquares[rowIndex+1][columnIndex]! =null) candown=false; } returncandown; } privatebooleancanStepLeft() { booleancanLeft=true; for(inti=0;i Squares=block.squares[i]; if(s.getLocationX()<=0)/*不超左界*/ { canLeft=false; break; } introwIndex=s.getLocationY()/height; intcolumnIndex=s.getLocationX()/width;/*不往左穿越*/ if(rowIndex>=0&&rowIndex if(gameFieldSquares[rowIndex][columnIndex-1]! =null) canLeft=false; } returncanLeft; } privatebooleancanStepRight(){ booleancanRight=true; for(inti=0;i Squares=block.squares[i];/*不超右界*/ if(s.getLocationX()>=(columnCount-1)*width){ canRight=false; break; } introwIndex=s.getLocationY()/height; intcolumnIndex=s.getLocationX()/width; if(rowIndex>=0&&rowIndex if(gameFieldSquares[rowIndex][columnIndex+1]! =null)/*不往右穿越*/ canRight=false; } returncanRight; } privatebooleancanrorate(){ booleancanRorate=true; for(inti=0;i { Squares; s=block.squares[i];//不超下界 //if(s.getLocationX()>=(columnCount)*width||s.getLocationX()+width<=0||s.getLocationY()>=(rowCount) *height) { introwIndex=s.getLocationY()/height; intcolumnIndex=s.getLocationX()/width;//不往下穿越 //System.out.println(rowIndex+""+columnIndex); if(rowIndex>=0&&rowIndex if(rowIndex>=this.rowCount-1||columnIndex>=this.columnCount-1) break; //System.out.println("2="+rowIndex+""+columnIndex); if(rowIndex>=this.rowCount-1) break; if(gameFieldSquares[rowIndex+1][columnIndex]! =null) canRorate=false; if(columnIndex<=0) break; if(gameFieldSquares[rowIndex][columnIndex-1]! =null) canRorate=false; if(columnIndex>=this.columnCount-1) break; if(gameFieldSquares[rowIndex][columnIndex+1]! =null)//不往右穿越 canRorate=false; } } } block.rotateBlock(); for(inti=0;i Squares=block.squares[i]; if(s.getLocationX()>=(columnCount)*width||s.getLocationX()+width<=0||s.getLocationY()>=(rowCount) *height){ canRorate=false; break; } } for(intk=1;k<=3;k++) block.rotateBlock(); returncanRorate; } privatebooleanfullJudgement(introwIndex) { booleanisFullrow=true; for(intcolumnIndex=0;columnIndex if(gameFieldSquares[rowIndex][columnIndex]==null) isFullrow=false; returnisFullrow; } privatevoidclearRow(introwIndex){ for(intcolumnIndex=0;columnIndex if(this.gameFieldSquares[rowIndex][columnIndex]! =null){ this.gameFieldSquares[rowIndex][columnIndex].clear(); this.gameFieldSquares[rowIndex][columnIndex]=null; } } } privatevoidmoveRowDown(introwIndex){ for(introw=rowIndex;row>0;row--){ for(intcolumnIndex=0;columnIndex if(this.gameFieldSquares[row-1][columnIndex]! =null){ this.gameFieldSquares[row-1][columnIndex].clear(); this.gameFieldSquares[row-1][columnIndex].stepDown(); this.gameFieldSquares[row][columnIndex]=this.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 俄罗斯方块 源码