俄罗斯方块.docx
- 文档编号:25732332
- 上传时间:2023-06-12
- 格式:DOCX
- 页数:20
- 大小:22.36KB
俄罗斯方块.docx
《俄罗斯方块.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块.docx(20页珍藏版)》请在冰豆网上搜索。
俄罗斯方块
俄罗斯方块一共三个类中间用等号隔开
软件的开发过程
1明确业务需求
用自然语言,将业务功能描述清楚
...
2业务分析
找到有哪些业务对象,和图片的分析
tetris(俄罗斯方块)
|--score累计分数
|--lines销毁的行数
|--Wall(墙20行x10列)
|--20row(行)
|--10colcell(列)
|--tetromino(4格方块,有7种形态)
|--4cell
|--nextOne下一个准备下落的方块
|--4cell
3数据模型,一切业务对象转换为数字表示
场地按照行列划分为20x10格子
格子有属性row,col,color
4类设计
Tetris
|--intscore
|--intlines
|--Cell[20][10]wall
|--Tetrominotetromino
| |--Cell[4]cells
|--row
|--col
|--color
5算法设计,就是如何利用数据的计算实现软件的功能
4格方块的初始形态:
ISZJLTO
就在初始数据的数值状态设计
四格方块的下落计算:
就是将每个格子的row+1
就是将下落的业务功能,转换为数字计算实现
左右移动
下落流程控制:
控制方块下落与墙之间的控制关系
1 合理的文字流程描述
2 分析文字描述中的功能(动作)为方法
3 用流程控制语句连接方法实现功能
4 严格测试结果!
TestCase
左右移动流程控制
分数计算
界面的绘制
键盘事件控制
旋转流程控制
加速下降流程控制
开始流程控制(Timer)
暂停流程控制
继续流程控制
结束流程控制
首先是Cell类,最基本的类包含3个私有属性和get,set方法,重写Object类的toString输出方法,并规定格子所具有的3个移动功能
packagecom.tarena.tetris;
//包:
小写英文字母,域名倒写.项目名
/**
*最小的格子
*/
publicclassCell{
privateintrow;
privateintcol;
privateintcolor;
publicCell(introw,intcol,intcolor){
super();
this.row=row;
this.col=col;
this.color=color;
}
publicintgetCol(){
returncol;
}
publicvoidsetCol(intcol){
this.col=col;
}
publicintgetColor(){
returncolor;
}
publicvoidsetColor(intcolor){
this.color=color;
}
publicintgetRow(){
returnrow;
}
publicvoidsetRow(introw){
this.row=row;
}
publicvoidleft(){
col--;
}
publicvoidright(){
col++;
}
publicvoiddrop(){
row++;
}
publicStringtoString(){
returnrow+","+col;
}
}
===============================================================
packagecom.tarena.tetris;
importjava.util.Arrays;
importjava.util.Timer;
importjava.util.TimerTask;
importjavax.swing.JPanel;//是能够显示的矩形面板区域
importjavax.swing.JFrame;//窗口框
importjavax.swing.border.LineBorder;//实现边框
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyListener;
importjava.awt.event.KeyEvent;
/*
*俄罗斯方块类
*俄罗斯方块扩展了(extends)系统的显示面板,增加了墙和
*正在下落的方块
**/
publicclassTetrisextendsJPanel{
publicstaticfinalintROWS=20;
publicstaticfinalintCOLS=10;
/*代表方块下落着陆的墙*/
privateCell[][]wall=newCell[ROWS][COLS];
/*是正在下落的方块*/
privateTetrominotetromino;
/*下一个进入的方块*/
privateTetrominonextOne;
privatestaticintscore;
privateintlines;
Timertimer;
privatebooleangameOver=false;
privatebooleanpause=false;//暂停
privatestaticfinalint[]SCORE_LEVEL={0,1,4,10,100};
privatestaticfinalGraphicsGraphics=null;
/*销毁(destory)满行*/ // 012 3 4
/*在Tetris中添加方法,检查游戏是否结束*/
publicvoidrotateRightAction(){
tetromino.rotateRight();
if(outOfBounds()||coincide()){
tetromino.rotateLeft();
}
}
publicvoidrotateLeftAction(){
tetromino.rotateLeft();
if(outOfBounds()||coincide()){
tetromino.rotateRight();
}
}
/*在Tetris中添加方法,检查游戏是否结束*/
privatebooleangameOver(){
gameOver=wall[0][4]!
=null;
returngameOver;
}
/*在Tetris中添加方法*/
publicvoidhardDropAction(){
while(canDrop()){
tetromino.softDrop();
}
tetrominoLandToWall();
destroy();
if(gameOver()){
gameOverAction();
}
nextTetromino();
}
publicvoiddestroy(){
intlines=0;//统计本次销毁的行数
for(introw=0;row Cell[]line=wall[row]; if(fullCell(line)){ clearLine(row,wall); lines++;//每消除一行就累计加1 } } score+=SCORE_LEVEL[lines]; this.lines+=lines; } publicstaticvoidclearLine(introw,Cell[][]wall){ for(inti=row;i>1;i--){ System.arraycopy(wall[i-1],0,wall[i],0,wall[i].length); } Arrays.fill(wall[0],null); } publicstaticbooleanfullCell(Cell[]line){ for(intcol=0;col if(line[col]==null){ returnfalse;//找到空格子,这行没有满 } } returntrue; } publicStringtoString(){//显示全部的墙 Stringstr=""; for(introw=0;row Cell[]line=wall[row]; for(intcol=0;col Cellcell=line[col]; if(tetromino.contains(row,col)){ str+=row+","+col+" "; }else{ str=str+cell+""; } } str+="\n"; } returnstr; } /*4格方块下降流程 *方块移动到区域最下方或是着地到其他方块上无法移动时, *就会固定到该处,而新的方法快出现在区域上方开始下落。 *如果能下降就继续下降, *否则就着陆到墙上,并且生成(随机)下一个方块 **/ publicvoidsoftDropAction(){ if(canDrop()){//如果能下降 tetromino.softDrop();//方块继续下降 }else{ tetrominoLandToWall();//着陆到墙上 destroy();// if(gameOver()){ gameOverAction(); } nextTetromino();//生产(随机)下一个方块 } } privatevoidstartGameAction(){ gameOver=false; pause=false; score=0; lines=0; emptyWall(); nextTetromino(); repaint(); timer=newTimer(); timer.schedule(newTimerTask(){ publicvoidrun(){ softDropAction(); repaint(); } },500,500); } privatevoidemptyWall(){ for(introw=0;row Arrays.fill(wall[row],null); } } /*清理游戏结束现场,如: 停止定时器等*/ privatevoidgameOverAction(){ timer.cancel();//停止定时器 } /*检查方块是否能够继续下落: 到底最低部,或者墙上 *的下方有方块,返回false不能下降,返回true可以下降 **/ publicbooleancanDrop(){ //检查到底部 Cell[]cells=tetromino.getCells(); for(Cellcell: cells){ if(cell.getRow()==ROWS-1){ returnfalse; } } //检查墙上下方是否有方块 for(Cellcell: cells){ introw=cell.getRow(); intcol=cell.getCol(); Cellblock=wall[row+1][col]; if(block! =null){ returnfalse; } } returntrue; } /*方块“着陆”到墙上, *取出每个小cell *找到cell的行号row和列号col *将cell放置到wall[row][col]位置上 **/ publicvoidtetrominoLandToWall(){ Cell[]cells=tetromino.getCells(); for(Cellcell: cells){ introw=cell.getRow(); intcol=cell.getCol(); wall[row][col]=cell; } } /*生产(随机)下一个方块 *1下一个变为当前的 *2随机产生下一个 **/ publicvoidnextTetromino(){ if(nextOne==null){//第一次nextOne是null时候先生产一个 nextOne=Tetromino.randomTetromino(); } tetromino=nextOne;//下一个变为当前的 nextOne=Tetromino.randomTetromino();//随机产生下一个 if(tetromino==null){//处理第一次使用时候下一个是null tetromino=Tetromino.randomTetromino(); } } /*以格子为单位左右移动方块 *1)如果遇到左右边界就不能移动了 *2)如果与墙上的格子相撞就不能移动了 *变通为: *1)先将方块左移动, *2)检查(移动结果是否出界),或者(重合) *3)如果检查失败,就右移的回来 * **/ publicvoidmoveLeftAction(){ tetromino.moveLeft(); if(outOfBounds()||coincide()){ tetromino.moveRight(); } } privatebooleanoutOfBounds(){ Cell[]cells=tetromino.getCells(); for(inti=0;i Cellcell=cells[i]; introw=cell.getRow(); intcol=cell.getCol(); if(row==ROWS||col<0||col>=COLS){ returntrue; } } returnfalse; } privatebooleancoincide(){ Cell[]cells=tetromino.getCells(); for(inti=0;i Cellcell=cells[i]; introw=cell.getRow(); intcol=cell.getCol(); if(row>0&&row &&wall[row][col]! =null){ returntrue;//重合 } } returnfalse; } publicvoidmoveRightAction(){ tetromino.moveRight(); if(outOfBounds()||coincide()){ tetromino.moveLeft(); } } publicstaticfinalintCELL_SIZE=25; /*在Tetris.java中添加main方法作为软件的启动方法*/ publicstaticvoidmain(String[]args){ JFrameframe=newJFrame("俄罗斯方块"); intwigth=(COLS+8)*CELL_SIZE+100; intheight=ROWS*CELL_SIZE+100; frame.setSize(wigth,height); frame.setLocationRelativeTo(null);//居中 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);//设置关闭窗口就关闭软件 frame.setLayout(null);//取消默认布局,取消自动充满 Tetrispanel=newTetris(); panel.setLocation(45,25); panel.setSize((COLS+8)*CELL_SIZE,ROWS*CELL_SIZE); panel.setBorder(newLineBorder(Color.black)); frame.add(panel);//窗口中添加面板 frame.setVisible(true);//显示窗口时候调用paint() panel.action(); } /*动作方法,这里是让软件开始动作,*/ publicvoidaction(){ //wall[18][2]=newCell(18,2,0xff0000); startGameAction(); //重绘方法->尽快调用paint()
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 俄罗斯方块
![提示](https://static.bdocx.com/images/bang_tan.gif)