导入Java。啊。Bordlayout
导入Java。啊。颜色;
导入Java。啊。维度;
导入Java。啊。字体;
导入Java。啊。FontMetrics
导入Java。啊。图形;
导入Java。啊。点;
导入Java。啊。事件。行动事件;
导入Java。啊。事件。actionlistener
导入Java。啊。事件。KeyEvent
导入Java。啊。事件。KeyListener
导入Java。乌提尔。收藏;
导入Java。乌提尔。迭代器;
导入Java。乌提尔。LinkedList
导入Java。乌提尔。随机的;
导入Java。乌提尔。计时器;
导入Java。乌提尔。timertask
导入javax。摇摆。JButton
导入javax。摇摆。jframe
导入javax。摇摆。jpanel
公共类蛇游戏扩展了JFrame {
公众的蛇游戏(){ 0
这个。setresizable(false);
这个。setdefaultcloseoperation(EXIT _ ON _ CLOSE);
游戏面板=新建我的面板();
尺寸=游戏面板。get preferred size();
this.setSize(Size.width 6,size。身高56);
添加(游戏面板,边框布局.中心);
新游戏=新的' JButton('新游戏');
newGame.addActionListener(新的ActionListener(){ 0
公共无效操作已执行(操作事件e){ 0
游戏面板。新游戏();
}
});
newGame.addKeyListener(侦听器);
this.add(newGame,BorderLayout .南方);
}
公共静态void main(String[]参数){ 0
新的蛇游戏()。setVisible(真);
}
私人按钮新游戏
私人我的面板游戏面板;
私有密钥侦听器侦听器=新密钥侦听器(){ 0
@覆盖
公共void KeyPress(KeyEvent e){ 0
int code=e . getkey code();
如果(代码==KeyEvent .VK _ UP){ 0
游戏面板。移动(蛇。向上);
}
否则如果(代码==KeyEvent .VK _右)
游戏面板。移动(蛇。右);
}
否则如果(代码==KeyEvent .VK _左){ 0
游戏面板。移动(蛇。左);
}
否则如果(代码==KeyEvent .VK _向下){ 0
游戏面板。移动(蛇。向下);
}
}
@覆盖
public void KeyReleased(KeyEvent e){ 0
}
@覆盖
公共void KeyType(KeyEvent e){ 0
}
};
}
类我的面板扩展了JPanel {
public MyPanel(){ 0
食物=这个。getfoodpoint(蛇。getbody());
}
public void paint组件(){ 0
图形g=这个。getgraphics();
if(g==null)
返回;
//清除
g.clearRect(0,0,this.getWidth()、this。get height());
//绘制边框
设置颜色。黑色);
for(int I=0;我