谁帮我写的一个java贪吃蛇游戏,包括得分等功能!谢谢希望大家能帮忙!
导入javax . swing . ui manager;
导入Java . awt . *;
/**
* & ltp & gt标题:贪吃蛇游戏
* & ltp & gt描述:& lt/p & gt;
* & ltp & gt版权所有:版权所有(c)2005 & lt;/p & gt;
* & ltp & gt公司:& lt/p & gt;
* @作者zsb
* @版本1.0
*/
公共类蛇{
私有布尔packFrame = false
//构造应用程序
公共蛇(){
大型机框架=新大型机();
//验证具有预设大小的帧
//打包具有有用的首选大小信息的帧,例如来自其布局的信息
if (packFrame) {
frame . pack();
}
否则{
frame.validate()。
}
//将窗口居中
dimension screenSize = toolkit . getdefaulttoolkit()。getScreenSize();
dimension frameSize = frame . getsize();
if(framesize . height & gt;screenSize.height) {
framesize . height = screensize . height;
}
if(framesize . width & gt;screenSize.width) {
framesize . width = screensize . width;
}
frame . set location((screensize . width-framesize . width)/2,(screensize . height-framesize . height)/2);
frame . set visible(true);
}
//主方法
公共静态void main(String[] args) {
/*尝试{
ui manager . setlookandfeel(ui manager . getsystemlookandfeelclassname());
}
捕捉(异常e) {
e . printstacktrace();
}*/
new Snake();
}
}
包蛇;
导入Java . awt . *;
导入javax . swing . *;
/**
* & ltp & gt标题:贪吃蛇游戏
* & ltp & gt描述:& lt/p & gt;
* & ltp & gt版权所有:版权所有(c)2005 & lt;/p & gt;
* & ltp & gt公司:& lt/p & gt;
* @作者zsb
* @版本1.0
*/
公共类Help_Dialog扩展JDialog {
JPanel panel 1 = new JPanel();
BorderLayout BorderLayout 1 = new BorderLayout();
JTextArea JTextArea 1 = new JTextArea();
私串help = "游戏描述:\n1:方向键控制蛇的移动方向。"+
" \n2:按开始键开始游戏。"+
\n3:按暂停键暂停游戏,然后按暂停键继续玩游戏+
" \n4:黄色是常见的食物。吃一个就有100积分。”+
" \n5:青色是穿宝。吃一个就能获得100积分。这个宝藏允许玩家穿过蛇一次"+
" \n6:红色是穿墙宝。吃一个就200分。这个宝藏允许玩家穿越一次墙。”+
" \n7:分数达到一定级别,自动升级。到了最高级别,就不升级了。”;
gridbag layout gridbag layout 1 = new gridbag layout();
public Help_Dialog(框架框架、字符串标题、布尔模态){
超级(框架、标题、模态);
尝试{
JB init();
pack();
}
catch(例外){
ex . printstacktrace();
}
}
public Help_Dialog() {
this(null,“”,false);
}
私有void jbInit()抛出异常{
panel 1 . set layout(borderlayout 1);
jtextarea 1 . set background(system color . control);
jtextarea 1 . set editable(false);
JTextArea1.setText("游戏描述:\n1:箭头键控制蛇的移动方向。\n2:按开始键开始游戏。\n3:按暂停键暂停游戏,再按暂停键继续玩游戏。\n4:黄色是普通食物,吃一个会获得100积分。
“5:青色是佩戴宝,吃一个获得100积分。这个宝藏允许玩家通过蛇一次。\n6:红色是穿墙宝,吃一个获得100积分。\n"+
“7:分数到了一定程度,会自动升级。到了最高级别,就不升级了。”);
this . setresizable(false);
this . settitle(" help ");
this.getContentPane()。set layout(gridbaglayout 1);
getContentPane()。add(panel1,new GridBagConstraints(0,0,1,1,1.0,1.0
,GridBagConstraints。中心,网格约束。BOTH,new Insets(0,0,0,0),45,83));
panel 1 . add(jtextarea 1,BorderLayout。中心);
}
}
包蛇;
导入Java . awt . *;
导入Java . awt . event . *;
导入javax . swing . *;
导入com . borland . jbcl . layout . *;
/**
* & ltp & gt标题:贪吃蛇游戏
* & ltp & gt描述:& lt/p & gt;
* & ltp & gt版权所有:版权所有(c)2005 & lt;/p & gt;
* & ltp & gt公司:& lt/p & gt;
* @作者zsb
* @版本1.0
*/
公共类大型机扩展JFrame {
私有JPanel contentPane//表单内容网格
private JToolBar JToolBar 1 = new JToolBar();//工具栏
private JButton JButton 1 = new JButton();//游戏开始按钮
private JButton JButton 2 = new JButton();//游戏暂停按钮
private JButton JButton 3 = new JButton();//游戏退出按钮
private JButton JButton 4 = new JButton();//帮助按钮
private JPanel JPanel 1 = new JPanel();//游戏主体面板容器
private JPanel JPanel _ PlayArea = new JPanel();//游戏面板容器
private XYLayout XYLayout 1 = new XYLayout();//容器布局管理器
private XYLayout XYLayout 2 = new XYLayout();
private XYLayout XYLayout 3 = new XYLayout();
private static final int UP = 1,LEFT = 2,DOWN = 3,RIGHT = 4;//蛇移动的方向
private static final int初学者= 1,中间= 2,专家= 3;//游戏等级常数
私有静态final int ROWS = 30//游戏区域中的行数
私有静态final int COLS = 50//游戏区域中的列数
私有布尔值isPause = false//游戏暂停标志
私有布尔isEnd//游戏结束标志
私蛇身蛇;//蛇
private int得分= 0;//当前分数
private int level =初学者;//当前游戏级别
snake thread thread = new snake thread();//游戏主线程
private GridLayout GridLayout 1 = new GridLayout(ROWS,COLS,0,0);//游戏区域布局
私有JButton[][]play blocks;//游戏区域中的所有方块
JPanel JPanel 2 = new JPanel();
JLabel JLabel 1 = new JLabel();
JLabel JLabel 2 = new JLabel();
JLabel JLabel 3 = new JLabel();
JLabel JLabel 4 = new JLabel();
JLabel JLabel 5 = new JLabel();
JLabel JLabel 6 = new JLabel();
JLabel JLabel 7 = new JLabel();
button group button group 1 = new button group();
JRadioButton JRadioButton 1 = new JRadioButton();
JRadioButton JRadioButton 2 = new JRadioButton();
JRadioButton JRadioButton 3 = new JRadioButton();
//构造框架
公共大型机(){
enableEvents(AWTEvent。窗口_事件_掩码);
尝试{
JB init();
}
捕捉(异常e) {
e . printstacktrace();
}
}
//组件初始化
私有void jbInit()抛出异常{
//seticon image(toolkit . getdefaulttoolkit()。create image(mainframe . class . get resource("[你的图标]");
content pane =(JPanel)this . getcontentpane();
content pane . set layout(xylayout 2);
this . setresizable(false);
this.setSize(新维度(512,414));
This.setTitle(“蛇”);
this . addkey listener(new MainFrame _ this _ key adapter(this));
jbutton 1 . set font(new Java . awt . font(" dialog input ",0,12));
jButton1.setMaximumSize(新维度(79,39));
jButton1.setMinimumSize(新维度(79,39));
jbutton 1 . setpreferredsize(新维度(79,39));
jbutton 1 . setfocuspainted(false);
jbutton 1 . settext(" Start ");
jbutton 1 . addactionlistener(new MainFrame _ jbutton 1 _ actionAdapter(this));
jbutton 1 . addkey listener(new MainFrame _ this _ key adapter(this));
jbutton 2 . settext(" pause ");
jbutton 2 . addactionlistener(new MainFrame _ jbutton 2 _ actionAdapter(this));
jButton2.setPreferredSize(新维度(79,39));
jbutton 2 . setfocuspainted(false);
jButton2.setMinimumSize(新维度(79,39));
jButton2.setMaximumSize(新维度(79,39));
jbutton 2 . set font(new Java . awt . font(" dialog input ",0,12));
jbutton 2 . addkey listener(new MainFrame _ this _ key adapter(this));
jbutton 3 . settext(" exit ");
jbutton 3 . addactionlistener(new MainFrame _ jbutton 3 _ action adapter(this));
jButton3.setPreferredSize(新维度(79,39));
jbutton 3 . setfocuspainted(false);
jButton3.setMinimumSize(新维度(79,39));
jButton3.setMaximumSize(新维度(79,39));
jbutton 3 . set font(new Java . awt . font(" dialog input ",0,12));
jbutton 3 . addkey listener(new MainFrame _ this _ key adapter(this));
jbutton 4 . settext(" Help ");
jbutton 4 . addactionlistener(new MainFrame _ jbutton 4 _ actionAdapter(this));
jButton4.setPreferredSize(新维度(79,39));
jbutton 4 . setfocuspainted(false);
jButton4.setMinimumSize(新维度(79,39));
jButton4.setMaximumSize(新维度(79,39));
jbutton 4 . set font(new Java . awt . font(" dialog input ",0,12));
jbutton 4 . addkey listener(new MainFrame _ this _ key adapter(this));
jpanel 1 . set layout(xylayout 1);
jpanel 1 . addkey listener(new MainFrame _ this _ key adapter(this));
jPanel _ playarea . set layout(gridlayout 1);
jPanel _ playarea . set border(border factory . createetchedborder());
jPanel _ playarea . addkey listener(new MainFrame _ this _ key adapter(this));
jlabel 1 . set font(new Java . awt . font(" dialog input ",0,12));
jlabel 1 . settext(" score:");
jpanel 2 . set layout(xylayout 3);
jlabel 2 . set font(new Java . awt . font(" dialog input ",0,12));
jlabel 2 . settooltiptext(" ");
jlabel 2 . settext(" 0 ");
JLabel3.setText("穿好衣服:");
jlabel 3 . set font(new Java . awt . font(" dialog input ",0,12));
jlabel 4 . set font(new Java . awt . font(" dialog input ",0,12));
jlabel 4 . settext(" 0 ");
JLabel5.setText("穿墙:");
jlabel 5 . set font(new Java . awt . font(" dialog input ",0,12));
jlabel 5 . settooltiptext(" ");
jlabel 6 . settext(" 0 ");
jlabel 6 . set font(new Java . awt . font(" dialog input ",0,12));
jlabel 6 . settooltiptext(" ");
jradiobutton 1 . set font(new Java . awt . font(" dialog input ",0,12));
jradiobutton 1 . settext(" primary ");
jradiobutton 1 . addactionlistener(new MainFrame _ jradiobutton 1 _ actionAdapter(this));
jradiobutton 1 . addkey listener(new MainFrame _ this _ key adapter(this));
jradiobutton 2 . settext(" intermediate ");
jradiobutton 2 . addactionlistener(new MainFrame _ jradiobutton 2 _ actionAdapter(this));
jradiobutton 2 . addkey listener(new MainFrame _ this _ key adapter(this));
jradiobutton 2 . set font(new Java . awt . font(" dialog input ",0,12));
jradiobutton 3 . settext(" advanced ");
jradiobutton 3 . addactionlistener(new MainFrame _ jradiobutton 3 _ actionAdapter(this));
jradiobutton 3 . set font(new Java . awt . font(" dialog input ",0,12));
jradiobutton 3 . addkey listener(new MainFrame _ this _ key adapter(this));
button group 1 . add(jradiobutton 1);
button group 1 . add(jradiobutton 2);
button group 1 . add(jradiobutton 3);
jradiobutton 1 . set selected(true);
jlabel 7 . set font(new Java . awt . font(" dialog input ",0,12));
jlabel 7 . settext(" rank:");
content pane . add(jtoolbar 1,new XYConstraints(0,0,512,-1));
jtoolbar 1 . add(jbutton 1,null);
jToolBar1.add(jButton2,null);
jToolBar1.add(jButton3,null);
jToolBar1.add(jButton4,null);
jtoolbar 1 . addkey listener(new MainFrame _ this _ key adapter(this));
contentPane.add(jPanel1,new XYConstraints(0,43,512,371));
jPanel 1 . add(jPanel _ PlayArea,new XYConstraints(0,33,524,314));
jPanel1.add(jPanel2,new XYConstraints(0,0,498,30));
jpanel 2 . add(jradiobutton 1,new XYConstraints(331,1,-1,-1));
jPanel2.add(jRadioButton3,new XYConstraints(443,1,-1,-1));
jPanel2.add(jRadioButton2,new XYConstraints(390,1,-1,-1));
jPanel2.add(jLabel6,new XYConstraints(245,6,30,-1));
jPanel2.add(jLabel7,new XYConstraints(287,5,47,-1));
jPanel2.add(jLabel5,new XYConstraints(197,6,47,-1));
jPanel2.add(jLabel4,new XYConstraints(166,7,26,-1));
jPanel2.add(jLabel3,new XYConstraints(120,6,38,-1));
jPanel2.add(jLabel1,new XYConstraints(23,6,39,-1));
jPanel2.add(jLabel2,new XYConstraints(62,6,54,-1));
//创建并初始化游戏框。
play blocks = new JButton[ROWS][COLS];
for(int I = 0;我& lt行;i++) {
for(int j = 0;j & ltCOLSj++) {
play blocks[I][j]= new JButton();
播放块[i][j]。set background(color . light gray);
播放块[i][j]。set visible(false);
jPanel _ playarea . add(play blocks[I][j]);
}
}
}
//被覆盖,以便我们可以在窗口关闭时退出
受保护的void processWindowEvent(window event e){
super . processwindowevent(e);
if (e.getID() == WindowEvent。窗口_关闭){
system . exit(0);
}
}
public void start() {
//初始化游戏参数
得分= 0;
isPause = false
isEnd = false//
jbutton 2 . settext(" pause ");
//初始化游戏区域
for(int I = 0;我& lt行;i++) {
for(int j = 0;j & ltCOLSj++) {
播放块[i][j]。set background(color . light gray);
播放块[i][j]。set visible(false);
}
}
//创建一个蛇身
snake = new snake body();
//在游戏区随机放置食物。
int x =(int)(math . random()* ROWS);
int y =(int)(math . random()* COLS);
while(播放块[x][y])。isVisible()) {
x =(int)(math . random()* ROWS);
y =(int)(math . random()* COLS);
}
播放块[x][y]。set background(color . yellow);
播放块[x][y]。set visible(true);
//初始化状态提示区
jlabel 2 . settext(integer . tostring(score));
jlabel 4 . settext(integer . tostring(snake . through body));
jlabel 2 . settext(integer . tostring(snake . through wall));
尝试{
//开始游戏
thread . start();
}
catch(IllegalThreadStateException IllegalThreadStateException){ }
}
//complete/cslg 0000/blog/category/Java