欢迎光临
我们一直在努力

J2ME小游戏-fly-JSP教程,J2ME开发

建站超值云服务器,限时71元/月

1、flymidlet.java

package fly;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.util.*;

/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright: copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/

public class flymidlet extends midlet {

navigate ng;

public flymidlet() {

ng=navigate.getinstance(this);

}

protected void startapp() {

system.out.println("startapp");

ng.display.setcurrent(ng.mc);

printinfo();

}

protected void pauseapp() {

system.out.println("pauseapp");

}

protected void destroyapp(boolean parm1) {

system.out.println("destroyapp");

navigate.mc.stop();

mygamecanvas.cleanjob();

navigate.cleanjob();

}

private void printinfo(){

system.out.println("flymidlet printinfo() start:");

system.out.println("flymidlet printinfo() end:");

}

}

2、bullets.java

package fly;

import javax.microedition.lcdui.game.*;

import javax.microedition.lcdui.*;

import java.util.random;

/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright: copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/

public class bullets extends gameobject {

private int[][] bullets;

private int bulletstotal;

private random rnd;

public static final int bullet_type_left=0;

public static final int bullet_type_right=1;

public static final int bullet_type_top=2;

public static final int bullet_type_bottom=3;

private int width,height;

public bullets(image img,int picwidth,int picheight,int bulletstotal,int width,int height) {

super(img,picwidth,picheight);

this.bulletstotal=bulletstotal;

bullets=new int[bulletstotal][6];

rnd=new random();

this.width=width;

this.height=height;

}

public void initbullets(){

for (int i = 0; i < bullets.length; i++) {

initbullet(i);

}

}

private void initbullet(int i) {

bullets[i][0] = (rnd.nextint() & 0x7fffffff) % 4; //type

bullets[i][5] = 1; //alive

switch (bullets[i][0]) {

case bullet_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;

case bullet_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;

case bullet_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;

case bullet_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;

}

}

public void updata(int i){

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;

}

}

private void paint(graphics g,int i){

updataspritepos(i);

sprite.paint(g);

}

public void paint(graphics g) {

for (int i = 0; i < bullets.length; i++) {

if(bullets[i][5]==0){

continue;

}

sprite.setposition(bullets[i][1],bullets[i][2]);

sprite.paint(g);

}

}

public void refreshbullets(sprite planesprite, boolean needcollision){

for (int i = 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);

}

}

private boolean iscollision(sprite sprite,int i,int range){

//updataspritepos(i);

//return sprite.collideswith(this.sprite,true);

boolean result=false;

int planexcenter=sprite.getx()+12;

int planeycenter=sprite.gety()+12;

int bulletxcenter=bullets[i][1]+3;

int bulletycenter=bullets[i][2]+3;

if(math.abs(planexcenter-bulletxcenter) < range){

if (math.abs(planeycenter – bulletycenter )< range) {

result = true;

}

}

return result;

}

private void updataspritepos(int i){

sprite.setposition(bullets[i][1],bullets[i][2]);

}

/* no use now

public void resetdeadbullet(){

for (int i = 0; i < bullets.length; i++) {

if(bullets[i][5]==0){//dead bullet

initbullet(i);

}

}

}

*/

public void killbullets(sprite planesprite,int range){

for (int i = 0; i < bullets.length; i++) {

if(bullets[i][5]!=0){//alive bullets

if(iscollision(planesprite, i, range)){

bullets[i][5]=0;

initbullet(i);

}

}

}

}

}

3、font.java

package fly;

import javax.microedition.lcdui.*;

import javax.microedition.lcdui.game.*;

/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright: copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/

public class font {

sprite sprite;

int width,height;

int[] charhash;

graphics g;

public font(graphics g,image img, int width, int height, char[] chars) {

this.g=g;

sprite=new sprite(img,width,height);

this.width=width;

this.height=height;

charhash=new int[128];

for (int i = 0; i < charhash.length; i++) {

charhash[i]=-1;

}

character c;

for (int i = 0; i < chars.length; i++) {

c=new character(chars[i]);

charhash[c.hashcode()]=i;

}

}

public void drawchar(char ch, int x, int y){

character c=new character(ch);

int hashcode=c.hashcode();

sprite.setposition(x,y);

if(hashcode>=0){

sprite.setframe(charhash[hashcode]);

sprite.paint(g);

}

}

public void drawstring(string str, int x, int y){

int length=str.length();

for (int i = 0; i < length; i++) {

drawchar(str.charat(i),x+width*i,y);

}

}

}

4、gameobject.java

package fly;

import javax.microedition.lcdui.game.*;

import javax.microedition.lcdui.*;

/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright: copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/

public class gameobject {

public sprite sprite;

public boolean alive;

private int lifecount=0;

public int lifetime=0;

public int speed=0;

private int animcount=0;

public gameobject(image img,int width,int height){

sprite=new sprite(img,width,height);

reset();

}

public void move(int dx,int dy){

sprite.move(dx,dy);

}

public void moveto(int x,int y){

sprite.setposition(x,y);

}

public void update(){

if(!alive)

return;

if(++animcount>speed){

animcount=0;

sprite.nextframe();

if(lifetime!=0 && ++lifecount>lifetime)

alive=false;

}

}

public void paint(graphics g){

if(!alive)

return;

sprite.paint(g);

}

public void reset(){

alive=true;

lifecount=0;

animcount=0;

sprite.setframe(0);

}

}

5、imagetools.java

package fly;

import javax.microedition.lcdui.*;

/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright: copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/

public class imagetools {

protected imagetools() {

}

public static image getimage(string str){

image img=null;

try {

img = image.createimage(str);

}

catch (exception ex) {

system.out.println(ex);

}

finally{

return img;

}

}

}

6、mygamecanvas.java

package fly;

import javax.microedition.lcdui.game.gamecanvas;

import javax.microedition.lcdui.*;

import javax.microedition.lcdui.game.*;

/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright: copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/

public class mygamecanvas extends gamecanvas

implements runnable, commandlistener{

private static mygamecanvas instance;

graphics g;

boolean running;

thread t;

command startcmd,exitcmd,restartcmd;

int keystate;

boolean keyevent;

boolean key_up,key_down,key_left,key_right,key_fire;

private boolean allowinput;

public int screenwidth;

public int screenheight;

boolean gameover;

//define your variable here

long gametimeoffset;

long gametime;

int bombnum;

int []bombaward;

int bombawardtop;

gameobject plane;

int planedirection;

tiledlayer background;

bullets bullets;

gameobject explosion;

gameobject bomb;

image bomb_ico;

font fontbig,fontsmall;

//define your variable end

protected mygamecanvas() {

super(true);

g=getgraphics();

running=false;

t=null;

addcommand(startcmd=new command("start",command.ok,1));

addcommand(exitcmd=new command("exit",command.exit,1));

setcommandlistener(this);

screenwidth=getwidth();

screenheight=getheight();

//put your init once code here

image img=imagetools.getimage("/pic/myplaneframes.png");

plane=new gameobject(img,24,24);

planedirection=0;

img=imagetools.getimage("/pic/back_water.png");

int backcolumns=screenwidth/img.getwidth()+1;

int backrows=screenheight/img.getheight()+1;

background=new tiledlayer(backcolumns,backrows,img,img.getwidth(),img.getheight());

int x,y;

for (int i = 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=new bullets(img,img.getwidth(),img.getheight(),20,screenwidth,screenheight);

img=imagetools.getimage("/pic/explosion.png");

explosion=new gameobject(img,32,32);

bomb_ico=imagetools.getimage("/pic/bomb_icon.png");

img=imagetools.getimage("/pic/b_number.png");

fontbig=new font(g,img,10,15,new char[]{0,1,2,3,4,5,6,7,8,9});

img=imagetools.getimage("/pic/s_number.png");

fontsmall=new font(g,img,5,7,new char[]{0,1,2,3,4,5,6,7,8,9});

img=imagetools.getimage("/pic/bomb.png");

bomb=new gameobject(img,65,65);

bombaward=new int[]{0,1,1,1,1,1};

bombawardtop=bombaward.length-1;

//put your init once code end

}

//private void initinstance(){ }

synchronized public static mygamecanvas getinstance() {

if (instance == null) {

instance = new mygamecanvas();

system.out.println("new mygamecanvas");

}

return instance;

}

public void run(){

system.out.println("mygamecanvas run start");

long st=0,et=0,diff=0;

int rate=50;//16-17 frame per second

while(running){

st=system.currenttimemillis();

//put your code here

//input();

//gamelogic();

//your code end

gameinput();

gamemain();

et=system.currenttimemillis();

diff=et-st;

if(diff<rate){

//system.out.println("sleep "+(rate-diff));

try {

thread.sleep(rate – diff);

}

catch (interruptedexception ex) {}

}else{

//system.out.println("rush , and the frame using time: "+diff);

}

}

system.out.println("mygamecanvas run end");

}

public void start(){

if(!running){

running=true;

t=new thread(this);

t.start();

}

}

private void gamemain() {

g.setcolor(0,0,0);//clear screen

g.fillrect(0,0,getwidth(),getheight());

background.paint(g);//draw background

//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("fly 0.1 ver by favo yang",2,100,g.left|g.top);

g.drawstring("e-mail : favoyang@yahoo.com",2,115,g.left|g.top);

g.drawstring("simulate from:",2,130,g.left|g.top);

g.drawstring("mr.tony s <hold on 20sec 1.20> ",2,145,g.left|g.top);

g.drawstring("hello tony, just funny.",2,160,g.left|g.top);

}

}else{

gametime=(system.currenttimemillis()-gametimeoffset)/1000;

int awardindex=(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){//bomb isnt actived and theres enough bomb .

bomb.reset();

bomb.alive=true;

bombnum–;

}

}

}

else {

plane.sprite.setframe(0);

}

}

flushgraphics();

}

private void gameinit() {

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 (int i = 0; i < bombaward.length; i++) {

bombaward[i]=1;

}

bombaward[0]=0;

printinfo();

}

public void stop(){

if(running){

running = false;

}

}

private void printinfo(){

system.out.println("mygamecanvas printinfo() start:");

system.out.println("width : "+ getwidth()+ " height: "+getheight());

java.lang.runtime rt=java.lang.runtime.getruntime() ;

system.out.println("total memory: "+rt.totalmemory());

system.out.println("free memory: "+rt.freememory());

system.out.println("mygamecanvas printinfo() end:");

}

public void commandaction(command c, displayable d) {

string cmdstr=c.getlabel();

if(cmdstr.equals("start")){

gameinit();

start();

removecommand(startcmd);

addcommand(restartcmd=new command("restart",command.ok,1));

}else if(cmdstr.equals("restart")){

stop();

while(t.isalive());

gameinit();

start();

}else if(cmdstr.equals("exit")){

stop();

navigate.midlet.destroyapp(false);

navigate.midlet.notifydestroyed();

}

}

private void gameinput() {

if(allowinput){

keystate=getkeystates();

keyevent=false;

if((keystate & up_pressed)!=0){//up

key_up=true;keyevent=true;

//deal your unstop job code here

planedirection=1;

//system.out.println("up press");

//deal your unstop job code end

}else if((keystate & up_pressed)==0){//release key

if(key_up==true){

key_up=false;

//deal your one press-one job code here

//system.out.println("up release");

//deal your one press-one job code end

}

}

if((keystate & down_pressed)!=0){//down

key_down=true;keyevent=true;

//deal your unstop job code here

planedirection=2;

//system.out.println("down press");

//deal your unstop job code end

}else if((keystate & down_pressed)==0){//release key

if(key_down==true){

key_down=false;

//deal your one press-one job code here

//system.out.println("down release");

//deal your one press-one job code end

}

}

if((keystate & left_pressed)!=0){//left

key_left=true;keyevent=true;

//deal your unstop job code here

planedirection=3;

//system.out.println("left press");

//deal your unstop job code end

}else if((keystate & left_pressed)==0){//release key

if(key_left==true){

key_left=false;

//deal your one press-one job code here

//system.out.println("left release");

//deal your one press-one job code end

}

}

if((keystate & right_pressed)!=0){//right

key_right=true;keyevent=true;

//deal your unstop job code here

planedirection=4;

//system.out.println("right press");

//deal your unstop job code end

}else if((keystate & right_pressed)==0){//release key

if(key_right==true){

key_right=false;

//deal your one press-one job code here

//system.out.println("right release");

//deal your one press-one job code end

}

}

if((keystate & fire_pressed)!=0){//fire

key_fire=true;keyevent=true;

//deal your unstop job code here

planedirection=0;

//system.out.println("fire press");

//deal your unstop job code end

}else if((keystate & fire_pressed)==0){//release key

if(key_fire==true){

key_fire=false;

//deal your one press-one job code here

//system.out.println("fire release");

//deal your one press-one job code end

}

}

if(!keyevent){

//no keyevent here

//system.out.println("no key press");

//no keyevent end

}

}

}

public static void cleanjob(){

instance=null;

}

}

7、navigate.java

package fly;

import javax.microedition.lcdui.*;

/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright: copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/

public class navigate {

private static navigate instance;

public static mygamecanvas mc;

public static flymidlet midlet;

public static display display;

protected navigate(flymidlet midlet) {

navigate.midlet=midlet;

navigate.mc=mygamecanvas.getinstance();

navigate.display=display.getdisplay(midlet);

}

synchronized public static navigate getinstance(flymidlet midlet){

if (instance == null) {

instance = new navigate(midlet);

system.out.println("new navigate");

}

return instance;

}

public static void cleanjob(){

instance=null;

}

}

8、stringtools.java

package fly;

/**

* <p>title: </p>

* <p>description: </p>

* <p>copyright: copyright (c) 2004</p>

* <p>company: </p>

* @author not attributable

* @version 1.0

*/

public class stringtools {

protected stringtools() {

}

public static string timeopinion(long gametime){

if(gametime<10){

return "do you play with your foot?";

//return "i cant belive,your are a game master";

}else if(gametime<16){

return "come boy, you can do it!";

}else if(gametime<20){

return "what a pity! try again.";

}else if(gametime<25){

return "very well, you are a real man.";

}else if(gametime<30){

return "i know you have talent of this game.";

}else if(gametime<40){

return "i cant belive, your are a game master.";

}else{

return "oh my god, are you a human?";

}

}

}

9.fly.jad

midlet-1: flymidlet, , fly.flymidlet

midlet-jar-size: 18499

midlet-jar-url: fly.jar

midlet-name: my midlet suite

midlet-vendor: my vendor

midlet-version: 1.0

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » J2ME小游戏-fly-JSP教程,J2ME开发
分享到: 更多 (0)