|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你通过从书的数量和开发周期及运行速度来证明:net和ruby要比java简单。
这是一个吃豆游戏的Java完成,详细代码较多请下载文件。
下载地点:http://download.csdn.net/source/396925
- packageorg.loon.test;importjava.awt.Color;importjava.awt.Event;import
- java.awt.Frame;importjava.awt.Graphics;importjava.awt.Image;import
- java.awt.Insets;importjava.awt.MediaTracker;importjava.awt.Panel;import
- java.awt.Toolkit;importjava.awt.event.WindowAdapter;import
- java.awt.event.WindowEvent;importjava.awt.image.BufferedImage;import
- java.awt.image.VolatileImage;/****<p>*Title:LoonFramework*
- </p>*<p>*Description:*</p>*<p>*Copyright:
- Copyright(c)2008*</p>*<p>*Company:LoonFramework*
- </p>*<p>*License:[url]http://www.apache.org/licenses/LICENSE-2.0
- [/url]*</p>**@authorchenpeng*@email:ceponline@yahoo.com.cn*
- @version0.1*/classTimerimplementsRunnable{Pacgameap;
- intduration;privateThreadmyThread;privatebooleanloopf=
- true;privatelongtickCount;publicTimer(Pacgameap,intduration)
- {this.ap=ap;this.duration=duration;
- }publicTimer(Pacgameap){this(ap,16);}
- publicvoidon(){myThread=newThread(this);
- myThread.start();}publicvoidoff(){loopf=
- false;}publicvoidrun(){loopf=true;
- tickCount=System.currentTimeMillis();while(loopf){
- try{Thread.sleep(duration);
- }catch(Exceptione){
- e.printStackTrace();}ap.notifyFrame
- ();}}publicintframes(){
- longnowTick=System.currentTimeMillis();intframe=(int)(nowTick-
- tickCount)/duration;if(frame<=0){
- frame=1;tickCount=nowTick;}elseif
- (frame>5){frame=5;tickCount
- =nowTick;}else{tickCount+=frame*
- duration;}returnframe;}}public
- classPacgameextendsPanelimplementsRunnable{/***//***
- */privatestaticfinallongserialVersionUID=1L;private
- staticintWIDTH=256;privatestaticintHEIGHT=224;Thread
- thread;//机关一个双缓冲的、可变的imageVolatileImageoffImage;
- Imagebg,bg0;CGloaderloader;MazeManagermzmanager;
- MapManageremanager;SpriteManagerspmanager,chipmanager;
- MonsterSpriteredmon,ao,pink,monster;PacSpritepac;AttractManager
- atMng;Timertimer;intkey=0;intscore;int
- number1;booleanloadOK=false;booleanisAttr=false;
- publicvoidinit(){ClassLoadercl=getClass().getClassLoader();
- Toolkittk=Toolkit.getDefaultToolkit();bg0=
- tk.createImage(cl.getResource("main.png"));bg=new
- BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB_PRE);
- MediaTrackermt=newMediaTracker(this);mt.addImage(bg0,0);
- try{mt.waitForID(0);}catch
- (Exceptione){e.printStackTrace();}
- loader=newCGloader("pacchr.png",this);
- loader.setAlpha(0,8);mzmanager=newMazeManager();
- emanager=newMapManager();spmanager=newSpriteManager(4);
- chipmanager=newSpriteManager(5);pac=newPacSprite(10,20,
- loader);redmon=newRedMonster(10,10,mzmanager,loader);
- redmon.setTarget(pac);spmanager.add(redmon);ao
- =newBlueMonster(9,13,mzmanager,loader);ao.setTarget(pac);
- spmanager.add(ao);pink=newPinkMonster(10,13,mzmanager,
- loader);pink.setTarget(pac);spmanager.add(pink);
- monster=newMonster(11,13,mzmanager,loader);
- monster.setTarget(pac);spmanager.add(monster);for
- (inti=0;i<5;i++){chipmanager.add(newChipSprite(i*
- 16,0,loader,i*16,130,16,16));
- }atMng=newAttractManager(loader);timer=
- newTimer(this,16);setBackground(Color.black);
- setSize(WIDTH,HEIGHT);offImage=createVolatileImage(WIDTH,
- HEIGHT);}publicvoidpaint(Graphicsg){if(!
- loadOK)return;do{
- intreturnCode=offImage.validate(getGraphicsConfiguration());
- if(returnCode==VolatileImage.IMAGE_INCOMPATIBLE){
- offImage=createVolatileImage(WIDTH,HEIGHT);}
- GraphicsvolG=offImage.getGraphics();volG.drawImage
- (bg,0,0,this);spmanager.draw(volG,this);
- pac.draw(volG,this);chipmanager.draw(volG,this);
- if(isAttr)atMng.draw(volG,
- this);volG.dispose();g.drawImage
- (offImage,0,0,this);}while(offImage.contentsLost());}
- publicvoidupdate(Graphicsg){paint(g);}
- publicvoidstart(){System.gc();if(thread==
- null){thread=newThread(this);
- thread.start();}}@SuppressWarnings
- ("deprecation")publicvoidstop(){if(thread!=
- null){thread.stop();thread=
- null;}}publicvoidrun(){
- while(true){//MAINLOOPscore=
- 0;number1=3;intstage=1;
- esaInit();loadOK=true;
- while(number1>0){while(gameMain(stage)==
- 2){if(stage==2)
- gameTheater();stage++;
- number1++;//1:DEAD2:CLEAR
- esaInit();}
- }//GAMEOVERisAttr=true;
- spmanager.setShow(false);pac.setShow
- (false);atMng.player=true;
- atMng.ready=false;atMng.gameover=true;
- repaint();timer.on();key=
- 0;while(true){if(key>
- 0)break;
- waitFrame();}timer.off();
- atMng.player=false;atMng.gameover=false;
- }}publicvoidesaInit(){Graphics
- g=bg.getGraphics();g.drawImage(bg0,0,0,null);
- g.dispose();emanager.init(bg);}publicint
- gameMain(intstage){atMng.init(bg,score,number1);
- spmanager.init();pac.init();//游戏形态1:DEAD
- 2:CLEARintgamestat=0;intrenzoku=0;
- booleanispower=false;booleaniseyesnd=false;
- Spritechip;isAttr=true;intwait;
- if((score==0)&&(number1==3)){wait=
- 2000;}else{wait=1000;
- }spmanager.setShow(false);pac.setShow(false);
- atMng.player=true;atMng.ready=true;
- repaint();sleep(wait);atMng.setBattle(--
- number1);spmanager.setShow(true);pac.setShow
- (true);atMng.player=false;atMng.ready=true;
- repaint();sleep(wait);isAttr=
- false;inttime=0;timer.on();
- intframe=1;while(gamestat==0){for
- (intl=0;l<frame;l++){intscore0=score;
- if(time==60*55){
- spmanager.setNawabari(false);}elseif(time==60*
- 50){spmanager.setNawabari(true);
- }elseif(time==60*30){
- spmanager.setNawabari(false);}elseif(time==60*25)
- {spmanager.setNawabari(true);
- }elseif(time==60*5){
- spmanager.setNawabari(false);}
- spmanager.run();MonsterSpritehitmon=
- spmanager.isMonsterHit(pac);if(hitmon!=null){
- intstate=hitmon.getState();
- if(state==0)gamestat
- =1;if(state==1){
- renzoku++;score
- =score+(1<<renzoku)*100;
- chip=chipmanager.getSprite(renzoku);
- intx=hitmon.getX();inty=
- hitmon.getY();chip.setXY(x,y);
- chip.setShow(true);
- hitmon.setShow(false);
- pac.setShow(false);try{
- Thread.sleep(50);
- }catch(Exceptione){
- }repaint();
- for(inti=0;i<50;i++){
- try{
- Thread.sleep(10);}catch
- (Exceptione){}
- }
- chip.setShow(false);hitmon.setShow
- (true);pac.setShow(true);
- hitmon.activity();
- iseyesnd=true;if
- (ispower){if(iseyesnd)
- try{
- Thread.sleep(10);
- }catch(Exceptione){
- }}
- }}
- pac.setKey(key);intesa=
- pac.run(mzmanager,emanager);if(esa>0){
- intremain=emanager.getRemain();
- score+=10;if(remain<=
- 0)gamestat=2;
- }if(esa==2){
- redmon.state();ao.state();
- pink.state();
- monster.state();pac.powerup();
- renzoku=0;score+=
- 40;ispower=true;
- }if(ispower){
- if(!pac.isPower()||
- ((redmon.getState()!=1)
- &&(ao.getState()!=1)
- &&(pink.getState()!=1)&&(monster
- .getState()!=1))){
- ispower=false;
- }}if(iseyesnd)
- {if((redmon.getState()!=2)&&
- (ao.getState()!=2)&&
- (pink.getState()!=2)&&
- (monster.getState()!=2)){iseyesnd=
- false;}}
- if(score!=score0){
- atMng.setScore(score);}
- time++;score0=score;
- if(gamestat>0)break;
- }//FPSLOOPENDrepaint();
- frame=waitFrame();System.gc();}
- timer.off();try{Thread.sleep
- (2000);}catch(Exceptione){}
- returngamestat;}publicvoidgameTheater(){
- Graphicsg=bg.getGraphics();g.setColor(Color.black);
- g.fillRect(0,0,WIDTH,HEIGHT);g.dispose();int
- pacx=WIDTH;intp;intindex=288;
- spmanager.setShow(false);pac.init();pac.setShow
- (true);redmon.init();redmon.setShow(true);
- timer.on();intframe=1;inti=0;
- while(i<380){for(intj=0;j<frame;
- j++){p=2;switch
- ((i>>2)%4){case1:
- p+=4;break;
- case3:p=8;
- break;}
- pac.setP(p);pac.setXY(pacx--,100);
- redmon.setXY(index--,100);
- i++;if(i>=360)
- break;}repaint();
- frame=waitFrame();}pacx=-32;
- redmon.state();index=0;i=0;
- while(i<380){for(intj=0;j<frame;
- j++){p=0;switch
- ((i>>2)%4){case1:
- p+=4;break;
- case3:p=8;
- break;}
- pac.setP(p);pac.setXY(pacx++,100);
- redmon.setXY(index++,100);
- i++;if(i>=360)
- break;}repaint();
- frame=waitFrame();}timer.off();
- }publicvoidsleep(inttm){try{
- Thread.sleep(tm);}catch(Exceptione){
- e.printStackTrace();}}publicboolean
- handleEvent(Evente){switch(e.id){case
- Event.KEY_PRESS:caseEvent.KEY_ACTION:switch
- (e.key){caseEvent.RIGHT:
- key=1;break;case
- Event.DOWN:key=2;
- break;caseEvent.LEFT:key=
- 4;break;caseEvent.UP:
- key=8;break;
- }returntrue;case
- Event.KEY_RELEASE:caseEvent.KEY_ACTION_RELEASE:
- default:}returnfalse;}/**
- *//***fps盘算**@return*/
- privatesynchronizedintwaitFrame(){try{
- wait();}catch(InterruptedExceptione){}
- //前往fpsreturntimer.frames();}public
- synchronizedfinalvoidnotifyFrame(){notifyAll();}
- publicstaticvoidmain(String[]args){finalPacgameap=new
- Pacgame();ap.setSize(WIDTH,HEIGHT);Framefrm=new
- Frame("Java吃豆游戏");frm.addWindowListener(newWindowAdapter
- (){publicvoidwindowClosing(WindowEvente){
- ap.stop();System.exit(0);
- }});frm.addNotify();
- Insetsinset=frm.getInsets();intw0=inset.left+inset.right;
- inth0=inset.top+inset.bottom;frm.add(ap);
- frm.setSize(ap.getWidth()+w0,ap.getHeight()+h0);
- frm.setResizable(false);frm.setLocationRelativeTo(null);
- frm.setVisible(true);ap.requestFocus();ap.init
- ();ap.start();}}
复制代码 本文出自“Java事实怎样玩”博客,请务必保存此出处http://cping1982.blog.51cto.com/601635/116833
由于这些智能化家电的市场需求没有预期的高,Sun放弃了该项计划。就在Oak几近失败之时,随着互联网的发展,Sun看到了Oak在计算机网络上的广阔应用前景,于是改造了Oak, |
|