?? mygamecanvas.java
字號:
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.Command;
import java.util.Random;
public class MyGameCanvas extends GameCanvas implements Runnable, CommandListener{
private static MyGameCanvas instance;
Graphics g;
boolean running;
Thread t;
Command exitcmd,restartcmd,easycmd,normalcmd,hardcmd;//命令操作
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;//游戲運行時間
public int bombnum;//保險的數量
int []bombaward;
int bombawardtop;
int planedirection;//飛機的運動方向
TiledLayer background,openingphoto;
private Bullets bullets;//子彈數組
private Bullets bombadded;
private Bullets ufobullets;
GameObject plane;//飛機
GameObject explosion;//爆炸
GameObject bomb;//保險
GameObject ufo;//飛碟
Image bomb_ico;//保險圖標
Font fontbig;//時間字體
Font fontsmall;//保險所剩的數量顯示
Font font;
Image img;
private int bulletspeed;
private int bulletnum;
private Random rdm;//隨機數
int ufox,ufoy;//飛碟坐標
boolean ufotrue;
int planex,planey;
int ufobulletsnum;
//////////////////////////////////////
protected MyGameCanvas() {
super(true);
g=getGraphics();
running=false;
t=null;
addCommand(easycmd=new Command("Easy",Command.OK,1));
addCommand(normalcmd=new Command("Normal",Command.OK,1));
addCommand(hardcmd=new Command("Hard",Command.OK,1));
addCommand(exitcmd=new Command("退出",Command.EXIT,0));
img=ImageTools.getImage("/plane.png");//初始化圖片
g.drawImage(img,getWidth()/2,getHeight()/2,g.VCENTER|g.HCENTER);
setCommandListener(this);
screenwidth=getWidth();
screenheight=getHeight();
gametime=0;
gametimeoffset=System.currentTimeMillis();//初始化系統時間
//put your init once code here
img=ImageTools.getImage("/MyPlaneFrames.png");//飛機圖片24*24
plane=new GameObject(img,24,24);//
img=ImageTools.getImage("/UFO.png");//UFO圖片24*24
ufo=new GameObject(img,40,32);//
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("/bullet3.png");//子彈圖片8*8
bullets=new Bullets(img,8,8,screenwidth,screenheight);
img=ImageTools.getImage("/bullet2.png");//飛碟子彈圖片8*8
ufobullets=new Bullets(img,8,8,screenwidth,screenheight);
img=ImageTools.getImage("/explosion.png");//爆炸效果圖32*32
explosion=new GameObject(img,32,32);
img=ImageTools.getImage("/bomb_icon.png");//圖片18*18
bombadded=new Bullets(img,18,18,screenwidth,screenheight);
bomb_ico=ImageTools.getImage("/bomb_icon.png");//保險圖標
img=ImageTools.getImage("/s_number.png");//時間圖片10*15
fontsmall=new Font(g,img,5,7,new char[]{'0','1','2','3','4','5','6','7','8','9'});
img=ImageTools.getImage("/b_number.png");//保險所剩的數量顯示5*7
fontbig=new Font(g,img,10,15,new char[]{'0','1','2','3','4','5','6','7','8','9'});
img=ImageTools.getImage("/bomb.png");//保險圖片65*65
bomb=new GameObject(img,65,65);
bombaward=new int[]{0,1,1,1,1,1};
bombawardtop=bombaward.length-1;
}
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=35;
while(running){
st=System.currentTimeMillis();
gameinput();
gameMain();
et=System.currentTimeMillis();
diff=et-st;
if(diff<rate){
try {
Thread.sleep(rate - diff);
}
catch (InterruptedException ex) {}
}else{}
}
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);//清屏
g.fillRect(0,0,getWidth(),getHeight());
background.paint(g);
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);
if(!gameover){
gametime=(System.currentTimeMillis()-gametimeoffset)/1000;
}
long abc=(System.currentTimeMillis ()-gametimeoffset)/100;
if(abc%100==0){//每隔10秒初始化一次保險
bombadded.initBullets(5,1);
}
if(gametime%10>0&&gametime%10<=6&&(gametime>6)){//在背景上顯示保險6秒
bombadded.paint(g);
if(bombadded.addbomb(plane.sprite,!gameover && !bomb.alive)){
bombnum+=1;
}
}
if(!gameover){
gametime=(System.currentTimeMillis()-gametimeoffset)/1000;
}
abc=(System.currentTimeMillis ()-gametimeoffset)/100;
//System.out.println(abc);
///////////////顯示飛碟
if(abc%150==0){
rdm=new Random();
ufox=(rdm.nextInt() & 0x7fffffff)%(screenwidth-80);
System.out.println(ufox);
ufoy=(rdm.nextInt() & 0x7fffffff)%(screenheight/2-15);
System.out.println(ufoy);
System.out.println("MyGameCanvas printInfo() asdf");
ufo.moveto(ufox,ufoy);
ufobullets.initufoBullets(bulletspeed,ufobulletsnum,ufox,ufoy);
}
if(gametime%15>0&&gametime%15<=3&&gametime>3){
ufo.paint(g);
//ufo.update();
ufobullets.paint(g);//
ufobullets.refreshBullets(plane.sprite,!gameover && !bomb.alive);
}
fontsmall.drawString(String.valueOf(gametime),screenwidth/2-2,10);
fontbig.drawString(String.valueOf(bombnum),19,screenheight-16);
if (gameover) {
explosion.paint(g);
explosion.update();
img=ImageTools.getImage("/mm.png");
g.drawImage(img,getWidth()/2,getHeight()/2+50,g.VCENTER|g.HCENTER);
if(!explosion.alive){
plane.alive=false;
//font=getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD ,Font.SIZE_SMALL);
//g.setFont(font);
g.setColor(255,255,255);//設置顯示字體的顏色
g.drawString(StringTools.timeOpinion(gametime),5,22,g.LEFT|g.TOP);
g.drawString("繼續努力 ! 你的時間是"+gametime+"秒 !" ,screenwidth/2-70,screenheight/2,g.LEFT|g.TOP);//顯示漢字
}
}
else{
gametime=(System.currentTimeMillis()-gametimeoffset)/1000;
int x,y;
if (keyevent) {
if(key_up){
x=plane.getX();
y=plane.getY();
plane.move(0, -7,screenwidth,screenheight,x,y);
plane.sprite.setFrame(0);
}
if(key_down){
x=plane.getX();
y=plane.getY();
plane.move(0, 7,screenwidth,screenheight,x,y);
plane.sprite.setFrame(0);
}
if(key_left){
x=plane.getX();
y=plane.getY();
plane.move( -7, 0,screenwidth,screenheight,x,y);
plane.sprite.setFrame(1);
}
if(key_right){
x=plane.getX();
y=plane.getY();
plane.move(7, 0,screenwidth,screenheight,x,y);
plane.sprite.setFrame(2);
}
if(key_fire){
if(!bomb.alive && bombnum>0){//保險未被激活,且保險還有剩余.
bomb.reset();
bomb.alive=true;
bombnum--;
}
}
}
else {
plane.sprite.setFrame(0);
}
}
flushGraphics();
}
private void gameInit(int bulletspeed,int num) {
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(bulletspeed,num);
bombadded.initBullets(1,1);//初始化保險
plane.reset();
explosion.reset();
explosion.lifetime=3;
bomb.reset();
bomb.lifetime=6;
bomb.alive=false;
bombnum=3;
///////////////////////////////////
bomb.reset();
bomb.lifetime=6;
bomb.alive=false;
///////////////////////////////////
/*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("Easy")){
bulletspeed=2;
bulletnum=10;
ufobulletsnum=3;
gameInit(bulletspeed,bulletnum);
start();
removeCommand(restartcmd);
addCommand(restartcmd=new Command("重玩",Command.OK,1));
}else if(cmdstr.equals("Normal")){
bulletspeed=3;
bulletnum=12;
ufobulletsnum=6;
gameInit(bulletspeed,bulletnum);
start();
removeCommand(restartcmd);
addCommand(restartcmd=new Command("重玩",Command.OK,1));
}else if(cmdstr.equals("Hard")){
bulletspeed=4;
bulletnum=16;
ufobulletsnum=10;
gameInit(bulletspeed,bulletnum);
start();
removeCommand(restartcmd);
addCommand(restartcmd=new Command("重玩",Command.OK,1));
}else if(cmdstr.equals("重玩")){
stop();
while(t.isAlive());
gameInit(bulletspeed,bulletnum);
start();
}else if(cmdstr.equals("退出")){
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;
planedirection=1;
}else if((keystate & UP_PRESSED)==0){
if(key_up==true){
key_up=false;
}
}
if((keystate & DOWN_PRESSED)!=0){//down
key_down=true;keyevent=true;
planedirection=2;
}else if((keystate & DOWN_PRESSED)==0){//release key
if(key_down==true){
key_down=false;
}
}
if((keystate & LEFT_PRESSED)!=0){//left
key_left=true;keyevent=true;
planedirection=3;
}else if((keystate & LEFT_PRESSED)==0){
if(key_left==true){
key_left=false;
}
}
if((keystate & RIGHT_PRESSED)!=0){//right
key_right=true;keyevent=true;
planedirection=4;
}else if((keystate & RIGHT_PRESSED)==0){
if(key_right==true){
key_right=false;
}
}
if((keystate & FIRE_PRESSED)!=0){//fire
key_fire=true;keyevent=true;
planedirection=0;
}else if((keystate & FIRE_PRESSED)==0){//release key
if(key_fire==true){
key_fire=false;
}
}
if(!keyevent){}
}
}
public static void cleanJob(){
instance=null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -