版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、snakegame.java package snakegame; import javax.swing.*; public class snakegame public static void main( string args ) jdialog.setdefaultlookandfeeldecorated( true ); gameframe temp = new gameframe(); snake.java package snakegame; import java.awt.*; import java.util.*; class snake extends linkedlist
2、public intsnakedirection = 2; public intsnakeredirection = 4; public snake() this.add( new point( 3, 3 ) ); this.add( new point( 4, 3 ) ); this.add( new point( 5, 3 ) ); this.add( new point( 6, 3 ) ); this.add( new point( 7, 3 ) ); this.add( new point( 8, 3 ) ); this.add( new point( 9, 3 ) ); this.a
3、dd( new point( 10, 3 ) ); public void changedirection( point temp, int direction ) this.snakedirection = direction; switch( direction ) case 1:/up this.snakeredirection = 3; this.add( new point( temp.x, temp.y - 1 ) ); break; case 2:/right this.snakeredirection = 4; this.add( new point( temp.x + 1,
4、temp.y ) ); break; case 3:/down this.snakeredirection = 1; this.add( new point( temp.x, temp.y + 1 ) ); break; case 4:/left this.snakeredirection = 2; this.add( new point( temp.x - 1, temp.y ) ); break; public booleancheckbeanin( point bean ) point temp = (point) this.getlast(); if( temp.equals( bea
5、n ) ) return true; return false; public void removetail() this.remove( 0 ); public void drawsnake( graphics g, intsinglewidthx, intsingleheighty, intcoopos ) g.setcolor( colorgroup.color_snake ); iterator snakesq = this.iterator(); while ( snakesq.hasnext() ) point temppoint = (point)snakesq.next();
6、 this.drawsnakepiece( g, temppoint.x, temppoint.y, singlewidthx, singleheighty, coopos ); public void drawsnakepiece( graphics g, int temp1, int temp2, intsinglewidthx, intsingleheighty, intcoopos ) g.fillroundrect( singlewidthx * temp1 + 1, singleheighty * temp2 + 1, singlewidthx - 2, singleheighty
7、 - 2, coopos, coopos ); public void clearendsnakepiece( graphics g, int temp1, int temp2, intsinglewidthx, intsingleheighty, intcoopos ) g.setcolor( colorgroup.color_back ); g.fillroundrect( singlewidthx * temp1 + 1, singleheighty * temp2 + 1, singlewidthx - 2, singleheighty - 2, coopos, coopos ); g
8、ameframe.java package snakegame; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.awt.geom.*; class gameframe extends jframe private toolkit tempkit; private inthorizontalgrid, verticalgrid; private intsinglewidthx, singleheighty; private intcoopos; p
9、rivate snake mainsnake; private linkedlisteatedbean; eatedbean = new linkedlist(); private iterator snakesq; public javax.swing.timersnaketimer; private int direction = 2; private int score; private string info; private point bean, eatbean; bean = new point(); private boolean flag; private jmenubari
10、nfomenu; private jmenu tempmenu; private jmenuitem tempmenuitem; private jradiobuttonmenuitem levelmenuitem, versionmenuitem; private jlabelscorelabel; scorelabel = new jlabel(); private graphics2d g; private imageiconsnakehead; snakehead = new imageicon( logo.gif ); private configmenuconfigmenu; pr
11、ivate booleanhasstoped = true; public gameframe() this.tempkit = this.gettoolkit(); this.setsize( tempkit.getscreensize() ); this.setgrid( 60, 40, 5 ); this.getcontentpane().setbackground( colorgroup.color_back ); this.setundecorated( true ); this.setresizable( false ); this.addkeylistener( new keyh
12、andler() ); gameframe.this.snaketimer = new javax.swing.timer( 80, new timerhandler() ); this.getcontentpane().add( scorelabel, borderlayout.south ); this.scorelabel.setfont( new font( fixedsys, font.bold, 15 ) ); this.scorelabel.settext( pausespace - exitesc ); this.configmenu = new configmenu( thi
13、s ); this.setvisible( true ); public void setgrid( int temp1, int temp2, int temp3 ) this.horizontalgrid = temp1; this.verticalgrid = temp2; this.singlewidthx = this.getwidth() / temp1; this.singleheighty = this.getheight() / temp2; this.coopos = temp3; private class keyhandler extends keyadapter pu
14、blic void keypressed( keyevent e ) if( e.getkeycode() = 27 ) snaketimer.stop(); if( joptionpane.showconfirmdialog( null, are you sure to exit? ) = 0 ) system.exit( 0 ); snaketimer.start(); else if( e.getkeycode() = 37 &mainsnake.snakedirection != 2 )/left direction = 4; else if( e.getkeycode() = 39
15、&mainsnake.snakedirection != 4 )/right direction = 2; else if( e.getkeycode() = 38 &mainsnake.snakedirection != 3 )/up direction = 1; else if( e.getkeycode() = 40 &mainsnake.snakedirection != 1 )/down direction = 3; else if( e.getkeycode() = 32 ) if( !hasstoped ) if( !flag ) snaketimer.stop(); confi
16、gmenu.setvisible( true ); configmenu.setmenuenable( false ); flag = true; else snaketimer.start(); configmenu.setvisible( false ); configmenu.setmenuenable( true ); flag = false; private class timerhandler implements actionlistener public synchronized void actionperformed( actionevent e ) point temp
17、 = (point) mainsnake.getlast(); snakesq = mainsnake.iterator(); while ( snakesq.hasnext() ) point temppoint = (point)snakesq.next(); if( temp.equals( temppoint ) &snakesq.hasnext() != false ) snaketimer.stop(); stopgame(); joptionpane.showmessagedialog( null, your score is + score + nnyou loss! ); s
18、ystem.out.println( temp.x + + temp.y ); if( (temp.x = 0 & direction = 4) | (temp.x = horizontalgrid-1 & direction = 2) | (temp.y = 0 & direction = 1) | (temp.y = verticalgrid-1 & direction = 3) ) snaketimer.stop(); stopgame(); joptionpane.showmessagedialog( null, your score is + score + nnyou loss!
19、); if( direction != mainsnake.snakeredirection ) movesnake( direction ); mainsnake.drawsnake( getgraphics(), singlewidthx, singleheighty, coopos ); drawbeanandebean( getgraphics() ); public void stopgame() this.hasstoped = true; this.snaketimer.stop(); graphics2d g = (graphics2d) gameframe.this.getg
20、raphics(); g.setcolor( colorgroup.color_back ); super.paint( g ); configmenu.setvisible( true ); public void resetgame() system.gc(); this.hasstoped = false; graphics2d g = (graphics2d) gameframe.this.getgraphics(); g.setcolor( colorgroup.color_back ); super.paint( g ); this.mainsnake = new snake();
21、 this.createbean( bean ); this.eatedbean.clear(); mainsnake.drawsnake( getgraphics(), singlewidthx, singleheighty, coopos ); this.snaketimer.start(); this.direction = 2; this.score = 0; this.scorelabel.settext( pausespace - exitesc ); private void movesnake( int direction ) if( mainsnake.checkbeanin
22、( this.bean ) ) this.score += 100; this.scorelabel.settext( + current score: + this.score ); this.eatedbean.add( new point(this.bean) ); this.createbean( this.bean ); mainsnake.changedirection( (point) mainsnake.getlast(), direction ); point temp = (point) mainsnake.getfirst(); if( eatedbe
23、an.size() != 0 ) if( eatedbean.getfirst().equals( temp ) ) eatedbean.remove( 0 ); else mainsnake.clearendsnakepiece( getgraphics(), temp.x, temp.y, singlewidthx, singleheighty, coopos ); mainsnake.removetail(); else mainsnake.clearendsnakepiece( getgraphics(), temp.x, temp.y, singlewidthx, singlehei
24、ghty, coopos ); mainsnake.removetail(); private void drawbeanandebean( graphics g ) g.setcolor( colorgroup.color_bean ); this.drawpiece( g, this.bean.x, this.bean.y ); g.setcolor( colorgroup.color_eatedbean ); snakesq = eatedbean.iterator(); while ( snakesq.hasnext() ) point temppoint = (point)snake
25、sq.next(); this.drawpiece( g, temppoint.x, temppoint.y ); private void drawpiece( graphics g, int x, int y ) g.fillroundrect( this.singlewidthx * x + 1, this.singleheighty * y + 1, this.singlewidthx - 2, this.singleheighty - 2, this.coopos, this.coopos ); private void createbean( point temp ) lp: wh
26、ile( true ) temp.x = (int) (math.random() * this.horizontalgrid); temp.y = (int) (math.random() * this.verticalgrid); snakesq = mainsnake.iterator(); while ( snakesq.hasnext() ) if( snakesq.next().equals( new point( temp.x, temp.y ) ) ) continue lp; break; configmenu.java package snakegame; import j
27、ava.awt.*; import java.awt.event.*; import javax.swing.*; public class configmenu extends jmenubar gameframe owner; jmenu menu; jmenuitem menuitem; jradiobuttonmenuitem speeditem, modelitem, standarditem; private uimanager.lookandfeelinfo looks; public configmenu( gameframe owner ) this.owner = owne
28、r; owner.setjmenubar( this ); string menu_name = snake game, game configure, game help; menu = new jmenumenu_name.length; for( inti = 0; imenu_name.length; i+ ) menui = new jmenu( menu_namei ); menui.setfont( new font( courier, font.plain, 12 ) ); this.add( menui ); string menuitem_name = start game
29、, stop game, exit game, game color, about.; menuitem = new jmenuitemmenuitem_name.length; for( inti = 0; imenuitem_name.length; i+ ) menuitemi = new jmenuitem( menuitem_namei ); menuitemi.setfont( new font( courier, font.plain, 12 ) ); menuitemi.addactionlistener( new actionhandler() ); menu0.add( m
30、enuitem0 ); menu0.add( menuitem1 ); menu0.addseparator(); menu0.add( menuitem2 ); menu1.add( menuitem3 ); menu2.add( menuitem4 ); string inner_menu_name = game speed, window model, game standard ; jmenu inner_menu = new jmenuinner_menu_name.length; for( inti = 0; iinner_menu_name.length; i+ ) inner_
31、menui = new jmenu( inner_menu_namei ); inner_menui.setfont( new font( courier, font.plain, 12 ) ); menu1.add( inner_menui ); buttongroup temp1 = new buttongroup(); string speeditem_name = speed-1, speed-2, speed-3, speed-4, speed-5; speeditem = new jradiobuttonmenuitemspeeditem_name.length; for( int
32、i = 0; ispeeditem_name.length; i+ ) speeditemi = new jradiobuttonmenuitem( speeditem_namei ); inner_menu0.add( speeditemi ); speeditemi.setfont( new font( courier, font.plain, 12 ) ); speeditemi.additemlistener( new itemhandler() ); temp1.add( speeditemi ); buttongroup temp2 = new buttongroup(); str
33、ing modelitem_name = linux, mac, windows ; modelitem = new jradiobuttonmenuitemmodelitem_name.length; for( inti = 0; imodelitem_name.length; i+ ) modelitemi = new jradiobuttonmenuitem( modelitem_namei ); inner_menu1.add( modelitemi ); modelitemi.setfont( new font( courier, font.plain, 12 ) ); modeli
34、temi.additemlistener( new itemhandler() ); temp2.add( modelitemi ); buttongroup temp3 = new buttongroup(); string standarditem_name = 60 * 40, 45 * 30, 30 * 20 ; standarditem = new jradiobuttonmenuitemstandarditem_name.length; for( inti = 0; istandarditem_name.length; i+ ) standarditemi = new jradio
35、buttonmenuitem( standarditem_namei ); inner_menu2.add( standarditemi ); standarditemi.setfont( new font( courier, font.plain, 12 ) ); standarditemi.additemlistener( new itemhandler() ); temp3.add( standarditemi ); looks = uimanager.getinstalledlookandfeels(); private class actionhandler implements a
36、ctionlistener public void actionperformed( actionevent e ) if( e.getsource() = menuitem0 ) owner.resetgame(); configmenu.this.setvisible( false ); else if( e.getsource() = menuitem1 ) owner.stopgame(); configmenu.this.setvisible( true ); configmenu.this.setmenuenable( true ); else if( e.getsource()
37、= menuitem2 ) system.exit( 0 ); else if( e.getsource() = menuitem3 ) configdialog temp = new configdialog( owner ); temp.setvisible( true ); else if( e.getsource() = menuitem4 ) joptionpane.showmessagedialog( null, sanke game 2.0 version!nn + author: finalcorenn ); private class itemhandler implemen
38、ts itemlistener public void itemstatechanged( itemevent e ) for( inti = 0; ispeeditem.length; i+ ) if( e.getsource() = speeditemi ) owner.snaketimer.setdelay( 150 - 30 * i ); if( e.getsource() = standarditem0 ) owner.setgrid( 60, 40, 5 ); else if( e.getsource() = standarditem1 ) owner.setgrid( 45, 3
39、0, 10 ); else if( e.getsource() = standarditem2 ) owner.setgrid( 30, 20, 15 ); for( inti = 0; imodelitem.length; i+ ) if( e.getsource() = modelitemi ) try uimanager.setlookandfeel( looksi.getclassname() ); catch(exception ex) public void setmenuenable( boolean temp ) menu1.setenabled( temp ); config
40、dialog.java package snakegame; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class configdialog extends jdialog private container c; private jframe owner; private ownpanel panel = new ownpanel4; box box1, box2; private jbuttoncommitbutton, cancelbutton; color color = new c
41、olor4; public configdialog( frame owner ) this.owner = (jframe) owner; this.setsize( 400, 200 ); this.setresizable( false ); this.settitle( config your game ); this.c = this.getcontentpane(); this.c.setbackground( color.white ); this.c.setlayout( new flowlayout() ); this.box1 = box.createverticalbox
42、(); for( inti = 0; ipanel.length; i+ ) paneli = new ownpanel(); paneli.addactionlistener( new actionhandler() ); this.box1.add( paneli ); this.box1.add( box.createverticalstrut( 4 ) ); this.panel0.settext( background ); this.panel1.settext( snake ); this.panel2.settext( bean ); this.panel3.settext(
43、eatedbean ); this.panel0.setback( colorgroup.color_back ); this.panel1.setback( colorgroup.color_snake ); this.panel2.setback( colorgroup.color_bean ); this.panel3.setback( colorgroup.color_eatedbean ); this.box2 = box.createhorizontalbox(); mitbutton = new jbutton( 确定 ); mitbutton.setfont( font.get
44、font( fixedsys ) ); mitbutton.addactionlistener( new actionhandler() ); this.cancelbutton = new jbutton( 取消 ); this.cancelbutton.setfont( font.getfont( fixedsys ) ); this.cancelbutton.addactionlistener( new actionhandler() ); this.box2.add( mitbutton ); this.box2.add( box.createhorizontalstrut( 20 )
45、 ); this.box2.add( this.cancelbutton ); this.box1.add( this.box2 ); this.c.add( this.box1, borderlayout.north ); this.setlocation( ( this.gettoolkit().getscreensize().width - this.getwidth() )/2, ( this.gettoolkit().getscreensize().height - this.getheight() )/2 ); this.setvisible( true ); public voi
46、d setownercolor( color temp ) this.owner.getcontentpane().setbackground( temp ); private class actionhandler implements actionlistener public void actionperformed( actionevent e ) for( inti = 0; icolor.length; i+ ) if( e.getsource() = paneli.rebutton() ) colori = jcolorchooser.showdialog( configdial
47、og.this, choose background color, color.white ); if( colori != null ) paneli.setback( colori ); if( e.getsource() = commitbutton ) color0 = (color0=null?colorgroup.color_back:color0); color1 = (color1=null?colorgroup.color_snake:color1); color2 = (color2=null?colorgroup.color_bean:color2); color3 =
48、(color3=null?colorgroup.color_eatedbean:color3); configdialog.this.setvisible( false ); colorgroup.setcolor_back( color0 ); owner.getcontentpane().setbackground( color0 ); colorgroup.setcolor_snake( color1 ); colorgroup.setcolor_bean( color2 ); colorgroup.setcolor_eatedbean( color3 ); configdialog.t
49、his.dispose(); else if( e.getsource() = cancelbutton ) configdialog.this.setvisible( false ); configdialog.this.dispose(); class ownpanel extends jpanel private jlabel temp1; private jtextfield temp2; private jbutton temp3; ownpanel() temp1 = new jlabel(); temp1.setfont( font.getfont( fixedsys ) );
50、temp2 = new jtextfield(); temp3 = new jbutton( change ); temp3.setfont( font.getfont( fixedsys ) ); temp2.seteditable( false ); temp2.setcolumns( 10 ); this.add( temp1 ); this.add( temp2 ); this.add( temp3 ); this.setlayout( new gridlayout( 1, 3 ) ); public void setbuttonname( string temp ) temp3.se
51、tname( temp ); public void setback( color temp ) temp2.setbackground( temp ); public void settext( string temp ) temp1.settext( temp ); public object rebutton() return temp3; public void addactionlistener( actionlistener ac ) temp3.addactionlistener( ac ); tools.java package snakegame; import java.awt.*; import javax.swing.*; class colorgroup static color color_back = color.white; static color color_snake = new color( 43, 110, 187 ); static color color_bean = color.blue; static color color_eatedbean = color.cyan; static void setcolor_back( color te
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026北京西城区北自科技校园招聘参考考试试题及答案解析
- 2026年郑州商贸旅游职业学院单招综合素质考试备考试题含详细答案解析
- 2026年宁夏职业技术学院单招综合素质考试模拟试题含详细答案解析
- 2026年广东茂名农林科技职业学院单招综合素质考试备考题库含详细答案解析
- 2026年铁岭师范高等专科学校高职单招职业适应性测试模拟试题及答案详细解析
- 2026年长沙电力职业技术学院单招综合素质笔试备考试题含详细答案解析
- 2026年长白山职业技术学院单招综合素质考试备考试题含详细答案解析
- 2026年辽宁工程职业学院单招综合素质考试参考题库含详细答案解析
- 2026广西崇左凭祥市退役军人服务中心见习人员招聘1人考试参考题库及答案解析
- 2026年海南外国语职业学院单招职业技能考试备考试题含详细答案解析
- 物资管理实施细则
- 2025年北京东城区天街集团有限公司招聘笔试参考题库含答案解析
- 结肠炎与肠道菌群的关系
- 安全健康与自我管理学习通超星期末考试答案章节答案2024年
- 2024年《国际货运代理实务》考试复习题库资料(含答案)
- 人教版2019高中英语选择性必修二(课文语法填空)
- 乳腺炎与乳腺癌关联研究
- 2022年初会《经济法》真题答案
- 新华人寿培训讲义
- GB/T 22551-2023旅居车辆居住要求
- mdvx节能证书及第三方检测报告cqc
评论
0/150
提交评论