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

📄 gameworld.java

📁 实现了 移动 碰撞检测
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.rms.RecordStore;
public class Gameworld extends Canvas implements Runnable
{
    /*****************MOTO L6键值设定*************************/
//	private static final byte K_OK = -21;	//左软键盘
//	private static final byte K_BACK = -22;	//右软键盘
    /*****************游戏键值设定*************************/
	private static final byte K_OK = -6;	//左软键盘
	private static final byte K_BACK = -7;	//右软键盘
    static byte gameState; //游戏状态
    static byte RUN = 1;
    int speed = 50; //主线程速度
    final int positionY = 20;//整个游戏上提20个像素点
    private boolean isNew = true; 	//是否刚刚开始进入游戏
    private boolean startMenu = false;//是否进入开始菜单
    private boolean gameMenu; 		//是否打开了游戏菜单,游戏中的暂停
    private boolean isLoading; 		//是否正在读取数据
    private boolean setGame; 		//游戏设置
    private boolean help; 			//帮助菜单
    private boolean gameStart; 		//主游戏是否开始
    
    /** **************** LOGO 图片 ******************** */
    private Image[] logoImage;	//LOGO 图片
    Image nameImage = null;		//游戏名称
    Image anykeyImage = null;	//任意键
    int nameStart = 208;		//游戏名称图片的开始位置
	int showAnyKey =0;			//任意键图片的显示时间控制
    int left;					//左边
    int right=0;				//右边
    private long logoTime = 0; 	//记录LOGO页面显示的时长
    private byte logoCount = 0; //显示第几张LOGO图片

    boolean sound; 				//音效
    private byte menuItem = 1; 	//开始菜单项
    private byte gameMenuItem = 1; //游戏中的菜单项

    int screenWidth, screenHeight; //屏幕的宽和高
    Enemy[] allEnemy = null;//保存所有敌人精灵的实例
    Player player = null; 	//主角

    /** ********************按***键************************* */
    //确定方向键是否被按下
    private boolean upPress;
    private boolean downPress;
    private boolean leftPress;
    private boolean rightPress;
    private boolean upRightPress;
    private boolean upLeftPress;
    private boolean downRightPress;
    private boolean downLeftPress;

    /** ********************************************** */
    private int way; 		//按键的方位
    private int inertia; 	//玩家瞄准镜移动的延时控制
    int offsetX; 			//背景运动的偏仪移量
    private long changeOffsetTime;//背景移动的速度
    long lastTime; 			//时间周期

    private byte[][] horntest_map; //角色类型数据
    int column,endColumn,tempEnd; //下标

    private boolean isTop; 	//是否已经到达关底,到达关底后将不再移动背景
    private boolean isClean;//是否过关
    byte enemyCount = 0; 	//同一屏幕中敌人出现的总个数

    boolean isKillHP; 		//玩家是否被轻型武器攻击,用来控制显示击中动画
    boolean isKillHPBom; 	//玩家是否被重型武器攻击,用来控制显示击中动画

    private Random random = null;
    long needSleepTime = 0; //每次循环需要休眠的时间

    private int randomEnemyType;//随机出现敌人的类型
    long logicTime;				//主逻辑记时
    int randomEnmeyTime;		//对随机敌人出现时间的控制
    private Image buffImag = Image.createImage(91,72);//动画显示不超屏大小控制
    private Image[] caption = null;//动画图片
    int startCaptionFlag=0;		//动画桢切换控制标签
    int vx1,vx2,vx3,vx4;		//开始动画
    
    private int useTime; 		//过关切换所需要的时间
    private long usePassTime;

    private boolean isStartCaption;//开场序幕
    private int keyTime;		//键盘延时
    private int menuAdd = 60; 	//开始菜单动画的偏移动量

    int start, end, bgWidth; 	//控制背景图片移动
    private int loadScale; 		//读取数据的比例
    int level; 					//当前游戏关数
    private boolean isNewEnemy; //是否生成了一个新敌人
    private int addSpeed; 		//空袭导弹下落增量
	
    private int helpItem;		//帮助翻页
    private boolean keyMenu;	//控制按键延时

    javax.microedition.media.Player[] soundSFX = null;

    // 构造
	public Gameworld()
	{
		jbInit();
	}

	// 初始化刚进入游戏时的数据
	private void jbInit()
	{
		this.setFullScreenMode(true);
		gameMenu = false;
		screenWidth = 176;
		screenHeight = 208;
		left = screenWidth >> 1;
		right = 0;
		random = new Random();
		// 读取LOGO图片
		logoImage = new Image[3];
		// LOGO
		try
		{
			logoImage[0] = Image.createImage("/mt.png");
			logoImage[1] = Image.createImage("/bx.png");
			logoImage[2] = Image.createImage("/gamelogo.png");
			nameImage = Image.createImage("/name.png");
			anykeyImage = Image.createImage("/anykey.png");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		soundSFX = new javax.microedition.media.Player[4];
		try
		{
			soundSFX[0] = Manager.createPlayer(getClass().getResourceAsStream(
					"/SFX/button2.mid"), "audio/midi");
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (MediaException e)
		{
			e.printStackTrace();
		}
		level = 8;
		if (!this.testRMS())
		{
			this.initRMS();
		}
		keyMenu = true;
		sound = loadSetUP();

		icoImage = loadImage("/ico.png");
		mainMenu = loadImage("/menu.png");
		gameLogoImage = loadImage("/gamelogo.png");// 开始位置
		vx1 = 0;
		vx2 = 0;
		vx3 = 0;
		vx4 = 0;// 开机动画控制
		menuSelect = loadImage("/select.png");
		gameState = RUN;

		new Thread(this).start();
	}

	//返回菜单时加载的图片
	public void reset()
	{
	    windowImage = null;
	    casernImage = null;
	    stoneImage = null;
		if (sound)
		{
			soundSFX[1] = null;
		}
	    backgroundImage=null;
	    grenadeImage = null;
	    smallEnemyImage = null;
		if (sound)
		{
			soundSFX[2] = null;
		}
	    bigEnemyImage = null;
	    bombImage = null;
	    hpImage = null;
	    shipImage = null;
		if (sound)
		{
			soundSFX[3] = null;
		}
	    tankImage = null;
	    playerImage = null;
	    towerImage = null;
	    planeImage = null;
	    houseImage2 = null;
	    footmanImage3 = null;
	    coconutImage = null;
	    grassImage = null;
	    potImage = null;
	    busImage = null;
		keyMenu = true;
	    gameoverImage = null;	//任务结果字样
	    uiImage = null;			//画板
	    numberImage = null;		//数字
	    itemImage = null;		//物品
	    bulletVector = null;
	    bombVector = null;
	    
	    bulletImageX=null;
	    bulletImageY=null;
	    bombImageX=null;

	    System.gc();
		gameStart = false;
		startMenu = true;
		
		vx1 = 0;
		vx2 = 0;
		vx3 = 0;
		vx4 = 0;
		icoImage = loadImage("/ico.png");
		mainMenu = loadImage("/menu.png");
		gameLogoImage = loadImage("/gamelogo.png");
	}

	/**
	 * 初始每一关的游戏数据
	 * 
	 * @param totalSprite
	 *            每一关中同时出现敌人的最大数量
	 * @param level
	 *            第几个关卡
	 */
	public void initGame(int totalSprite, int level)
	{
		if (right >= screenWidth || left <= 0)
		{
			left = screenWidth >> 1;
			right = 0;
		}
		bulletImageX=null;
		bulletImageY=null;
		bombImageX=null;
		bulletVector = null;
		bombVector = null;
	    windowImage = null;
	    casernImage = null;
	    stoneImage = null;
		if (sound)
		{
			soundSFX[1] = null;
		}
	    backgroundImage=null;
	    grenadeImage = null;
	    smallEnemyImage = null;
		if (sound)
		{
			soundSFX[2] = null;
		}
	    bigEnemyImage = null;
	    bombImage = null;
	    hpImage = null;
	    shipImage = null;
		if (sound)
		{
			soundSFX[3] = null;
		}
	    tankImage = null;
	    playerImage = null;
	    towerImage = null;
	    planeImage = null;
	    houseImage2 = null;
	    footmanImage3 = null;
	    coconutImage = null;
	    grassImage = null;
	    potImage = null;
	    busImage = null;
	    mainMenu = null;
	    icoImage = null;
	    gameLogoImage = null;
	    gameoverImage = null;	//任务结果字样
	    uiImage = null;			//画板
	    numberImage = null;		//数字
	    itemImage = null;		//物品	    
	    System.gc();
	    loadScale = 0; // 加载比例

	    bulletImageX=new int[]{164,159,154,149,144,139,134,129,124,119, 164,159,154,149,144,139,134,129,124,119, 164,159,154,149,144,139,134,129,124,119};
	    bulletImageY=new int[]{200,200,200,200,200,200,200,200,200,200, 195,195,195,195,195,195,195,195,195,195, 190,190,190,190,190,190,190,190,190,190};
	    bombImageX=new int[]{9,16,23,30,37};	    
	    keyMenu = true;
		isLoading = true;
		gameStart = false;
		isTop = false;
		isClean = false;
		screenWidth = 176;// getWidth();
		screenHeight = 208;// getHeight();
		enemyCount = 0; // 初始化清空敌人
		offsetX = 0; // 屏幕偏移
		logicTime = 0; // 主逻辑记时
		start = 0; // 地图位置
		end = 0; // 地图位置
		loadScale = 1;
		// 载入关卡中需要用到的图片
		if (sound)
		{
			try
			{
				soundSFX[2] = Manager.createPlayer(getClass()
						.getResourceAsStream("/SFX/item.mid"), "audio/midi");
			} catch (IOException e)
			{
				e.printStackTrace();
			} catch (MediaException e)
			{
				e.printStackTrace();
			}
		}
		loadScale = 2;
		serviceRepaints();
		playerImage = loadImage("/player.png"); // 玩家视口
		grenadeImage = loadImage("/grenade.png"); // 手雷图片
		itemImage = loadImage("/item.png"); // 游戏物品

		loadScale = 3;
		if (sound)
		{
			try
			{
				soundSFX[3] = Manager.createPlayer(getClass()
						.getResourceAsStream("/SFX/dead.mid"), "audio/midi");
			} catch (IOException e)
			{
				e.printStackTrace();
			} catch (MediaException e)
			{
				e.printStackTrace();
			}
		}
		loadScale = 4;
		serviceRepaints();
		numberImage = loadImage("/number.png"); // 显示的数字图片
		uiImage = loadImage("/ui.png"); // UI绘制
		hpImage = loadImage("/hp.png"); //血瓶
		serviceRepaints();
		loadScale = 5;
		/** 向量储存子弹图标 */
		bulletVector = new Vector();
		// 添加小子弹图标
		for (int i = 0; i < 30; i++)
		{
			bulletVector.addElement(itemImage);
		}
		loadScale = 6;
		// 添加导弹图标
		bombVector = new Vector();
		for (int i = 0; i < 5; i++)
		{
			bombVector.addElement(itemImage);
		}
		gameoverImage = loadImage("/gameover.png"); // 任务结果字样
		serviceRepaints();
		bombImage = loadImage("/bomb.png"); // 爆炸图片
		loadScale = 7;
		if (sound)
		{
			try
			{
				soundSFX[1] = Manager.createPlayer(getClass()
						.getResourceAsStream("/SFX/explosion.mid"),
						"audio/midi");
			} catch (IOException e)
			{
				e.printStackTrace();
			} catch (MediaException e)
			{
				e.printStackTrace();
			}
		}
		loadScale = 8;
		serviceRepaints();

		casernImage = loadImage("/casern.png");// 城堡
		windowImage = loadImage("/window.png");// 窗户
		stoneImage = loadImage("/stone.png"); // 石头
		loadScale = 9;
		serviceRepaints();
		tankImage = loadImage("/tank.png"); // 坦克图片
		loadScale = 10;
		serviceRepaints();
		// 每一关所用到的图片
		switch (level)
		{
		case 1:
			horntest_map = this.loadMap("1");
			loadScale = 11;
			serviceRepaints();
			smallEnemyImage = loadImage("/soldier_small1.png");// 随机敌人
			bigEnemyImage = loadImage("/soldier_big1.png");// 丢炸弹的敌人
			backgroundImage = loadImage("/bg_1.png"); // 背景
			loadScale = 12;
			serviceRepaints();
			break;
		case 2:
			horntest_map = this.loadMap("2");
			towerImage = loadImage("/tower.png");// 塔
			loadScale = 11;
			serviceRepaints();
			smallEnemyImage = loadImage("/soldier_small1.png");// 随机敌人
			bigEnemyImage = loadImage("/soldier_big1.png");// 丢炸弹的敌人
			backgroundImage = loadImage("/bg_1.png");
			loadScale = 12;
			serviceRepaints();
			break;
		case 3:
			// 初始地图数组
			horntest_map = this.loadMap("3");
			loadScale = 11;
			serviceRepaints();
			this.planeImage = loadImage("/plane.png");
			towerImage = loadImage("/tower.png");// 塔
			smallEnemyImage = loadImage("/soldier_small1.png");// 随机敌人
			bigEnemyImage = loadImage("/soldier_big1.png");// 丢炸弹的敌人
			backgroundImage = loadImage("/bg_1.png");
			loadScale = 12;
			serviceRepaints();
			break;

		case 4:
			// 初始地图数组
			horntest_map = this.loadMap("4");
			grassImage = loadImage("/grass.png");
			coconutImage = loadImage("/coconut.png");
			loadScale = 11;
			serviceRepaints();

			smallEnemyImage = loadImage("/soldier_small2.png");// 随机敌人
			bigEnemyImage = loadImage("/soldier_big2.png");// 丢炸弹的敌人
			backgroundImage = loadImage("/bg_2.png");
			loadScale = 12;
			serviceRepaints();
			break;

		case 5:
			horntest_map = this.loadMap("5");
			this.footmanImage3 = loadImage("/specialsoldier3.png");
			loadScale = 11;
			serviceRepaints();
			grassImage = loadImage("/grass.png");
			coconutImage = loadImage("/coconut.png");
			this.shipImage = loadImage("/ship.png");
			smallEnemyImage = loadImage("/soldier_small2.png");// 随机敌人
			bigEnemyImage = loadImage("/soldier_big2.png");// 丢炸弹的敌人
			backgroundImage = loadImage("/bg_2.png");
			loadScale = 12;
			serviceRepaints();
			break;

		case 6:
			horntest_map = this.loadMap("6");
			this.footmanImage3 = loadImage("/specialsoldier3.png");
			this.shipImage = loadImage("/ship.png");
			loadScale = 11;
			serviceRepaints();
			this.planeImage = loadImage("/plane.png");
			grassImage = loadImage("/grass.png");
			coconutImage = loadImage("/coconut.png");
			smallEnemyImage = loadImage("/soldier_small2.png");// 随机敌人
			bigEnemyImage = loadImage("/soldier_big2.png");// 丢炸弹的敌人
			backgroundImage = loadImage("/bg_2.png");
			loadScale = 12;
			serviceRepaints();
			break;

		case 7:
			horntest_map = this.loadMap("7");

⌨️ 快捷键说明

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