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

📄 gamecanvas.java~11~

📁 J2ME飞行射击类游戏
💻 JAVA~11~
字号:

import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;

public class GameCanvas extends Canvas implements Runnable {

	private Image d_logo, menuBK, menu; //Logo图片 菜单背静 菜单选项
	public long lastTime;
	private byte menuState = 0;
	public Image im; // 脱屏画布对象
	public Graphics offG; // 脱屏画笔对象
	protected short SCREENWIDTH = 176, SCREENHEIGHT = 208;
	public static final byte MENU = -2;
	public static final byte LOAD = 0;
	public static final byte GAMEING = 1;
	public static final byte GAMEOVER = 2;
	public static final byte PAUSE = 4;
	public byte gameState = 0;
	public static boolean ISRUN = true;
	public int level_Mode = 1, proess = 0;
	public int nowFramemenu = 0;
	public boolean soundOn = true;
	public Image mapbk, mirromap;
	public byte UFO[][];//敌人
	public long levelStart = 0;// 记录时间
	public int mapLength;
	public int mappy = 0;
	public Image plane1, plane2, transform;
	public Image fire;

	public Image imPlane;
	public int px,py;
	public int direct;//0 stop 1up 2down 3left 4 right
	public int dirFrame[]={0,4,3,1,2};

	public Image imBullet;
	public Vector allBullet;
	public Timer t;
	public boolean isFire=false;//是否开火
	public GameCanvas() {
		this.setFullScreenMode(true);
        //		 创建脱屏画布
		im = Image.createImage(SCREENWIDTH, SCREENHEIGHT);
		offG = im.getGraphics(); // 脱屏画笔

		try {
			menuBK = Image.createImage("/caidan.png");
			menu = Image.createImage("/caidan1.png");
			imPlane=Image.createImage("/zhujue1.png");
			imBullet=Image.createImage("/bullet12.png");
		} catch (Exception e) {
			System.out.println("nopic");
		}
	   px=176/2-15;
	   py=208-30;
	   direct=0;

	   allBullet=new Vector();
	   t=new Timer();
	   TimerTask ts1=new TimerTask()
	   {
	     public void run()
	     {//产生子弹
	       	if(isFire)
	       	{int tmp[]=new int[3];
	       	 tmp[0]=px+10;
	       	 tmp[1]=py;
             tmp[2]=0;//当前子弹的帧
	       	 allBullet.addElement(tmp);
	       	}
	     }
	   };
	   t.schedule(ts1,0,300);//每隔300毫秒产生1颗子弹
	   TimerTask ts2=new TimerTask()
	   {
	   	public void run()
	   	{for(int i=0;i<allBullet.size();i++)
	   	{int tmp[]=(int [])allBullet.elementAt(i);
	   	 tmp[2]++;
	   	 if(tmp[2]>2)
	   	 {tmp[2]=0;}
	   	  tmp[1]-=8;//向上移动8像素
	   	  if(tmp[1]<0)
	   	  { allBullet.removeElementAt(i);
	   	  	}
	   	}
	   	repaint();
	   	}
	   };
	   t.schedule(ts2,0,100);//每隔100毫秒向上移动8像素
		(new Thread(this)).start();//启动线程

	}
    //绘制菜单背静的方法
	public void drawmenubk(Graphics offG) {
		offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
		if (menuBK == null) {
			offG.setColor(0xFFFFFF);
			offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
		} else {
			offG.drawImage(menuBK, 0, 0, 0);
		}
		if (this.gameState == PAUSE) {
			offG.setColor(0);
			offG.fillRect(0, 0, 176, 208);
			offG.setColor(0, 255, 128);

		}
	}
    //绘制声音设定
	public void drawSound(Graphics offG) {
		offG.setClip(0, 0, this.getWidth(), this.getHeight());
		offG.setColor(0);
		offG.fillRect(0, 0, this.getWidth(), this.getHeight());
		offG.setColor(0, 255, 128);
		offG.drawString("声 音 设 置", 20, 20, 0);
		if (this.soundOn) {
			offG.drawString("声音开关 :开 ", 25, 40, 0);
		} else {
			offG.drawString("声音开关 :关 ", 25, 40, 0);
		}
	}
    //绘制菜单标题
	public void drawmenu(Graphics offG) {
        //设定剪裁区  x   y    w    h
		//剪裁区的范围在屏幕重绘的时候受到影响其它区域不受影响
		offG.setClip(0, 178, 130, 30);
		offG.drawImage(menu, 0, 178, 0);
		offG.setClip(0, 182, 94, 20);
		offG.drawImage(menu, 20, 182 - 30 - 20 * nowFramemenu, 0);

	}

	public static String strHelp[] = new String[] {
			"数字键2 : 上",
			"数字键8 : 下",
			"数字键4:  左",
			"左软键6:  右",
			"数字7,9:  ",
			" **客户服务**",
			"电话:010-85895xxx",
			"E-mail:tantoxxx@163.com", };
    //绘制帮助
	public void drawhelp(Graphics offG) {
		offG.setColor(0);
		offG.fillRect(0, 0, this.getWidth(), this.getHeight());
		offG.setColor(0, 255, 128);

		offG.drawString("帮        助", 20, 20, 0);
		for (int i = 0; i < strHelp.length; i++) {
			offG.drawString(strHelp[i], 5, 40 + i * 20, 0);
		}

	}

	public int aboutIndex = 0;

	public static String strAbout[] = new String[] {
			"上海幽幽XXX",
			"客服联系人",
			"姓名 谭xx ",
			"客服电话 01085895xx",
			"传真 01085895xxx ",
			"移动电话 13520557xxx",
			"客服联系人EMAIL ",
			"tantovxx@163.com",
			"各地客服电话 02128239xxx",
			"客服手机 13681717275 ",
			"WEB地址",
			"www.XXX.com.cn" };
    //绘制关于
	public void drawabout(Graphics offG) {
		offG.setColor(0);
		offG.fillRect(0, 0, this.getWidth(), this.getHeight());
		offG.setColor(0, 255, 128);

		offG.drawString("关        于", 20, 20, 0);
		for (int i = aboutIndex; i < strAbout.length; i++) {
			if (i > aboutIndex + 5) {
				break;
			}
			offG.drawString(strAbout[i], 5, 60 + (i % 6) * 20, 0);

		}
	}
    //绘制游戏背静
	public void drawBack() {
		offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
		switch (this.level_Mode) {
		case 1:
			offG.drawImage(mapbk, 0, mappy, 0);
			offG.drawImage(mapbk, 0, mappy - 208, 0);
		}
		mappy += 1;
		if (mappy >= 208) {
			mappy = 0;
		}
	}
    //初始化关卡
	public void initLevel(int n) {
		UFO = null;
		levelStart = System.currentTimeMillis();
		switch (n) {
		case 1:
			try {
				mapbk = Image.createImage("/map02.png");
				this.mapLength = mapbk.getHeight();
				mappy = 0;
			} catch (Exception e) {
			}
			UFO = new byte[30][5];
			break;
		case 2:
            //...
		case 3:
			//...
		case 4:
			//...
		}
	}

		public void paint(Graphics g) {
		offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
		switch (gameState) {
		case PAUSE: //暂停状态
		case MENU:  //菜单状态

			switch (this.menuState) {
			case 0:
				try {
					d_logo = Image.createImage("/logo1.png");
				} catch (Exception e) {
				}
				offG.drawImage(d_logo, 0, 0, 0);
				break;
			case 1:
				try {
					d_logo = null;
					System.gc();
					d_logo = Image.createImage("/logo2.png");
				} catch (Exception e) {
				}
				offG.fillRect(0, 0, this.SCREENHEIGHT, this.SCREENWIDTH);
				offG.drawImage(d_logo, 0, 0, 0);
				break;
			case 2:
				try {
					d_logo = null;
					System.gc();
				} catch (Exception e) {
				}
				offG.drawImage(menuBK, 0, 0, 0);
				break;
			case 3:
				drawmenubk(offG);
				drawmenu(offG);
				break;
			case 4:
				drawhelp(offG);
				break;
			case 5:
				drawSound(offG);
				break;
			case 6:
				drawabout(offG);
			}

			break;
		case LOAD: //游戏加载状态
			offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
			offG.setColor(0);
			offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
			offG.setColor(0, 255, 128);
			offG.drawString("第" + level_Mode + "关 游戏加载 ", 30, 50, 0);
			offG.drawString(proess + "%", 60, 70, 0);
			if (proess == 0 && this.mapbk == null) {
				this.initLevel(level_Mode);
			}
		break;

		case GAMEING://游戏进行中状态
			drawBack(); //画背景
			drawPlane();//画飞机
			drawBullet();//画子弹
			offG.setColor(0xff0000);
			offG.setClip(0, 0, 70, 15);
			break;
		case GAMEOVER://游戏结束状态
			offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
			offG.setColor(0);
			offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
			if (level_Mode > 3) {
				offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
				endCount++;
				if (endCount > 20) {
					ISRUN = false;
					gameStart.midlet.notifyDestroyed();
				}
			}
			offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
			offG.setColor(0, 255, 128);
			offG.drawString("游戏结束", 50, 50, 0);
			offG.drawString("起点娱乐", 30, 80, 0);
			break;
		}
        //将脱屏缓冲区的内容绘制到屏幕
		g.drawImage(im, 0, 0, 0);
	}

	public byte endCount = 0;

	public void drawPlane() {//画飞机
		offG.setClip(px,py,30,30);
		offG.drawImage(imPlane,px-dirFrame[direct]*30,py,0);
		offG.setClip(0,0,176,208);
	}

	public void drawBullet() //画子弹
	{  for(int i=0;i<allBullet.size();i++)
	{ int tmp[]=(int [])allBullet.elementAt(i);
		offG.setClip(tmp[0],tmp[1],10,10);
		offG.drawImage(imBullet,tmp[0]-tmp[2]*10,tmp[1],0);
	}
		offG.setClip(0,0,176,208);
	}
	public void keyPressed(int n) {
		System.out.println("nkey" + n);
		if (gameState == 5) {
			if (n == -6 || n == 6) {
				gameState = 1;
			}
			this.repaint();
			this.serviceRepaints();
			return;
		}
        if(gameState==GAMEING)
        { int k=this.getGameAction(n);
          switch(k)
		  { case Canvas.UP:
		  	   direct=1;
		  	   py-=8;
		       break;
		    case Canvas.DOWN:
		    	direct=2;
		    	py+=8;
		        break;
		  	case Canvas.LEFT:
		  		direct=3;
		  		px-=8;
		  	   break;
		  	case Canvas.RIGHT:
		  		direct=4;
		  		px+=8;
		  	    break;
		  	case Canvas.FIRE:
		  		direct=1;
		  	   this.isFire=true;
		  }

        }
		else if (gameState == MENU || gameState == PAUSE) {
			//处理音乐
			if (menuState == 5 && (n == Canvas.KEY_NUM5 || n == -5)) {
				this.soundOn = !this.soundOn;
				this.repaint();
				return;
			}
			//处理关于
			if (menuState == 6
					&& (n == Canvas.KEY_NUM2 || n == -1 || n == Canvas.KEY_NUM8 || n == -2)) {
				if (aboutIndex == 0)
					aboutIndex = 6;
				else
					aboutIndex = 0;
					this.repaint();
				return;
			}

			if (menuState >= 3) {
				if (n == Canvas.KEY_NUM2 || n == -1 || n == 1
						&& (menuState != 4 && menuState != 6)) {
					nowFramemenu--;
					if (nowFramemenu <= 0) {
						nowFramemenu = 0;
					}

				}
				if (n == Canvas.KEY_NUM8 || n == -2 || n == 2
						&& (menuState != 4 && menuState != 6)) {
					nowFramemenu++;
					if (nowFramemenu >= 4) {
						nowFramemenu = 4;
					}
				}
				if ((n == -7 || n == 7) && (this.menuState != 3)) {
					this.menuState = 3;
					this.repaint();
					return;
				}
				boolean b1 = (gameState == MENU) && (n == 6 || n == -6)
						&& (this.menuState == 3);
				boolean b2 = (gameState == PAUSE) && (n == 6 || n == -6)
						&& (this.menuState == 3);
				if (menuState == 3 && n == Canvas.KEY_NUM5 || (n == -5)
						|| (n == 5) || (b1) || (b2)) {
					if (nowFramemenu == 0) {
						//
						if (gameState == MENU) {
							System.gc();
							gameState = LOAD;
							proess = 0;
						} else if (gameState == PAUSE) {
							gameState = GAMEING;
						}

					}
					if (nowFramemenu == 1) {
						this.menuState = 4;
					}

					if (nowFramemenu == 2) {
						this.menuState = 6;
					}
					if (nowFramemenu == 3) {
						this.menuState = 5;
					}

					if (nowFramemenu == 4) {
						ISRUN = false;
						gameStart.midlet.notifyDestroyed();

					}
				}
				this.repaint();
			}
			return;
		}

		else if (n == -7 || n == 7) {
			gameState = PAUSE;
			this.repaint();
			return;
		} else if (n == -6 || n == 6) {
			gameState = 5;
			this.repaint();
			return;
		}

	}

	public void keyReleased(int n) {
      direct=0;
      if(this.getGameAction(n)==Canvas.FIRE)
      {this.isFire=false;
      	}
	}

	public boolean flag = true;
	public void run() {
		while (ISRUN) {
			if (flag) {
				this.repaint();
				try {
					Thread.sleep(1000);
				} catch (Exception E) {
				}

				if (this.menuState < 3)
					this.menuState++;
				else {
					flag = false;
				}
				continue;
			}
			switch (this.gameState) {
			case LOAD:
				//System.out.println("loading");
				Thread.yield();
				System.gc();
				this.proess += 5;
				if (this.proess >= 100) {
					this.gameState = GAMEING;

				}
				this.repaint(); // 重绘
				this.serviceRepaints();
				break;
			case GAMEOVER:
			case GAMEING:

				break;
			case PAUSE:
				break;
			}

			repaint();
			serviceRepaints();
			long currTime = System.currentTimeMillis();
			long delay = 100 - (currTime - lastTime);
			lastTime = currTime;

			try {
				if (delay <= 0)
					delay = 1;
				Thread.sleep(delay);
			} catch (Exception exception) {
			}
		}

	}
}

⌨️ 快捷键说明

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