JAVA网站制作之J2ME小游戏-fly
还得说上一点,就java本质而言,是面相对象的,但是你有没有发现,java也不全是,比如说基本类型,int,那他就是整型而不是对象,转换类型是还得借助包装类。1、FlyMidlet.javapackagefly;
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;
rnd=newRandom();
this.width=width;
this.height=height;
}
publicvoidinitBullets(){
for(inti=0;i<bullets.length;i++){
initBullet(i);
}
}
privatevoidinitBullet(inti){
bullets=(rnd.nextInt()&0x7fffffff)%4;//type
bullets=1;//alive
switch(bullets){
caseBULLET_TYPE_LEFT:
bullets=-5;
bullets=(rnd.nextInt()&0x7fffffff)%height;
bullets=(rnd.nextInt()&0x7fffffff)%3+1;//vx
bullets=(rnd.nextInt())%3;//vy
break;
caseBULLET_TYPE_RIGHT:
bullets=width+5;
bullets=(rnd.nextInt()&0x7fffffff)%height;
bullets=((rnd.nextInt()&0x7fffffff)%3+1)*-1;//vx
bullets=(rnd.nextInt())%3;//vy
break;
caseBULLET_TYPE_TOP:
bullets=(rnd.nextInt()&0x7fffffff)%width;
bullets=-5;
bullets=(rnd.nextInt())%3;//vx
bullets=(rnd.nextInt()&0x7fffffff)%3+1;//vy
break;
caseBULLET_TYPE_BOTTOM:
bullets=(rnd.nextInt()&0x7fffffff)%width;
bullets=height+5;
bullets=(rnd.nextInt())%3;//vx
bullets=((rnd.nextInt()&0x7fffffff)%3+1)*-1;//vy
break;
}
}
publicvoidupdata(inti){
bullets+=bullets;
bullets+=bullets;
if(bullets<-5||bullets>width+5){
bullets*=-1;
}
if(bullets<-5||bullets>height+5){
bullets*=-1;
}
}
privatevoidpaint(Graphicsg,inti){
updataspritepos(i);
sprite.paint(g);
}
publicvoidpaint(Graphicsg){
for(inti=0;i<bullets.length;i++){
if(bullets==0){
continue;
}
sprite.setPosition(bullets,bullets);
sprite.paint(g);
}
}
publicvoidrefreshBullets(Spriteplanesprite,booleanneedcollision){
for(inti=0;i<bullets.length;i++){
if(bullets==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-16,
bullets-16);
bullets=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+3;
intbulletYCenter=bullets+3;
if(Math.abs(planeXCenter-bulletXCenter)<range){
if(Math.abs(planeYCenter-bulletYCenter)<range){
result=true;
}
}
returnresult;
}
privatevoidupdataspritepos(inti){
sprite.setPosition(bullets,bullets);
}
/*nousenow
publicvoidresetDeadBullet(){
for(inti=0;i<bullets.length;i++){
if(bullets==0){//deadbullet
initBullet(i);
}
}
}
*/
publicvoidkillbullets(Spriteplanesprite,intrange){
for(inti=0;i<bullets.length;i++){
if(bullets!=0){//alivebullets
if(isCollision(planesprite,i,range)){
bullets=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;
for(inti=0;i<charhash.length;i++){
charhash=-1;
}
Characterc;
for(inti=0;i<chars.length;i++){
c=newCharacter(chars);
charhash=i;
}
}
publicvoiddrawChar(charch,intx,inty){
Characterc=newCharacter(ch);
inthashcode=c.hashCode();
sprite.setPosition(x,y);
if(hashcode>=0){
sprite.setFrame(charhash);
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!=0){
bombnum+=bombaward;
bombaward=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=1;
}
bombaward=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工具的整合。 接着就是EJB了,EJB就是Enterprise JavaBean, 看名字好象它是Javabean,可是它和Javabean还是有区别的。它是一个体系结构,你可以搭建更安全、更稳定的企业应用。它的大量代码已由中间件(也就是我们常听到的 Weblogic,Websphere这些J2EE服务器)完成了,所以我们要做的程序代码量很少,大部分工作都在设计和配置中间件上。 Java 编程语言的风格十分接近C、C++语言。 是一种使用者不需花费很多时间学习的语言 学Java必读的两个开源程序就是Jive和Pet Store.。 Jive是国外一个非常著名的BBS程序,完全开放源码。论坛的设计采用了很多先进的技术,如Cache、用户认证、Filter、XML等,而且论坛完全屏蔽了对数据库的访问,可以很轻易的在不同数据库中移植。论坛还有方便的安装和管理程序,这是我们平时编程时容易忽略的一部份(中国程序员一般只注重编程的技术含量,却完全不考虑用户的感受,这就是我们与国外软件的差距所在)。 是一种由美国SUN计算机公司(Sun Microsystems, Inc.)所研究而成的语言 是一种使网页(Web Page)产生生动活泼画面的语言 Sun公司看见Oak在互联网上应用的前景,于是改造了Oak,于1995年5月以Java的名称正式发布。Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。 如果要向java web方向发展也要吧看看《Java web从入门到精通》学完再到《Struts2.0入门到精通》这样你差不多就把代码给学完了。有兴趣可以看一些设计模块和框架的包等等。 是一种使网页(Web Page)产生生动活泼画面的语言 是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能 是一种使网页(Web Page)产生生动活泼画面的语言 是一种突破用户端机器环境和CPU Jive的资料在很多网站上都有,大家可以找来研究一下。相信你读完代码后,会有脱胎换骨的感觉。遗憾的是Jive从2.5以后就不再无条件的开放源代码,同时有licence限制。不过幸好还有中国一流的Java程序员关注它,外国人不开源了,中国人就不能开源吗?这里向大家推荐一个汉化的Jive版本—J道。Jive(J道版)是由中国Java界大名 鼎鼎的banq在Jive 2.1版本基础上改编而成, 全中文,增加了一些实用功能,如贴图,用户头像和用户资料查询等,而且有一个开发团队在不断升级。你可以访问banq的网站 是一种为 Internet发展的计算机语言 Sun公司看见Oak在互联网上应用的前景,于是改造了Oak,于1995年5月以Java的名称正式发布。Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。 还好,SUN提供了Javabean可以把你的JSP中的 Java代码封装起来,便于调用也便于重用。 J2SE开发桌面应用软件比起 VC,VB,DEPHI这些传统开发语言来说,优势好象并不明显。J2ME对于初学者来说,好象又有点深奥,而且一般开发者很难有开发环境。 至于JDBC,就不用我多说了,你如果用java编过存取数据库的程序,就应该很熟悉。还有,如果你要用Java编发送电子邮件的程序,你就得看看Javamail 了。 是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言
页:
[1]