欢迎来到天天文库
浏览记录
ID:16164017
大小:17.47 KB
页数:10页
时间:2018-08-08
《java贪食蛇小程序代码解释》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、【java】贪食蛇=====MainFrame.java======主界面及其他===========packagepack1;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassMainFrameextendsJFrameimplementsActionListener,KeyListener{ JMenuBarmenubar=newJMenuBar(); JMenudifficulty=newJMenu("难度"); ButtonGr
2、oupbg=newButtonGroup(); JRadioButtoneasy=newJRadioButton("初级",true); JRadioButtonmiddle=newJRadioButton("中级",false); JRadioButtonhard=newJRadioButton("高级",false); JButtonstarted=newJButton("开始"); JButtonpaused=newJButton("暂停"); JButtonended =newJButton("结束"); JButto
3、nhelped=newJButton("帮助"); JButtonbtn[][]=newJButton[12][15]; JLabellblTitle=newJLabel(); intSnake1[]=newint[100]; intSnake2[]=newint[100]; intSnakeLength; intfood1; intfood2; inttemp1,temp2; intscore=0,cs=0,cq=0; intFoodScore; intSnakeD; intdirection; intrunFlag=0;
4、intspeed; intpause=0; publicMainFrame(){ super("贪食蛇游戏"); this.setJMenuBar(menubar); menubar.add(difficulty); bg.add(easy); bg.add(middle); bg.add(hard); easy.addActionListener(this); middle.addActionListener(this); hard.addActionListener(this); difficulty
5、.add(easy); difficulty.add(middle); difficulty.add(hard); speed=3; Containerc=getContentPane(); c.setLayout(newBorderLayout()); JPaneljp=newJPanel(newFlowLayout()); started.addActionListener(this); paused.addActionListener(this); ended.addActionListener
6、(this); helped.addActionListener(this); jp.add(started);jp.add(paused);jp.add(ended);jp.add(helped); c.add(jp,BorderLayout.NORTH); lblTitle.setText("得分:"+score+"穿身宝物:"+cs+"穿墙宝物:"+cq); lblTitle.setHorizontalAlignment(JLabel.CENTER); JPanelplayPanel=newJPanel(ne
7、wGridLayout(12,15)); started.addKeyListener(this); paused.addKeyListener(this); ended.addKeyListener(this); helped.addKeyListener(this); for(inti=0;i<12;i++) for(intj=0;j<15;j++) { playPanel.add(btn[i][j]=newJButton()); btn[i][j].setVisible(false); btn
8、[i][j].addActionListener(this); btn[i][j].setBackground(null); } JPanelshowFrame=newJPanel(newBorderLayout()); c.add(showFrame,Bord
此文档下载收益归作者所有