import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
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("/MyPlaneFrames.png");
plane=new GameObject(img,24,24);
planedirection=0;
img=ImageTools.getImage("/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("/bullet.png");
bullets=new Bullets(img,img.getWidth(),img.getHeight(),20,screenwidth,screenheight);
img=ImageTools.getImage("/explosion.png");
explosion=new GameObject(img,32,32);
bomb_ico=ImageTools.getImage("/bomb_icon.png");
img=ImageTools.getImage("/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("/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("/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("2008 帝国崛起",2,100,g.LEFT|g.TOP);
g.drawString("E-mail : iori4497@126.com",2,115,g.LEFT|g.TOP);
g.drawString("simulate from:",2,130,g.LEFT|g.TOP);
g.drawString("****************** ",2,145,g.LEFT|g.TOP);
g.drawString("hello everyone, 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 isn't actived and there's 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");
没有合适的资源?快使用搜索试试~ 我知道了~
sd.rar_j2me
共54个文件
png:24个
class:16个
java:8个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 120 浏览量
2022-09-23
23:46:57
上传
评论
收藏 56KB RAR 举报
温馨提示
一个很有意思的J2ME小游戏,希望给大家带来乐趣
资源推荐
资源详情
资源评论
收起资源包目录
sd.rar (54个子文件)
sd
.eclipseme 233B
sd.jad 183B
bin
back_water.png 557B
FlyMidlet.class 1KB
Bullets.class 4KB
GameObject.class 2KB
MyPlaneFrames.png 1020B
bullet.png 230B
Font.class 2KB
ImageTools.class 774B
b_number.png 1KB
s_number.png 223B
bomb.png 4KB
Navigate.class 1KB
explosion.png 1KB
StringTools.class 790B
MyGameCanvas.class 9KB
bomb_icon.png 407B
verified
classes
back_water.png 557B
FlyMidlet.class 1KB
Bullets.class 4KB
GameObject.class 2KB
MyPlaneFrames.png 1020B
bullet.png 230B
Font.class 2KB
ImageTools.class 882B
b_number.png 1KB
s_number.png 223B
bomb.png 4KB
Navigate.class 1KB
explosion.png 1KB
StringTools.class 851B
MyGameCanvas.class 9KB
bomb_icon.png 407B
libs
res
back_water.png 557B
MyPlaneFrames.png 1020B
bullet.png 230B
b_number.png 1KB
s_number.png 223B
bomb.png 4KB
explosion.png 1KB
bomb_icon.png 407B
.settings
org.eclipse.jdt.core.prefs 166B
src
FlyMidlet.java 715B
Bullets.java 4KB
Navigate.java 632B
GameObject.java 891B
ImageTools.java 335B
StringTools.java 671B
Font.java 1KB
MyGameCanvas.java 10KB
.project 534B
.classpath 242B
www.pudn.com.txt 218B
共 54 条
- 1
资源评论
邓凌佳
- 粉丝: 65
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功