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

下载本文档

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

文档简介

II 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课程设计心得体会II19摘要在现代,高科技的飞跃发展,人们工作习惯的改变,特别是电脑的大量普及,人们生活节奏越来越快,一些有趣的桌面游戏已经成为人们在使用计算机进行工作或学习之余休闲娱乐的首选,而俄罗斯方块游戏是人们最熟悉的小游戏之一。俄罗斯方块(Tetris, 俄文:)是一款风靡全球的电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克谢帕基特诺夫发明,故得此名。俄罗斯方块的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。 为此,我们设计了一款简单的俄罗斯方块JAVA游戏程序,以便更好的满足广大电脑工作者闲暇之余的消遣,并且也让我们学到编程技术与团队意识。关键字:俄罗斯方块、JAVA游戏、编程第1章 课程设计要求题目名称:俄罗斯方块题目类型:设计型课程设计目的: 1)了解Java的基本使用方法。 2)熟悉eclipse的运行环境。 3)用JAVA来设计一个俄罗斯方块的小游戏。 4)基本实现俄罗斯方块的应用功能。实验原理:JAVA程序分析与设计、类的灵活运用、多态技术、模板技术、异常处理等。实验内容:本俄罗斯方块游戏是对于随机给出不同的形状(长条形、Z字形、反Z形、田字形、L字形、反L形、T字型)下落填充给定的区域,若填满一条便消掉,记分。若在游戏中各形状填满了给定区域,为输者。 第二章 设计概要2.1 功能设计本项目是为了实现俄罗斯方块的基本功能而设计的,基本能够达到俄罗斯方块的各种游戏性。项目需求分析如下:1)由方块组成的不同的随机图形会从区域上方开始缓慢落下。2)玩家可以做的操作有: 以90度为单位旋转方每一格块。以格子为单位左右移动方块,让方块加速落下。3)方块移到区域最下方或是着地到其他方块上无法移动时,就会固定在该处,而新的随机图形会出现在区域上方开始落下。4)当区域中某一列横向格子全部由方块填满,则该列会自动消除并成为玩家的得分。5)一次性销毁不同行数方块得分不同,一行1分,两行2分,三行5分,四行10分。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 javax.swing.JFrame;import javax.swing.JPanel;public class TetrisPanel extends JPanel/游戏主面板20行10列private static final int ROWS = 20;private static final int COLS = 10;/代表方块着陆的墙private Cell wall = new CellROWSCOLS;/定义每一小块的大小private static final int CELL_SIZE = 25;/游戏得分private int score;/游戏销毁行数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 Tetromino currentTetro ;/下一个下落的四格方块private Tetromino nextTetro ;/定义游戏定时器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);/取消系统默认布局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);/绘制游戏墙paintWall(g);/绘制分数墙paintScore(g);/绘制面板边框paintTetrisBorder(g);/绘制当前四格方块paintCurrentTetro(g);/绘制下个四格方块paintNextTetro(g); /private static final int BG_COLOR = 0xC3D5EA;/private static final int BORDER_COLOR = 0x667799;/private static final int FONT_COLOR = 0x000000;/绘制背景的方法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;rowROWS;row+)for(int col=0;colCOLS;col+)Cell cell = wallrowcol;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);elseg.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 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.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);elsemsg = 【P】暂停;y+=2*CELL_SIZE;g.drawString(msg, x, y);/绘制面板边框的方法public void paintTetrisBorder(Graphics g)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();int 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)/如果没有四格方块,则返回不绘画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_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=KeyEvent.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:hardDownAction();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, 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();/重画面板repaint();, 500, 500);/清空游戏主面板方法public void emptyWall()for(int row =0;rowROWS;row+)/wallrow这一行全部用null表示Arrays.fill(wallrow, null);/生成(随机)下一个四格方块,1.下一个变成当前的。2.随机生成下一个。public void nextTetromino()if(nextTetro=null)/第一次nextTetro是null时就随机生成一个nextTetro = Tetromino.randomTetromino();/下一个四格方块立即变成当前四格方块currentTetro = nextTetro;nextTetro = Tetromino.randomTetromino();/四格方块下落流程,方块移动到区域最下方或者移动到其他方块上则固定在此处。/而新的四个方块则会出现在区域上方并开始下落且随机生成下一个四格方块public void softDownAction()if(canDown()/如果能下落则继续下落currentTetro.softDown();else/不能下落则着陆到墙上tetrominoLandToWall();/每一个四格方块着陆清除满行且计分destroy();/每一个四格方块着陆需要检测游戏是否结束if(gameOver()/如果游戏结束gameOverAction();else/随机生成下一个四格方块nextTetromino();/检查方块是否能够继续下落(落到最底部或者墙上的下方有方块)private boolean canDown() /检查是否到底部Cell cells = currentTetro.cells;for(Cell cell:cells)if(cell.getRow()=ROWS-1)return false;/检查次四格方块下方是否有方块for(Cell cell:cells)int row = cell.getRow();int col = cell.getCol();Cell block = wallrow+1col;if(block!=null)return false;return true;/方块“着陆”到墙上,取出每个小方块找到对应的行号row和列号col,将cell小方块放到对应的wallrowcol位置上private void tetrominoLandToWall() Cell cells = currentTetro.cells;for(Cell cell:cells)int row = cell.getRow();int col = cell.getCol();/将cell小方块放到对应的wallrowcol位置上wallrowcol=cell;/销毁满行的方法private void destroy() /统计本次销毁的行数int lines =0;for(int row =0 ;rowROWS;row+)/判断此行是否是满行if(fullCells(row)/清除此行clearLine(row);/每清除一行就累计加一lines+;/整个游戏面板每一行判断之后要记录销毁行数与计分score += SCORE_LEVELlines;this.lines += lines;/判断某一行是否填满cell小方块private boolean 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在则游戏结束)private boolean gameOver() gameOver = wall03!=null|wall04!=null;return gameOver;/清零游戏结束现场(停止计时器)private void gameOverAction() /停止计时器timer.cancel();/以四格方块为单位左右移动的方法:1.遇到左右边界不能移动。2.遇到左右有方块不能移动。/左移的方法private void moveLeftAction() currentTetro.moveLeft();if(outOfBounds()|coincide()currentTetro.moveRight();/判断四格方块是否与墙上的方块重合的方法private boolean coincide() for(Cell cell:currentTetro.cells)int col = cell.getCol();int row = cell.getRow();/System.out.println(col+:+row);if(wallrowcol!= null)return true;return false;/判断四格方块是否出边界的方法 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();/四格方块加速下落的方法private void hardDownAction() /四格方块继续下落while(canDown()currentTetro.softDown();/着陆到墙上tetrominoLandToWall();/清除满行并计分destroy();if(gameOver()gameOverAction();elsenextTetromino();/旋转流程控制private void rotateRightAction() currentTetro.rotateRight();if(outOfBounds()|coincide()currentTetro.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() return 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-;/小方块向右移动public void right() col+;2.2.3七种方块旋转属性定义类 代码:package Tetris;import java.util.Random;/四个方块类,有7种子类:I T S Z J L Opublic abstract class Tetromino /每一种四个方块都有自己的颜色public static final int I_COLOR =0xff9988;public static final int T_COLOR =0x44ff88;public static final int S_COLOR =0x704077;public static final int Z_COLOR =0xccee00;public static final int J_COLOR =0xff1144;public static final int L_COLOR =0x1122ff;public static final int O_COLOR =0x6642bb;/四个方块有四个小方块(Cell)组成protected Cellcells = new Cell4;/每一种四格方块都有旋转状态(旋转方式)protected Offset states;/定义成员内部类protected class Offsetint row0,col0;int row1,col1;int row2,col2;int row3,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;/随机生成一个具体的四格方块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:return new L();case 6:return new Z();return null;/四格方块的下落,是四个小方块一起下落public void softDown() for(int i=0;icells.length;i+)cellsi.down();public void moveLeft() for(int i=0;icells.length;i+)cellsi.left();public void moveRight() for(int i=0;icells.length;i+)cellsi.right();/四格方块旋转标记private int index = 9999;/向右旋转public void rotateRight() index+;Offset offset = statesindex%states.length;Cell axis = cells0;cells1.setRow(axis.getRow()+offset.row1);cells1.setCol(axis.getCol()+offset.col1);cells2.setRow(axis.getRow()+offset.row2);cells2.setCol(axis.getCol()+offset.col2);cells3.setRow(axis.getRow()+offset.row3);cells3.setCol(axis.getCol()+offset.col3);/向左旋转public void rotateLeft() index-;Offset offset = statesindex%states.length;Cell axis = cells0;cells1.setRow(axis.getRow()+offset.row1);cells1.setCol(axis.getCol()+offset.col1);cells2.setRow(axis.getRow()+offset.row2);cells2.setCol(axis.getCol()+offset.col2);cells3.setRow(axis.getRow()+offset.row3);cells3.setCol(axis.getCol()+offset.col3);/I形的四格方块class I extends Tetrominopublic 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 extends Tetrominopublic T()/T形的四格方块在游戏面板中的初始位置与颜色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 S extends 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 extends Tetrominopublic 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,1,1,1);/L形的四格方块class L extends 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 extends Tetrominopublic Z()/Z形的四格方块在游戏面板中的初始位置与颜色cells0 = new Cell(0,4,Z

温馨提示

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

评论

0/150

提交评论