world.java~1~

来自「基于J2ME的手机游戏软件。可以控制游戏人物在地图上上下左右行走;可以在地图上放」· JAVA~1~ 代码 · 共 1,085 行 · 第 1/2 页

JAVA~1~
1,085
字号
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.util.Random;
public class World
{		
	public static final int TILE0 = 0;//none
	public static final int TILE1 = 1;//metal
	public static final int TILE2 = 2;//brick
	public static final int ACTOR = 3;//actor
	public static final int ENEMY1 = 4;//enemy1
	public static final int ENEMY2 = 5;//enemy2
	public static final int ENEMY3 = 6;
	public static final int ENEMY4 = 7;
	public static final int BOSS = 8;//boss
	public static final int GATE = 9;
	public static final int KEY = 10;
	
	public static final int VIEW_WIDTH = 200;
	public static final int VIEW_HEIGHT = 200;
	
	public static final int PLAYER_LIFE = 3;
	
	public static final int DRAW_STATE_GAME = 1;
	public static final int DRAW_STATE_MESSAGE = 2;
	public static final int DRAW_STATE_OVER = 3;
	
	public static final int ACTOR_PER_CYC_TIME = 200;
	public static final int ENEMY_PER_CYC_TIME = 300;
	public static final int BLAST_PER_CYC_TIME = 500;
	public static final int BOMB_PER_CYC_TIME = 1000;
	public static final int DESTROY_PER_CYC_TIME = 2000;
	
	public static int ACTOR_RUN_UP;
	public static int ACTOR_RUN_DOWN;
	public static int ACTOR_RUN_LEFT;
	public static int ACTOR_RUN_RIGHT;
	public static int ACTOR_STATIC;
	public static int ACTOR_DEAD;
	
	public static int ENEMY1_RUN_UP;
	public static int ENEMY1_RUN_DOWN;
	public static int ENEMY1_RUN_LEFT;
	public static int ENEMY1_RUN_RIGHT;
	public static int ENEMY1_STATIC;
	
	public static int ENEMY_DEAD;
	
	public static int TILE_1;//metal
	public static int TILE_2;//brick
	public static int TILE_3;//none
	
	public static int PROP_GATE;
	public static int PROP_STAR;
	public static int PROP_LIGHTING;
	public static int PROP_LIFE;
	public static int PROP_MONEY;
	
	public static int BOMB_STATIC;
	
	public static int BRICK_DESTROY;
	
	public static int BLAST_MID;
	public static int BLAST_LEFT;
	public static int BLAST_UP;
	public static int BLAST_DOWN;
	public static int BLAST_RIGHT;
	public static int BLAST_H;
	public static int BLAST_V;
	
	public static int NUMBER;
	public static int NUMBER_RED;
	public static int GAMEOVER;
	public static int STAGE;
	public static int LIFE;
	public static int HEART;
	public static int MONEY;
	public static int CLOCK;
	
	public static final int PROP_TYPE_STAR = 1;
	public static final int PROP_TYPE_LIGHTING = 2;
	public static final int PROP_TYPE_LIFE = 3;
	public static final int PROP_TYPE_MONEY = 4;
	
	public static final int MAX_BOMB_NUM = 10;
	public static final int MAX_PROP_NUM = 10;
	public static final int MAX_SCORE = 99999;
	public static final int MAX_LIFE = 9;
	
	private GameScreen gameScreen;
	private int worldWidth;
	private int worldHeight;
	private int viewX;
	private int viewY;
	private int viewWidth;
	private int viewHeight;
	private int tileWidth;
	private int tileHeight;
	private Actor actor;
	private Enemy[] enemyArray;
	private GameMap gameMap;
	private ImageSet imageSet;
	private boolean isPause;
	private Bomb[] bombArray;
	private Sprite destroyBrick;
	private int[] destroyBrickX;
	private int[] destroyBrickY;
	private int destroyBrickNum;
	private boolean hasDestroy;
	private int destroy_remainCyc;
	private int enemyNum;
	private int drawState;
	private Image stageChangeImage;
	private int stopCyc;
	private int gateX;
	private int gateY;
	private int bombNum;
	private Prop[] propArray;
	private int baseNum;
	private int line;
	private int clock;
	
	
	private int playerLife;
	
	private int score;
	
	private Blast[] blastArray;
	
	private int[][] map;
	
	public World(GameScreen gs)
	{
		gameScreen = gs;
		imageSet = new ImageSet(10);
		initImageSet(imageSet);
		
		gameMap = new GameMap();
		startNewGame();
		
		viewWidth = VIEW_WIDTH;
		viewHeight = VIEW_HEIGHT;
		
		destroyBrick = new Sprite(imageSet,BRICK_DESTROY,0);
		stageChangeImage = Image.createImage(VIEW_WIDTH,VIEW_HEIGHT);
	}
	
	private void initStage(int stage)
	{
		stopCyc = 0;
		drawState = DRAW_STATE_MESSAGE;
		map = gameMap.getMap(stage);
		gateX = gameMap.getGateX(stage);
		gateY = gameMap.getGateY(stage);
		tileWidth = gameMap.getTileWidth(stage);
		tileHeight = gameMap.getTileHeight(stage);
		clock = gameMap.getTime(stage);
		worldWidth = map[0].length * tileWidth;
		worldHeight = map.length * tileHeight;
		viewX = 0;
		viewY = 0;
		enemyNum = GameMap.getEnemyNum(map);
		initDestroyBrickArray();
		initEnemyArray();
		initPropArray();
		initActor();
		initBombArray();
		initBlastArray();
		isPause = false;
		bombNum = 0;
		baseNum = 100;
		line = 0;
	}
	
	private void initPropArray()
	{
		propArray = new Prop[MAX_PROP_NUM];
	}
	
	private void initBombArray()
	{
		bombArray = new Bomb[MAX_BOMB_NUM];
	}
	
	private void initBlastArray()
	{
		blastArray = new Blast[MAX_BOMB_NUM];
	}
	
	private void initActor()
	{	
		int x = gameMap.getActorPosX(map,tileWidth);
		int y = gameMap.getActorPosY(map,tileHeight);
		if(actor == null)
		{
			actor = new Actor(this,x,y);
			actor.start();
		}
		else
		{
			actor.init(x,y);
			actor.start();
		}
	}
	
	public void startNewGame()
	{
		startNewGame(1);
	}
	
	public void startNewGame(int stage)
	{
		initStage(stage);
		score = 0;
		playerLife = PLAYER_LIFE;	
	}
	
	private void initEnemyArray()
	{
		enemyArray = new Enemy[enemyNum];
		int[][] bufArray = GameMap.getEnemyPos(map,tileWidth,tileHeight);
		for(int i = 0;i < bufArray.length;i++)
		{
			enemyArray[i] = new Enemy(this,bufArray[i][0],bufArray[i][1],bufArray[i][2]);
		}
	}
	
	private void initImageSet(ImageSet bufImageSet)
	{
		Image image = null;
		Image[] array = null;
		//初始化玩家图像集
		image = ImageSet.loadClippedImage("/game_actor1.png",0,0,72,128);
		array = ImageSet.extractFrames(image,0,0,3,1,24,32);
		ACTOR_RUN_UP = bufImageSet.addState(array,ACTOR_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,32,3,1,24,32);
		ACTOR_RUN_RIGHT = bufImageSet.addState(array,ACTOR_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,64,3,1,24,32);
		ACTOR_RUN_DOWN = bufImageSet.addState(array,ACTOR_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,96,3,1,24,32);
		ACTOR_RUN_LEFT = bufImageSet.addState(array,ACTOR_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,24,64,1,1,24,32);
		ACTOR_STATIC = bufImageSet.addState(array,0);
		image = ImageSet.loadClippedImage("/game_actor1.png",216,0,24,32);
		array = ImageSet.extractFrames(image,0,0,1,1,24,32);
		ACTOR_DEAD = imageSet.addState(array,0);
		//初始化敌人图像集
		image = ImageSet.loadClippedImage("/game_actor2.png",0,0,72,128);
		array = ImageSet.extractFrames(image,0,0,3,1,24,32);
		ENEMY1_RUN_UP = bufImageSet.addState(array,ENEMY_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,32,3,1,24,32);
		ENEMY1_RUN_RIGHT = bufImageSet.addState(array,ENEMY_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,64,3,1,24,32);
		ENEMY1_RUN_DOWN = bufImageSet.addState(array,ENEMY_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,96,3,1,24,32);
		ENEMY1_RUN_LEFT = bufImageSet.addState(array,ENEMY_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,24,64,1,1,24,32);
		ENEMY1_STATIC = bufImageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/game_tile_metal.png",0,0,32,32);
		array = ImageSet.extractFrames(image,0,0,1,1,32,32);
		TILE_1 = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/game_tile_brick.png",0,0,32,32);
		array = ImageSet.extractFrames(image,0,0,1,1,32,32);
		TILE_2 = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/game_bomb.png",0,0,160,32);
		array = ImageSet.extractFrames(image,0,0,5,1,32,32);
		BOMB_STATIC = imageSet.addState(array,BOMB_PER_CYC_TIME);
		//
		image = ImageSet.loadClippedImage("/game_blast.png",0,0,160,224);
		array = ImageSet.extractFrames(image,0,0,5,1,32,32);
		BLAST_MID = imageSet.addState(array,BLAST_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,32,5,1,32,32);
		BLAST_V = imageSet.addState(array,BLAST_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,64,5,1,32,32);
		BLAST_H = imageSet.addState(array,BLAST_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,96,5,1,32,32);
		BLAST_LEFT = imageSet.addState(array,BLAST_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,128,5,1,32,32);
		BLAST_UP = imageSet.addState(array,BLAST_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,160,5,1,32,32);
		BLAST_RIGHT = imageSet.addState(array,BLAST_PER_CYC_TIME);
		array = ImageSet.extractFrames(image,0,192,5,1,32,32);
		BLAST_DOWN = imageSet.addState(array,BLAST_PER_CYC_TIME);
		//
		image = ImageSet.loadClippedImage("/game_dead.png",0,0,24,32);
		array = ImageSet.extractFrames(image,0,0,1,1,24,32);
		ENEMY_DEAD = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/game_brick_destroy.png",0,0,160,32);
		array = ImageSet.extractFrames(image,0,0,5,1,32,32);
		BRICK_DESTROY = imageSet.addState(array,DESTROY_PER_CYC_TIME);
		//
		image = ImageSet.loadClippedImage("/stage.png",0,0,56,13);
		array = ImageSet.extractFrames(image,0,0,1,1,56,13);
		STAGE = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/life.png",0,0,50,29);
		array = ImageSet.extractFrames(image,0,0,1,1,50,29);
		LIFE = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/number.png",0,0,90,12);
		array = ImageSet.extractFrames(image,0,0,10,1,9,12);
		NUMBER = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/number_red.png",0,0,90,12);
		array = ImageSet.extractFrames(image,0,0,10,1,9,12);
		NUMBER_RED = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/gameover.png",0,0,135,18);
		array = ImageSet.extractFrames(image,0,0,1,1,135,18);
		GAMEOVER = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/gate.png",0,0,32,32);
		array = ImageSet.extractFrames(image,0,0,1,1,32,32);
		PROP_GATE = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/heart.png",0,0,12,12);
		array = ImageSet.extractFrames(image,0,0,1,1,12,12);
		HEART = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/prop_add_life.png",0,0,32,32);
		array = ImageSet.extractFrames(image,0,0,1,1,32,32);
		PROP_LIFE = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/prop_add_score.png",0,0,32,32);
		array = ImageSet.extractFrames(image,0,0,1,1,32,32);
		PROP_MONEY = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/prop_add_power.png",0,0,32,32);
		array = ImageSet.extractFrames(image,0,0,1,1,32,32);
		PROP_LIGHTING = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/prop_add_bomb.png",0,0,32,32);
		array = ImageSet.extractFrames(image,0,0,1,1,32,32);
		PROP_STAR = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/money.png",0,0,12,12);
		array = ImageSet.extractFrames(image,0,0,1,1,12,12);
		MONEY = imageSet.addState(array,0);
		//
		image = ImageSet.loadClippedImage("/clock.png",0,0,12,12);
		array = ImageSet.extractFrames(image,0,0,1,1,12,12);
		CLOCK = imageSet.addState(array,0);
		//
	}
	
	public void addDestroyBrick(int x,int y)
	{
		hasDestroy = true;
		destroyBrickNum++;
		if(destroyBrickNum <= 4)
		{
			destroyBrickX[destroyBrickNum - 1] = x;
			destroyBrickY[destroyBrickNum - 1] = y;
		}
	}
	
	public void initDestroyBrickArray()
	{
		hasDestroy = false;
		destroyBrickX = new int[4];
		destroyBrickY = new int[4];
		for(int i = 0;i < 4;i++)
		{
			destroyBrickX[i] = -1;
			destroyBrickY[i] = -1;
		}
		destroyBrickNum = 0;
		destroy_remainCyc = 40;
	}
	
	private void checkDestroyBrick()
	{
		if(destroy_remainCyc < 0)
		{
			initDestroyBrickArray();
			destroyBrick.reset();	
		}
		if(hasDestroy)
		{
			destroyBrick.cycle();
		}
		destroy_remainCyc--;
	}
	
	public void destroy(int tileX,int tileY)
	{
		int x = tileY * tileWidth;
		int y = tileX * tileHeight;
		map[tileX][tileY] = TILE0;
		addDestroyBrick(x,y);
		addProp(x,y);
	}
	
	private void addProp(int x,int y)
	{
		if(propArray != null)
		{
			int type = 0;
			Random r = new Random();
			int num = (r.nextInt() % baseNum);
			if(num < 0)
			{
				num = -num;
			}
		//	System.out.println(num+" "+baseNum);
			if(num == 1)
			{
				type = PROP_TYPE_LIFE;
				baseNum = 100;
			}
			else if(num == 2 || num == 3)
			{
				type = PROP_TYPE_LIGHTING;
				baseNum = 100;
			}
			else if(num >= 4 && num <= 6)
			{
				type = PROP_TYPE_STAR;
				baseNum = 100;
			}
			else if(num >= 7 && num <= 11)
			{
				type = PROP_TYPE_MONEY;
				baseNum = 100;
			}
			else
			{
				baseNum = baseNum - 10;
				if(baseNum <= 0)
				{
					baseNum = 10;
				}
			}
			for(int i = 0;i < propArray.length;i++)
			{
				if(propArray[i] == null)
				{
					propArray[i] = new Prop(this,x,y,type);
					break;
				}
			}
		}
	}
	
	public ImageSet getImageSet()
	{
		return imageSet;
	}
	
	public int getViewX()
	{
		return viewX;
	}
	
	public int getViewY()
	{
		return viewY;
	}
	
	public int getViewWidth()
	{
		return viewWidth;
	}
	
	public int getViewHeight()
	{
		return viewHeight;
	}
	
	public int[][] getMap()
	{
		return map;
	}
	
	public int getTileWidth()
	{
		return tileWidth;
	}
	
	public int getTileHeight()
	{
		return tileHeight;
	}
	
	public int getTileX(int x,int y)
	{
		return y / tileHeight;
	}
	
	public int getTileY(int x,int y)
	{
		return x / tileWidth;
	}
	
	public void cycle()
	{
		if(drawState == DRAW_STATE_GAME)
		{	
			checkViewPoint();
			checkBombAndBlastArray();
			checkEnemyArray();
			checkDestroyBrick();
			checkPropArray();
			if(actor != null)
			{
				actor.cycle();
				if(!actor.isLive() && actor.isDisuse())
				{
					stageOver();
				}
			}
			if(clock <= 0)
			{
				actor.dead();
				clock = 0;
			}
			else
			{
				clock--;
			}
		}
		else if(drawState == DRAW_STATE_MESSAGE)
		{
			stopCyc++;
			if(stopCyc > 60)
			{
				drawState = DRAW_STATE_GAME;
			}
		}
		else if(drawState == DRAW_STATE_OVER)
		{
			stopCyc++;
			if(stopCyc > 80)
			{
				gameScreen.close();//释放主线程
				actor = null;
			}
		}
	}
	

⌨️ 快捷键说明

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