java课程设计报告-俄罗斯方块

 JAVA程序设计课程设计

 俄罗斯方块

学院:计算机

年级:09级

班级:网络工程1班

指导老师:

小组成员:

时间:20##年12月15日

目录

摘要............................................................... 1

第一章 课程设计要求................................................ 2

第二章 设计概要.................................................... 3

2.1  功能设计................................................... 3

2.2  程序系统功能模块........................................... 3

2.3  功能分析................................................... 4

2.3.1 功能流程图............................................ 4

2.3.2 系统操作界面.......................................... 5

2.3.3  程序主要功能说明..................................... 5

1.界面按钮设计和功能的实现.................................. 5

第三章 调试分析与测试结果......................................... 13

3.1  游戏运行界面.............................................. 13

3.2  测试项目.................................................. 13

3.2.1  功能区按键测试...................................... 13

3.2.2  键盘功能测试........................................ 14

3.2.3  计分与过关测试...................................... 14

3.2.4  游戏结束测试........................................ 14

第四章 设计总结................................................... 15

4.1  改进意见.................................................. 15

4.2 《Java课程设计》心得体会.................................. 15

第五章 参考文献................................................... 16

摘要

在现代,高科技的飞跃发展,人们工作习惯的改变,特别是电脑的大量普及,人们生活节奏越来越快,一些有趣的桌面游戏已经成为人们在使用计算机进行工作或学习之余休闲娱乐的首选,而俄罗斯方块游戏是人们最熟悉的小游戏之一。俄罗斯方块(Tetris, 俄文:Тетрис)是一款风靡全球的电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克谢·帕基特诺夫发明,故得此名。俄罗斯方块的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。 为此,我们设计了一款简单的俄罗斯方块JAVA游戏程序,以便更好的满足广大电脑工作者闲暇之余的消遣,并且也让我们学到编程技术与团队意识。

关键字:俄罗斯方块、JAVA游戏、编程

第一章 课程设计要求

题目名称:俄罗斯方块

题目类型设计型

课程设计目的:

       1)了解Java的基本使用方法。

       2)熟悉eclipse的运行环境。

       3)用JAVA面向对象的方法来设计一个俄罗斯方块的小游戏。

       4)基本实现俄罗斯方块的应用功能。

实验原理:

JAVA程序分析与设计、类的灵活运用、多态技术、模板技术、异常处理等。

实验内容:

本俄罗斯方块游戏是对于随机给出不同的形状(长条形、Z字形、反Z形、田字形、7字形、反7形、T字型)下落填充给定的区域,若填满一条便消掉,记分,当达到一定的分数时,过关,每关方块下落的速度不同,若在游戏中各形状填满了给定区域,为输者。

 

第二章 设计概要

2.1  功能设计

本项目是为了实现俄罗斯方块的基本功能而设计的,基本能够达到俄罗斯方块的各种游戏性。项目需求分析如下:

1)由方块组成的不同的随机图形会从区域上方开始缓慢落下。

2)玩家可以做的操作有:

  以90度为单位旋转方每一格块。

以格子为单位左右移动方块,让方块加速落下。

3)方块移到区域最下方或是着地到其他方块上无法移动时,就会固定在该处,而新的随机图形会出现在区域上方开始落下。

4)当区域中某一列横向格子全部由方块填满,则该列会自动消除并成为玩家的得分。

5)当固定的方块堆到区域最上方,则游戏结束。

2.2  程序系统功能模块

java课程设计报告-俄罗斯方块

2.3  功能分析

2.3.1 功能流程图:

java课程设计报告-俄罗斯方块

2.3.2 系统操作界面

2.3.3  程序主要功能说明

1.界面按钮设计和功能的实现

代码:

class ControlPanel extends JPanel

{

    /**

     *

     */

    privatestaticfinallongserialVersionUID = 1L;

    protectedstaticfinal String DO_NOTHING_ON_CLOSE = null;

    protectedstaticfinal Frame Frame = null;

    protectedstaticfinalintINFORMATION_MESSAGE = 0;

    private JTextField

            tfLevel = new JTextField("" + ErsBlocksGame.initlevel),//初始级别

            tfScore = new JTextField("0");//初始得分

    private JButton

            btList=new JButton("保存高分"),

            btAbout=new JButton("关于"),

            btPlay = new JButton("开始"),

            btPause = new JButton("暂停"),

            btStop = new JButton("停止"),

            btExit=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("Level"));

        plInfo.add(tfLevel);

        plInfo.add(new JLabel("Score"));

        plInfo.add(tfScore);

        tfLevel.setEditable(false);

        tfScore.setEditable(false);

        plButton.add(btPlay);

        plButton.add(btPause);

        plButton.add(btStop);

        plButton.add(btExit);

        plButton.add(btAbout);

        plButton.add(btList);

        add(showbefore);

        add(plInfo);

        add(plButton);

        addKeyListener(new ControlKeyListener());

        btPlay.addActionListener(new ActionListener() {

            publicvoid actionPerformed(ActionEvent ae) {

                game.playGame();

            }

        });

        btPause.addActionListener(new ActionListener() {

            publicvoid actionPerformed(ActionEvent ae) {

                if (btPause.getText().equals(new String("暂停"))) {

                    game.pauseGame();

                }

                else

                {

                    game.resumeGame();

                }

            }

        });

        btStop.addActionListener(new ActionListener() {

            publicvoid actionPerformed(ActionEvent ae)

            {

                game.stopGame();

            }

        });

        btExit.addActionListener(new ActionListener(){

            publicvoid actionPerformed(ActionEvent ae){

                setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

                  int option=JOptionPane.showConfirmDialog(btExit, "确定退出游戏? ", "退出 ",JOptionPane.YES_NO_CANCEL_OPTION);

                      if(option==JOptionPane.YES_OPTION) {

                         System.exit(0);

                         }

                         else

                         {

                         return;

                         }

            }

       

           

            privatevoid setDefaultCloseOperation(String doNothingOnClose) {

                // TODO Auto-generated method stub

               

            }

        });

        btAbout.addActionListener(new ActionListener(){

            publicvoid actionPerformed(ActionEvent ae){

             String message="          俄罗斯方块V1.0";

             String title="关于";

             JOptionPane.showMessageDialog(Frame, message,title,INFORMATION_MESSAGE);

            }

            });

        btList.addActionListener(new ActionListener(){

            publicvoid actionPerformed(ActionEvent ae){

                JFileChooser jfc1=new JFileChooser();

                jfc1.setDialogTitle("保存到…");

                jfc1.showSaveDialog(null);

                jfc1.setVisible(true);

                String url1=jfc1.getSelectedFile().getAbsolutePath();

                BufferedWriter bw;

                try {

                    bw = new BufferedWriter(new FileWriter(url1+".txt"));

                    bw.write("高分"+tfScore.getText());

                    bw.close();

                } catch (IOException e1) {

                    // TODO Auto-generated catch block

                    e1.printStackTrace();

                }

            }

        }

        );

       

2.背景框图形功能

代码:

class GameCanvas extends JPanel //画布类

{

    /**

     *

     */

    privatestaticfinallongserialVersionUID = 1L;

    private Color backColor = Color.WHITE, frontColor = Color.BLUE;

    privateint rows, cols, score = 0, scoreForLevelUpdate = 0;

    private ErsBox[][] boxes;

    privateint boxWidth, boxHeight;

    public GameCanvas(int rows, int cols)

    {

        this.rows = rows;

        this.cols = cols;

        boxes = new ErsBox[rows][cols];

        for (int i = 0; i < boxes.length; i++)

        {

            for (int j = 0; j < boxes[i].length; j++)

            {

                boxes[i][j] = new ErsBox(false);

            }

        }

    }

    public GameCanvas(int rows, int cols,

                      Color backColor, Color frontColor)

    {

        this(rows, cols);

        this.backColor = backColor;

        this.frontColor = frontColor;

    }

    publicvoid setBackgroundColor(Color backColor)

    {

        this.backColor = backColor;

    }

    public Color getBackgroundColor()

    {

        return backColor;

    }

    publicvoid setBlockColor(Color frontColor)

    {

        this.frontColor = frontColor;

    }

    public Color getBlockColor()

    {

        return frontColor;

    }

    publicint getRows()

    {

        return rows;

    }

    publicint getCols()

    {

        return cols;

    }

    publicint getScore()

    {

        return score;

    }

    publicint getScoreForLevelUpdate()

    {

        return scoreForLevelUpdate;

    }

    publicvoid resetScoreForLevelUpdate()

    {

        scoreForLevelUpdate -= ErsBlocksGame.everylevelscore;

    }

    public ErsBox getBox(int row, int col)

    {

        if (row < 0 || row > boxes.length - 1|| col < 0 || col > boxes[0].length - 1)

            returnnull;

        return (boxes[row][col]);

    }

     

    publicvoid fanning()

    {

        boxWidth = getSize().width / cols;

        boxHeight = getSize().height / rows;

    }

    publicvoid paintComponent(Graphics g)

    {

        super.paintComponent(g);

        g.setColor(frontColor);

        for (int i = 0; i < boxes.length; i++)

        {

            for (int j = 0; j < boxes[i].length; j++)

            {

                g.setColor(boxes[i][j].isColorBox() ? frontColor : backColor);

                g.fill3DRect(j * boxWidth, i * boxHeight,

                        boxWidth, boxHeight, true);

            }

        }

    }

    publicsynchronizedvoid removeLine(int row)

    {

        for (int i = row; i > 0; i--)

        {

            for (int j = 0; j < cols; j++)

                boxes[i][j] = (ErsBox) boxes[i - 1][j].clone();

        }

        score += ErsBlocksGame.alinescore;

        scoreForLevelUpdate += ErsBlocksGame.alinescore;

        repaint();

    }

    publicvoid reset()

    {

        score = 0;

        scoreForLevelUpdate = 0;

        for (int i = 0; i < boxes.length; i++)

        {

            for (int j = 0; j < boxes[i].length; j++)

                boxes[i][j].setColor(false);

        }

        repaint();

    }

}

3.方块自动下落功能

代码:

publicvoid run()//方块自动下落

    {

        while (moving)

        {

            try

            {

                sleep(betweenleveltime

                        * (ErsBlocksGame.maxlevel - level));//级别越高速度越快(多线程)

            } catch (InterruptedException ie)

            {

                ie.printStackTrace();

            }

            if (!pausing)

                moving = (moveTo(y + 1, x) && moving);

        }

    }

4.游戏运行功能

代码:

privateclass Game implements Runnable //游戏流程!

    {

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

                            Thread.sleep(100);

                        } catch (InterruptedException ie)

                        {

                            ie.printStackTrace();

                        }

                        continue;

                    }

                }

                checkFullLine();      

                if (isGameOver())

                {     

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

            }

        }

第三章 调试分析与测试结果

3.1  游戏运行界面

3.2  测试项目

3.2.1  功能区按键测试

测试结果:

开始:游戏开始,方块下落;

暂停:游戏暂停;

继续:游戏继续;

停止:游戏停止,再按开始则重新开局;

退出:游戏退出;

关于:打印游戏信息;

保存高分:显示保存路径选择。

3.2.2  键盘功能测试

测试结果:

方向键“↑”:旋转方块;

方向键“↓”:使方块加速下落;

方向键“←”:使方块左移;

方向键“→”:使方块右移。

3.2.3  计分与过关测试

测试结果:经过测试员不断的亲身游戏,本程序的计分与过关项目基本无问题,由于能力有限,只能测试到第五关。

3.2.4  游戏结束测试

第四章 设计总结

4.1  改进意见

本程序中还存在一些不足之处,例如:

1.进一步地功能扩展,如添加音效等;

2.美化玩家进入游戏的界面;

     3.对软件进行进一步更详细的测试,以保证软件的可用性和适应性;

4.利用internet 进行用户意见的收集,以加强对软件的及时更新。

4.2 《Java课程设计》心得体会

通过短短的一周的时间,我们从一个对JAVA编程懵懂的学生到现在可以试着用JAVA进行简单程序的设计与编写,虽然在实验过程中,我们遇到了许多的困难,特别是因为大多数的同学还不太适应面向对象的编程风格和思想,看待问题时总是想到用什么结构来实现该功能,而没有将问题看成一个封装的整体来考虑,所以在这次课程设计中我们接触、体验了面向对象设计,使得思维在向面过程向面向对象过度。而且,在这个过程中,我们还学到了彼此之间怎么去配合,我们一致认为同伴之间的合作是最重要的。在程序的设计中,我们彼此之间在设计的选择中发生了很多的分歧,但是通过大家的讨论协商,都达到了一个共识,最后大家共同努力完成了这次设计实验。所以,首先,在这里,感谢古平老师在这周来的教导,以及感谢小组同学在这段时间里的帮助,因为有你们,才使我们这次课程设计能够预期完成老师布置的任务。

其次,因为这次的学习,使我们对JAVA面向对象的编程有了深一步的了解,因为这次的课程设计,使我在今后的工作和学习中,都有了很到的启发。我想,这对于我们都是个很好的经验,因为团队合作是一种很必要的能力,每个人的能力都是有局限的,没有人能只凭借自己一个人的能力就把一件事情做好,只有通过团队中每个成员的努力,发挥自身的长处,取长补短,最后才能将其做到最好。在这次课程设计实验中,我们学到的所有东西都将让我们获益良多,不管是对于以后的学习还是工作、生活。

第五章 参考文献

1.《JAVA程序设计》朱庆生 古平主编,清华大学出版社。

2.《java课程设计》黄明 梁旭 周绍斌编著,电子工业出版社出版。

3.《java课程设计案例精编》黄晓东编著,中国水利水电出版社出版。

4.《java程序设计实用教程》张永常主编,电子工业出版社出版。

相关推荐