当前位置首页 > 建筑/施工 > 施工组织
搜柄,搜必应! 快速导航 | 使用教程  [会员中心]

Java俄罗斯方块单人游戏课程设计

文档格式:DOC| 37 页|大小 164KB|积分 10|2022-08-24 发布|文档ID:141512892
第1页
下载文档到电脑,查找使用更方便 还剩页未读,继续阅读>>
1 / 37
此文档下载收益归作者所有 下载文档
  • 版权提示
  • 文本预览
  • 常见问题
  • JAVA程序设计 课程设计报告课 题: 俄罗斯方块单人游戏 姓 名: 赵 云 杰 学 号: 201417030202 同组姓名: 刘杨、汪世军、成功 专业班级: 网络工程14102班 指导教师: 谭 文 学 设计时间: 评阅意见:评定成绩: 指导老师签名: 年 月 日目 录1. 系统概述……………………………………………… 32. 设计说明书…………………………………………… 53. 系统操作界面………………………………………… 74. 源程序编码…………………………………………… 285. 测试计划……………………………………………… 296. 改进意见…………………………………………………317. 课程设计心得体会………………………………………328. 参考书籍、资料…………………………………………34系统概述1.1现状分析在个人电脑日益普及的今天,一些有趣的桌面游戏已经成为人们在使用计算机进行工作或学习之余休闲娱乐的首选,而俄罗斯方块游戏是人们最熟悉的小游戏之一,它以其趣味性强,易上手等诸多特点得到了大众的认可,因此开发此游戏软件可满足人们的一些娱乐的需求。

    此俄罗斯方块游戏可以为用户提供一个可在普通个人电脑上运行的,界面美观的,易于控制的俄罗斯方块游戏1.2项目要求俄罗斯方块游戏是一款适合大众的游戏软件,它适合不同年龄的人玩本软件要实现的功能如下:(1)游戏区:玩家可以在游戏区中堆积方块,并能够在游戏过程中随时了解得分情况2)游戏控制:玩家可以通过游戏控制功能来选择开始新的一局游戏,暂停或退出游戏3) 级别设置:玩家可以根据自己的需要自行设定游戏的开始级别,级别越高,游戏的速度越快,难度越大1.3系统功能模块示意图俄罗斯方块游戏游戏区游戏控制显示玩家操作显示操作结果开始暂停/继续提高等级退出降低等级 设计说明 1.1游戏区模块游戏区模块创建游戏区处理玩家游戏操作显示游戏结果1.2控制区模块游戏控制模块开始游戏暂停游戏初始级别设置退出游戏1.3系统流程图是否到顶部处理玩家操作开始设置初始级别创建游戏区游戏开局随机选择方块类型是否到顶部方块下落一行游戏结束是否1.4模块简介(1)模块功能简介:模块4-方块颜色、形状、变化此模块主要通过接口、类,来实现俄罗斯方块的颜色、形状(共28种形态)、变化(包括移动与变形)方块的颜色(出现时为绿色,提示出现时为红色)(2)功能模块图:方块的形状:28种方 块方块的变化(移动与变形) 模块实现代码: class 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 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 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, 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 ErsBox[boxes_rows][boxes_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++) { for (int j = 0; j < boxes[i].length; j++) { boolean isColor = ((style & key) != 0); boxes[i][j] = new ErsBox(isColor); key >>= 1; } } display(); } public void run() { while (moving) { try { sleep(betweenleveltime * (ErsBlocksGame.maxlevel - level + flatgene)); } catch (InterruptedException ie) { ie.printStackTrace(); } if (!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 (STYLES[i][j] == style) { int newStyle = STYLES[i][(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 < boxes[i].length; j++) { if (boxes[i][j].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 < boxes[i].length; j++) { if (boxes[i][j].isColorBox()) { ErsBox box = canvas.getBox(y + i, x + j); if (box == null) continue; box.setColor(true); } } } } private boolean isMoveAble(int newRow, int newCol) { earse(); for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if (boxes[i][j].isColorBox()) { ErsBox box = canvas.getBox(newRow + i, newCol + j); if (box == null || (box.isColorBox())) { display(); return false; } } } } display(); return 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++) { for (int j = 0; j < boxes[i].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(); int key = 0x8000; for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { boolean isColor = ((newStyle & key) != 0); boxes[i][j].setColor(isColor); key >>= 1; } } style = newStyle; display(); canvas.repaint(); return true; }}模块实现:颜色: (提示出现时为红色); (出现时为绿色) 形状: 注:每一个方块有四种形态,共计7*4=28种源程序编码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 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 JMenuItem miNewGame = new JMenuItem("新游戏"), milevelup = new JMenuItem("提高级数"), mileveldown = new JMenuItem("降低级数"), miExit = new JMenuItem("退出"), miPlay = new JMenuItem("开始"), miPause = new JMenuItem("暂停"), miResume = new JMenuItem("重新开始"), 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().height) / 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() { public 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); mGame.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(miCtrlBlock);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) setLevel(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.addActionListener(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(); } }); 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 isPlaying() { 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.setPauseButtonLabel(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) 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.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); canvas.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() { JOptionPane.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 { Thread.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; } block = 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.getRows(); 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++) { ErsBox box = canvas.getBox(0, i); if (box.isColorBox()) return true; } return false; } } public static void main(String[] args) { new ErsBlocksGame("俄罗斯方块游戏"); }}class ControlPanel extends JPanel{ private JTextField tfLevel = new JTextField("" + ErsBlocksGame.initlevel), tfScore = new JTextField("0"); 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 ShowBeforePanel 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)); 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.add(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(); } }); 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(); } }); btTurnLevelUp.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.addActionListener(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 componentResized(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.everylevelscore && 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 void 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"); plShowBeforeBlock.setStyle(0); } public void fanning() { plShowBeforeBlock.fanning(); } private class ShowBeforePanel extends JPanel { private Color backColor = Color.darkGray, frontColor = Color.red; private ErsBox[][] boxes =new ErsBox[ErsBlock.boxes_rows][ErsBlock.boxes_cols]; private int style, boxWidth, boxHeight; private boolean isTiled = false; public ShowBeforePanel() { for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) boxes[i][j] = new ErsBox(false); } } public ShowBeforePanel(Color backColor, Color frontColor) { this(); this.backColor = backColor; this.frontColor = frontColor; } public void setStyle(int style) { this.style = style; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); if (!isTiled) fanning(); int key = 0x8000; for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { Color color = (((key & style) != 0) ? frontColor : backColor); g.setColor(color); g.fill3DRect(j * boxWidth, i * boxHeight, boxWidth, boxHeight, true); key >>= 1; } } } public void fanning() { boxWidth = getSize().width / ErsBlock.boxes_cols; boxHeight = getSize().height / ErsBlock.boxes_rows; isTiled = true; } } private class ControlKeyListener extends KeyAdapter { public void keyPressed(KeyEvent ke) { if (!game.isPlaying()) return; ErsBlock block = game.getCurBlock(); switch (ke.getKeyCode()) { case KeyEvent.VK_DOWN: block.moveDown(); break; case KeyEvent.VK_LEFT: block.moveLeft(); break; case KeyEvent.VK_RIGHT: block.moveRight(); break; case KeyEvent.VK_UP: block.turnNext(); break; default: break; } } }} class GameCanvas extends JPanel { private Color。

    点击阅读更多内容
    卖家[上传人]:无极剑圣
    资质:实名认证