10+31=100小组项目第五周总结报告

项目进度

本周我们将之前未理解的代码,重新进行了理解消化,并且将完整代码加入了注释,代码如下:

package resource;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.Random;

@SuppressWarnings("serial")
public class ControlSnake extends JPanel implements ActionListener {
    Random rand;

    ArrayList<Point> list, listBody;

    String str, str1;

    static boolean key;

    int x, y, dx, dy, fx, fy, flag;

    int snakeBody;

    int speed;

    public ControlSnake() {
        snakeBody = 1;

        str = "上下左右方向键控制 P键暂停...";//显示操作方法
        str1 = "现在的长度为:" + snakeBody;//显示统计的蛇身长度
        key = true;
        flag = 1;

        speed = 700;//初始化速度
        rand = new Random();
        list = new ArrayList<Point>();
        listBody = new ArrayList<Point>();//定义一个数组动态记录蛇身长度

        x = 5;
        y = 5;
        list.add(new Point(x, y));
        listBody.add(list.get(0));

        dx = 10;//初始化移动每次距离
        dy = 0;

        fx = rand.nextInt(30) * 10 + 5;// 2
        fy = rand.nextInt(30) * 10 + 5;// 2

        setBackground(Color.WHITE);
        setSize(new Dimension(318, 380));

        final Timer time = new Timer(speed, this);
        time.start();
//根据键盘监听,通过上下左右键操作游戏,以及暂停、开始游戏
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == 37) {
                    dx = -10;
                    dy = 0;
                } else if (e.getKeyCode() == 38) {
                    dx = 0;
                    dy = -10;
                } else if (e.getKeyCode() == 39) {
                    dx = 10;
                    dy = 0;
                } else if (e.getKeyCode() == 40) {
                    dx = 0;
                    dy = 10;
                } else if (e.getKeyCode() == 80) {
                    if (flag % 2 == 1) {
                        time.stop();
                    }
                    if (flag % 2 == 0) {
                        time.start();
                    }
                    flag++;
                }
            }
        });

    }
//画线(边框)、设置食物颜色、蛇身颜色等,并用白色进行填充
    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 400, 400);
        g.setColor(Color.DARK_GRAY);
        g.drawLine(3, 3, 305, 3);
        g.drawLine(3, 3, 3, 305);
        g.drawLine(305, 3, 305, 305);
        g.drawLine(3, 305, 305, 305);
        g.setColor(Color.PINK);

        for (int i = 0; i < listBody.size(); i++) {
            g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9);//根据不断加长的蛇身填充颜色
        }
        g.fillRect(x, y, 9, 9);
        g.setColor(Color.ORANGE);
        g.fillRect(fx, fy, 9, 9);

        g.setColor(Color.DARK_GRAY);
        str1 = "现在的长度为:" + snakeBody;
        g.drawString(str, 10, 320);
        g.drawString(str1, 10, 335);
    }

    public void actionPerformed(ActionEvent e) {
        x += dx;
        y += dy;
        if (makeOut() == false) {
            JOptionPane.showMessageDialog(null, "重新开始......");//碰到边界重新开始

            speed = 700;

            snakeBody = 1;

            x = 5;
            y = 5;

            list.clear();
            list.add(new Point(x, y));
            listBody.clear();
            listBody.add(list.get(0));

            dx = 10;
            dy = 0;

        }
        addPoint(x, y);
        if (x == fx && y == fy) {
            speed = (int) (speed * 0.8);// 速度增加参数
            if (speed < 200) {
                speed = 100;//随蛇身长度增长速度逐渐变慢
            }
            fx = rand.nextInt(30) * 10 + 5;// 2
            fy = rand.nextInt(30) * 10 + 5;// 2
            snakeBody++;// 2
        } // 2
        repaint();
    }

    public void addPoint(int xx, int yy) {
        // 动态的记录最新发生的50步以内的移动过的坐标
        // 并画出最新的snakeBody
        if (list.size() < 100) {// 蛇身长度最长为100
            list.add(new Point(xx, yy));
        } else {
            list.remove(0);
            list.add(new Point(xx, yy));
        }
        if (snakeBody == 1) {
            listBody.remove(0);
            listBody.add(0, list.get(list.size() - 1));
        } else {
            listBody.clear();
            if (list.size() < snakeBody) {
                for (int i = list.size() - 1; i > 0; i--) {
                    listBody.add(list.get(i));
                }
            } else {
                for (int i = list.size() - 1; listBody.size() < snakeBody; i--) {
                    listBody.add(list.get(i));
                }
            }
        }
    }
//碰到边界即为游戏失败
    public boolean makeOut() {
        if ((x < 3 || y < 3) || (x > 305 || y > 305)) {
            return false;
        }
        for (int i = 0; i < listBody.size() - 1; i++) {
            for (int j = i + 1; j < listBody.size(); j++) {
                if (listBody.get(i).equals(listBody.get(j))) {
                    return false;
                }
            }
        }
        return true;
    }
}

package resource;

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class MainClass extends JFrame {
    ControlSnake control;

    Toolkit kit;

    Dimension dimen;

    public static void main(String[] args) {
        new MainClass("my snake");
    }

    public MainClass(String s) {
        super(s);
        control = new ControlSnake();
        control.setFocusable(true);
        kit = Toolkit.getDefaultToolkit();
        dimen = kit.getScreenSize();

        add(control);
        setLayout(new BorderLayout());
        setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3
        setSize(FWIDTH, FHEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public static final int FWIDTH = 315;

    public static final int FHEIGHT = 380;//限定游戏中贪吃蛇的活动范围
}

成员贡献

20145231 熊梓宏: 代码注释,博客编写;

20145210 姚思羽:代码完善;

两人共同完成:代码理解。

项目总结

至此,我们的项目已全部完成,在此次做项目的过程中,我们学习到了很多javaAPI的使用,掌握了很多类的用法,看到了java语言的强大用途,加强了想要学好Java的决心。对于我们的项目,虽然是在前人的基础上完成的,但是我依然觉得,我们能够理解代码、完善代码,这是对我们的努力的肯定。通过这次的项目,我们学会了严谨的思考问题,考虑问题更加全面周到,是一次不错的体验。

posted @ 2016-06-12 14:34  10+31=100  阅读(103)  评论(0编辑  收藏  举报