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

付费下载

下载本文档

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

文档简介

1、精品文档 JAVA程序设计课程设计 之 俄罗斯方块 年级: 13 级 班级: T412 网络工程 指导老师:朱林 小组成员: 20138346021 许浩洋 可编辑 时间: 2015 年11 月11 日 目录 摘要 第一章 课程设计要求 第二章 设计概要 2.1 功能设计 2.2 功能分析 2.2.1 系统操作界面 2.2.2 程序主要功能说明 第三章 调试分析与测试结果 3.1 游戏运行界面 3.2 测试项目 3.2.1 功能区按键测试 3.2.2 键盘功能测试 3.2.3 游戏结束测试 第四章 设计总结 4.1 改进意见 4.2 Java 课程设计心得体会 摘要 在现代,高科技的飞跃发展,

2、人们工作习惯的改变,特别是电脑 的大量普及,人们生活节奏越来越快,一些有趣的桌面游戏已经成 为人们在使用计算机进行工作或学习之余休闲娱乐的首选,而俄罗 斯方块游戏是人们最熟悉的小游戏之一。俄罗斯方块(Tetris,俄 文:Tempuc)是一款风靡全球的电视游戏机和掌上游戏机游戏,它 由俄罗斯人阿列克谢帕基特诺夫发明,故得此名。俄罗斯方块的 基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列 成完整的一行或多行并且消除得分。由于上手简单、老少皆宜,从 而家喻户晓,风靡世界。为此,我们设计了一款简单的俄罗斯方块 JAVA游戏程序,以便更好的满足广大电脑工作者闲暇之余的消 遣,并且也让我们学

3、到编程技术与团队意识。 关键字:俄罗斯方块、JAVA游戏、编程 第一章课程设计要求 题目名称:俄罗斯方块 题目类型:设计型 课程设计目的: 1)了解Java的基本使用方法。 2)熟悉eclipse的运行环境。 3)用JAVA来设计一个俄罗斯方块的小游戏。 4)基本实现俄罗斯方块的应用功能。 实验原理: JAVA程序分析与设计、类的灵活运用、多态技术、模板技术、 异常处理等。 实验内容: 本俄罗斯方块游戏是对于随机给出不同的形状(长条形、Z字形、反Z 形、田字形、L字形、反L形、T字型)下落填充给定的区域,若填满一条 便消掉,记分。若在游戏中各形状填满了给定区域,为输者。 第二章设计概要 2.1

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

5、2.2 功能分析 2.2.1系统操作界面 蹲斷方块一 n 222 程序主要功能说明 1.面板画笔类 代码: package Tetris; import java.awt.Color; import java.awt.F ont; import java.awt.Graphics; import java.awt.eve nt.KeyAdapter; import java.awt.eve nt.KeyEve nt; import java.util.Arrays; import java.util.Timer; import java.util.TimerTask; import javax

6、.swi ng.JFrame; import javax.swi ng.JPa nel; public class TetrisPanelextends JPanel /游戏主面板 20行 10 列 private static final int ROWS = 20; private static final int COLS = 10; /代表方块着陆的墙 private Cel l wall = new Cell ROWS COLS ; /定义每一小块的大小 private static final int CELL_SIZE = 25; /游戏得分 private int score

7、; /游戏销毁行数 private int lines ; / 一次性销毁行数的计分标准(0行=0分,1行=1分,2行=4分,3行=10分,4行=20分) private static final int SCORE_LEVEL =0,1,4,10,20; /游戏结束标记 private boolean gameOver = false ; /游戏暂停标记 private boolean pause = false ; /正在下落的四格方块 private Tetro minocurre ntTetro; /下一个下落的四格方块 private Tetro minon extTetro; /

8、定义游戏定时器 private Timer timer ; public static void main(String args) JFrame frame =new JFrame( 俄罗斯方块 ); int width = ( COLS +8)* CELL_SIZE +100; int height = ROWS * CELL_SIZE +100; frame.setSize(width, height); frame.setLocationRelativeTo( null ); frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );

9、 / 取消系统默认布局 frame.setLayout( null ); TetrisPanel panel =new TetrisPanel(); panel.setLocation(45, 35); panel.setSize( COLS +8)* CELL_SIZE ,ROWS *CELL_SIZE +1); frame.add(panel); frame.setVisible( true ); panel.action(); public void paint(Graphics g) super .paint(g); / 填充背景颜色 this .paintBackground(g);

10、 / 绘制游戏墙 paintWall(g); / 绘制分数墙 paintScore(g); / 绘制面板边框 paintTetrisBorder(g); / 绘制当前四格方块 paintCurrentTetro(g); / 绘制下个四格方块 paintNextTetro(g); / private / private / private static final int BG_COLOR = 0 xC3D5EA; static final int BORDER_COLOR = 0 x667799; static final int FONT_COLOR = 0 x000000; / 绘制背景的

11、方法 public void paintBackground(Graphics g) g.setColor( new Color( BG_COLOR ); /this.setBackground(new Color(BG_COLOR); g.fillRect(0, 0, this .getWidth(), this .getHeight(); / 绘制游戏墙的方法 public void paintWall(Graphics g) for (int row=0;row ROWS ;row+) for (int col=0;col COLS ;col+) Cell cell = wall row

12、col; int x =col* CELL_SIZE ; int y= row* CELL_SIZE ; if (cell= null ) g.setColor( new Color( BORDER_COLOR ); g.drawRect(x, y, CELL_SIZE , CELL_SIZE ); else g.setColor( new Color(cell.getColor(); g.fillRect(x, y, CELL_SIZE , CELL_SIZE ); g.setColor( new Color( BORDER_COLOR ); g.drawRect(x, y, CELL_SI

13、ZE , CELL_SIZE ); / 绘制分数墙的方法 public void paintScore(Graphics g) int x = 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, x, y); y+=2* CELL_SIZE ; msg = 行数: + lines ; g.

14、drawString(msg, x, y); if (gameOver ) msg = (T_T)【S 】再来; y+=2* CELL_SIZE ; x-= CELL_SIZE ; g.drawString(msg, x, y); else if (pause ) msg =【C】继续; y+=2* CELL_SIZE ; g.drawString(msg, x, y); else msg =【P】暂停; y+=2* CELL_SIZE ; g.drawString(msg, x, y); / 绘制面板边框的方法 public void paintTetrisBorder(Graphics g

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

16、nt x = col* CELL_SIZE ; int y = row* CELL_SIZE ; g.setColor( new Color(cell.getColor(); g.fillRect(x, y, CELL_SIZE , CELL_SIZE ); g.setColor( new Color( BORDER_COLOR ); g.drawRect(x, y, CELL_SIZE , CELL_SIZE ); / 绘制下个四格方块的方法 public void paintNextTetro( Graphics g) if (nextTetro = null ) / 如果没有四格方块,则

17、返回不绘画 return ; for (Cell cell: nextTetro .cells ) int row = cell.getRow(); int col = cell.getCol(); int x = (col+9)* CELL_SIZE ; int y = (row+1)* CELL_SIZE ; g.setColor( new Color(cell.getColor(); g.fillRect(x, y, CELL_SIZE , CELL_SIZE ); g.setColor( new Color( BORDER_COLOR ); g.drawRect(x, y, CELL_

18、SIZE , CELL_SIZE ); / 让四格方块动起来的方法 public void action() startGameAction(); / 请求此容器获取输入焦点 this .requestFocus(); this .addKeyListener( new KeyAdapter() public void keyPressed(KeyEvent e) int key= e.getKeyCode(); if (gameOver ) if (key=KeyEvent. VK_S ) startGameAction(); return ; if (pause ) if (key=Key

19、Event. VK_C ) continueAction(); return ; switch (key) case KeyEvent. VK_DOWN : softDownAction(); break ; case KeyEvent. VK_LEFT : moveLeftAction(); break ; case KeyEvent. VK_RIGHT : moveRightAction(); break ; case KeyEvent. VK_UP : rotateRightAction(); break ; case KeyEvent. VK_SPACE : hardDownActio

20、n(); break ; case KeyEvent. VK_P: pauseAction(); break ; repaint(); / 暂停游戏的方法 private void pauseAction() pause = true ; timer .cancel(); / 继续游戏的方法 private void continueAction() pause = false ; timer = new Timer(); timer .schedule( new TimerTask() public void run() softDownAction(); repaint(); , 500,

21、 500); ); /在游戏开始时调用或者【S】按下时调用 public void startGameAction() gameOver = false ; pause = false ; score = 0; lines = 0; / 清空游戏主面板 emptyWall(); / 生成下一个四格方块 nextTetromino(); / 生成定时器对象 timer = new Timer(); / 启动定时器工作 timer .schedule( new TimerTask() public void run() / 调用面板的四格方块下落方法(自由下落) softDownAction();

22、 / 重画面板 repaint(); , 500, 500); / 清空游戏主面板方法 public void emptyWall() for (int row =0;row ROWS ;row+) /wallrow 这一行全部用 null 表示 Arrays. fill (wall row, null ); 2. 随机生成下一个 / 生成(随机)下一个四格方块, 1. 下一个变成当前的 public void nextTetromino() if (nextTetro = null ) / 第一次 nextTetro 是 null 时就随机生成一个 nextTetro = Tetromino

23、. randomTetromino (); / 下一个四格方块立即变成当前四格方块 currentTetro = nextTetro ; nextTetro = Tetromino. randomTetromino (); / 四格方块下落流程,方块移动到区域最下方或者移动到其他方块上则固定在此处。 / 而新的四个方块则会出现在区域上方并开始下落且随机生成下一个四格方块 public void softDownAction() if (canDown() / 如果能下落则继续下落 currentTetro .softDown(); else / 不能下落则着陆到墙上 tetrominoLand

24、ToWall(); / 每一个四格方块着陆清除满行且计分 destroy(); / 每一个四格方块着陆需要检测游戏是否结束 if (gameOver() / 如果游戏结束 gameOverAction(); else / 随机生成下一个四格方块 nextTetromino(); / 检查方块是否能够继续下落(落到最底部或者墙上的下方有方块) private boolean canDown() / 检查是否到底部 Cell cells = currentTetro .cells ; for (Cell cell:cells) if (cell.getRow()= ROWS -1) return

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

26、cells ; for (Cell cell:cells) int row = cell.getRow(); int col = cell.getCol(); /将cell小方块放到对应的wallrowcol 位置上 wall rowcol=cell; /销毁满行的方法 private void destroy。 /统计本次销毁的行数 int lines =0; for (int row =0 ;row ROWS ;row+) /判断此行是否是满行 if (fullCells(row) /清除此行 clearL in e(row); /每清除一行就累计加一 lin es+; /整个游戏面板每一

27、行判断之后要记录销毁行数与计分 score += SCORE_LEVEL lines; this .lines += lines; /判断某一行是否填满cell小方块 private boolean fullCells( int row) Cellline = wall row; for (int i=0; i=1;i_) /wallrow = Arrays.copyOf(wallrow-1, COLS); System. arraycopy (wall row-1, 0, wall row, 0, COLS ); /第一行全部用null填充 Arrays. fill (wall 0, nul

28、l ); /检查游戏是否结束(思路:游戏主面板中第一行岀现四个方块的区域有小方块cell在则游戏 结束) privateboolea n gameOver() gameOver = wall 03!= null | wall 04!= null ; return gameOver ; /清零游戏结束现场(停止计时器) private void gameOverAction() /停止计时器 timer .cancel(); /以四格方块为单位左右移动的方法:1.遇到左右边界不能移动。2.遇到左右有方块不能移 动。 /左移的方法 private void moveLeftAction() cur

29、re ntTetro .moveLeft(); if (outOfBou nds()|coi ncide() curre ntTetro .moveRight(); /判断四格方块是否与墙上的方块重合的方法 privateboolea n coin cide() for (Cell cell: currentTetro. cells ) int col = cell.getCol(); int row = cell.getRow(); /System.out.pri ntln( col+:+row); if (wall rowcol!=null ) return true ; return f

30、alse ; / 判断四格方块是否出边界的方法 private boolean outOfBounds() for (Cell cell: currentTetro . cells ) int col = cell.getCol(); if (col= COLS ) return true ; return false ; / 右移的方法 private void moveRightAction() currentTetro .moveRight(); if (outOfBounds()|coincide() currentTetro .moveLeft(); / 四格方块加速下落的方法 pr

31、ivate void hardDownAction() / 四格方块继续下落 while (canDown() currentTetro .softDown(); / 着陆到墙上 tetrominoLandToWall(); / 清除满行并计分 destroy(); if (gameOver() gameOverAction(); else nextTetromino(); / 旋转流程控制 private void rotateRightAction() currentTetro .rotateRight(); if (outOfBounds()|coincide() currentTetr

32、o .rotateLeft(); 2.2.2 方块类 代码: package Tetris; / 代表游戏中每一个小格子方块 public class Cell / 小方块在游戏面板中的哪一行 private int row ; / 小方块在游戏面板中的哪一列 private int col ; / 小方块的颜色 private int color ; public Cell( int row, int col, int color) this .row = row; this .col = col; this .color = color; public int getRow() retur

33、n row ; public void setRow( int row) this .row = row; public int getCol() return col ; public void setCol( int col) this .col = col; public int getColor() return color ; public void setColor( int color) this .color = color; / 小方块向下移动 public void down() row +; / 小方块向左移动 public void left() col -; / 小方

34、块向右移动 public void right() col +; 2.2.3 七种方块旋转属性定义类 代码: package Tetris; import java.util.Random; / 四个方块类,有 7 种子类: I T S Z J L O public abstract class Tetromino / 每一种四个方块都有自己的颜色 public static final int I_COLOR =0 xff9988; public static final int T COLOR =0 x44ff88; public static final int S _COLOR =0

35、x704077; public static final int Z COLOR =0 xccee00; public static final int J_ COLOR =0 xff1144; public static final int L_ COLOR =0 x1122ff; public static final int O _COLOR =0 x6642bb; / 四个方块有四个小方块( Cell )组成 protected Cell cells = new Cell4; / 每一种四格方块都有旋转状态(旋转方式) protected Offset states ; / 定义成员内

36、部类 protected class Offset int row0 ,col0 ; int row1 ,col1 ; int row2 ,col2 ; int row3 ,col3 ; public Offset( int row0, int col0, int row1, int col1, int int row2, row3, int int col2, col3) this .row0 = row0; this .col0 = col0; this .row1 = row1; this .col1 = col1; this .row2 = row2; this .col2 = col

37、2; this .row3 = row3; this .col3 = col3; / 随机生成一个具体的四格方块 public static Tetromino randomTetromino() Random random =new Random(); / int type = random.nextInt(7); switch (type) case 0: return new I(); case 1: return new T(); case 2: return new J(); case 3: return new O(); case 4: return new S(); case 5

38、: return new L(); case 6: return new Z(); return null / 四格方块的下落,是四个小方块一起下落 public void softDown() for (int i=0;i cells . length ;i+) cells i.down(); public void moveLeft() for (int i=0;i cells . length ;i+) cells i.left(); public void moveRight() for (int i=0;i cells . length ;i+) cells i.right(); /

39、 四格方块旋转标记 private int index = 9999; / 向右旋转 public void rotateRight() index +; Offset offset = states index % states .length ; Cell axis = cells 0; cells 1.setRow(axis.getRow()+offset. row1 ); cells 1.setCol(axis.getCol()+offset. col1 ); cells 2.setRow(axis.getRow()+offset.row2 ); cells 2.setCol(axis

40、.getCol()+offset. col2 ); cells 3.setRow(axis.getRow()+offset.row3 ); cells 3.setCol(axis.getCol()+offset. col3 ); / 向左旋转 public void rotateLeft() index -; Offset offset = states index % states .length ; Cell axis = cells 0; cells 1.setRow(axis.getRow()+offset. row1 ); cells 1.setCol(axis.getCol()+o

41、ffset. col1 ); cells 2.setRow(axis.getRow()+offset.row2 ); cells 2.setCol(axis.getCol()+offset. col2 ); cells 3.setRow(axis.getRow()+offset.row3 ); cells 3.setCol(axis.getCol()+offset. col3 ); /I 形的四格方块 class I extends Tetromino public I() /I 形的四格方块在游戏面板中的初始位置与颜色 cells 0 = new Cell(0,4, I_COLOR ); c

42、ells 1 = new Cell(0,3, I_COLOR ); cells 2 = new Cell(0,5, I_COLOR ); cells 3 = new Cell(0,6, I_COLOR ); /I 形四格方块的旋转状态 states = new Offset new Offset(0,0,-1,0,1,0,2,0), new Offset(0,0,0,-1,0,1,0,2) ; /T 形的四格方块 class T extends Tetromino public T() /T 形的四格方块在游戏面板中的初始位置与颜色 cells 0 = new Cell(0,4, T_COLO

43、R ); cells 1 = new Cell(0,3, T_COLOR ); cells 2 = new Cell(0,5, T_COLOR ); cells 3 = new Cell(1,4, T_COLOR ); /T 形四格方块的旋转状态 states = new Offset new 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 S extends Tetromi

44、no public S() /S 形的四格方块在游戏面板中的初始位置与颜色 cells 0 = new Cell(0,4, S_COLOR ); cells 1 = new Cell(1,3, S_COLOR ); cells 2 = new Cell(0,5, S_COLOR ); cells 3 = new Cell(1,4, S_COLOR ); /S 形四格方块的旋转状态 states = new Offset new Offset(0,0,1,0,-1,-1,0,-1), new Offset(0,0,0,1,1,-1,1,0) ; /J 形的四格方块 class J extends

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

46、,0,1,0,-1,1), new Offset(0,0,0,-1,0,1,1,1) ; /L 形的四格方块 class L extends Tetromino public L() /L 形的四格方块在游戏面板中的初始位置与颜色 cells 0 = new Cell(0,4, L_COLOR ); cells 1 = new Cell(0,3, L_COLOR ); cells 2 = new Cell(0,5, L_COLOR ); cells 3 = new Cell(1,3, L_COLOR ); /L 形四格方块的旋转状态 states = new Offset new 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 extends Tetromino public Z() /Z 形的四格方块在游戏面板中的初始位置与

温馨提示

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

评论

0/150

提交评论