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

📄 gamemanager.java

📁 一个关于沉船的J2ME小游戏
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//
// Copyright 2002 Nokia Corporation
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Mobile
// Phones Ltd. retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Third-party brands and names are the property of their respective
// owners. Java(TM) and J2ME(TM) are registered trademarks of
// Sun Microsystems Inc.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
//


import java.util.Vector;
import javax.microedition.lcdui.*;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;


// GameManager is used by CloseableCanvas or NokiaCloseableCanvas.
// (BlockGameMIDlet creates a CloseableCanvas or NokiaCloseableCanvas
// depending on the capabilities of the MIDP device where the MIDlet
// was downloaded.)
//
// This GameManager tries to be as portable as possible. It doesn't
// use any drawing feature of the Nokia UI API's FullCanvas that an
// ordinary MIDP Canvas API doesn't support, other than the basic
// 'full canvas' property of FullCanvas.

class GameManager
    implements Runnable
{
	private int	 			MILLIS_PER_TICK = 50; // msec


	private final BlitzMIDlet	midlet;
	private final Dictionary	dict;
	private final GameEffects	gameEffects;
	private final Canvas		canvas;
	private final Plane		plane;
	private final Bomb		bomb;
	private final Vector 		city = new Vector();
	private Image			gfx;
	private final Image		loaded;

	private int				status;
	private int				level = 1;
	private int				ticks = 0;
	private int				maxBuilding;
	private boolean			redrawBuilding;
	private boolean			redrawBackground;
	private boolean			redrawScore;
	private int				buildingLeft;
	private int				score;
	private int				bonus;
	public int				hiScore;
	public int				hiLevel;

	/* Phone dependant vars */
	private int				buildsize;				// size of building block
	private int				buildsizeY;				// size of building block
	private int				build1;				// X start of building in gfx file
	private int				build2;				// X start of building rubble in gfx file
	private int				build3;				// X start of building roof in gfx file
	private int				buildstart;				// X start of building on ground
	private int				ground;				// Y start of building
	private int				buildInit;			// How many building does the game start with
	private int				buildMax;			// What is the maximum buildings we are going to have
	private int				buildMaxCol;		// Max number of blocks per one building
	private int				planeCrash;
	private int				scoreStyle;

	private int	 			gameWidth		= 128;
    	private int 			gameHeight		= 128;
	private int				landRow;
	private int				gfxStart;
	private int				iColOffsetA;
	private int				iColOffsetB;
	private int				iColOffsetC;
	private int				scoreX;
	private int				scoreY;
	private int				scoreHeight;
	private boolean			bJustLoaded		= false;
	private boolean			isGameOver		= false;
	private volatile boolean 	isPaused		= false;
	private volatile Thread		animationThread	= null;
	private final static Font GAME_FONT =
		Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	private final static int fontHeight = GAME_FONT.getHeight();

	public int gameActionFire = 0;

	public final int			GM_NEW_GAME  = 1;
	public final int			GM_PLAY      = 2;
	public final int			GM_CRASH	 = 3;
	public final int			GM_LANDED	 = 4;
	public final int			GM_NEW_LEVEL = 5;
	public final int			GM_HISCORE   = 6;
	public final int			GM_LEVEL_BONUS = 7;

	public String DEBUG;
	public long	  iDEBUG = 0;
	public long	  iDEBUG2 = 0;
	public long   iDEBUG3 = 0;
	public long   iDEBUG4 = 0;

	// hasBeenShown could be set by Canvas.showNotify()
    	private boolean hasBeenShown = false;

    	GameManager(BlitzMIDlet midlet, Dictionary dict,
        		GameEffects gameEffects, Canvas canvas)
	{
		Building building;

		gfx = null;

        	this.midlet = midlet;
        	this.dict = dict;
        	this.canvas = canvas;
        	this.gameEffects = gameEffects;

		this.loaded = this.gameEffects.getGfx("/bomber.png");

		this.gameWidth  = this.canvas.getWidth();
		this.gameHeight = this.canvas.getHeight();
		this.bomb  = new Bomb();
		this.bomb.init();

		switch (this.gameHeight)
		{
			case 65:
			{
				Graphics gGfx;
				int ix, iy, iz, ia;

				this.MILLIS_PER_TICK = 100;
				this.maxBuilding = 17;
				this.buildInit = 40;
				this.buildMax = 160;
				this.buildMaxCol = 11;
				this.scoreStyle = 2;
				this.gfxStart = 0;
				this.buildsize = 5;
				this.buildsizeY = 4;
				this.build1 = 19;
				this.build2 = 39;
				this.build3 = 59;
				this.buildstart = 7;
				this.ground = 62;

				this.gfx = Image.createImage(this.gameWidth, this.gameHeight);
				gGfx = this.gfx.getGraphics();
		            gGfx.setColor(0,0,0);
				gGfx.fillRect(0,0,this.gameWidth,this.gameHeight);
		            gGfx.setColor(255,0,0);
				gGfx.fillRect(0,0,this.gameWidth,4);
				for (ix = 0; ix < 10; ix++)
				{
			            gGfx.setColor(250 - (20 * ix),0,0);
					gGfx.fillRect(0,ix + 4,this.gameWidth,1);
				}
				iz = 42;
				for (iy = 1; iy < 4; iy++)
				{
					for (ix = 1; ix < (iy * 4); ix++)
					{
				            gGfx.setColor(0,0,(iy * 16) + ix + 32);
						gGfx.fillRect(0,iz,this.gameWidth,1);
						iz++;
					}
				}
		            gGfx.setColor(96,96,96);
				for (ix = 0; ix < (this.gameWidth >> 5); ix++)
				{
					for (iy = 0; iy < 3; iy++)
					{
						iz = Util.getRandomInt(4, 28);
						ia = Util.getRandomInt(4, 28);
						gGfx.drawLine((ix * 32) + iz, (iy * 32) + ia, (ix * 32) + iz, (iy * 32) + ia);
					}
				}
				gGfx.setClip(1, this.gameHeight - 4, this.gameWidth, 4);
				gGfx.drawImage(this.loaded, 1,30, Graphics.TOP | Graphics.LEFT);
				this.landRow = 55;
				this.plane = new Plane(14, 8, 2, 4, 1, 90, 1, this.landRow);
				this.bomb.maxDrop = 62;
				this.bomb.setSize(5, 8, 2, -3, 0);
				iColOffsetA = 15;
				iColOffsetB = 3;
				iColOffsetC = 4;
				scoreX = 1;
				scoreY = 1;
				scoreHeight = 5;
				MILLIS_PER_TICK = 100;
				planeCrash = 80;
				break;
			}
			case 128:
			{
				Graphics gGfx;
				int ix, iy, iz, ia;

				this.MILLIS_PER_TICK = 100;
				this.maxBuilding = 18;
				this.buildInit = 60;
				this.buildMax = 220;
				this.buildMaxCol = 13;
				this.scoreStyle = 1;
				this.gfxStart = 8;
				this.buildsize = 6;
				this.buildsizeY = 6;
				this.build1 = 22;
				this.build2 = 46;
				this.build3 = 70;
				this.buildstart = 9;
				this.ground = 107;

				this.gfx = Image.createImage(this.gameWidth, this.gameHeight);
				gGfx = this.gfx.getGraphics();
		            gGfx.setColor(0,0,0);
				gGfx.fillRect(1,1,this.gameWidth,this.gameHeight);
		            gGfx.setColor(255,0,0);
				gGfx.fillRect(1,1,this.gameWidth,5);
				for (ix = 1; ix < 25; ix++)
				{
			            gGfx.setColor(250 - (10 * ix),0,0);
					gGfx.fillRect(1,ix + 4,this.gameWidth,1);
				}
				iz = 74;
				for (iy = 1; iy < 4; iy++)
				{
					for (ix = 1; ix < (iy * 6); ix++)
					{
				            gGfx.setColor(0,0,(iy * 16) + ix + 32);
						gGfx.fillRect(1,iz,this.gameWidth,1);
						iz++;
					}
				}
		            gGfx.setColor(96,96,96);
				for (ix = 0; ix < (this.gameWidth >> 5); ix++)
				{
					for (iy = 0; iy < 3; iy++)
					{
						iz = Util.getRandomInt(4, 28);
						ia = Util.getRandomInt(4, 28);
						gGfx.drawLine((ix * 32) + iz, (iy * 32) + ia, (ix * 32) + iz, (iy * 32) + ia);
					}
				}
				gGfx.setClip(1, this.gameHeight - 21, this.gameWidth, 21);
				gGfx.drawImage(this.loaded, 1,74, Graphics.TOP | Graphics.LEFT);
				this.landRow = 98;
				this.plane = new Plane(16, 10, 3, 6, 1, 116, 2, this.landRow);
				this.bomb.maxDrop = 106;
				this.bomb.setSize(6, 10, 3, -3, 0);
				iColOffsetA = 17;
				iColOffsetB = 3;
				iColOffsetC = 4;
				scoreX = 92;
				scoreY = 3;
				scoreHeight = 20;
				MILLIS_PER_TICK = 100;
				planeCrash = 94;
				break;
			}
			default:
			{
				Graphics gGfx;
				int ix, iy, iz, ia;

				this.maxBuilding = 18;
				this.buildInit = 80;
				this.buildMax = 280;
				this.buildMaxCol = 16;
				this.scoreStyle = 1;
				this.gfx = Image.createImage(this.gameWidth, this.gameHeight);
				this.gfxStart = 18;
				this.buildsize = 8;
				this.buildsizeY = 8;
				this.build1 = 29;
				this.build2 = 61;
				this.build3 = 93;
				this.buildstart = 16;
				this.ground = 169;
				this.gfx = Image.createImage(this.gameWidth, this.gameHeight);
				gGfx = this.gfx.getGraphics();
		            gGfx.setColor(0,0,0);
				gGfx.fillRect(1,1,this.gameWidth,this.gameHeight);
		            gGfx.setColor(255,0,0);
				gGfx.fillRect(1,1,this.gameWidth,5);
				for (ix = 1; ix < 50; ix++)
				{
			            gGfx.setColor(250 - (5 * ix),0,0);
					gGfx.fillRect(1,ix + 4,this.gameWidth,1);
				}
				iz = 109;
				for (iy = 1; iy < 4; iy++)
				{
					for (ix = 1; ix < (iy * 12); ix++)
					{
				            gGfx.setColor(0,0,(iy * 16) + (ix / 2) + 16);
						gGfx.fillRect(1,iz,this.gameWidth,1);
						iz++;
					}
				}
		            gGfx.setColor(96,96,96);
				for (ix = 0; ix < (this.gameWidth >> 5); ix++)
				{
					for (iy = 0; iy < 4; iy++)
					{
						iz = Util.getRandomInt(4, 28);
						ia = Util.getRandomInt(4, 28);
						gGfx.drawLine((ix * 32) + iz, (iy * 32) + ia, (ix * 32) + iz, (iy * 32) + ia);
					}
				}
				gGfx.setClip(1, this.gameHeight - 40, this.gameWidth, 29);
				gGfx.drawImage(this.loaded, 1, this.gameHeight - 93, Graphics.TOP | Graphics.LEFT);

				this.landRow = 153;
				this.plane = new Plane(21, 11, 2, 8, 1, 160, 6, this.landRow);
				this.bomb.maxDrop = 162;
				this.bomb.setSize(8, 14, 3, 0, 2);
				iColOffsetA = 21;
				iColOffsetB = 8;
				iColOffsetC = 0;
				scoreX = 124;
				scoreY = 10;
				scoreHeight = 25;
				MILLIS_PER_TICK = 50;
				planeCrash = 125;
				break;
			}

		}

		// Make the city
		for (int i = 0; i < this.maxBuilding; i++)
		{
			building = new Building();
      	      city.addElement(building);
		}
		init();
	}

	void init()
	{
		animationThread	= null;
		isGameOver		= false;
		isPaused		= false;
		hasBeenShown	= false;
		gameActionFire 	= 0;
		this.status 	= GM_NEW_GAME;
		this.score 		= 0;
		this.bonus 		= 0;
		this.level		= 1;
		this.ticks		= 0;
		// Plane gets init() at start of level
//		this.gameEffects.lightsOn();
	}

	int getStatus()
	{
		return this.status;
	}

	int getTicks()
	{
		return this.ticks;
	}

	ByteArrayOutputStream getGameData()
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream outputStream = new DataOutputStream(baos);
		if (this.status == GM_NEW_LEVEL && this.ticks != 0)
		{
			this.status = GM_PLAY;
			this.ticks = 0;
		}
		try
		{
			outputStream.writeInt(this.hiLevel);
			outputStream.writeInt(this.hiScore);
			outputStream.writeInt(this.status);
			outputStream.writeInt(this.score);
			outputStream.writeInt(this.level);
			outputStream.writeInt(this.bonus);
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in getGameData");
		}

		return baos;
	}

	void setGameData(ByteArrayInputStream bais)
	{
		DataInputStream inputStream = new DataInputStream(bais);
		try
		{
			this.hiLevel = inputStream.readInt();
			this.hiScore = inputStream.readInt();
			this.status	 = inputStream.readInt();
			this.score	 = inputStream.readInt();
			this.level	 = inputStream.readInt();
			this.bonus   = inputStream.readInt();
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in setGameData(1)");
		}
		this.redrawAll();

	}
	// Game not continued, so this should handle retrieving the HiScore / HiLevel

⌨️ 快捷键说明

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