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不有点多余吗?