qq_32171635 2016-06-14 04:37
浏览 1013

贪吃蛇监听器自定义问题

packagesnake.box;

importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.Point;
importjava.util.HashSet;
importjava.util.LinkedList;
importjava.util.Set;

importsnake.listener.SnakeListener;

publicclassSnake {
身体
LinkedList body =newLinkedList<>();

//方向
privatestaticintdirection;
//方向常量
publicstaticfinalintUP=1;
publicstaticfinalintDOWN=-1;
publicstaticfinalintLEFT=2;
publicstaticfinalintRIGHT=-2;

//监听器

Set listeners =newHashSet<>();

/*
 * 构造器
 */
publicSnake(){
    init();
}
/*
 * 初始化
 */
publicvoidinit(){
    intx=(Rec.Width*Rec.Cell_Size)/2;
    inty=(Rec.height*Rec.Cell_Size)/2;
    for(inti=0;i<3;i++){
        this.body.addFirst(newPoint(x--,y));

    }
    this.direction=Snake.LEFT;
}


publicvoidmove(){
    body.removeLast();
    intx=body.getFirst().x;
    inty=body.getFirst().y;
    switch(this.direction){
    caseSnake.UP:
        y--;
        break;
    caseSnake.DOWN:
        y++;
        break;
    caseSnake.LEFT:
        x--;
        break;
    caseSnake.RIGHT:
        x++;
        break;
    }
    body.addFirst(newPoint(x,y));
}

publicvoidchangeDirection(intdirection){
        this.direction=direction;
}

publicvoideatFood(){

}
publicvoideatBody(){

}
publicvoiddraw(Graphics g){
    Color c=g.getColor();

    for(Point p :body){
        g.setColor(Color.black);
        g.fill3DRect(p.x, p.y, Rec.Cell_Size, Rec.Cell_Size,true);
    }
    g.setColor(c);
}

publicvoidaddListener(SnakeListener l){
    if(l!=null){
        listeners.add(l);
    }
}

publicvoidbegin(){
    newThread(newSnakeDriver()).start();
}

classSnakeDriverimplementsRunnable{

        @Override
        publicvoidrun() {
            // TODO Auto-generated method stub
            while(true){
                try{
                    move();

for(SnakeListener listener :listeners){

listener.SnakeMoved(Snake.this);

                    }
                    Thread.sleep(100);
                }catch(InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

}

packagesnake.game;

importjava.awt.BorderLayout;

importjavax.swing.JFrame;

importsnake.box.Food;
importsnake.box.Rec;
importsnake.box.Snake;
importsnake.box.Stone;
importsnake.control.Control;
importsnake.view.GamePanel;

publicclassgameTest {
publicstaticvoidmain(String[] args) {
Food food =newFood();
Snake snake =newSnake();
Stone stone =newStone();
GamePanel gamepanel=newGamePanel();
Control control =newControl(food, snake, stone, gamepanel);

    JFrame f =newJFrame("贪吃蛇");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(newBorderLayout());
    f.add(gamepanel,BorderLayout.CENTER);
    f.addKeyListener(control);
    f.setVisible(true);
    f.setSize(Rec.Width*Rec.Cell_Size,
            Rec.height*Rec.Cell_Size);
    snake.addListener(control);
    control.newGame();

}

}


packagesnake.game;

importjava.awt.BorderLayout;

importjavax.swing.JFrame;

importsnake.box.Food;
importsnake.box.Rec;
importsnake.box.Snake;
importsnake.box.Stone;
importsnake.control.Control;
importsnake.view.GamePanel;

publicclassgameTest {
publicstaticvoidmain(String[] args) {
Food food =newFood();
Snake snake =newSnake();
Stone stone =newStone();
GamePanel gamepanel=newGamePanel();
Control control =newControl(food, snake, stone, gamepanel);

    JFrame f =newJFrame("贪吃蛇");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(newBorderLayout());
    f.add(gamepanel,BorderLayout.CENTER);
    f.addKeyListener(control);
    f.setVisible(true);
    f.setSize(Rec.Width*Rec.Cell_Size,
            Rec.height*Rec.Cell_Size);

snake.addListener(control);

    control.newGame();

}

}
为什么用使用Set来装listener,只声明一个Listener不行吗?在主函数中,最终也只加了一个监听。使用set不有点多余吗?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料