|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
还得说上一点,就java本质而言,是面相对象的,但是你有没有发现,java也不全是,比如说基本类型,int,那他就是整型而不是对象,转换类型是还得借助包装类。1、FlyMidlet.java
packagefly;
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
importjava.util.*;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassFlyMidletextendsMIDlet{
Navigateng;
publicFlyMidlet(){
ng=Navigate.getInstance(this);
}
protectedvoidstartApp(){
System.out.println("startApp");
ng.display.setCurrent(ng.mc);
printInfo();
}
protectedvoidpauseApp(){
System.out.println("pauseApp");
}
protectedvoiddestroyApp(booleanparm1){
System.out.println("destroyApp");
Navigate.mc.stop();
MyGameCanvas.cleanJob();
Navigate.cleanJob();
}
privatevoidprintInfo(){
System.out.println("FlyMidletprintInfo()start:");
System.out.println("FlyMidletprintInfo()end:");
}
}
2、Bullets.java
packagefly;
importjavax.microedition.lcdui.game.*;
importjavax.microedition.lcdui.*;
importjava.util.Random;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassBulletsextendsGameObject{
privateint[][]bullets;
privateintbulletstotal;
privateRandomrnd;
publicstaticfinalintBULLET_TYPE_LEFT=0;
publicstaticfinalintBULLET_TYPE_RIGHT=1;
publicstaticfinalintBULLET_TYPE_TOP=2;
publicstaticfinalintBULLET_TYPE_BOTTOM=3;
privateintwidth,height;
publicBullets(Imageimg,intpicwidth,intpicheight,intbulletstotal,intwidth,intheight){
super(img,picwidth,picheight);
this.bulletstotal=bulletstotal;
bullets=newint[bulletstotal][6];
rnd=newRandom();
this.width=width;
this.height=height;
}
publicvoidinitBullets(){
for(inti=0;i<bullets.length;i++){
initBullet(i);
}
}
privatevoidinitBullet(inti){
bullets[i][0]=(rnd.nextInt()&0x7fffffff)%4;//type
bullets[i][5]=1;//alive
switch(bullets[i][0]){
caseBULLET_TYPE_LEFT:
bullets[i][1]=-5;
bullets[i][2]=(rnd.nextInt()&0x7fffffff)%height;
bullets[i][3]=(rnd.nextInt()&0x7fffffff)%3+1;//vx
bullets[i][4]=(rnd.nextInt())%3;//vy
break;
caseBULLET_TYPE_RIGHT:
bullets[i][1]=width+5;
bullets[i][2]=(rnd.nextInt()&0x7fffffff)%height;
bullets[i][3]=((rnd.nextInt()&0x7fffffff)%3+1)*-1;//vx
bullets[i][4]=(rnd.nextInt())%3;//vy
break;
caseBULLET_TYPE_TOP:
bullets[i][1]=(rnd.nextInt()&0x7fffffff)%width;
bullets[i][2]=-5;
bullets[i][3]=(rnd.nextInt())%3;//vx
bullets[i][4]=(rnd.nextInt()&0x7fffffff)%3+1;//vy
break;
caseBULLET_TYPE_BOTTOM:
bullets[i][1]=(rnd.nextInt()&0x7fffffff)%width;
bullets[i][2]=height+5;
bullets[i][3]=(rnd.nextInt())%3;//vx
bullets[i][4]=((rnd.nextInt()&0x7fffffff)%3+1)*-1;//vy
break;
}
}
publicvoidupdata(inti){
bullets[i][1]+=bullets[i][3];
bullets[i][2]+=bullets[i][4];
if(bullets[i][1]<-5||bullets[i][1]>width+5){
bullets[i][3]*=-1;
}
if(bullets[i][2]<-5||bullets[i][2]>height+5){
bullets[i][4]*=-1;
}
}
privatevoidpaint(Graphicsg,inti){
updataspritepos(i);
sprite.paint(g);
}
publicvoidpaint(Graphicsg){
for(inti=0;i<bullets.length;i++){
if(bullets[i][5]==0){
continue;
}
sprite.setPosition(bullets[i][1],bullets[i][2]);
sprite.paint(g);
}
}
publicvoidrefreshBullets(Spriteplanesprite,booleanneedcollision){
for(inti=0;i<bullets.length;i++){
if(bullets[i][5]==0){
continue;
}
if(needcollision){
//System.out.println("needcollision");
if(isCollision(planesprite,i,10)){
//System.out.println("collision");
Navigate.mc.gameover=true;
Navigate.mc.explosion.sprite.setPosition(bullets[i][1]-16,
bullets[i][2]-16);
bullets[i][5]=0;
continue;
}
}
updata(i);
}
}
privatebooleanisCollision(Spritesprite,inti,intrange){
//updataspritepos(i);
//returnsprite.collidesWith(this.sprite,true);
booleanresult=false;
intplaneXCenter=sprite.getX()+12;
intplaneYCenter=sprite.getY()+12;
intbulletXCenter=bullets[i][1]+3;
intbulletYCenter=bullets[i][2]+3;
if(Math.abs(planeXCenter-bulletXCenter)<range){
if(Math.abs(planeYCenter-bulletYCenter)<range){
result=true;
}
}
returnresult;
}
privatevoidupdataspritepos(inti){
sprite.setPosition(bullets[i][1],bullets[i][2]);
}
/*nousenow
publicvoidresetDeadBullet(){
for(inti=0;i<bullets.length;i++){
if(bullets[i][5]==0){//deadbullet
initBullet(i);
}
}
}
*/
publicvoidkillbullets(Spriteplanesprite,intrange){
for(inti=0;i<bullets.length;i++){
if(bullets[i][5]!=0){//alivebullets
if(isCollision(planesprite,i,range)){
bullets[i][5]=0;
initBullet(i);
}
}
}
}
}
3、Font.java
packagefly;
importjavax.microedition.lcdui.*;
importjavax.microedition.lcdui.game.*;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassFont{
Spritesprite;
intwidth,height;
int[]charhash;
Graphicsg;
publicFont(Graphicsg,Imageimg,intwidth,intheight,char[]chars){
this.g=g;
sprite=newSprite(img,width,height);
this.width=width;
this.height=height;
charhash=newint[128];
for(inti=0;i<charhash.length;i++){
charhash[i]=-1;
}
Characterc;
for(inti=0;i<chars.length;i++){
c=newCharacter(chars[i]);
charhash[c.hashCode()]=i;
}
}
publicvoiddrawChar(charch,intx,inty){
Characterc=newCharacter(ch);
inthashcode=c.hashCode();
sprite.setPosition(x,y);
if(hashcode>=0){
sprite.setFrame(charhash[hashcode]);
sprite.paint(g);
}
}
publicvoiddrawString(Stringstr,intx,inty){
intlength=str.length();
for(inti=0;i<length;i++){
drawChar(str.charAt(i),x+width*i,y);
}
}
}
4、GameObject.java
packagefly;
importjavax.microedition.lcdui.game.*;
importjavax.microedition.lcdui.*;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassGameObject{
publicSpritesprite;
publicbooleanalive;
privateintlifecount=0;
publicintlifetime=0;
publicintspeed=0;
privateintanimcount=0;
publicGameObject(Imageimg,intwidth,intheight){
sprite=newSprite(img,width,height);
reset();
}
publicvoidmove(intdx,intdy){
sprite.move(dx,dy);
}
publicvoidmoveto(intx,inty){
sprite.setPosition(x,y);
}
publicvoidupdate(){
if(!alive)
return;
if(++animcount>speed){
animcount=0;
sprite.nextFrame();
if(lifetime!=0&&++lifecount>lifetime)
alive=false;
}
}
publicvoidpaint(Graphicsg){
if(!alive)
return;
sprite.paint(g);
}
publicvoidreset(){
alive=true;
lifecount=0;
animcount=0;
sprite.setFrame(0);
}
}
5、ImageTools.java
packagefly;
importjavax.microedition.lcdui.*;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassImageTools{
protectedImageTools(){
}
publicstaticImagegetImage(Stringstr){
Imageimg=null;
try{
img=Image.createImage(str);
}
catch(Exceptionex){
System.out.println(ex);
}
finally{
returnimg;
}
}
}
6、MyGameCanvas.java
packagefly;
importjavax.microedition.lcdui.game.GameCanvas;
importjavax.microedition.lcdui.*;
importjavax.microedition.lcdui.game.*;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassMyGameCanvasextendsGameCanvas
implementsRunnable,CommandListener{
privatestaticMyGameCanvasinstance;
Graphicsg;
booleanrunning;
Threadt;
Commandstartcmd,exitcmd,restartcmd;
intkeystate;
booleankeyevent;
booleankey_up,key_down,key_left,key_right,key_fire;
privatebooleanallowinput;
publicintscreenwidth;
publicintscreenheight;
booleangameover;
//defineyourvariablehere
longgametimeoffset;
longgametime;
intbombnum;
int[]bombaward;
intbombawardtop;
GameObjectplane;
intplanedirection;
TiledLayerbackground;
Bulletsbullets;
GameObjectexplosion;
GameObjectbomb;
Imagebomb_ico;
Fontfontbig,fontsmall;
//defineyourvariableend
protectedMyGameCanvas(){
super(true);
g=getGraphics();
running=false;
t=null;
addCommand(startcmd=newCommand("start",Command.OK,1));
addCommand(exitcmd=newCommand("exit",Command.EXIT,1));
setCommandListener(this);
screenwidth=getWidth();
screenheight=getHeight();
//putyourinitoncecodehere
Imageimg=ImageTools.getImage("/pic/MyPlaneFrames.png");
plane=newGameObject(img,24,24);
planedirection=0;
img=ImageTools.getImage("/pic/back_water.png");
intbackcolumns=screenwidth/img.getWidth()+1;
intbackrows=screenheight/img.getHeight()+1;
background=newTiledLayer(backcolumns,backrows,img,img.getWidth(),img.getHeight());
intx,y;
for(inti=0;i<backcolumns*backrows;i++){
x=i%backcolumns;
y=i/backcolumns;
System.out.println("x="+x+"y="+y);
background.setCell(x,y,1);
}
img=ImageTools.getImage("/pic/bullet.png");
bullets=newBullets(img,img.getWidth(),img.getHeight(),20,screenwidth,screenheight);
img=ImageTools.getImage("/pic/explosion.png");
explosion=newGameObject(img,32,32);
bomb_ico=ImageTools.getImage("/pic/bomb_icon.png");
img=ImageTools.getImage("/pic/b_number.png");
fontbig=newFont(g,img,10,15,newchar[]{0,1,2,3,4,5,6,7,8,9});
img=ImageTools.getImage("/pic/s_number.png");
fontsmall=newFont(g,img,5,7,newchar[]{0,1,2,3,4,5,6,7,8,9});
img=ImageTools.getImage("/pic/bomb.png");
bomb=newGameObject(img,65,65);
bombaward=newint[]{0,1,1,1,1,1};
bombawardtop=bombaward.length-1;
//putyourinitoncecodeend
}
//privatevoidInitInstance(){}
synchronizedpublicstaticMyGameCanvasgetInstance(){
if(instance==null){
instance=newMyGameCanvas();
System.out.println("newMyGameCanvas");
}
returninstance;
}
publicvoidrun(){
System.out.println("MyGameCanvasrunstart");
longst=0,et=0,diff=0;
intrate=50;//16-17framepersecond
while(running){
st=System.currentTimeMillis();
//putyourcodehere
//input();
//gameLogic();
//yourcodeend
gameinput();
gameMain();
et=System.currentTimeMillis();
diff=et-st;
if(diff<rate){
//System.out.println("Sleep"+(rate-diff));
try{
Thread.sleep(rate-diff);
}
catch(InterruptedExceptionex){}
}else{
//System.out.println("rush,andtheframeusingtime:"+diff);
}
}
System.out.println("MyGameCanvasrunend");
}
publicvoidstart(){
if(!running){
running=true;
t=newThread(this);
t.start();
}
}
privatevoidgameMain(){
g.setColor(0,0,0);//clearscreen
g.fillRect(0,0,getWidth(),getHeight());
background.paint(g);//drawbackground
//g.setColor(255,255,255);
//g.drawString("hello",1,1,g.TOP|g.LEFT);
if(bomb.alive){
bomb.moveto(plane.sprite.getX()-20,plane.sprite.getY()-20);
bomb.paint(g);
bomb.update();
bullets.killbullets(plane.sprite,32);
}
bullets.paint(g);
plane.paint(g);
bullets.refreshBullets(plane.sprite,!gameover&&!bomb.alive);
g.drawImage(bomb_ico,0,screenheight-1,g.BOTTOM|g.LEFT);
fontbig.drawString(String.valueOf(gametime),screenwidth/2-15,10);
fontsmall.drawString(String.valueOf(bombnum),bomb_ico.getWidth(),screenheight-fontsmall.height);
if(gameover){
explosion.paint(g);
explosion.update();
if(!explosion.alive){
plane.alive=false;
g.setColor(255,255,255);
g.drawString(StringTools.timeOpinion(gametime),5,22,g.LEFT|g.TOP);
g.drawString("fly0.1verbyfavoyang",2,100,g.LEFT|g.TOP);
g.drawString("E-mail:favoyang@yahoo.com",2,115,g.LEFT|g.TOP);
g.drawString("simulatefrom:",2,130,g.LEFT|g.TOP);
g.drawString("Mr.tonys<holdon20sec1.20>",2,145,g.LEFT|g.TOP);
g.drawString("hellotony,justfunny.",2,160,g.LEFT|g.TOP);
}
}else{
gametime=(System.currentTimeMillis()-gametimeoffset)/1000;
intawardindex=(int)gametime/20;
if(awardindex>bombawardtop)
awardindex=bombawardtop;
if(bombaward[awardindex]!=0){
bombnum+=bombaward[awardindex];
bombaward[awardindex]=0;
}
if(keyevent){
if(key_up){
plane.move(0,-3);
plane.sprite.setFrame(0);
}
if(key_down){
plane.move(0,3);
plane.sprite.setFrame(0);
}
if(key_left){
plane.move(-3,0);
plane.sprite.setFrame(1);
}
if(key_right){
plane.move(3,0);
plane.sprite.setFrame(2);
}
if(key_fire){
if(!bomb.alive&&bombnum>0){//bombisntactivedandtheresenoughbomb.
bomb.reset();
bomb.alive=true;
bombnum--;
}
}
}
else{
plane.sprite.setFrame(0);
}
}
flushGraphics();
}
privatevoidgameInit(){
gameover=false;
gametime=0;
gametimeoffset=System.currentTimeMillis();
allowinput=true;
key_up=key_down=key_left=key_right=key_fire=false;
plane.moveto((screenwidth-plane.sprite.getWidth())/2,
(screenheight-plane.sprite.getHeight())/2);
bullets.initBullets();
plane.reset();
explosion.reset();
explosion.lifetime=3;
bomb.reset();
bomb.lifetime=6;
bomb.alive=false;
bombnum=3;
for(inti=0;i<bombaward.length;i++){
bombaward[i]=1;
}
bombaward[0]=0;
printInfo();
}
publicvoidstop(){
if(running){
running=false;
}
}
privatevoidprintInfo(){
System.out.println("MyGameCanvasprintInfo()start:");
System.out.println("width:"+getWidth()+"Height:"+getHeight());
java.lang.Runtimert=java.lang.Runtime.getRuntime();
System.out.println("totalmemory:"+rt.totalMemory());
System.out.println("freememory:"+rt.freeMemory());
System.out.println("MyGameCanvasprintInfo()end:");
}
publicvoidcommandAction(Commandc,Displayabled){
Stringcmdstr=c.getLabel();
if(cmdstr.equals("start")){
gameInit();
start();
removeCommand(startcmd);
addCommand(restartcmd=newCommand("restart",Command.OK,1));
}elseif(cmdstr.equals("restart")){
stop();
while(t.isAlive());
gameInit();
start();
}elseif(cmdstr.equals("exit")){
stop();
Navigate.midlet.destroyApp(false);
Navigate.midlet.notifyDestroyed();
}
}
privatevoidgameinput(){
if(allowinput){
keystate=getKeyStates();
keyevent=false;
if((keystate&UP_PRESSED)!=0){//up
key_up=true;keyevent=true;
//dealyourunstopjobcodehere
planedirection=1;
//System.out.println("uppress");
//dealyourunstopjobcodeend
}elseif((keystate&UP_PRESSED)==0){//releasekey
if(key_up==true){
key_up=false;
//dealyouronepress-onejobcodehere
//System.out.println("uprelease");
//dealyouronepress-onejobcodeend
}
}
if((keystate&DOWN_PRESSED)!=0){//down
key_down=true;keyevent=true;
//dealyourunstopjobcodehere
planedirection=2;
//System.out.println("downpress");
//dealyourunstopjobcodeend
}elseif((keystate&DOWN_PRESSED)==0){//releasekey
if(key_down==true){
key_down=false;
//dealyouronepress-onejobcodehere
//System.out.println("downrelease");
//dealyouronepress-onejobcodeend
}
}
if((keystate&LEFT_PRESSED)!=0){//left
key_left=true;keyevent=true;
//dealyourunstopjobcodehere
planedirection=3;
//System.out.println("leftpress");
//dealyourunstopjobcodeend
}elseif((keystate&LEFT_PRESSED)==0){//releasekey
if(key_left==true){
key_left=false;
//dealyouronepress-onejobcodehere
//System.out.println("leftrelease");
//dealyouronepress-onejobcodeend
}
}
if((keystate&RIGHT_PRESSED)!=0){//right
key_right=true;keyevent=true;
//dealyourunstopjobcodehere
planedirection=4;
//System.out.println("rightpress");
//dealyourunstopjobcodeend
}elseif((keystate&RIGHT_PRESSED)==0){//releasekey
if(key_right==true){
key_right=false;
//dealyouronepress-onejobcodehere
//System.out.println("rightrelease");
//dealyouronepress-onejobcodeend
}
}
if((keystate&FIRE_PRESSED)!=0){//fire
key_fire=true;keyevent=true;
//dealyourunstopjobcodehere
planedirection=0;
//System.out.println("firepress");
//dealyourunstopjobcodeend
}elseif((keystate&FIRE_PRESSED)==0){//releasekey
if(key_fire==true){
key_fire=false;
//dealyouronepress-onejobcodehere
//System.out.println("firerelease");
//dealyouronepress-onejobcodeend
}
}
if(!keyevent){
//nokeyeventhere
//System.out.println("NOKEYpress");
//nokeyeventend
}
}
}
publicstaticvoidcleanJob(){
instance=null;
}
}
7、Navigate.java
packagefly;
importjavax.microedition.lcdui.*;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassNavigate{
privatestaticNavigateinstance;
publicstaticMyGameCanvasmc;
publicstaticFlyMidletmidlet;
publicstaticDisplaydisplay;
protectedNavigate(FlyMidletmidlet){
Navigate.midlet=midlet;
Navigate.mc=MyGameCanvas.getInstance();
Navigate.display=Display.getDisplay(midlet);
}
synchronizedpublicstaticNavigategetInstance(FlyMidletmidlet){
if(instance==null){
instance=newNavigate(midlet);
System.out.println("newNavigate");
}
returninstance;
}
publicstaticvoidcleanJob(){
instance=null;
}
}
8、StringTools.java
packagefly;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassStringTools{
protectedStringTools(){
}
publicstaticStringtimeOpinion(longgametime){
if(gametime<10){
return"Doyouplaywithyourfoot?";
//return"icantbelive,yourareagamemaster";
}elseif(gametime<16){
return"comeboy,youcandoit!";
}elseif(gametime<20){
return"whatapity!tryagain.";
}elseif(gametime<25){
return"verywell,youarearealman.";
}elseif(gametime<30){
return"iknowyouhavetalentofthisgame.";
}elseif(gametime<40){
return"icantbelive,yourareagamemaster.";
}else{
return"ohmygod,areyouahuman?";
}
}
}
9.fly.jad
MIDlet-1:FlyMidlet,,fly.FlyMidlet
MIDlet-Jar-Size:18499
MIDlet-Jar-URL:fly.jar
MIDlet-Name:MyMIDletSuite
MIDlet-Vendor:MyVendor
MIDlet-Version:1.0
最后就是我对java的几点希望:首先是IDE工具,有人说java已经很好了,有jbuilder,eclipse,netBeans等等,但是我认为如果java想超越.net,那么他首先要解决的就是IDE工具的整合。 |
|