java课程设计贪吃蛇游戏设计_第1页
java课程设计贪吃蛇游戏设计_第2页
java课程设计贪吃蛇游戏设计_第3页
java课程设计贪吃蛇游戏设计_第4页
java课程设计贪吃蛇游戏设计_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 importjava.awt.*;importjavax.swing.*;importjava.util.*;importjava.awt.event.*;publicclassSheextendsJFrameimplementsActionListener,WindowListenerJLabelbt1;JLabelbt2;MyPaintmp;JMenuItemkaishi;JMenuItemzanting;JMenuItemtuichu;JRadioButtonchu;JRadioButtonzhong;JRadioButtongao;JRadioButtonchao;JMenuItem

2、guanyu;Fontf;publicShe()f=newFont(宋体,Font.PLAIN,12);mp=newMyPaint();mp.setShe(this);JMenuBarcaidan=newJMenuBar();JMenushezhi=newJMenu(设置);shezhi.setFont(f);JMenunandu=newJMenu(难度);nandu.setFont(f);JMenubangzhu=newJMenu(帮助);bangzhu.setFont(f);caidan.add(shezhi);caidan.add(nandu);caidan.add(bangzhu);k

3、aishi=newJMenuItem(开始游戏F2);kaishi.setFont(f);kaishi.addActionListener(this);zanting=newJMenuItem(暂停/继续F3);zanting.setFont(f);zanting.addActionListener(this);tuichu=newJMenuItem(退出F4);tuichu.setFont(f);tuichu.addActionListener(this);chu=newJRadioButton(初级,最快速度195,true);chu.setFont(f);chu.addActionLis

4、tener(this);zhong=newJRadioButton(中级,最快速度225);zhong.setFont(f);zhong.addActionListener(this);gao=newJRadioButton(高级,最快速度255);gao.setFont(f);gao.addActionListener(this);chao=newJRadioButton(超级,最快速度275);chao.setFont(f);chao.addActionListener(this);ButtonGroupbg=newButtonGroup();bg.add(chu);bg.add(zhon

5、g);bg.add(gao);bg.add(chao);nandu.add(chu);nandu.add(zhong);nandu.add(gao);nandu.add(chao);guanyu=newJMenuItem(关于);guanyu.setFont(f);guanyu.addActionListener(this);shezhi.add(kaishi);shezhi.add(zanting);shezhi.add(tuichu);bangzhu.add(guanyu);bt1=newJLabel(长度:0);bt1.setFont(f);bt2=newJLabel(移动:5);bt2

6、.setFont(f);JPanelp=newJPanel();p.setLayout(newGridLayout(1,2);p.add(bt1);p.add(bt2);this.addWindowListener(this);/添加窗体监听器this.setJMenuBar(caidan);this.add(p,BorderLayout.NORTH);this.add(mp);this.add(newJLabel(),BorderLayout.WEST);this.setSize(335,385);this.setResizable(false);this.setLocation(300,1

7、50);this.setTitle(MyPaint);this.setVisible(true);publicvoidsetDS(inta,intb)bt1.setText(长度:+a);bt2.setText(移动:+b);publicvoidsetChu()chu.setSelected(true);mp.setSD(300,110);this.setDS(0,305-300);chu.setEnabled(true);zhong.setEnabled(true);gao.setEnabled(true);chao.setEnabled(true);publicvoidsetNanDu()

8、chu.setEnabled(false);zhong.setEnabled(false);gao.setEnabled(false);chao.setEnabled(false);publicvoidactionPerformed(ActionEvente)if(e.getSource()=zanting)mp.setZanTing();elseif(e.getSource()=kaishi)mp.setKaiShi();elseif(e.getSource()=tuichu)mp.setTuiChu();elseif(e.getSource()=guanyu)JLabelgy=newJLa

9、bel(小游戏贪吃蛇V1.0);gy.setFont(f);JOptionPane.showMessageDialog(this,gy);elseif(e.getSource()=chu)mp.setSD(300,110);this.setDS(0,305-300);elseif(e.getSource()=zhong)mp.setSD(250,80);this.setDS(0,305-250);elseif(e.getSource()=gao)mp.setSD(200,50);this.setDS(0,305-200);elseif(e.getSource()=chao)mp.setSD(1

10、50,30);this.setDS(0,305-150);publicstaticvoidmain(Stringargs)newShe();/*MethodwindowOpened*parame*/publicvoidwindowOpened(WindowEvente)/TODO:AddyourcodeherepublicvoidwindowClosing(WindowEvente)System.exit(0);publicvoidwindowClosed(WindowEvente)/TODO:AddyourcodeherepublicvoidwindowIconified(WindowEve

11、nte)/TODO:AddyourcodeherepublicvoidwindowDeiconified(WindowEvente)/TODO:AddyourcodeherepublicvoidwindowActivated(WindowEvente)/TODO:AddyourcodeherepublicvoidwindowDeactivated(WindowEvente)/TODO:AddyourcodehereclassMyPaintextendsJPanelimplementsRunnable,KeyListener,FocusListenerShes;Fontf=newFont(宋体,

12、Font.PLAIN,12);intx=150;inty=0;Randomr=newRandom();intsjx=r.nextInt(300)/10*10;intsjy=r.nextInt(300)/10*10;intfx=2;intsw=0;intsd=300;intcs=0;intd=150;Stringzx=newString1000;booleanzanting=false;booleanzanting1=false;booleansiwang=false;booleanshenti=false;publicMyPaint()Threadt=newThread(this);t.sta

13、rt();this.addKeyListener(this);this.setFocusable(true);this.addFocusListener(this);publicvoidsetShe(Shess)s=ss;publicvoidsetZanTing()if(zanting)zanting=false;elsezanting=true;publicvoidsetKaiShi()JLabelks=newJLabel(是否重新开始?);ks.setFont(f);inta=JOptionPane.showConfirmDialog(s,ks);if(a=0)x=150;y=0;sjx=

14、r.nextInt(300)/10*10;sjy=r.nextInt(300)/10*10;fx=2;sw=0;sd=300;cs=0;zanting=false;siwang=false;shenti=false;s.setChu();s.setDS(sw,305-sd);publicvoidsetTuiChu()JLabeltc=newJLabel(确定退出吗?);tc.setFont(f);inta=JOptionPane.showConfirmDialog(s,tc);if(a=0)s.dispose();System.exit(0);publicvoidsetSD(inta,intb

15、)sd=a;d=b;publicvoidpaint(Graphicsg)zxcs=x+/+y;super.paint(g);g.setColor(Color.BLACK);g.drawRect(0,0,300,300);g.setColor(Color.BLACK);g.fillRect(sjx,sjy,10,10);g.setColor(Color.BLUE);g.fillRect(x,y,10,10);g.setColor(Color.RED);for(inti=1;i=sw;i+)intj=cs-i;if(j0)j=cs-i+999+1;Strings=zxj.split(/);if(x

16、=Integer.parseInt(s0)&y=Integer.parseInt(s1)/蛇撞到自己身上了siwang=true;shenti=true;if(sjx=Integer.parseInt(s0)&sjy=Integer.parseInt(s1)/食物在蛇身体上g.setColor(Color.BLACK);g.fillRect(sjx,sjy,10,10);g.setColor(Color.RED);continue;g.fillRect(Integer.parseInt(s0),Integer.parseInt(s1),10,10);publicvoidrun()while(t

17、rue)cs+;if(cs=1000)cs=0;switch(fx)case1:y-=10;/上break;case2:y+=10;/下break;case3:x-=10;/左break;case4:x+=10;/右break;this.repaint();if(x=sjx&y=sjy)sjx=r.nextInt(300)/10*10;sjy=r.nextInt(300)/10*10;sw+;if(sw=1)s.setNanDu();if(sd=d)sd=d;elsesd-=5;s.setDS(sw,305-sd);if(x290|y290|shenti)JOptionPane.showMes

18、sageDialog(s,游戏结束!);siwang=true;shenti=false;while(zanting|zanting1)tryThread.sleep(100);catch(Exceptione)while(siwang)tryThread.sleep(100);catch(Exceptione)tryThread.sleep(sd);catch(Exceptione)publicvoidkeyPressed(KeyEvente)switch(e.getKeyCode()case38:fx=1;/上break;case40:fx=2;/下break;case37:fx=3;/左break;case39:fx=4;/右brea

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论