java课程设计报告-俄罗斯方块_第1页
java课程设计报告-俄罗斯方块_第2页
java课程设计报告-俄罗斯方块_第3页
java课程设计报告-俄罗斯方块_第4页
java课程设计报告-俄罗斯方块_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、-. z. - - . -可修- .JAVA程序设计课程设计 之俄罗斯方块年级:13级 班级:T412 网络工程指导教师:朱林小组成员: 许浩洋时间:2015年11月11日目录 TOC o HYPERLINK l _Toc22273 摘要 HYPERLINK l _Toc26508 第一章 课程设计要求 HYPERLINK l _Toc10423 第二章 设计概要 HYPERLINK l _Toc24314 2.1 功能设计 HYPERLINK l _Toc15232 2.2 功能分析 HYPERLINK l _Toc31030 2.2.1 系统操作界面 HYPERLINK l _Toc193

2、83 2.2.2 程序主要功能说明 HYPERLINK l _Toc8934 第三章 调试分析与测试结果 HYPERLINK l _Toc15243 3.1 游戏运行界面 HYPERLINK l _Toc457 3.2 测试工程 HYPERLINK l _Toc24634 3.2.1 功能区按键测试 HYPERLINK l _Toc19547 3.2.2 键盘功能测试 HYPERLINK l _Toc2898 3.2.3 游戏完毕测试 HYPERLINK l _Toc15576 第四章 设计总结 HYPERLINK l _Toc6939 4.1 改良意见 HYPERLINK l _Toc220

3、16 4.2 Java课程设计心得体会-. z. - - . -可修- .摘要在现代,高科技的飞跃开展,人们工作习惯的改变,特别是电脑的大量普及,人们生活节奏越来越快,一些有趣的桌面游戏已经成为人们在使用计算机进展工作或学习之余休闲娱乐的首选,而俄罗斯方块游戏是人们最熟悉的小游戏之一。俄罗斯方块Tetris, 俄文:是一款风行全球的电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克帕基特诺夫创造,故得此名。俄罗斯方块的根本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。由于上手简单、老少皆宜,从而家喻户晓,风行世界。为此,我们设计了一款简单的俄罗斯方块JAVA

4、游戏程序,以便更好的满足广阔电脑工作者闲暇之余的消遣,并且也让我们学到编程技术与团队意识。关键字:俄罗斯方块、JAVA游戏、编程课程设计要求题目名称:俄罗斯方块题目类型:设计型课程设计目的:1)了解Java的根本使用方法。 2)熟悉eclipse的运行环境。 3)用JAVA来设计一个俄罗斯方块的小游戏。 4)根本实现俄罗斯方块的应用功能。实验原理:JAVA程序分析与设计、类的灵活运用、多态技术、模板技术、异常处理等。实验容:本俄罗斯方块游戏是对于随机给出不同的形状长条形、Z字形、反Z形、田字形、L字形、反L形、T字型下落填充给定的区域,假设填满一条便消掉,记分。假设在游戏中各形状填满了给定区域

5、,为输者。 第二章 设计概要2.1 功能设计本工程是为了实现俄罗斯方块的根本功能而设计的,根本能够到达俄罗斯方块的各种游戏性。工程需求分析如下:1由方块组成的不同的随机图形会从区域上方开场缓慢落下。2)玩家可以做的操作有: 以90度为单位旋转方每一格块。以格子为单位左右移动方块,让方块加速落下。3)方块移到区域最下方或是着地到其他方块上无法移动时,就会固定在该处,而新的随机图形会出现在区域上方开场落下。4)当区域中*一列横向格子全部由方块填满,则该列会自动消除并成为玩家的得分。5一次性销毁不同行数方块得分不同,一行1分,两行2分,三行5分,四行10分。6)当固定的方块堆到区域最上方,则游戏完毕

6、。处理玩家操作2.2 功能分析2.2.1 系统操作界面2.2.2 程序主要功能说明1.面板画笔类代码:package Tetris;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.Arrays;import java.util.Timer;import java.util.TimerTask;import java*.swing.JFrame;im

7、port java*.swing.JPanel;publicclassTetrisPanele*tends JPanel/游戏主面板20行10列privatestaticfinalintROWS = 20;privatestaticfinalintCOLS = 10;/代表方块着陆的墙private Cell wall = new CellROWSCOLS;/定义每一小块的大小privatestaticfinalintCELL_SIZE = 25;/游戏得分privateintscore;/游戏销毁行数privateintlines;/一次性销毁行数的计分标准0行=0分,1行=1分,2行=4分

8、,3行=10分,4行=20分privatestaticfinalint SCORE_LEVEL =0,1,4,10,20;/游戏完毕标记privatebooleangameOver = false;/游戏暂停标记privatebooleanpause = false;/正在下落的四格方块private Tetromino currentTetro ;/下一个下落的四格方块private Tetromino ne*tTetro ;/定义游戏定时器private Timer timer;publicstaticvoid main(String args)JFrame frame = new JFr

9、ame(俄罗斯方块);int width = (COLS+8)*CELL_SIZE+100;int height = ROWS*CELL_SIZE+100;frame.setSize(width, height);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.E*IT_ON_CLOSE);/取消系统默认布局frame.setLayout(null);TetrisPanel panel = new TetrisPanel();panel.setLocation(45, 35);panel.setSi

10、ze(COLS+8)*CELL_SIZE,ROWS*CELL_SIZE+1);frame.add(panel);frame.setVisible(true);panel.action();publicvoid paint(Graphics g)super.paint(g);/填充背景颜色this.paintBackground(g);/绘制游戏墙paintWall(g);/绘制分数墙paintScore(g);/绘制面板边框paintTetrisBorder(g);/绘制当前四格方块paintCurrentTetro(g);/绘制下个四格方块paintNe*tTetro(g);/private

11、staticfinalintBG_COLOR = 0*C3D5EA;/privatestaticfinalintBORDER_COLOR = 0*667799;/privatestaticfinalintFONT_COLOR = 0*000000;/绘制背景的方法publicvoidpaintBackground(Graphics g)g.setColor(new Color(BG_COLOR);/this.setBackground(new Color(BG_COLOR);g.fillRect(0, 0, this.getWidth(), this.getHeight();/绘制游戏墙的方法

12、publicvoid paintWall(Graphics g)for(int row=0;rowROWS;row+)for(int col=0;colCOLS;col+)Cell cell = wallrowcol;int * =col*CELL_SIZE;int y= row*CELL_SIZE;if(cell=null)g.setColor(new Color(BORDER_COLOR);g.drawRect(*, y, CELL_SIZE, CELL_SIZE);elseg.setColor(new Color(cell.getColor();g.fillRect(*, y, CELL

13、_SIZE, CELL_SIZE);g.setColor(new Color(BORDER_COLOR);g.drawRect(*, y, CELL_SIZE, CELL_SIZE);/绘制分数墙的方法publicvoid paintScore(Graphics g)int * = 12*CELL_SIZE;int y = 6*CELL_SIZE;Font font = new Font(楷体,Font.BOLD,23);String msg =分数:+score;g.setColor(new Color(FONT_COLOR);g.setFont(font);g.drawString(msg

14、, *, y);y+=2*CELL_SIZE;msg = 行数:+lines;g.drawString(msg, *, y);if(gameOver)msg = (T_T)【S】再来;y+=2*CELL_SIZE;*-=CELL_SIZE;g.drawString(msg, *, y);elseif(pause)msg = 【C】继续;y+=2*CELL_SIZE;g.drawString(msg, *, y);elsemsg = 【P】暂停;y+=2*CELL_SIZE;g.drawString(msg, *, y);/绘制面板边框的方法publicvoid paintTetrisBorde

15、r(Graphics g)g.setColor(new Color(BORDER_COLOR);g.drawRect(0, 0, CELL_SIZE*(COLS+8)-1, CELL_SIZE*ROWS);/绘制当前四格方块的方法publicvoid paintCurrentTetro( Graphics g)if(currentTetro=null)/如果没有四格方块,则返回不绘画return;for(Cell cell:currentTetro.cells)int row = cell.getRow();int col = cell.getCol();int * = col*CELL_SI

16、ZE;int y = row*CELL_SIZE;g.setColor(new Color(cell.getColor();g.fillRect(*, y, CELL_SIZE, CELL_SIZE);g.setColor(new Color(BORDER_COLOR);g.drawRect(*, y, CELL_SIZE, CELL_SIZE);/绘制下个四格方块的方法publicvoid paintNe*tTetro( Graphics g)if(ne*tTetro=null)/如果没有四格方块,则返回不绘画return;for(Cell cell:ne*tTetro.cells)int

17、row = cell.getRow();int col = cell.getCol();int * = (col+9)*CELL_SIZE;int y = (row+1)*CELL_SIZE;g.setColor(new Color(cell.getColor();g.fillRect(*, y, CELL_SIZE, CELL_SIZE);g.setColor(new Color(BORDER_COLOR);g.drawRect(*, y, CELL_SIZE, CELL_SIZE);/让四格方块动起来的方法publicvoid action()startGameAction();/请求此容

18、器获取输入焦点this.requestFocus();this.addKeyListener(new KeyAdapter()publicvoid keyPressed(KeyEvent e)int key= e.getKeyCode();if(gameOver)if(key=KeyEvent.VK_S)startGameAction();return;if(pause)if(key=KeyEvent.VK_C)continueAction();return;switch(key)case KeyEvent.VK_DOWN:softDownAction();break;case KeyEven

19、t.VK_LEFT:moveLeftAction();break;case KeyEvent.VK_RIGHT:moveRightAction();break;case KeyEvent.VK_UP:rotateRightAction();break;case KeyEvent.VK_SPACE:hardDownAction();break;case KeyEvent.VK_P:pauseAction();break;repaint();/暂停游戏的方法privatevoid pauseAction() pause = true;timer.cancel();/继续游戏的方法privatevo

20、id continueAction() pause = false;timer = new Timer();timer.schedule(new TimerTask() publicvoid run() softDownAction();repaint();, 500, 500););/在游戏开场时调用或者【S】按下时调用publicvoid startGameAction()gameOver = false;pause = false;score = 0;lines = 0;/清空游戏主面板emptyWall();/生成下一个四格方块ne*tTetromino();/生成定时器对象timer

21、 = new Timer();/启动定时器工作timer.schedule(new TimerTask() publicvoid run() /调用面板的四格方块下落方法自由下落softDownAction();/重画面板repaint();, 500, 500);/清空游戏主面板方法publicvoid emptyWall()for(int row =0;rowROWS;row+)/wallrow这一行全部用null表示Arrays.fill(wallrow, null);/生成随机下一个四格方块,1.下一个变成当前的。2.随机生成下一个。publicvoid ne*tTetromino()

22、if(ne*tTetro=null)/第一次ne*tTetro是null时就随机生成一个ne*tTetro = Tetromino.randomTetromino();/下一个四格方块立即变成当前四格方块currentTetro = ne*tTetro;ne*tTetro = Tetromino.randomTetromino();/四格方块下落流程,方块移动到区域最下方或者移动到其他方块上则固定在此处。/而新的四个方块则会出现在区域上方并开场下落且随机生成下一个四格方块publicvoid softDownAction()if(canDown()/如果能下落则继续下落currentTetro

23、.softDown();else/不能下落则着陆到墙上tetrominoLandToWall();/每一个四格方块着陆去除满行且计分destroy();/每一个四格方块着陆需要检测游戏是否完毕if(gameOver()/如果游戏完毕gameOverAction();else/随机生成下一个四格方块ne*tTetromino();/检查方块是否能够继续下落落到最底部或者墙上的下方有方块privateboolean canDown() /检查是否到底部Cell cells = currentTetro.cells;for(Cell cell:cells)if(cell.getRow()=ROWS-

24、1)returnfalse;/检查次四格方块下方是否有方块for(Cell cell:cells)int row = cell.getRow();int col = cell.getCol();Cell block = wallrow+1col;if(block!=null)returnfalse;returntrue;/方块着陆到墙上,取出每个小方块找到对应的行号row和列号col,将cell小方块放到对应的wallrowcol位置上privatevoid tetrominoLandToWall() Cell cells = currentTetro.cells;for(Cell cell:

25、cells)int row = cell.getRow();int col = cell.getCol();/将cell小方块放到对应的wallrowcol位置上wallrowcol=cell;/销毁满行的方法privatevoid destroy() /统计本次销毁的行数int lines =0;for(int row =0 ;rowROWS;row+)/判断此行是否是满行if(fullCells(row)/去除此行clearLine(row);/每去除一行就累计加一lines+;/整个游戏面板每一行判断之后要记录销毁行数与计分score += SCORE_LEVELlines;this.l

26、ines += lines;/判断*一行是否填满cell小方块privateboolean fullCells(int row) Cellline = wallrow;for(int i=0; i=1;i-)/wallrow = Arrays.copyOf(wallrow-1, COLS);System.arraycopy(wallrow-1, 0, wallrow, 0, COLS);/第一行全部用null填充Arrays.fill(wall0, null);/检查游戏是否完毕思路:游戏主面板中第一行出现四个方块的区域有小方块cell在则游戏完毕privateboolean gameOver

27、() gameOver = wall03!=null|wall04!=null;returngameOver;/清零游戏完毕现场停顿计时器privatevoid gameOverAction() /停顿计时器timer.cancel();/以四格方块为单位左右移动的方法:1.遇到左右边界不能移动。2.遇到左右有方块不能移动。/左移的方法privatevoid moveLeftAction() currentTetro.moveLeft();if(outOfBounds()|coincide()currentTetro.moveRight();/判断四格方块是否与墙上的方块重合的方法privat

28、eboolean coincide() for(Cell cell:currentTetro.cells)int col = cell.getCol();int row = cell.getRow();/System.out.println(col+:+row);if(wallrowcol!= null)returntrue;returnfalse;/判断四格方块是否出边界的方法privateboolean outOfBounds() for(Cell cell:currentTetro.cells)int col = cell.getCol();if(col=COLS)returntrue;

29、returnfalse;/右移的方法privatevoid moveRightAction() currentTetro.moveRight();if(outOfBounds()|coincide()currentTetro.moveLeft();/四格方块加速下落的方法privatevoid hardDownAction() /四格方块继续下落while(canDown()currentTetro.softDown();/着陆到墙上tetrominoLandToWall();/去除满行并计分destroy();if(gameOver()gameOverAction();elsene*tTet

30、romino();/旋转流程控制privatevoid rotateRightAction() currentTetro.rotateRight();if(outOfBounds()|coincide()currentTetro.rotateLeft();2.2方块类代码:package Tetris;/代表游戏中每一个小格子方块publicclass Cell /小方块在游戏面板中的哪一行privateintrow;/小方块在游戏面板中的哪一列privateintcol;/小方块的颜色privateintcolor;public Cell(int row, int col, int colo

31、r)this.row = row;this.col = col;this.color = color;publicint getRow() returnrow;publicvoid setRow(int row) this.row = row;publicint getCol() returncol;publicvoid setCol(int col) this.col = col;publicint getColor() returncolor;publicvoid setColor(int color) this.color = color;/小方块向下移动publicvoid down(

32、) row+;/小方块向左移动publicvoid left() col-;/小方块向右移动publicvoid right() col+;七种方块旋转属性定义类代码:package Tetris;import java.util.Random;/四个方块类,有7种子类:I T S Z J L Opublicabstractclass Tetromino /每一种四个方块都有自己的颜色publicstaticfinalintI_COLOR =0*ff9988;publicstaticfinalintT_COLOR =0*44ff88;publicstaticfinalintS_COLOR =0

33、*704077;publicstaticfinalintZ_COLOR =0*ccee00;publicstaticfinalintJ_COLOR =0*ff1144;publicstaticfinalintL_COLOR =0*1122ff;publicstaticfinalintO_COLOR =0*6642bb;/四个方块有四个小方块Cell组成protected Cellcells = new Cell4;/每一种四格方块都有旋转状态旋转方式protected Offset states;/定义成员部类protectedclass Offsetintrow0,col0;introw1,

34、col1;introw2,col2;introw3,col3;public Offset(int row0,int col0,int row1,int col1,int row2,int col2,int row3,int col3)this.row0 = row0;this.col0 = col0;this.row1 = row1;this.col1 = col1;this.row2 = row2;this.col2 = col2;this.row3 = row3;this.col3 = col3;/随机生成一个具体的四格方块publicstatic Tetromino randomTetr

35、omino()Random random = new Random();/int type = random.ne*tInt(7);switch(type)case 0:returnnew I();case 1:returnnew T();case 2:returnnew J();case 3:returnnew O();case 4:returnnew S();case 5:returnnew L();case 6:returnnew Z();returnnull;/四格方块的下落,是四个小方块一起下落publicvoid softDown() for(int i=0;icells.leng

36、th;i+)cellsi.down();publicvoid moveLeft() for(int i=0;icells.length;i+)cellsi.left();publicvoid moveRight() for(int i=0;icells.length;i+)cellsi.right();/四格方块旋转标记privateintinde* = 9999;/向右旋转publicvoid rotateRight() inde*+;Offset offset = statesinde*%states.length;Cell a*is = cells0;cells1.setRow(a*is

37、.getRow()+offset.row1);cells1.setCol(a*is.getCol()+offset.col1);cells2.setRow(a*is.getRow()+offset.row2);cells2.setCol(a*is.getCol()+offset.col2);cells3.setRow(a*is.getRow()+offset.row3);cells3.setCol(a*is.getCol()+offset.col3);/向左旋转publicvoid rotateLeft() inde*-;Offset offset = statesinde*%states.l

38、ength;Cell a*is = cells0;cells1.setRow(a*is.getRow()+offset.row1);cells1.setCol(a*is.getCol()+offset.col1);cells2.setRow(a*is.getRow()+offset.row2);cells2.setCol(a*is.getCol()+offset.col2);cells3.setRow(a*is.getRow()+offset.row3);cells3.setCol(a*is.getCol()+offset.col3);/I形的四格方块class I e*tends Tetro

39、minopublic I()/I形的四格方块在游戏面板中的初始位置与颜色cells0 = new Cell(0,4,I_COLOR);cells1 = new Cell(0,3,I_COLOR);cells2 = new Cell(0,5,I_COLOR);cells3 = new Cell(0,6,I_COLOR);/I形四格方块的旋转状态states = new Offsetnew Offset(0,0,-1,0,1,0,2,0),new Offset(0,0,0,-1,0,1,0,2);/T形的四格方块class T e*tends Tetrominopublic T()/T形的四格方块

40、在游戏面板中的初始位置与颜色cells0 = new Cell(0,4,T_COLOR);cells1 = new Cell(0,3,T_COLOR);cells2 = new Cell(0,5,T_COLOR);cells3 = new Cell(1,4,T_COLOR);/T形四格方块的旋转状态states = new Offsetnew Offset(0,0,1,0,-1,0,0,-1),new Offset(0,0,0,1,0,-1,-1,0),new Offset(0,0,-1,0,1,0,0,1),new Offset(0,0,0,-1,0,1,1,0);/S形的四格方块class

41、 S e*tends Tetrominopublic S()/S形的四格方块在游戏面板中的初始位置与颜色cells0 = new Cell(0,4,S_COLOR);cells1 = new Cell(1,3,S_COLOR);cells2 = new Cell(0,5,S_COLOR);cells3 = new Cell(1,4,S_COLOR);/S形四格方块的旋转状态states = new Offsetnew Offset(0,0,1,0,-1,-1,0,-1),new Offset(0,0,0,1,1,-1,1,0);/J形的四格方块class J e*tends Tetromino

42、public J()/J形的四格方块在游戏面板中的初始位置与颜色cells0 = new Cell(0,4,J_COLOR);cells1 = new Cell(0,3,J_COLOR);cells2 = new Cell(0,5,J_COLOR);cells3 = new Cell(1,5,J_COLOR);/J形四格方块的旋转状态states = new Offsetnew Offset(0,0,-1,0,1,0,1,-1),new Offset(0,0,0,1,0,-1,-1,-1),new Offset(0,0,-1,0,1,0,-1,1),new Offset(0,0,0,-1,0,

43、1,1,1);/L形的四格方块class L e*tends Tetrominopublic L()/L形的四格方块在游戏面板中的初始位置与颜色cells0 = new Cell(0,4,L_COLOR);cells1 = new Cell(0,3,L_COLOR);cells2 = new Cell(0,5,L_COLOR);cells3 = new Cell(1,3,L_COLOR);/L形四格方块的旋转状态states = new Offsetnew Offset(0,0,-1,0,1,0,-1,-1),new Offset(0,0,0,1,0,-1,-1,1),new Offset(0,0,1,0,-1,0,1,1),new Offset(0,0,0,-1,0,1,1,-1);/Z形的四格方块class Z e*tends Tetrominopublic Z()

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论