maincanvas.java

来自「用J2ME实现的战棋类小游戏DEMO,寻路用A星算法,因为时间关系物品使用功能请」· Java 代码 · 共 1,739 行 · 第 1/4 页

JAVA
1,739
字号
	private void getGHF(int x, int y, int tx, int ty) {
		openList[y][x][1] = getG(x, y);
		openList[y][x][2] = getH(x, y, tx, ty);
		openList[y][x][3] = openList[y][x][1] + openList[y][x][2];
	}

	private int getG(int x, int y) {
		switch (openList[y][x][4]) {
		default:
			return 1;
		case 1:

			return openList[y - 1][x][1] + 1;

		case 2:
			return openList[y + 1][x][1] + 1;
		case 3:
			return openList[y][x - 1][1] + 1;
		case 4:
			return openList[y][x + 1][1] + 1;
		}
	}

	private int getH(int x, int y, int tx, int ty) {
		return Math.abs(x - tx) + Math.abs(y - ty);
	}

	private void AAsterisk(int bx, int by, int tx, int ty) {
		closeList = null;
		openList = null;
		closeList = new boolean[GameTools.mHeight][GameTools.mWidth];
		openList = new int[GameTools.mHeight][GameTools.mWidth][5];
		closeListLength = 0;
		openListLength = 0;
		for (int i = 0; i < GameTools.mHeight; i++) {
			for (int j = 0; j < GameTools.mWidth; j++) {

				openList[i][j][0] = 0;

				openList[i][j][1] = map_tl2[i][j];

				openList[i][j][2] = Math.abs(i - ty) + Math.abs(j - tx);

				openList[i][j][3] = openList[i][j][1] + openList[i][j][2];

				openList[i][j][4] = 0;
			}
		}

		addInOpenList(bx, by);

		if (!isBalk(bx + 1, by)) {
			addInOpenList(bx + 1, by);
			setFather(bx + 1, by, 3);
			getGHF(bx + 1, by, tx, ty);
		}

		if (!isBalk(bx - 1, by)) {
			addInOpenList(bx - 1, by);
			setFather(bx - 1, by, 4);
			getGHF(bx - 1, by, tx, ty);
		}

		if (!isBalk(bx, by + 1)) {
			addInOpenList(bx, by + 1);
			setFather(bx, by + 1, 1);
			getGHF(bx, by + 1, tx, ty);
		}

		if (!isBalk(bx, by - 1)) {
			addInOpenList(bx, by - 1);
			setFather(bx, by - 1, 2);
			getGHF(bx, by - 1, tx, ty);
		}

		removeFromOpenList(bx, by);
		addInCloseList(bx, by);

		int ttx = bx, tty = by, minf = 2550;
		for (int i = 0; i < GameTools.mHeight; i++) {
			for (int j = 0; j < GameTools.mWidth; j++) {
				if (openList[i][j][0] == 1) {
					if (minf > openList[i][j][3]) {
						minf = openList[i][j][3];
						ttx = j;
						tty = i;
					}
				}
			}
		}

		AAsterisk_t(ttx, tty, tx, ty);
	}

	private void AAsterisk_t(int ttx, int tty, int tx, int ty) {

		if ((ttx == tx && tty == ty) || openListLength == 0)
			return;

		removeFromOpenList(ttx, tty);
		addInCloseList(ttx, tty);

		if (!isBalk(ttx + 1, tty)) {
			if (openList[tty][ttx + 1][0] == 0) {
				addInOpenList(ttx + 1, tty);
				setFather(ttx + 1, tty, 3);
				getGHF(ttx + 1, tty, tx, ty);
			} else if (openList[tty][ttx + 1][0] == 1) {
				if (openList[tty][ttx][1] + map_tl2[tty][ttx + 1] < openList[tty][ttx + 1][1]) {
					setFather(ttx + 1, tty, 3);
					getGHF(ttx + 1, tty, tx, ty);
				}
			}
		}

		if (!isBalk(ttx - 1, tty)) {
			if (openList[tty][ttx - 1][0] == 0) {
				addInOpenList(ttx - 1, tty);
				setFather(ttx - 1, tty, 4);
				getGHF(ttx - 1, tty, tx, ty);
			} else if (openList[tty][ttx - 1][0] == 1) {
				if (openList[tty][ttx][1] + map_tl2[tty][ttx - 1] < openList[tty][ttx - 1][1]) {
					setFather(ttx - 1, tty, 4);
					getGHF(ttx - 1, tty, tx, ty);
				}
			}
		}

		if (!isBalk(ttx, tty + 1)) {
			if (openList[tty + 1][ttx][0] == 0) {
				addInOpenList(ttx, tty + 1);
				setFather(ttx, tty + 1, 1);
				getGHF(ttx, tty + 1, tx, ty);
			} else if (openList[tty + 1][ttx][0] == 1) {
				if (openList[tty][ttx][1] + map_tl2[tty + 1][ttx] < openList[tty + 1][ttx][1]) {
					setFather(ttx, tty + 1, 1);
					getGHF(ttx, tty + 1, tx, ty);
				}
			}
		}

		if (!isBalk(ttx, tty - 1)) {
			if (openList[tty - 1][ttx][0] == 0) {
				addInOpenList(ttx, tty - 1);
				setFather(ttx, tty - 1, 2);
				getGHF(ttx, tty - 1, tx, ty);
			} else if (openList[tty - 1][ttx][0] == 1) {
				if (openList[tty][ttx][1] + map_tl2[tty - 1][ttx] < openList[tty - 1][ttx][1]) {
					setFather(ttx, tty - 1, 2);
					getGHF(ttx, tty - 1, tx, ty);
				}
			}
		}

		int bx = ttx, by = tty, minf = 255;
		for (int i = 0; i < GameTools.mHeight; i++) {
			for (int j = 0; j < GameTools.mWidth; j++) {
				if (openList[i][j][0] == 1) {
					if (minf > openList[i][j][3]) {
						minf = openList[i][j][3];
						bx = j;
						by = i;
					}
				}
			}
		}

		AAsterisk_t(bx, by, tx, ty);
	}

	// xiaBiaoA-1-movSteps
	private void goRoad(int x, int y) {
		if (x == begin_x && y == begin_y) {

			return;
		}

		switch (openList[y][x][4]) {
		case 1:
			movShuZu[xiaBiaoA] = 2;
			xiaBiaoA += 1;

			goRoad(x, y - 1);
			break;
		case 2:
			movShuZu[xiaBiaoA] = 1;
			xiaBiaoA += 1;
			goRoad(x, y + 1);
			break;
		case 3:
			movShuZu[xiaBiaoA] = 4;
			xiaBiaoA += 1;
			goRoad(x - 1, y);
			break;
		case 4:
			movShuZu[xiaBiaoA] = 3;
			xiaBiaoA += 1;
			goRoad(x + 1, y);
			break;
		// default:
		// break;
		}
	}

	public void goPlayer() {
		try{
		for (int g = xiaBiaoA - 1; g > xiaBiaoA - 1 - movSteps; g--) {
			if (player.getX() == fangPlayer.getX()
					&& player.getY() == fangPlayer.getY()) {
				xiaBiaoA = 0;

				return;
			} else {
				switch (movShuZu[g]) {
				case 1:
					player.setCurrentFrames(player.getup());
					for (int i = 0; i < 4 * 5; i++) {
						player.moveSprite(-1);
						repaint();
					}
					break;
				case 2:
					player.setCurrentFrames(player.getdown());
					for (int i = 0; i < 4 * 5; i++) {
						player.moveSprite(-2);
						repaint();
					}
					break;
				case 3:
					player.setCurrentFrames(player.getleft());
					for (int i = 0; i < 4 * 5; i++) {
						player.moveSprite(-3);
						repaint();
					}
					break;
				case 4:
					player.setCurrentFrames(player.getright());
					for (int i = 0; i < 4 * 5; i++) {
						player.moveSprite(-4);
						repaint();
					}
					break;
				default:
					break;
				}
			}
		
		}
		}catch(Exception e){System.out.println("!!!!!!!!!");GameTools.currentAction=4;};
	}

	

	public void goEnemy(Sprite enemyA) {

		// System.out.println(openList[end_x-1][end_y-1][4]);
		// map_tl2[enemyA.getX()/24][enemyA.getY()/24]=0;

		for (int g = xiaBiaoA
				- 1; g > xiaBiaoA - 1 - movStepsA; g--) {
			if ((enemyA.getX() == player.getX() - 24 && enemyA.getY() == player
					.getY())
					|| (enemyA.getX() == player.getX() + 24 && enemyA.getY() == player
							.getY())
					|| (enemyA.getX() == player.getX() && enemyA.getY() == player
							.getY() + 24)
					|| (enemyA.getX() == player.getX() && enemyA.getY() == player
							.getY() - 24)) {
				xiaBiaoA = 0;

				return;
			} else {

				switch (movShuZu[g]) {
				case 1:
					enemyA.setCurrentFrames(enemyA.getup());
					for (int i = 0; i < 4 * 5; i++) {
						enemyA.moveSprite(-1);
						repaint();
					}
					break;
				case 2:
					enemyA.setCurrentFrames(enemyA.getdown());
					for (int i = 0; i < 4 * 5; i++) {
						enemyA.moveSprite(-2);
						repaint();
					}
					break;
				case 3:
					enemyA.setCurrentFrames(enemyA.getleft());
					for (int i = 0; i < 4 * 5; i++) {
						enemyA.moveSprite(-3);
						repaint();
					}
					break;
				case 4:
					enemyA.setCurrentFrames(enemyA.getright());
					for (int i = 0; i < 4 * 5; i++) {
						enemyA.moveSprite(-4);
						repaint();
					}
					break;
				// default:
				// break;
				}
			}
		}
	}
	
	
	public void PlayerToEnemyA(){
		enemyA.shuXing[0]-=(player.shuXing[2]-enemyA.shuXing[3]);
		System.out.println("enemyA.shuXing[0]"+enemyA.shuXing[0]);
		if(enemyA.shuXing[0]<=0){
			inGameingIndex=0;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
			
		//	gameUserFace = WIN;
			}
	}
	public void showZhuangTai(){
		 if((fangPlayer.getX()==enemyA.getX())&&(fangPlayer.getY()==enemyA.getY())){
         	currentjuese=enemyA;
         	for(int i=0;i<4;i++)
         	currentHMAD[i]=""+currentjuese.HMAD[i];
         
         	inGameingIndex=16;	
         }else  if((fangPlayer.getX()==enemyB.getX())&&(fangPlayer.getY()==enemyB.getY())){
         	currentjuese=enemyB;
         	for(int i=0;i<4;i++)
             	currentHMAD[i]=""+currentjuese.HMAD[i];
         	inGameingIndex=16;
         }else  if((fangPlayer.getX()==player.getX())&&(fangPlayer.getY()==player.getY())){
         	currentjuese=player;
         	for(int i=0;i<4;i++)
             	currentHMAD[i]=""+currentjuese.HMAD[i];
         	inGameingIndex=16;
         }else{inGameingIndex=1;}
	}
	
	
	
	public void enemyAttack(Sprite enemyA,int inGameIndex,int currentAction,Sprite enemyAWLAttacker){
		if((player.getX()==enemyA.getX()-24)&&(player.getY()==enemyA.getY())){
			indexPWLA++;
			if(directP!=1){
				enemyAWLAttacker.setCurrentFrames(enemyAWLAttacker.left);
			enemyAWLAttacker.setCurrentFrame(0);
			
			directP++;
			}
			
			enemyAWLAttacker.setPosition(enemyA.getX(),enemyA.getY());
			inGameingIndex = inGameIndex;	
			enemyAWLAttacker.moveSpritePWLA();
			if(indexPWLA==9){
				enemyXAttack(enemyA);
			    enemyA.setCurrentFrames(enemyA.left);
			    enemyA.setCurrentFrame(0);
			
				indexPWLA=0;
				GameTools.currentAction=currentAction;
			}
		}else
			if((player.getX()==enemyA.getX()+24)&&(player.getY()==enemyA.getY())){
				indexPWLA++;
				if(directP!=1){
					enemyAWLAttacker.setCurrentFrames(enemyAWLAttacker.right);
				enemyAWLAttacker.setCurrentFrame(0);
				directP++;
				}
			
				enemyAWLAttacker.setPosition(enemyA.getX(),enemyA.getY());
				inGameingIndex = inGameIndex;	
				enemyAWLAttacker.moveSpritePWLA();
				if(indexPWLA==9){
					enemyXAttack(enemyA);
				    enemyA.setCurrentFrames(enemyA.right);
				    enemyA.setCurrentFrame(0);
				
					indexPWLA=0;
					GameTools.currentAction=currentAction;
				}
			}else
				if((player.getX()==enemyA.getX())&&(player.getY()==enemyA.getY()-24)){
					indexPWLA++;
					if(directP!=1){
						enemyAWLAttacker.setCurrentFrames(enemyAWLAttacker.up);
					enemyAWLAttacker.setCurrentFrame(0);
					directP++;
					}
					
					enemyAWLAttacker.setPosition(enemyA.getX(),enemyA.getY());
					inGameingIndex = inGameIndex;	
					enemyAWLAttacker.moveSpritePWLA();
					if(indexPWLA==9){
						enemyXAttack(enemyA);
					    enemyA.setCurrentFrames(enemyA.up);
					    enemyA.setCurrentFrame(0);
					
						indexPWLA=0;
						GameTools.currentAction=currentAction;
					}
				}else
					if((player.getX()==enemyA.getX())&&(player.getY()==enemyA.getY()+24)){
						indexPWLA++;
						if(directP!=1){
							enemyAWLAttacker.setCurrentFrames(enemyAWLAttacker.down);
						enemyAWLAttacker.setCurrentFrame(0);
						directP++;
						}
						
						enemyAWLAttacker.setPosition(enemyA.getX(),enemyA.getY());
						inGameingIndex = inGameIndex;	
						enemyAWLAttacker.moveSpritePWLA();
						if(indexPWLA==9){
							enemyXAttack(enemyA);
						    enemyA.setCurrentFrames(enemyA.down);
						    enemyA.setCurrentFrame(0);
						
							indexPWLA=0;
							GameTools.currentAction=currentAction;
						}
					}else{indexPWLA=0;GameTools.currentAction=currentAction;}			
	}
	

}

⌨️ 快捷键说明

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