码迷,mamicode.com
首页 > 其他好文 > 详细

20135129李畅宇实验三

时间:2015-06-11 21:08:06      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

《Java程序设计》课实验报告

 

 

班级:201351              姓名及学号:李畅宇 20135129

指导教师:娄佳鹏            必修/选修:选修

实验日期: 2015年6月4日      实验时间: 15:30~17:30

实验序号:(三)           成绩:

实验步骤

(一)敏捷开发与XP

     学习软件工程的标准和流程,其中主要以敏捷开发流程为主,而其中有一极限编程作为学习流程的主要软件开发方法。包括:编码,测试,倾听,设计,作为学习者,我主要关注和研究了编码标准、结对编程、代码集体所有、测试、重构等。

(二)编码标准

      编码是一个即主观又客观的过程,每个程序员都有他自己的编程风格,但是不管到底是怎么编程,都要有一个良好的编程风格,而这个风格,就是编程标准,让代码具有了良好的可读性。

实验过程记录:

技术分享

技术分享

技术分享

(三)结对编程

     结对编程是我新认识的编程形式,总体来说就是两个人进行一边编程一边测试的过程。我(20135127)和李畅宇(20135129)组成了小组,进行了小游戏和其他小程序的开发。

     在试验中,我俩对于团队精神和合作有了新的认识,相互决策,相互帮助,由于实验楼问题多,所以我和他分别敲了一部分代码,再结合到一起构成完整代码。

(四)版本控制

  对于大型的软件编程,一定会有很多个程序员对整个软件工程进行修缮和维护,在这之间,必然会产生很多个软件版本,为了能够更好的保证代码的安全和高效,产生了代码仓库。

本次实验,我们用了实验楼我的代码库,进行了代码库的开通以及git的实验练习。

实验过程记录:

技术分享

技术分享

技术分享

(五)重构

对于程序的维护和开发便利,重构对于程序员应该说是一个很有利的工具,主要体现在对于大规模程序和代码段的修改中极大的提高效率和安全性。

主要包括,修改名称,对于方法、变量名、常量、类、接口以及参数对象等java编程常常修改和更正的地方。

实验过程记录:

技术分享

技术分享

技术分享

(六)实践项目

   本次java项目选择了“俄罗斯方块”,在试验过程中,使用到了GUI用户界面显示,并同时试写了测试代码。

通过不断地磨合与协作,小游戏成功的运行,并进行了试玩,效果良好,未发现结果错误。

实验过程记录:

技术分享

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

//20135127陶俊杰

/**

 * 游戏主类,继承自JFrame类,负责游戏的全局控制。

 * 内含

 * 1, 一个GameCanvas画布类的实例引用,

 * 2, 一个保存当前活动块(ErsBlock)实例的引用,

 * 3, 一个保存当前控制面板(ControlPanel)实例的引用;

 */

public class ErsBlocksGame extends JFrame {

         /**

          *  每填满一行计多少分

          */

         public final static int PER_LINE_SCORE = 100;

         /**

          * 积多少分以后能升级

          */

         public final static int PER_LEVEL_SCORE = PER_LINE_SCORE * 20;

         /**

          * 最大级数是10级

          */

         public final static int MAX_LEVEL = 10;

         /**

          * 默认级数是5

          */

         public final static int DEFAULT_LEVEL = 5;

 

         private GameCanvas canvas;

         private ErsBlock block;

         private boolean playing = false;

         private ControlPanel ctrlPanel;

 

         private JMenuBar bar = new JMenuBar();

         private JMenu

                 mGame = new JMenu("Game"),

                            mControl = new JMenu("Control"),

                            mWindowStyle = new JMenu("WindowStyle"),

                            mInfo = new JMenu("Information");

         private JMenuItem

                 miNewGame = new JMenuItem("New Game"),

                            miSetBlockColor = new JMenuItem("Set Block Color ..."),

                            miSetBackColor = new JMenuItem("Set Background Color ..."),

                            miTurnHarder = new JMenuItem("Turn up the level"),

                            miTurnEasier = new JMenuItem("Turn down the level"),

                            miExit = new JMenuItem("Eixt"),

 

                            miPlay = new JMenuItem("Play"),

                            miPause = new JMenuItem("Pause"),

                            miResume = new JMenuItem("Resume"),

                            miStop = new JMenuItem("Stop"),

 

                            miAuthor = new JMenuItem("Author : apple@radiantek.com"),

                            miSourceInfo = new JMenuItem("You can get the source code / document by email");

 

 

         private JCheckBoxMenuItem

                 miAsWindows = new JCheckBoxMenuItem("Windows"),

                            miAsMotif = new JCheckBoxMenuItem("Motif"),

                            miAsMetal = new JCheckBoxMenuItem("Metal", true);

 

         /**

          * 主游戏类的构造函数

          * @param title String,窗口标题

          */

         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();

         }

 

         /**

          * 让游戏“复位”

          */

         public void reset() {

                   ctrlPanel.reset();

                   canvas.reset();

         }

 

         /**

          * 判断游戏是否还在进行

          * @return boolean, true-还在运行,false-已经停止

          */

         public boolean isPlaying() {

                   return playing;

         }

 

         /**

          * 得到当前活动的块

          * @return ErsBlock, 当前活动块的引用

          */

         public ErsBlock getCurBlock() {

                   return block;

         }

 

         /**

          * 得到当前画布

          * @return GameCanvas, 当前画布的引用

          */

         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);

         }

 

         /**

          * 得到当前游戏者设置的游戏难度

          * @return int, 游戏难度1-MAX_LEVEL

          */

         public int getLevel() {

                   return ctrlPanel.getLevel();

         }

 

         /**

          * 让用户设置游戏难度

          * @param level int, 游戏难度1-MAX_LEVEL

          */

         public void setLevel(int level) {

                   if (level < 11 && level > 0) ctrlPanel.setLevel(level);

         }

 

         /**

          * 得到游戏积分

          * @return int, 积分。

          */

         public int getScore() {

                   if (canvas != null) return canvas.getScore();

                   return 0;

         }

 

         /**

          * 得到自上次升级以来的游戏积分,升级以后,此积分清零

          * @return int, 积分。

          */

         public int getScoreForLevelUpdate() {

                   if (canvas != null) return canvas.getScoreForLevelUpdate();

                   return 0;

         }

 

         /**

          * 当分数累计到一定的数量时,升一次级

          * @return boolean, ture-update successufl, false-update fail

          */

         public boolean levelUpdate() {

                   int curLevel = getLevel();

                   if (curLevel < MAX_LEVEL) {

                            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 reportGameOver() {

                   JOptionPane.showMessageDialog(this, "Game Over!");

         }

 

         /**

          * 建立并设置窗口菜单

          */

         private void createMenu() {

                   bar.add(mGame);

                   bar.add(mControl);

                   bar.add(mWindowStyle);

                   bar.add(mInfo);

 

                   mGame.add(miNewGame);

                   mGame.addSeparator();

                   mGame.add(miSetBlockColor);

                   mGame.add(miSetBackColor);

                   mGame.addSeparator();

                   mGame.add(miTurnHarder);

                   mGame.add(miTurnEasier);

                   mGame.addSeparator();

                   mGame.add(miExit);

 

                   mControl.add(miPlay);

                   mControl.add(miPause);

                   mControl.add(miResume);

                   mControl.add(miStop);

 

                   mWindowStyle.add(miAsWindows);

                   mWindowStyle.add(miAsMotif);

                   mWindowStyle.add(miAsMetal);

 

                   mInfo.add(miAuthor);

                   mInfo.add(miSourceInfo);

 

                   setJMenuBar(bar);

 

                   miPause.setAccelerator(

                           KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_MASK));

                   miResume.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));

 

                   miNewGame.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent ae) {

                                     stopGame();

                                     reset();

                                     setLevel(DEFAULT_LEVEL);

                            }

                   });

                   miSetBlockColor.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent ae) {

                                     Color newFrontColor =

                                             JColorChooser.showDialog(ErsBlocksGame.this,

                                                     "Set color for block", canvas.getBlockColor());

                                     if (newFrontColor != null)

                                               canvas.setBlockColor(newFrontColor);

                            }

                   });

                   miSetBackColor.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent ae) {

                                     Color newBackColor =

                                             JColorChooser.showDialog(ErsBlocksGame.this,

                                                     "Set color for block", canvas.getBackgroundColor());

                                     if (newBackColor != null)

                                               canvas.setBackgroundColor(newBackColor);

                            }

                   });

                   miTurnHarder.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent ae) {

                                     int curLevel = getLevel();

                                     if (curLevel < MAX_LEVEL) setLevel(curLevel + 1);

                            }

                   });

                   miTurnEasier.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();

                            }

                   });

                   miAsWindows.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent ae) {

                                     String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

                                     setWindowStyle(plaf);

                                     canvas.fanning();

                                     ctrlPanel.fanning();

                                     miAsWindows.setState(true);

                                     miAsMetal.setState(false);

                                     miAsMotif.setState(false);

                            }

                   });

                   miAsMotif.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent ae) {

                                     String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";

                                     setWindowStyle(plaf);

                                     canvas.fanning();

                                     ctrlPanel.fanning();

                                     miAsWindows.setState(false);

                                     miAsMetal.setState(false);

                                     miAsMotif.setState(true);

                            }

                   });

                   miAsMetal.addActionListener(new ActionListener() {

                            public void actionPerformed(ActionEvent ae) {

                                     String plaf = "javax.swing.plaf.metal.MetalLookAndFeel";

                                     setWindowStyle(plaf);

                                     canvas.fanning();

                                     ctrlPanel.fanning();

                                     miAsWindows.setState(false);

                                     miAsMetal.setState(true);

                                     miAsMotif.setState(false);

                            }

                   });

         }

 

         /**

          * 根据字串设置窗口外观

          * @param plaf String, 窗口外观的描述

          */

         private void setWindowStyle(String plaf) {

                   try {

                            UIManager.setLookAndFeel(plaf);

                            SwingUtilities.updateComponentTreeUI(this);

                   } catch (Exception e) {

                   }

         }

 

         /**

          * 一轮游戏过程,实现了Runnable接口

          * 一轮游戏是一个大循环,在这个循环中,每隔100毫秒,

          * 检查游戏中的当前块是否已经到底了,如果没有,

          * 就继续等待。如果到底了,就看有没有全填满的行,

          * 如果有就删除它,并为游戏者加分,同时随机产生一个

          * 新的当前块,让它自动下落。

          * 当新产生一个块时,先检查画布最顶上的一行是否已经

          * 被占了,如果是,可以判断Game Over了。

          */

         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) {    //第一次循环时,block为空

                                               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.setTipStyle(style);

                            }

                   }

 

                   /**

                    * 检查画布中是否有全填满的行,如果有就删除之

                    */

                   public void checkFullLine() {

                            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);

                                     }

                            }

                   }

 

                   /**

                    * 根据最顶行是否被占,判断游戏是否已经结束了。

                    * @return boolean, true-游戏结束了,false-游戏未结束

                    */

                   private boolean isGameOver() {

                            for (int i = 0; i < canvas.getCols(); i++) {

                                     ErsBox box = canvas.getBox(0, i);

                                     if (box.isColorBox()) return true;

                            }

                            return false;

                   }

         }

 

         /**

          * 程序入口函数

          * @param args String[], 附带的命令行参数

          */

         public static void main(String[] args) {

                   new ErsBlocksGame("Russia Blocks by apple@radiantek.com");

         }

}

 

此代码由我和陶俊杰同学共同完成http://www.cnblogs.com/lcyyyyy/

共分为,方块控制,游戏主体,游戏测试;他负责俄罗斯方块的GUI,控制方块形状,判断是否连成一行,生成方块等。我主要负责分数计算,结束判断,游戏界面信息等。

步骤

耗时

百分比

需求分析

 30min

 5%

设计

 1h

 10%

代码实现

 4h

 40%

测试

 4h

 40%

20135129李畅宇实验三

标签:

原文地址:http://www.cnblogs.com/lcyyyyy/p/4569927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!