了解下JAVA的Java版吃豆游戏及源码
你通过从书的数量和开发周期及运行速度来证明: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:http://www.apache.org/licenses/LICENSE-2.0
*</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, 是一种使用者不需花费很多时间学习的语言 科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。 另外编写和运行Java程序需要JDK(包括JRE),在sun的官方网站上有下载,thinking in java第三版用的JDK版本是1.4,现在流行的版本1.5(sun称作J2SE 5.0,汗),不过听说Bruce的TIJ第四版国外已经出来了,是专门为J2SE 5.0而写的。 Java 编程语言的风格十分接近C、C++语言。 象、泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发。 是一种语言,用以产生「小应用程序(Applet(s)) 接着就是EJB了,EJB就是Enterprise JavaBean, 看名字好象它是Javabean,可是它和Javabean还是有区别的。它是一个体系结构,你可以搭建更安全、更稳定的企业应用。它的大量代码已由中间件(也就是我们常听到的 Weblogic,Websphere这些J2EE服务器)完成了,所以我们要做的程序代码量很少,大部分工作都在设计和配置中间件上。 Sun公司看见Oak在互联网上应用的前景,于是改造了Oak,于1995年5月以Java的名称正式发布。Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。 Java 编程语言的风格十分接近C、C++语言。 是一种突破用户端机器环境和CPU
页:
[1]