ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:21.55KB ,
资源ID:6186863      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6186863.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java俄罗斯方块.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java俄罗斯方块.docx

1、java俄罗斯方块俄罗斯方块一共三个类中间用等号隔开软件的开发过程1 明确业务需求用自然语言,将业务功能描述清楚.2 业务分析 找到有哪些业务对象,和图片的分析 tetris(俄罗斯方块) score 累计分数 lines 销毁的行数 Wall(墙 20行x10列) 20row(行) 10 col cell(列) tetromino(4格方块,有7种形态) 4 cell nextOne 下一个准备下落的方块 4 cell 3 数据模型,一切业务对象转换为数字表示 场地按照行列划分为20x10格子 格子有属性row,col,color4 类 设计 Tetris |- int score |- i

2、nt lines |- Cell2010 wall |- Tetromino tetromino | |-Cell4 cells |- row |- col |- color5 算法设计,就是如何利用数据的计算实现软件的功能 4格方块的初始形态: I S Z J L T O 就在初始数据的数值状态设计 四格方块的下落计算:就是将每个格子的row+1 就是将下落的业务功能,转换为数字计算实现 左右移动 下落流程控制:控制方块下落与墙之间的控制关系 合理的文字流程描述 分析文字描述中的功能(动作)为方法 用流程控制语句连接方法实现功能 4 严格测试结果!estCase左右移动流程控制分数计算界面的

3、绘制键盘事件控制旋转流程控制加速下降流程控制 开始流程控制(imer) 暂停流程控制 继续流程控制 结束流程控制首先是Cell类,最基本的类包含3个私有属性和get,set方法,重写Object类的toString输出方法,并规定格子所具有的3个移动功能package com.tarena.tetris;/包:小写英文字母,域名倒写.项目名/* 最小的格子*/public class Cell private int row; private int col; private int color; public Cell(int row, int col, int color) super()

4、; this.row = row; this.col = col; this.color = color; 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 int getRow() return row; public void setRow(int row) this.row = row; public

5、 void left() col-; public void right() col+; public void drop() row+; public String toString() return row+,+col; package com.tarena.tetris;import java.util.Arrays;import java.util.Timer;import java.util.TimerTask;import javax.swing.JPanel;/是能够显示的矩形面板区域import javax.swing.JFrame;/窗口框import javax.swing

6、.border.LineBorder;/实现边框import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.event.KeyAdapter;import java.awt.event.KeyListener;import java.awt.event.KeyEvent;/* 俄罗斯方块类* 俄罗斯方块 扩展了(extends)系统的显示面板,增加了墙和* 正在下落的方块* */public class Tetris extends JPanel public static final

7、int ROWS = 20; public static final int COLS= 10; /*代表方块下落着陆的墙*/ private Cell wall = new CellROWSCOLS; /*是正在下落的方块*/ private Tetromino tetromino; /*下一个进入的方块*/ private Tetromino nextOne; private static int score; private int lines; Timer timer; private boolean gameOver = false; private boolean pause =

8、false;/暂停 private static final int SCORE_LEVEL=0,1,4,10,100; private static final Graphics Graphics = null; /*销毁(destory)满行*/ / 0 1 2 3 4 /*在Tetris中添加方法,检查游戏是否结束*/ public void rotateRightAction() tetromino.rotateRight(); if(outOfBounds()|coincide() tetromino.rotateLeft(); public void rotateLeftActio

9、n() tetromino.rotateLeft(); if(outOfBounds()|coincide() tetromino.rotateRight(); /*在Tetris中添加方法,检查游戏是否结束*/ private boolean gameOver() gameOver = wall04!=null; return gameOver; /*在Tetris中添加方法*/ public void hardDropAction() while(canDrop() tetromino.softDrop(); tetrominoLandToWall(); destroy(); if(gam

10、eOver() gameOverAction(); nextTetromino(); public void destroy() int lines = 0;/统计本次销毁的行数 for(int row = 0 ;row1;i-) System.arraycopy(walli-1,0,walli,0,walli.length); Arrays.fill(wall0,null); public static boolean fullCell(Cell line) for(int col = 0;colline.length;col+) if(linecol=null) return false;

11、/找到空格子,这行没有满 return true; public String toString()/显示全部的墙 String str = ; for(int row = 0;rowROWS;row+) Cell line = wallrow; for(int col = 0;colCOLS;col+) Cell cell = linecol; if(tetromino.contains (row,col) str +=row+,+col+ ; else str = str + cell + ; str +=n; return str; /*4格方块下降流程 * 方块移动到区域最下方或是着地

12、到其他方块上无法移动时, * 就会固定到该处,而新的方法快出现在区域上方开始下落。 * 如果能下降就继续下降, * 否则就着陆到墙上,并且生成(随机)下一个方块 * */ public void softDropAction() if(canDrop()/如果能下降 tetromino.softDrop();/方块继续下降 else tetrominoLandToWall();/着陆到墙上 destroy();/ if(gameOver() gameOverAction(); nextTetromino();/生产(随机)下一个方块 private void startGameAction()

13、 gameOver = false; pause = false; score = 0; lines = 0; emptyWall(); nextTetromino(); repaint(); timer = new Timer(); timer.schedule(new TimerTask() public void run() softDropAction(); repaint(); , 500, 500); private void emptyWall() for(int row=0;rowROWS;row+) Arrays.fill(wallrow,null); /*清理游戏结束现场,

14、如:停止定时器等*/ private void gameOverAction() timer.cancel();/停止定时器 /*检查 方块 是否能够继续下落:到底最低部,或者墙上 * 的下方有方块,返回false不能下降,返回true可以下降 * */ public boolean canDrop() /检查到底部 Cell cells = tetromino.getCells(); for(Cell cell:cells) if(cell.getRow()=ROWS-1) return false; /检查墙上下方是否有方块 for(Cell cell:cells) int row = c

15、ell.getRow(); int col = cell.getCol(); Cell block = wallrow+1col; if(block!=null) return false; return true; /*方块“着陆”到墙上, * 取出每个小cell * 找到cell的行号row和列号col * 将cell放置到wallrowcol位置上 * */ public void tetrominoLandToWall() Cell cells = tetromino.getCells(); for(Cell cell:cells) int row = cell.getRow(); i

16、nt col = cell.getCol(); wallrowcol = cell; /*生产(随机)下一个方块 * 1 下一个变为当前的 * 2 随机产生下一个 * */ public void nextTetromino() if(nextOne=null)/第一次nextOne是null时候先生产一个 nextOne = Tetromino.randomTetromino(); tetromino = nextOne;/下一个变为当前的 nextOne = Tetromino.randomTetromino();/随机产生下一个 if(tetromino=null)/处理第一次使用时候下

17、一个是null tetromino=Tetromino.randomTetromino(); /*以格子为单位左右移动方块 * 1)如果遇到左右边界就不能移动了 * 2)如果与墙上的格子相撞就不能移动了 * 变通为: * 1)先将方块左移动, * 2)检查(移动结果是否出界),或者(重合) * 3)如果检查失败,就右移的回来 * * */ public void moveLeftAction() tetromino.moveLeft(); if(outOfBounds() | coincide() tetromino.moveRight(); private boolean outOfBoun

18、ds() Cell cells = tetromino.getCells(); for (int i = 0; i cells.length; i+) Cell cell = cellsi; int row = cell.getRow(); int col = cell.getCol(); if(row = ROWS|col=COLS) return true; return false; private boolean coincide() Cell cells = tetromino.getCells(); for (int i = 0; i 0&rowROWS&col0 &wallrow

19、col!=null) return true;/重合 return false; public void moveRightAction() tetromino.moveRight(); if(outOfBounds() | coincide() tetromino.moveLeft(); public static final int CELL_SIZE = 25; /*在Tetris.java中添加main方法 作为软件的启动方法*/ public static void main(String args) JFrame frame = new JFrame(俄罗斯方块); int wig

20、th =(COLS+8)*CELL_SIZE +100; int height =ROWS*CELL_SIZE +100; frame.setSize(wigth,height); frame.setLocationRelativeTo(null);/居中 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);/设置关闭窗口就关闭软件 frame.setLayout(null);/取消默认布局,取消自动充满 Tetris panel = new Tetris(); panel.setLocation(45,25); panel.setSize(COLS+8)*CELL_SIZE,ROWS*CELL_SIZE); panel.setBorder(new LineBorder(Color.black); frame.add(panel);/窗口中添加面板 frame.setVisible(true);/显示窗口时候调用paint() panel.action(); /*动作方法,这里是让软件开始动作,*/ public void action() /wall182 = new Cell(18,2,0xff0000); startGameAction(); /重绘方法尽快调用paint()

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1