达内学习心得俄罗斯方块项目总结.docx
- 文档编号:24315669
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:29
- 大小:21.70KB
达内学习心得俄罗斯方块项目总结.docx
《达内学习心得俄罗斯方块项目总结.docx》由会员分享,可在线阅读,更多相关《达内学习心得俄罗斯方块项目总结.docx(29页珍藏版)》请在冰豆网上搜索。
达内学习心得俄罗斯方块项目总结
达内学员:
我的俄罗斯方块项目总结
获奖学员:
陈启壮
所获奖项:
三等奖
内容
俄罗斯方块一共三个类中间用等号隔开
软件的开发过程
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)满行*///01234
/*在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() //startGameAction(); //this是当前Tetris面板 this.requestFocus();//为当前面板请求获得输入焦点 //this对象就获得了输入焦点,以后任何的 //键盘输入(包括左右方向键)目标就是这个面板对象了! //addKeyLIstener添加键盘监听,监听那些按键输入了 this.addKeyListener(newKeyAdapter(){ publicvoidkeyPressed(KeyEvente){ intkey=e.getKeyCode();//key按键 if(gameOver){ if(key==KeyEvent.VK_S){ startGameAction();//启动游戏开始流程 } return; } if(pause){ if(key==KeyEvent.VK_C){ continueAction(); }return; } //System.out.println("Type: "+e.getKeyCode()); switch(key){ caseKeyEvent.VK_RIGHT: moveRightAction();break; caseKeyEvent.VK_LEFT: moveLeftAction();break; caseKeyEvent.VK_DOWN: softDropAction();break; caseKeyEvent.VK_UP: rotateRightAction();break; caseKeyEvent.VK_SPACE: hardDropAction();break; caseKeyEvent.VK_P: pasueAction();break; } //按键->方块移动方法->改变方块数据->repaint() //->尽快调用paint()->利用新数据绘制 repaint(); } privatevoidcontinueAction(){ pause=false; timer=newTimer(); timer.schedule(newTimerTask(){ publicvoidrun(){ softDropAction(); repaint(); } },500,500); } privatevoidpasueAction(){ pause=true; timer.cancel(); } }); } //JPanel类利用paint(涂画)方法绘制界面 //子类重写paint方法可以修改绘图逻辑 publicstaticfinalintBORDER_COLOR=0x667799; publicstaticfinalintBG_COLOR=0xC3D5EA; publicstaticfinalintFONT_COLOR=0; publicvoidpaint(Graphicsg){ //g代表绑定在当前面板上的画笔 //利用画笔在当前面板上绘制了一串字符! paintBackground(g);//填充背景 paintWall(g);//绘制墙 paintTetromino(g);//绘制当前方块 paintNextOne(g);//绘制下一个方块 paintScore(g);//绘制分数 paintTetrisBorder(g);//绘制边线 } privatevoidpaintScore(Graphicsg){ intx=12*CELL_SIZE; inty=5*CELL_SIZE; Fontfont=newFont(getFont().getName(),Font.BOLD,25); Stringstr="分数: "+score; g.setColor(newColor(FONT_COLOR)); g.setFont(font); g.drawString(str,x,y); y+=2*CELL_SIZE; str="行数: "+lines; g.drawString(str,x,y); if(gameOver){ str="(T_T)! [s]再来! "; y+=2*CELL_SIZE; g.drawString(str,x,y); } if(pause){ str="[c]继续! "; y+=2*CELL_SIZE; g.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 学习心得 俄罗斯方块 项目 总结
