| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1031 人关注过本帖
标题:麻烦帮我解释一下这个程序的构造函数的作用
只看楼主 加入收藏
hellenism
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2009-2-27
结帖率:76.47%
收藏
已结贴  问题点数:10 回复次数:1 
麻烦帮我解释一下这个程序的构造函数的作用
import javax.swing.*;

public class GreedSnake extends JApplet implements Runnable { //继承JApplet类 继承Runnable接口
    /**
     *
     */
    private static final long serialVersionUID = -6948242468600811775L;
    public static int[] snake = new int[80]; //数组
    public static int sl = 3;//蛇头连身子一起的长度
    public static int dir = 0;// 蛇头方向,0右1左2下3上 当dir=0 表示向右 dir=1表示向左 dir=2表示向下
    private Thread thread1 = null;
    private static int time = 300;
    private static boolean moving = true;
    private static boolean stop = false;

    public GreedSnake() { // 这里看不懂
        snake[0] = 200;// 蛇身初始化
        snake[1] = 100;//??
        snake[2] = 0;//??
    }

    public void start() {//重写线程的start()方法
        if (thread1 == null) {
            thread1 = new Thread(this, "Moving");

            thread1.start();
        }

    }

    public void run() {//线程的run()方法
        setFocusable(true);//将此 Component 的焦点状态设置为指定值。此值覆盖 Component 的默认焦点状态。
        //建立按键事监听类 监听发生了什么事件
        //发生按下按钮事件时候 该控件能够接受到事件做出的响应
        addKeyListener(new KeyLis());//添加指定的按键侦听器,以接收发自此组件的按键事件。如果 l 为 null,则不会抛出异常并且不执行动作。
        
        Canvas.j = (int) Math.round(Math.random() * 29 + 0);// 食物位置 随即生成的
        Canvas.w = (int) Math.round(Math.random() * 18 + 0);

        //因为它是一个线程
        
        while (!stop)
            while (!stop)
            {
                switch (dir)//这个值由实践响应机制来实现传入
                {// 键盘控制蛇头方向
                case 0:
                {
                    for (int i = sl - 1; i > 0; i--) // i=2 i=1
                        // 0右1左2下3上
                        snake[i] = snake[i - 1]; //snake[2]=snake[1];
                    snake[0] = snake[0] + 100;

                    break;
                }
                case 1:
                {
                    for (int i = sl - 1; i > 0; i--) // i=2 i=1
                        // 0右1左2下3上
                        snake[i] = snake[i - 1];
                    snake[0] = snake[0] - 100;
                    break;
                }
                case 2:
                {
                    for (int i = sl - 1; i > 0; i--) // i=2 i=1
                        // 0右1左2下3上
                        snake[i] = snake[i - 1];
                    snake[0] = snake[0] + 1;
                    break;
                }
                case 3:
                {
                    for (int i = sl - 1; i > 0; i--) // i=2 i=1
                        // 0右1左2下3上
                        snake[i] = snake[i - 1];
                    snake[0] = snake[0] - 1;
                    break;
                }
                default:
                    break;
               
                }//switch结束 按下方向键之后
               
                tail();//每次按了按钮都要去判断是否咬到尾巴
               
                if (snake[0] / 100 == Canvas.j && snake[0] % 100 == Canvas.w)
                {//如果吃到食物
                    int i = 0;
                    snake[sl] = snake[sl - 1];
                    //在生产食物坐标
                    
                    Canvas.j = (int) Math.round(Math.random() * 29 + 0);
                    Canvas.w = (int) Math.round(Math.random() * 18 + 0);
                    
                    while (i >= 0)
                    {
                        Canvas.j = (int) Math.round(Math.random() * 29 + 0);
                        Canvas.w = (int) Math.round(Math.random() * 18 + 0);
                        
                        for (i = sl; i >= 0; i--)
                        {
                            if (snake[i] / 100 != Canvas.j|| snake[i] % 100 != Canvas.w)//如果没吃到食物 一直for语句
                                ;//空语句 意思就是继续for循环 如果不满足就跳出循环
                            else
                                break;
                        }

                    }
                    
                    sl++;//能来到这里 说明吃到食物了
                    
                    if (sl == 80) {// 判断是否胜利
                        JOptionPane.showMessageDialog(null, "You Win!");
                        moving = false;
                    }
                    
                    if (sl % 10 == 0)// 游戏升级
                        time = time - 30; //间隔时间减少 蛇的移动速度加快

                }//吃食物完毕
               
               
                if (snake[0] / 100 >= 0 && snake[0] % 100 >= 0&& snake[0] / 100 < 30 && snake[0] % 100 < 19 && moving) {
                    repaint();//重绘此组件。
                    
                    try {
                        Thread.sleep(time); //线程的睡眠时间 时间越小 睡的越短 蛇移动速度越快速
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else
                    stop = true;

            }
        JOptionPane.showMessageDialog(null, "GameOver");// 游戏结束

    }

    public void tail() {// 判断是否咬到自己尾巴
        for (int i = sl; i > 0; i--) {
            if (snake[0] == snake[i])
                moving = false;
        }
    }

    public void init() {
        Canvas p = new Canvas();

        p.setBorder(BorderFactory.createCompoundBorder(BorderFactory
                .createRaisedBevelBorder(), BorderFactory
                .createLoweredBevelBorder()));// 设置画布边界隆起与内陷
        setLayout(null);
        p.setBounds(0, 0, 452, 312);
        add(p);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame(); //创建框架容器
        GreedSnake game = new GreedSnake(); //创建蛇
        game.init();//创建窗口大小
        game.start();//开始线程
        frame.add(game);
        frame.setTitle("贪吃蛇");
        frame.setSize(455, 314);
        frame.setLocationRelativeTo(null);//设置窗口相对于指定组件的位置
        frame.setVisible(true);//是否可见
        frame.setResizable(false);//设置此 dialog 是否可以由用户调整大小 是否可以调整大小
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置当用户在此对话框上启动 "close" 时默认执行的操作
    }
}





import java.awt.*;

import javax.swing.*;

class Canvas extends JPanel { //画画类
    /**
     *
     */
    private static final long serialVersionUID = -4636326156229619914L;
    public static int j, w;// 食物坐标

    public void paintComponent(Graphics g) {
        super.paintComponent(g);//画一个BOX
        
        setBackground(Color.pink);// 设置画布背景颜色
        
        g.setColor(Color.red);// 蛇头颜色
        
        //snake[0]=200; 200/100 =2 2*15=30 snake[0]%100 =1 1*
        g.fill3DRect((GreedSnake.snake[0] / 100) * 15 + 1,
                (GreedSnake.snake[0] % 100) * 15 + 1, 15, 15, true);// 蛇头位置 绘制一个用当前颜色填充的 3-D 高亮显示矩形。矩形的边是高亮显示的,以至于从左上角看呈斜面并加亮。高亮显示效果所用的颜色根据当前颜色确定
        
        //fill3DRect(x坐标,y坐标,宽度,长度)
        
        g.setColor(Color.gray);// 蛇身颜色
        
        for (int i = 1; i < GreedSnake.sl; i++) { //根据s1来画身子个个数 不用-1应为执行不到s1这个值

            //snake[1]=100; 100/100=1; 1*15+1=16;
            //snake[1]=100; 100%100=0; 0+1=1;
            //snake[0]=0; 0/100=0;     0+1=1;
            //                             1;
            
            
            g.fill3DRect((GreedSnake.snake[i] / 100) * 15 + 1,
                    (GreedSnake.snake[i] % 100) * 15 + 1, 15, 15, true);// 绘制蛇身

        }
        g.setColor(Color.green);// 食物颜色
        g.fill3DRect(j * 15 + 1, w * 15 + 1, 15, 15, true);// 食物位置

    }
}




import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

class KeyLis implements KeyListener {
    static int temptime;

    public void keyPressed(KeyEvent e) {

        switch (e.getKeyCode()) {

        case KeyEvent.VK_LEFT: { //如果事件是按左键
            GreedSnake.dir = 1;
        }
            break;

        case KeyEvent.VK_RIGHT: { //如果事件是按右键
            GreedSnake.dir = 0;
        }
            break;

        case KeyEvent.VK_UP: {  //上
            GreedSnake.dir = 3;
        }
            break;
        case KeyEvent.VK_DOWN: { //下
            GreedSnake.dir = 2;
        }
            break;
        default:
            break;
        }

    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}
搜索更多相关主题的帖子: 函数 构造 麻烦 解释 
2010-06-02 20:14
爱OO你
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:30
专家分:181
注 册:2010-4-13
收藏
得分:10 

      public GreedSnake() { // 这里看不懂
        snake[0] = 200;// 蛇身初始化
        snake[1] = 100;//??
        snake[2] = 0;//??
   
      是根据画布 类Canvas的界面参数所定义的,画布的单个网格的具体绘画的参数,
      这个程序的构造函数的作用就很简单了,就是初始化蛇身的长度为三个网格(在画布上显示的)。



我就是我
2010-06-03 01:00
快速回复:麻烦帮我解释一下这个程序的构造函数的作用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018039 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved