求Java 大神给个可以在eclipse上运行的贪吃蛇源代码在线等急
展开全部
packagecom.tarena.elts.test;importjava.awt.Color;importjava.awt.Font;importjava.awt.Point;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.HashSet;importjava.util.List;importjava.util.Map;importjava.util.Random;importjava.util.Set;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;publicclassWormFrameextendsJFrame{privatestaticfinallongserialVersionUID=1L;publicstaticfinalintUP=-10;publicstaticfinalintDOWN=10;publicstaticfinalintLEFT=-200;publicstaticfinalintRIGHT=200;privateintspeed=200;//蛇的移动速度,越小越快privateJPaneljPanel=null;//游戏面板privateJLabeljLabel=null;//显示游戏结束的标签privateJButtonreset=null;//从新开始游戏的按钮privateJButtoncontrol=null;//控制方向的按钮privateListworm=newArrayList();//贪吃蛇//将整个面板划分成节点,蛇走到那里,就那整个节点点亮privateMapnodes=newHashMap();privateintdir=LEFT;//方向privatePointfood;//食物privatebooleanisContinue=false;//判断蛇是否行动的标记publicstaticvoidmain(String[]args){newWormFrame();}publicWormFrame(){initialize();start();}//游戏初始化privatevoidinitialize(){this.setSize(500,500);this.setLocation(250,100);this.setResizable(false);this.add(getJPanel());//添加面板this.setTitle("贪吃蛇,空格键暂停,回车开始");this.setVisible(true);}//游戏开始privatevoidstart(){isContinue=true;while(true){while(isContinue){try{Pointp=worm.get(0);intx=(int)p.getX()+dir/20;inty=(int)p.getY()+dir%100;if(food.equals(newPoint(x,y))){worm.add(0,food);if(worm.size()%1==0){speed-=10;}getFood();continue;}p=newPoint(x,y);if(worm.contains(p)){thrownewException();}nodes.get(p).setVisible(false);worm.add(0,p);nodes.get(worm.remove(worm.size()-1)).setVisible(true);Thread.sleep(speed);}catch(Exceptione){jLabel.setVisible(true);reset.setVisible(true);//不停止内循环,jLabel和reset不消失isContinue=false;}}try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}}}//游戏从新开始privatevoidreset(){worm=newArrayList();for(Pointp:nodes.keySet()){nodes.get(p).setVisible(false);}addWorm();dir=LEFT;getFood();isContinue=true;}//获取游戏面板的方法,面板中有记录游戏时间的标签,//代表游戏角色的按钮和重新开始游戏的按钮privateJPanelgetJPanel(){if(jPanel==null){//显示游戏结束的标签getOver();//从新开始的按钮getReset();//控制按钮getControl();//游戏面板jPanel=newJPanel();jPanel.setLayout(null);//设置面板布局为空//jPanel.setForeground(newColor(255,255,255));//设置面板前景色jPanel.setBackground(newColor(0,0,0));//设置面板背景色jPanel.add(jLabel,null);//面板中添加显示游戏结束的标签jPanel.add(reset,null);//面板中添加从新开始的按钮jPanel.add(control,null);for(inti=0;i<490;i+=10){for(intj=0;j<470;j+=10){JButtonjb=newJButton();Pointp=newPoint(i,j);jb.setBounds(i,j,10,10);jb.setBackground(newColor(255,255,255));jb.setEnabled(false);//jb.setVisible(true);nodes.put(p,jb);jPanel.add(jb,null);}}addWorm();//添加一条蛇getFood();//食物按钮jPanel.setVisible(true);//设置面板可见}returnjPanel;}//游戏结束标签privatevoidgetOver(){jLabel=newJLabel();jLabel.setBounds(170,200,200,50);//设置标签大小和位置jLabel.setFont(newFont("Dialog",Font.BOLD,24));//设置标签字体jLabel.setForeground(newColor(250,2,2));//设置标签前景颜色jLabel.setText("游戏结束");jLabel.setEnabled(true);//设置此标签可用jLabel.setVisible(false);//设置此标签不可见}//从新开始按钮privatevoidgetReset(){if(reset==null){reset=newJButton();//新建一个按钮reset.setBounds(170,300,164,51);//设置按钮的大小reset.setText("重新开始");//设置按钮的内容reset.setVisible(false);//设置按钮不可见//添加按钮的时间监听器reset.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){reset.setVisible(false);//点击重新开始按钮后,按钮消失jLabel.setVisible(false);//记录游戏时间的按钮也消失try{Thread.sleep(1000);}catch(InterruptedExceptione1){e1.printStackTrace();}reset();}});}}//控制方向的按钮privatevoidgetControl(){if(control==null){control=newJButton();control.addKeyListener(newKeyAdapter(){@OverridepublicvoidkeyPressed(KeyEvente){switch(e.getKeyCode()){caseKeyEvent.VK_LEFT:chDir(LEFT);break;caseKeyEvent.VK_RIGHT:chDir(RIGHT);break;caseKeyEvent.VK_UP:chDir(UP);break;caseKeyEvent.VK_DOWN:chDir(DOWN);break;caseKeyEvent.VK_ENTER:isContinue=true;break;caseKeyEvent.VK_SPACE:isContinue=false;break;}}});}}//生成食物privatevoidgetFood(){Randomran=newRandom();//横坐标最大480,纵坐标最大460intx=ran.nextInt(49)*10;inty=ran.nextInt(47)*10;food=newPoint(x,y);Setset=newHashSet(worm);while(set.contains(food)){x=ran.nextInt(50)*10;y=ran.nextInt(50)*10;food=newPoint(x,y);}nodes.get(food).setVisible(false);}//改变方向privatevoidchDir(intdir){if(this.dir+dir!=0){this.dir=dir;}}//添加Worm的方法privatevoidaddWorm(){for(inti=250;i<300;i+=10){Pointp=newPoint(i,250);worm.add(p);nodes.get(p).setVisible(true);}}}//给分
追问
复制过去就可以了吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询