⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mygamecanvas.java

📁 j2me 是男人就撑20秒源代码 我
💻 JAVA
字号:
package fly;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;//用户界面包,主要用于构造程序的用户界面
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.*;//是MIDlet程序的声明周期包

public class MyGameCanvas extends GameCanvas implements Runnable, CommandListener{//主类

  private static MyGameCanvas instance;//MyGameCanvas 的唯一实例
  Graphics g;//基础的绘图类对象实例,通过 getGraphics 获得
  boolean running;//游戏运行的状态,初始值为 false
  Thread t;//游戏运行线程,负责处理用户的按键输入和图像同步输出的线程实例,初始为 null
  Command startcmd,restartcmd,exitcmd,easycmd,hardcmd,crazycmd;//命令对象
  public int screenwidth;//屏幕宽度
  public int screenheight;//屏幕高度
  TiledLayer background;//绘制游戏背景的对象

  GameObject plane;//游戏中的飞机
  int planedirection;//飞机飞行的方向

  int keystate;//按键的状态
  boolean keyevent;//按键事件,当有键按下时,keyevent 的值为 true
  boolean key_up,key_down,key_left,key_right,key_fire; //按键的方向,上、下、左、右,开火

  private int planeX1;//飞机在X1点的坐标
  private int planeY1;
  private int planeX2;//飞机在X2点的坐标
  private int planeY2;

  Bullets bullets;//子弹对象
  boolean gameover;//游戏结束标志

  GameObject explosion;//爆炸效果对象

  long gametime;//游戏进行时间
  long gametimeoffset;//游戏开始的时间

  Font fontbig,fontsmall;//用于输出游戏计时时间的字体和炸弹数量的字体

  private boolean allowinput;//接受键盘输入控制

  int bombnum;//记录炸弹的个数
  int []bombaward;//奖励炸弹
  int bombawardtop;//增加炸弹的增量
  GameObject bomb;//炸弹对象
  Image bomb_ico;//炸弹的图像,位于屏幕左下角

  Bullets feidie;//飞碟对象

  //以上为定义的变量

  protected MyGameCanvas() {//主类的构造函数
    super(true);
    g=getGraphics();
    running=false;
    t=null;

    //addCommand(startcmd=new Command("开始",Command.OK,1));//添加开始命令
	addCommand(startcmd=new Command("简单",Command.OK,1));
	addCommand(startcmd=new Command("普通",Command.OK,1));
	addCommand(startcmd=new Command("困难",Command.OK,1));
	addCommand(exitcmd=new Command("退出",Command.EXIT,1));//添加退出命令
	setCommandListener(this);
	screenwidth=getWidth();
	screenheight=getHeight();

	Image 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/MyPlaneFrames.png");
	plane=new GameObject(img,24,24);
	planedirection=0;

	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);

	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'});

	bomb_ico=ImageTools.getImage("/pic/bomb_icon.png");
	img=ImageTools.getImage("/pic/bomb.png");
	bomb=new GameObject(img,65,65);
	bombaward=new int[]{0,1,1,1,1,1,1,1,1,1,1};
	bombawardtop=bombaward.length-1;

	img=ImageTools.getImage("/pic/mg.png");
	feidie = new Bullets(img,img.getWidth(),img.getHeight(),1,screenwidth,screenheight);
  }//MyGameCanvas

  synchronized public static MyGameCanvas getInstance() {
    if (instance == null) {
      instance = new MyGameCanvas();
      System.out.println("new MyGameCanvas");
    }
    return instance;
  }//getInstance

  public void run(){
	  System.out.println("run 开始执行");
	  long st=0,et=0,diff=0;
	  int rate=50;
	  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("run 结束");
  }//run

  public void start(){
      if(!running){
        running=true;
        t=new Thread(this);
        t.start();
      }
  }//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);
		  feidie.killbullets(plane.sprite,32);
	  }

      bullets.paint(g);
	  feidie.paint(g);
	  plane.paint(g);
	  bullets.refreshBullets(plane.sprite,!gameover&&!bomb.alive);
	  feidie.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),screenwidth/2,60,g.HCENTER|g.TOP);
		  g.drawString("游戏版本:0.01",screenwidth/2,130,g.HCENTER|g.TOP);
		  g.drawString("游戏制作:地狱鼠",screenwidth/2,145,g.HCENTER|g.TOP);
		  }
	  }else{
		  gametime=(System.currentTimeMillis()-gametimeoffset)/100;
		  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) {
			  planeX1=plane.sprite.getX();
			  plane.move(0,-10);
			  planeY2=plane.sprite.getY();
			  if(planeY2<0)
			  {
				  plane.moveto(planeX1,0);
			  }
			  plane.sprite.setFrame(0);
		  }
	      if(key_down) {
			  planeX1=plane.sprite.getX();
			  plane.move(0,10);
			  planeY2=plane.sprite.getY()+20;
			  if(planeY2 > screenheight)
			  {
				  plane.moveto(planeX1,screenheight-20);
			  }
			  plane.sprite.setFrame(0);
		  }
		  if(key_left) {
			  planeY1=plane.sprite.getY();
			  plane.move(-10,0);
			  planeX2=plane.sprite.getX();
			  if(planeX2<0)
			  {
				  plane.moveto(0,planeY1);
			  }
			  plane.sprite.setFrame(1);
		  }
		  if(key_right) {
			  planeY1=plane.sprite.getY();
			  plane.move(10,0);
			  planeX2=plane.sprite.getX()+24;
			  if(planeX2 > screenwidth)
			  {
				  plane.moveto(screenwidth-24,planeY1);
			  }
			  plane.sprite.setFrame(2);
		  }
		  if(key_fire) {
			  if(!bomb.alive && bombnum>0){
            bomb.reset();
            bomb.alive=true;
            bombnum--;
		  }
		  }
	  }
	  else{
		  plane.sprite.setFrame(0);
	  }
	  }

	  flushGraphics();
  }//gameMain

  private void gameInit() {
	  gameover=false;
	  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();

	  feidie.initBullets();
	  feidie.reset();

	  explosion.reset();
	  explosion.lifetime=3;

	  gametime=0;
	  gametimeoffset=System.currentTimeMillis();

	  allowinput=true;

	  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();
  }//gameInit

  public void stop(){
    if(running){
      running = false;
    }
  }//stop

  private void printInfo(){
	  System.out.println("MyGameCanvas printInfo() 开始:");
	  System.out.println("宽:"+getWidth()+"高:"+getHeight());
	  java.lang.Runtime rt=java.lang.Runtime.getRuntime();
	  System.out.println("总的内存:"+rt.totalMemory());
	  System.out.println("剩余内存:"+rt.freeMemory());
	  System.out.println("MyGameCanvas printInfo() 结束:");
  }//printInfo

  public void commandAction(Command c, Displayable d) {
    String cmdstr=c.getLabel();
    if(cmdstr.equals("简单")){
		Image img=ImageTools.getImage("/pic/bullet.png");
		bullets=new Bullets(img,img.getWidth(),img.getHeight(),10,screenwidth,screenheight);
		img=ImageTools.getImage("/pic/mg.png");
	feidie = new Bullets(img,img.getWidth(),img.getHeight(),1,screenwidth,screenheight);
        gameInit();
        start();
       //removeCommand(startcmd);
	}else if(cmdstr.equals("普通")){
		Image img=ImageTools.getImage("/pic/bullet.png");
		bullets=new Bullets(img,img.getWidth(),img.getHeight(),25,screenwidth,screenheight);
		img=ImageTools.getImage("/pic/mg.png");
	feidie = new Bullets(img,img.getWidth(),img.getHeight(),2,screenwidth,screenheight);
		gameInit();
		start();
		//removeCommand(startcmd);
	}else if(cmdstr.equals("困难")){
		Image img=ImageTools.getImage("/pic/bullet.png");
		bullets=new Bullets(img,img.getWidth(),img.getHeight(),40,screenwidth,screenheight);
		img=ImageTools.getImage("/pic/mg.png");
	feidie = new Bullets(img,img.getWidth(),img.getHeight(),3,screenwidth,screenheight);
		gameInit();
		start();
		//removeCommand(startcmd);
       /*addCommand(restartcmd=new Command("重新开始",Command.OK,1));
    }else if(cmdstr.equals("重新开始")){
		stop();
		while(t.isAlive());
		gameInit();
		start();*/
    }else if(cmdstr.equals("退出")){
		stop();
        Navigate.midlet.destroyApp(false);
		Navigate.midlet.notifyDestroyed();
    }
  }//commandAction

  private void gameinput() {
	  if(allowinput){
	  keystate=getKeyStates();
	  keyevent=false;
	  if((keystate & UP_PRESSED)!=0) {
		  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){
		  key_down=true;
		  keyevent=true;
		  planedirection=2;
	  }else if((keystate & DOWN_PRESSED)==0) {
		  if(key_down==true){
			  key_down=false;
		  }
	  }

	  if((keystate & LEFT_PRESSED)!=0){
		  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){
		  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){
		  key_fire=true;
		  keyevent=true;
		  planedirection=0;
	  }else if((keystate & FIRE_PRESSED)==0){
		  if(key_fire==true){
			  key_fire=false;
		  }
	  }

	  if(!keyevent){
	  }
	  }

  }//gameinput

  public static void cleanJob(){
    instance=null;
  }//cleanJob

}//MyGameCanvas

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -