Java俄罗斯方块单人游戏课程设计_第1页
Java俄罗斯方块单人游戏课程设计_第2页
Java俄罗斯方块单人游戏课程设计_第3页
Java俄罗斯方块单人游戏课程设计_第4页
Java俄罗斯方块单人游戏课程设计_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

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

文档简介

1、 JAVA程序设计 课程设计报告课 题: 俄罗斯方块单人游戏 姓 名: 赵 云 杰 学 号: 2 同组姓名: 刘杨、汪世军、成功 专业班级: 网络工程14102班 指导教师: 谭 文 学 设计时间: 评阅意见:评定成绩: 指导老师签名: 年 月 日目 录1. 系统概述 32. 设计说明书 53. 系统操作界面 74. 源程序编码 285. 测试计划 296. 改进意见317 课程设计心得体会328. 参考书籍、资料34系统概述1.1现状分析在个人电脑日益普及的今天,一些有趣的桌面游戏已经成为人们在使用计算机进行工作或学习之余休闲娱乐的首选,而俄罗斯方块游戏是人们最熟悉的小游戏之一,它以其趣味性

2、强,易上手等诸多特点得到了大众的认可,因此开发此游戏软件可满足人们的一些娱乐的需求。此俄罗斯方块游戏可以为用户提供一个可在普通个人电脑上运行的,界面美观的,易于控制的俄罗斯方块游戏。1.2项目要求俄罗斯方块游戏是一款适合大众的游戏软件,它适合不同年龄的人玩。本软件要实现的功能如下:(1)游戏区:玩家可以在游戏区中堆积方块,并能够在游戏过程中随时了解得分情况。(2)游戏控制:玩家可以通过游戏控制功能来选择开始新的一局游戏,暂停或退出游戏。(3) 级别设置:玩家可以根据自己的需要自行设定游戏的开始级别,级别越高,游戏的速度越快,难度越大。1.3系统功能模块示意图俄罗斯方块游戏游戏区游戏控制显示玩家

3、操作显示操作结果开始暂停/继续提高等级退出降低等级 设计说明 1.1游戏区模块游戏区模块创建游戏区处理玩家游戏操作显示游戏结果1.2控制区模块游戏控制模块开始游戏暂停游戏初始级别设置退出游戏1.3系统流程图是否到顶部处理玩家操作开始设置初始级别创建游戏区游戏开局随机选择方块类型是否到顶部方块下落一行游戏结束是否1.4模块简介(1)模块功能简介:模块4-方块颜色、形状、变化此模块主要通过接口、类,来实现俄罗斯方块的颜色、形状(共28种形态)、变化(包括移动与变形)方块的颜色(出现时为绿色,提示出现时为红色)(2)功能模块图:方块的形状:28种方 块方块的变化(移动与变形) 模块实现代码: cla

4、ss ErsBox implements Cloneable private boolean isColor;private Dimension size = new Dimension(); public ErsBox(boolean isColor)this.isColor = isColor;public boolean isColorBox() return isColor;public void setColor(boolean isColor) this.isColor = isColor;public Dimension getSize() return size;public

5、void setSize(Dimension size) this.size = size;public Object clone() Object cloned = null;try cloned = super.clone(); catch (Exception ex)ex.printStackTrace();return cloned; class ErsBlock extends Thread public final static int boxes_rows = 4;public final static int boxes_cols = 4;public final static

6、 int flatgene = 3;public final static int betweenleveltime = 50;private final static int blockkindnum = 7;private final static int blockstatusnum = 4;public final static int STYLES = / 共28种状态0x0f00, 0x4444, 0x0f00, 0x4444, / 长条型的四种状态0x04e0, 0x0464, 0x00e4, 0x04c4, / 'T'型的四种状态0x4620, 0x6c00,

7、0x4620, 0x6c00, / 反'Z'型的四种状态0x2640, 0xc600, 0x2640, 0xc600, / 'Z'型的四种状态0x6220, 0x1700, 0x2230, 0x0740, / '7'型的四种状态0x6440, 0x0e20, 0x44c0, 0x8e00, / 反'7'型的四种状态0x0660, 0x0660, 0x0660, 0x0660, / 方块的四种状态;private GameCanvas canvas;private ErsBox boxes = new ErsBoxboxes_row

8、sboxes_cols;private int style, y, x, level;private boolean pausing = false, moving = true;public ErsBlock(int style, int y, int x, int level, GameCanvas canvas) this.style = style;this.y = y;this.x = x;this.level = level;this.canvas = canvas;int key = 0x8000;for (int i = 0; i < boxes.length; i+)

9、for (int j = 0; j < boxesi.length; j+) boolean isColor = (style & key) != 0);boxesij = new ErsBox(isColor);key >>= 1;display();public void run()while (moving)try sleep(betweenleveltime * (ErsBlocksGame.maxlevel - level + flatgene); catch (InterruptedException ie)ie.printStackTrace();if

10、(!pausing)moving = (moveTo(y + 1, x) && moving);public void moveLeft() moveTo(y, x - 1);public void moveRight()moveTo(y, x + 1);public void moveDown()moveTo(y + 1, x);public void turnNext()for (int i = 0; i < blockkindnum; i+) for (int j = 0; j < blockstatusnum; j+)if (STYLESij = style

11、)int newStyle = STYLESi(j + 1) % blockstatusnum;turnTo(newStyle);return;public void pauseMove() pausing = true; public void resumeMove()pausing = false;public void stopMove() moving = false;private void earse() for (int i = 0; i < boxes.length; i+) for (int j = 0; j < boxesi.length; j+)if (box

12、esij.isColorBox()ErsBox box = canvas.getBox(i + y, j + x);if (box = null)continue;box.setColor(false);private void display() for (int i = 0; i < boxes.length; i+)for (int j = 0; j < boxesi.length; j+)if (boxesij.isColorBox()ErsBox box = canvas.getBox(y + i, x + j);if (box = null) continue;box.

13、setColor(true);private boolean isMoveAble(int newRow, int newCol)earse();for (int i = 0; i < boxes.length; i+)for (int j = 0; j < boxesi.length; j+) if (boxesij.isColorBox() ErsBox box = canvas.getBox(newRow + i, newCol + j);if (box = null | (box.isColorBox() display();return false;display();r

14、eturn true;private synchronized boolean moveTo(int newRow, int newCol) if (!isMoveAble(newRow, newCol) | !moving) return false;earse();y = newRow;x = newCol;display();canvas.repaint();return true;private boolean isTurnAble(int newStyle)int key = 0x8000;earse();for (int i = 0; i < boxes.length; i+

15、)for (int j = 0; j < boxesi.length; j+) if (newStyle & key) != 0) ErsBox box = canvas.getBox(y + i, x + j);if (box = null | box.isColorBox()display();return false;key >>= 1;display();return true;private boolean turnTo(int newStyle)if (!isTurnAble(newStyle) | !moving) return false;earse(

16、);int key = 0x8000;for (int i = 0; i < boxes.length; i+)for (int j = 0; j < boxesi.length; j+) boolean isColor = (newStyle & key) != 0);boxesij.setColor(isColor);key >>= 1;style = newStyle;display();canvas.repaint();return true;模块实现:颜色: (提示出现时为红色); (出现时为绿色) 形状: 注:每一个方块有四种形态,共计7*4=28种

17、源程序编码import javax.swing.*;import java.awt.*;import javax.swing.border.Border;import java.awt.event.*;public class ErsBlocksGame extends JFrame public final static int alinescore = 100;public final static int everylevelscore = alinescore * 20;public final static int maxlevel = 10;public final static

18、int initlevel = 5;private Game Canvas canvas;private ErsBlock block;private boolean playing = false;private ControlPanel ctrlPanel;private JMenuBar bar = new JMenuBar();private JMenu mGame = new JMenu("游戏"),mControl = new JMenu("控制"),mhelp = new JMenu("帮助");private JMen

19、uItem miNewGame = new JMenuItem("新游戏"),milevelup = new JMenuItem("提高级数"),mileveldown = new JMenuItem("降低级数"),miExit = new JMenuItem("退出"), miPlay = new JMenuItem("开始"),miPause = new JMenuItem("暂停"),miResume = new JMenuItem("重新开始")

20、,miStop = new JMenuItem("停止"),miCtrlBlock = new JMenuItem("方块控制键"); public ErsBlocksGame(String title)super(title); setSize(315, 392);Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();setLocation(scrSize.width - getSize().width) / 2, (scrSize.height - getSize().heig

21、ht) / 2);createMenu();Container container = getContentPane();container.setLayout(new BorderLayout(6, 0);canvas = new GameCanvas(20, 12);ctrlPanel = new ControlPanel(this);container.add(canvas, BorderLayout.CENTER);container.add(ctrlPanel, BorderLayout.EAST);addWindowListener(new WindowAdapter() publ

22、ic void windowClosing(WindowEvent we) stopGame();System.exit(0););addComponentListener(new ComponentAdapter() public void componentResized(ComponentEvent ce) canvas.fanning(); );show();canvas.fanning();private void createMenu()bar.add(mGame);bar.add(mControl);bar.add(mhelp);mGame.add(miNewGame);mGam

23、e.addSeparator();mGame.add(milevelup);mGame.addSeparator();mGame.add(mileveldown);mGame.addSeparator();mGame.add(miExit);mControl.add(miPlay);mControl.addSeparator();mControl.add(miPause);mControl.addSeparator();mControl.add(miResume);mControl.addSeparator();mControl.add(miStop);mhelp.add(miCtrlBloc

24、k);setJMenuBar(bar);miNewGame.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) stopGame();reset();setLevel(initlevel);); mileveldown.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae)int curLevel = getLevel();if (curLevel > 1) setL

25、evel(curLevel - 1););milevelup.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae)int curLevel = getLevel();if (curLevel > 1) setLevel(curLevel+1););miExit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) System.exit(0););miPlay.add

26、ActionListener(new ActionListener() public void actionPerformed(ActionEvent ae)playGame(););miPause.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae)pauseGame(););miResume.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae)resumeGame();

27、);miStop.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae)stopGame(););miCtrlBlock.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) reportGameMethod(); ); public void reset() ctrlPanel.reset(); canvas.reset(); public boolean isPlayin

28、g() return playing;public ErsBlock getCurBlock()return block;public GameCanvas getCanvas()return canvas;public void playGame()play();ctrlPanel.setPlayButtonEnable(false);miPlay.setEnabled(false);ctrlPanel.requestFocus();public void pauseGame() if (block != null) block.pauseMove();ctrlPanel.setPauseB

29、uttonLabel(false);miPause.setEnabled(false);miResume.setEnabled(true);public void resumeGame() if (block != null)block.resumeMove();ctrlPanel.setPauseButtonLabel(true);miPause.setEnabled(true);miResume.setEnabled(false);ctrlPanel.requestFocus();public void stopGame()playing = false;if (block != null

30、)block.stopMove();miPlay.setEnabled(true);miPause.setEnabled(true);miResume.setEnabled(false);ctrlPanel.setPlayButtonEnable(true);ctrlPanel.setPauseButtonLabel(true);public int getLevel() return ctrlPanel.getLevel();public void setLevel(int level)if (level < 11 && level > 0) ctrlPanel.

31、setLevel(level);public int getScore() if (canvas != null)return canvas.getScore();return 0;public int getScoreForLevelUpdate()if (canvas != null) return canvas.getScoreForLevelUpdate();return 0;public boolean levelUpdate()int curLevel = getLevel();if (curLevel < maxlevel)setLevel(curLevel + 1);ca

32、nvas.resetScoreForLevelUpdate();return true;return false;private void play() reset();playing = true;Thread thread = new Thread(new Game();thread.start();private void reportGameMethod() JOptionPane.showMessageDialog(this, "J为向左移动,L为向右移动,K为加速向下,I 为翻转变化");private void reportGameOver() JOption

33、Pane.showMessageDialog(this, "游戏结束!"); private class Game implements Runnable public void run() int col = (int) (Math.random() * (canvas.getCols() - 3), style = ErsBlock.STYLES(int) (Math.random() * 7)(int) (Math.random() * 4);while (playing)if (block != null) if (block.isAlive() try Threa

34、d.currentThread().sleep(100); catch (InterruptedException ie) ie.printStackTrace();continue;checkFullLine(); if (isGameOver() miPlay.setEnabled(true);miPause.setEnabled(true);miResume.setEnabled(false);ctrlPanel.setPlayButtonEnable(true);ctrlPanel.setPauseButtonLabel(true);reportGameOver();return;bl

35、ock = new ErsBlock(style, -1, col, getLevel(), canvas);block.start();col = (int) (Math.random() * (canvas.getCols() - 3);style = ErsBlock.STYLES(int) (Math.random() * 7)(int) (Math.random() * 4);ctrlPanel.setShowBeforeStyle(style);public void checkFullLine() int row ;for (int i = 0; i < canvas.ge

36、tRows(); i+) /int row = 1;boolean fullLineColorBox = true;for (int j = 0; j < canvas.getCols(); j+) if (!canvas.getBox(i, j).isColorBox()fullLineColorBox = false;break;if (fullLineColorBox)row = i;canvas.removeLine(row);private boolean isGameOver()for (int i = 0; i < canvas.getCols(); i+)ErsBo

37、x box = canvas.getBox(0, i);if (box.isColorBox() return true;return false;public static void main(String args)new ErsBlocksGame("俄罗斯方块游戏");class ControlPanel extends JPanelprivate JTextField tfLevel = new JTextField("" + ErsBlocksGame.initlevel), tfScore = new JTextField("0&

38、quot;);private JButton btPlay = new JButton("开始"), btPause = new JButton("暂停"), btStop = new JButton("停止"), btTurnLevelUp = new JButton("提高等级"), btTurnLevelDown = new JButton("降低等级");private JPanel showbefore = new JPanel(new BorderLayout();private S

39、howBeforePanel plShowBeforeBlock = new ShowBeforePanel();private JPanel plInfo = new JPanel(new GridLayout(4, 1);private JPanel plButton = new JPanel(new GridLayout(5, 1);private Timer timer;private ErsBlocksGame game;public ControlPanel(final ErsBlocksGame game) setLayout(new GridLayout(3, 1, 0, 4)

40、;this.game = game;showbefore.add(new JLabel("下一个方块"), BorderLayout.NORTH);showbefore.add(plShowBeforeBlock);plInfo.add(new JLabel("等级");plInfo.add(tfLevel);plInfo.add(new JLabel("得分");plInfo.add(tfScore); tfLevel.setEditable(false);tfScore.setEditable(false);plButton.ad

41、d(btPlay);plButton.add(btPause);plButton.add(btStop);plButton.add(btTurnLevelUp);plButton.add(btTurnLevelDown);add( showbefore);add(plInfo);add(plButton);addKeyListener(new ControlKeyListener();btPlay.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) game.playGame();

42、);btPause.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) if (btPause.getText().equals(new String("暂停") game.pauseGame();else game.resumeGame(););btStop.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) game.stopGame(););b

43、tTurnLevelUp.addActionListener(new ActionListener() public void actionPerformed(ActionEvent ae) try int level = Integer.parseInt(tfLevel.getText();if (level < ErsBlocksGame.maxlevel)tfLevel.setText("" + (level + 1); catch (NumberFormatException e) requestFocus(););btTurnLevelDown.addAct

44、ionListener(new ActionListener() public void actionPerformed(ActionEvent ae) try int level = Integer.parseInt(tfLevel.getText();if (level > 1)tfLevel.setText("" + (level - 1); catch (NumberFormatException e) requestFocus(););addComponentListener(new ComponentAdapter() public void compon

45、entResized(ComponentEvent ce)plShowBeforeBlock.fanning(););timer = new Timer(500, new ActionListener() public void actionPerformed(ActionEvent ae) tfScore.setText("" + game.getScore();int scoreForLevelUpdate = game.getScoreForLevelUpdate();if (scoreForLevelUpdate >= ErsBlocksGame.everyl

46、evelscore && scoreForLevelUpdate > 0)game.levelUpdate(); );timer.start(); public void setShowBeforeStyle(int style)plShowBeforeBlock.setStyle(style); public int getLevel() int level = 0;try level = Integer.parseInt(tfLevel.getText(); catch (NumberFormatException e) return level;public voi

47、d setLevel(int level) if (level > 0 && level < 11)tfLevel.setText("" + level);public void setPlayButtonEnable(boolean enable)btPlay.setEnabled(enable);public void setPauseButtonLabel(boolean pause) btPause.setText(pause ? "暂停" : "继续");public void reset() tfScore.setText("0

温馨提示

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

最新文档

评论

0/150

提交评论