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

📄 gamecanvas.java

📁 j2me手机游戏《都市历险记》的源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
import java.io.DataInputStream;
import java.io.IOException;
//import java.io.InputStream;
import java.util.*;

import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;

//import com.nokia.mid.ui.*;

//import javax.microedition.lcdui.game.Sprite;

public class GameCanvas extends Canvas implements Runnable {
	public gameStart gs;

	private Image d_logo, icon, menuBK; // 创建Logo图片

	private int b_time = 2000;

	private boolean help;

	private boolean about;

	private boolean hiScore;

	// public gameStart gs;
	private int nowFramemenu = 0; // 设置当前帧

	private boolean flag = true;

	private byte menuState = 0;

	public Image im, person; // 脱屏画布对象

	public int proess = 0;

	public Graphics offG; // 脱屏画笔对象

	protected short SCREENWIDTH = 176, SCREENHEIGHT = 220;

	public int level_Mode = 1; // 关数

	public static short[][] mapSize = { // --地图大小
	{ 9, 32 }, { 9, 33 }, { 9, 33 }, { 9, 33 } };

	public byte nowMapCol[] = { 0, 10, 10, 10, 10 };

	public int mapW, mapH;

	public int px, py; // 人物的坐标

	static short[][] imgDataPlayer = { { 0, 0, 42, 53 },
	/* 0 站立0 */
	{ 45, 0, 42, 53 },
	/* 1 站立1 */
	{ 0, 53, 44, 53 },
	/* 2 走1 */
	{ 88, 53, 44, 53 },
	/* 3 走2 */
	{ 44, 53, 44, 53 },
	/* 4 走3 */
	{ 89, 0, 42, 53 },
	/* 5 跳&落下 */
	{ 134, 0, 40, 53 },
	/* 6 蹲 */
	{ 134, 0, 40, 53 },
	/* 7 趴下 */
	{ 0, 0, 1, 1 }, { // 8空
			0, 106, 45, 50 }, { // 9死亡一
			49, 106, 54, 50 }, // 10死亡二
	};

	static byte[][] BottleData = { { 0, 0, 12, 22 },/* 0瓶子1 */
	{ 43, 5, 17, 17 },/* 1瓶子2 */
	{ 60, 11, 22, 11 },/* 2瓶子3 */
	{ 60, 11, 22, 11 },/* 3瓶子3 */
	{ 24, 10, 19, 12 },/* 4纸 */
	{ 12, 7, 12, 15 },/* 5水果 */
	{ 82, 10, 17, 12 },/* 6西瓜 */
	}; // 瓶子

	// 人物动画帧------------------------------
	byte index = 0; // 0 站立 1 走 2跳 3 落

	byte[] playerStop = { 0, 0, 0, 0, 1, 1, 1, 1 }; // --------------主角站立,我加的

	Image pStopim[] = new Image[2];

	byte[] RunMotion = { 2, 3, 4, 3 }; // -------------------------主角的跑步

	Image pRunim[] = new Image[5];

	static byte[] playerDead = { 9, 9, 10, 10, 9, 9, 10, 10, 9, 9, 10, 10, 9,
			9, 10, 10, 9, 9, 10, 10, 9, 9, 10, 10 };

	public boolean mirro = false;

	public int direction; // 左右方向

	public int dir2 = DIR_DOWN; // 上下方向

	public int dir3; // 左右偏移

	public boolean dir2_Flag;

	public static final int DIR_LEFT = 2, DIR_RIGHT = 4, DIR_UP = 16,
			DIR_DOWN = 32; // 左右方向

	public int nowFrame; // 当前侦

	// public int frameFlag; // 侦的标志
	public boolean drawPersonFlag = true; // 画人物动作的标志左或右

	public int upFlag = 18; // 向上跳时的高度

	public Image level_Image, mon_Img, mon_Img1, mon_Img2; // 每一关需要的图片元素图

	public static byte map[][]; // 地图数组

	// public byte map_Move[][];// 行走地图
	//
	// public byte mon_Move[][];// 怪物行走地图

	// public Vector mon; // 怪物

	public int map_CurX, map_CurY; // 地图当前的坐标

	// public int unCross; // 不能通过时的y坐标

	public Image back; // 背景数组

	public int back_Y, back_X; // 背景的坐标

	// public DirectGraphics dg;// nokia画笔

	public int lifeState; // 1为活着,0为死时

	public int deadState; // 死时侦

	public boolean deadFlag = false; // 死的标志

	public int life = 9;

	public static final byte MENU = -2;

	public static final byte NOSET = -1;

	public static final byte LOAD = 0;

	public static final byte GAMEING = 1;

	public static final byte GAMEOVER = 2;

	// public static final byte MIDMENU = 3;
	public static final byte PAUSE = 4;

	public static final byte PAUSE1 = 5;

	public byte gameState = MENU;

	public static boolean ISRUN = true;

	public int monPx, monPy; // 没死之前的状态

	public Random r1;

	public int monFrame;

	public boolean drawMonFlag = false; // 是否画敌人的标志

	public int monMove = 4; // 敌人每次移动的象素

	public int monMax; // 敌人最最多移动多少象素

	public int monLorR; // 敌人向左还是向右走的标志 0向左,1向右

	// public Vector currentMon; // 当前屏屏幕的敌人

	public Image overImg, imgIcon, imgNumber, Bottle;// ,TailGas;

	byte[][] iconData = { { 0, 0, 26, 22 }, { 26, 0, 27, 22 },
			{ 53, 0, 26, 22 } };

	byte[] iconIndex = { 0, 0, 1, 1, 2, 2, 1, 1 };

	static short[][] enemyData = {
	// {X坐标,Y坐标,种类,移动长度}
			{ 300, 170, 4, 100 },
			// {1086 ,216, 2, 80 },
			// {846 ,216, 2, 80 },
			// {658 ,216, 2, 60 },
			// {1269 ,216, 2, 140},
			{ 500, 170, 4, 100 },
	// {400 ,125, 1, 2000},
	// {560 ,125, 1, 2000},
	// {1000 ,125, 1, 2000},

	};

	short[][] enemy;// 敌人当前状态{坐标x,y,方向,是否可见}

	public Image imgEnemy;

	public byte TailGasData[][] = { { 0, 7, 15, 13 }, { 17, 3, 20, 10 },
			{ 17, 13, 14, 13 }, { 39, 0, 27, 27 },

	};

	byte TailGasMove[] = { 0, 1, 2, 3, 2, 1, 0 };

	byte tailFrame = 0;

	public GameCanvas(gameStart g) {
		this.setFullScreenMode(true);
		gs = g;
		im = Image.createImage(SCREENWIDTH, SCREENHEIGHT); // 创建脱屏画布
		offG = im.getGraphics(); // 等到脱屏画笔

		// dg = DirectUtils.getDirectGraphics(offG);// 得到nokia画笔

		try {
			icon = Image.createImage("/icon.png");
			menuBK = Image.createImage("/menuBG.png");
			// TailGas = Image.createImage("/weiqi.png");
			Bottle = Image.createImage("/A1.png");
			person = Image.createImage("/player.png");
			imgIcon = Image.createImage("/icon.png"); // 主角的头像
			imgNumber = Image.createImage("/number.png");
			pStopim[0] = Image.createImage(person, imgDataPlayer[0][0],
					imgDataPlayer[0][1], imgDataPlayer[0][2],
					imgDataPlayer[0][3], 2);
			pStopim[1] = Image.createImage(person, imgDataPlayer[1][0],
					imgDataPlayer[1][1], imgDataPlayer[1][2],
					imgDataPlayer[1][3], 2);
			pRunim[0] = Image.createImage(person, imgDataPlayer[2][0],
					imgDataPlayer[2][1], imgDataPlayer[2][2],
					imgDataPlayer[2][3], 2);
			pRunim[1] = Image.createImage(person, imgDataPlayer[3][0],
					imgDataPlayer[3][1], imgDataPlayer[3][2],
					imgDataPlayer[3][3], 2);
			pRunim[2] = Image.createImage(person, imgDataPlayer[4][0],
					imgDataPlayer[4][1], imgDataPlayer[4][2],
					imgDataPlayer[4][3], 2);
			pRunim[3] = Image.createImage(person, imgDataPlayer[5][0],
					imgDataPlayer[5][1], imgDataPlayer[5][2],
					imgDataPlayer[5][3], 2);
			pRunim[4] = Image.createImage(person, imgDataPlayer[6][0],
					imgDataPlayer[6][1], imgDataPlayer[6][2],
					imgDataPlayer[6][3], 2);

			back = Image.createImage("/back1.png");
			// overImg = Image.createImage("/gameover.png");
			// mon_Img1 = Image.createImage("/mon1.png");
			// mon_Img2 = Image.createImage("/mon2.png");
		} catch (Exception e) {
			System.out.println("nopic");
		}
		r1 = new Random();
		// mon = new Vector();
		(new Thread(this)).start();

	}

	public int faceIndex = 0;

	private void drawInterface() {// --------------------界面

		offG.setClip(3, SCREENHEIGHT - 43, iconData[iconIndex[faceIndex]][2],
				iconData[iconIndex[faceIndex]][3]);
		offG.drawImage(imgIcon, 3 - iconData[iconIndex[faceIndex]][0],
				SCREENHEIGHT - 43 - iconData[iconIndex[faceIndex]][1],
				Graphics.TOP | Graphics.LEFT);

		faceIndex = faceIndex + 1 >= iconIndex.length ? 0
				: (byte) (faceIndex + 1);
		// 画number.png里面的乘号
		offG.setClip(35, SCREENHEIGHT - 33, 11, 10);
		offG.drawImage(imgNumber, 35 - 110, SCREENHEIGHT - 33, Graphics.TOP
				| Graphics.LEFT);
		offG.setClip(50, SCREENHEIGHT - 33, 11, 10); // x坐标 y坐标 画的方式 字符间隙
		offG.drawImage(imgNumber, 50 - life * 11, SCREENHEIGHT - 33,
				Graphics.LEFT | Graphics.TOP);
		// drawNumber(g, imgNumber, player.dartNum, x + 165, y + 195,
		// Graphics.RIGHT,1);
	}

	/***************************************************************************
	 * 对背景的操作
	 **************************************************************************/
	public void drawBack() { // 画背景
		if (this.level_Mode > 1) {
			offG.setColor(0xFFFFFF);
			offG.setClip(0, 0, SCREENWIDTH, 100);
			offG.fillRect(0, 0, SCREENWIDTH, 100);
		} else {
			offG.setClip(back_X, back_Y, SCREENWIDTH, 100);
			offG.drawImage(back, back_X, back_Y, 0);
			offG.setClip(back_X + SCREENWIDTH, back_Y, SCREENWIDTH, 100);
			offG.drawImage(back, back_X + SCREENWIDTH, back_Y, 0);
		}
	}

	public void backMove() { // 背景移动
		if (direction == 1) { // 向左走时减5
			if (back_X < 0) {
				back_X += 4;
			} else {
				back_X = -SCREENWIDTH * 2;
			}
			// System.out.println("back_X= " + back_X);
		}
		if (direction == 2) { // 向右走时加5
			if (back_X > -SCREENWIDTH * 2) {
				back_X -= 4;
			} else {
				back_X = 0;
			}
			// System.out.println("back_X= " + back_X);
		}
	}

	private int bottle[] = { 0, -20, 3 };// bottle x ,bottle y,nowFrame

	private boolean flashPlayer = false;

	private boolean flashEnemy = false;

	private byte flashPlayerframe = 0, flashEnemyFrame = 0;

	public void makeBottle() {
		try {
			if (bottle[2] == 3) {
				bottle[0] = Math.abs(r1.nextInt() % 120 + 20);
				bottle[1] = -20;
				if (bottle[0] % 20 == 0) {
					bottle[2] = 0;
				} else {
					bottle[2] = 3;
				}
			} else {
				if (bottle[2] == 0)
					bottle[1] += 10;
				else {
					bottle[2]++;
				}

				if ((bottle[0] > px) && (bottle[0] < (px + 40))
						&& ((bottle[1] - py) < 10 && ((bottle[1] - py) >= 0))
						&& (bottle[2] == 0)) {
					if (!flashPlayer) {
						life--;
						bottle[2]++;
						flashPlayer = true;
					}
				}
				int row = (bottle[1] + BottleData[bottle[2]][3] + map_CurY) / 24;
				int col = (bottle[0] + map_CurX) / 24;
				// System.out.println("row="+row+"col="+col);
				// System.out.println("map= " + map[row][col]);
				// boolean b1= (map[row][col] == 0 || map[row][col] == 19 ||
				// map[row][col] == 20 ||
				// map[row][col] == 21);
				// boolean b2=(row==9);
				// if((this.level_Mode==1&&b1)||(level_Mode>1&&b2))
				if (row == 7) {

					bottle[2]++;
				}

			}
			offG.setClip(bottle[0], bottle[1], BottleData[bottle[2]][2],
					BottleData[bottle[2]][3]);
			offG.drawImage(Bottle, bottle[0] - BottleData[bottle[2]][0],
					bottle[1] - BottleData[bottle[2]][1], 0);
		} catch (Exception e) {
			e.printStackTrace();
			bottle[0] = Math.abs(r1.nextInt() % 120 + 20);
			bottle[1] = -20;
			bottle[2] = 0;
		}
	}

	public void drawThrow() {
		/*
		 * //////////////draw h\throw if(Throw) { ThrowP[0]-=3; ThrowP[1]+=5;
		 * 
		 * //draw
		 * offG.setClip(ThrowP[0],ThrowP[1],BottleData[ThrowP[2]][2],BottleData[ThrowP[2]][3]);
		 * offG.drawImage(Bottle,ThrowP[0]-BottleData[ThrowP[2]][0],ThrowP[1]-BottleData[ThrowP[2]][1],0);
		 * 
		 * //check int row = (ThrowP[1] + BottleData[ThrowP[2]][3] + map_CurY) /
		 * 24; int col = (ThrowP[0] + map_CurX) / 24;
		 * 
		 * boolean b1= (map[row][col] == 0 || map[row][col] == 19 ||
		 * map[row][col] == 20 || map[row][col] == 21); boolean b2=(row==9);
		 * if((this.level_Mode==1&&b1)||(level_Mode>1&&b2)) { Throw=false;
		 * ThrowP[0]+=map_CurX; ThrowP[1]+=map_CurY; // mon.addElement(ThrowP); } }
		 * //System.out.println(" mon size"+mon.size()); //if(mon.size() >0) {
		 * for(int ii=0;ii<mon.size();ii++) {int tm[]=(int
		 * [])mon.elementAt(ii);
		 * 
		 * int tmp1=tm[0]-map_CurX; int tmp2=tm[1]-map_CurY;
		 * //System.out.println(" tmp1"+tmp1); if((tmp1>0)&&(tmp1<SCREENWIDTH)) {
		 * offG.setClip(tmp1,tmp2,BottleData[tm[2]][2],BottleData[tm[2]][3]);
		 * offG.drawImage(Bottle,tmp1-BottleData[tm[2]][0],tmp2-BottleData[tm[2]][1],0);
		 *  } } }
		 * 
		 */
	}

	//
	public byte bossCount = 0;

	public void drawEnemy() {
		for (int i = 0; i < this.enemy.length; i++)
		// int i=0;
		{
			System.gc();
			if ((enemy[i][3] == 1) && (enemy[i][0] + 130 - map_CurX > 0)
					&& (enemy[i][0] - map_CurX < SCREENWIDTH)) {
				if (enemy[i][2] == 1) {
					enemy[i][0] -= 4;
				} else {
					enemy[i][0] += 4;
				}
				if (Math.abs(enemy[i][0] - enemyData[i][0]) >= enemyData[i][3]) {
					if (enemy[i][2] == 1) {
						enemy[i][2] = 0;
					} else {
						enemy[i][2] = 1;
					}

				}

				// drawEnemy(enemyData[i][2]);
				short type = enemyData[i][2];
				// switch(type)
				// {
				//
				// case 1:
				//
				// if (imgData[type] == null) {
				// imgData[type] = new short[][] {
				// {
				// 0, 0, 131, 53}
				// , {
				// 133,11, 132, 42}
				// , {
				// 268, 23, 60,30}
				// };
				// }
				// if(enemyMotion[type]==null){
				// enemyMotion[type] = new byte[] {
				// 0, 1, 2};
				// }
				// break;
				// case 3:
				//
				// if (imgData[type] == null) {
				// imgData[type]=new short[][]{
				// {0 , 0 ,30,47}, /*0站立0*/
				// {0 , 0 , 1, 1}, /*1空*/
				// {30, 0 ,30,47}, /*2行走1*/
				// {60, 0 ,30,47}, /*3行走2*/
				// {90, 0 ,30,47},};/*4行走3*/
				// }
				//
				// if(enemyMotion[type]==null){
				// enemyMotion[type] = new byte[] {
				// 2, 3, 4, 3};
				// }
				// break;
				// case 2:
				// if(enemyMotion[type]==null){
				// enemyMotion[type] =new byte[]{0,0,2,2,1,1};
				// }
				// if(imgData[type] == null){
				// imgData[type] = new short[][] {
				// {0 ,11,40,29}, /*0*/
				// {40 ,3 ,37,37}, /*1*/
				// {77 ,9 ,40,31}, /*2*/
				// {117,0 ,36,40},};
				// }
				//
				// if(enemyMotion[type]==null){
				// enemyMotion[type] =new byte[]{0,0,2,2,1,1};
				// }
				// break;
				// case 4:
				// if (enemyMotion[type] == null) {
				// enemyMotion[type] = new byte[] {
				// 3 ,2, 4, 2};
				// }
				// if (imgData[type] == null) {
				// imgData[type] = new short[][] {
				// {0 ,0,40,70},/*0站立*/
				// {0 ,0,1 ,1 },/*1空*/
				// {40 ,0,40,70},/*2走1*/
				// {80 ,0,40,70},/*3走2*/
				// {120,0,40,70},/*4走3*/
				// {160,1,40,69},/*5受伤*/
				// };
				// }break;
				//
				// }
				// System.out.println(" type"+type);
				if (type != 1) {
					enemy[i][4]++;
					if (enemy[i][4] > enemyMotion.length - 1) {
						enemy[i][4] = 0;
					}
					// System.out.println(" f"+enemy[i][4]);
				}
				int tmpw = imgData[enemyMotion[enemy[i][4]]][2], tmph = imgData[enemyMotion[enemy[i][4]]][3];
				int tmpx = enemy[i][0] - map_CurX, tmpy = enemy[i][1]
						- map_CurY - tmph;

				if (flashEnemy && (enemy[i][5] >= 1)) {
					enemy[i][5]++;
					if (enemy[i][5] > 8) {
						if (i != 0) {
							enemy[i][3] = 0;
						} else {
							bossCount++;
							if (bossCount > 3) {
								enemy[i][3] = 0;
							}
						}

						flashEnemy = false;
					}
					if (enemy[i][5] % 2 == 0) {
						return;
					}
				}
				// System.out.println(" type"+type+"
				// frame"+enemyMotion[type][enemy[i][4]]);
				offG.setClip(tmpx, tmpy, tmpw, tmph);
				int tmpleft = imgData[enemyMotion[enemy[i][4]]][0];
				int tmptop = imgData[enemyMotion[enemy[i][4]]][1];
				// if(type==2)
				// {

⌨️ 快捷键说明

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