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

📄 gamemanager.java

📁 一个关于沉船的J2ME小游戏
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	void setGameHi(ByteArrayInputStream bais)
	{
		DataInputStream inputStream = new DataInputStream(bais);
		try
		{
			this.hiLevel = inputStream.readInt();
			this.hiScore = inputStream.readInt();
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in setGameData(2)");
		}
		this.redrawAll();

	}

	ByteArrayOutputStream getPlaneData()
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream outputStream = new DataOutputStream(baos);
		try
		{
			outputStream.writeInt(this.plane.x);
			outputStream.writeInt(this.plane.y);
			outputStream.writeInt(this.plane.down);
			outputStream.writeInt(this.plane.mx);
			outputStream.writeInt(this.plane.speedInc);
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in getPlaneData");
		}

		return baos;
	}
	void setPlaneData(ByteArrayInputStream bais)
	{
		DataInputStream inputStream = new DataInputStream(bais);
		try
		{
			this.plane.x = inputStream.readInt();
			this.plane.y = inputStream.readInt();
			this.plane.down = inputStream.readInt();
			this.plane.mx = inputStream.readInt();
			this.plane.speedInc = inputStream.readInt();
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in setPlaneData");
		}
	}

	ByteArrayOutputStream getBombData()
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream outputStream = new DataOutputStream(baos);
		try
		{
			outputStream.writeInt(this.bomb.x);
			outputStream.writeInt(this.bomb.y);
			outputStream.writeInt(this.bomb.status);
			outputStream.writeInt(this.bomb.columnHit);
			outputStream.writeBoolean(this.bomb.redraw);
			outputStream.writeInt(this.bomb.oldX);
			outputStream.writeInt(this.bomb.oldY);
			outputStream.writeInt(this.bomb.strength);
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in getBombData");
		}

		return baos;
	}
	void setBombData(ByteArrayInputStream bais)
	{
		DataInputStream inputStream = new DataInputStream(bais);
		try
		{
			this.bomb.x = inputStream.readInt();
			this.bomb.y = inputStream.readInt();
			this.bomb.status = inputStream.readInt();
			this.bomb.columnHit = inputStream.readInt();
			this.bomb.redraw = inputStream.readBoolean();
			this.bomb.oldX = inputStream.readInt();
			this.bomb.oldY = inputStream.readInt();
			this.bomb.strength = inputStream.readInt();
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in setBombData");
		}
	}

	ByteArrayOutputStream getBuildingData()
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream outputStream = new DataOutputStream(baos);

		try
		{
			Building building;
			for (int ix = 0; ix < this.maxBuilding; ix++)
			{
				building = (Building) city.elementAt(ix);
				outputStream.writeInt(building.blocks);
				outputStream.writeInt(building.color);
				outputStream.writeBoolean(building.hit);
			}
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in getBuildingData");
		}

		return baos;
	}
	void setBuildingData(ByteArrayInputStream bais)
	{
		DataInputStream inputStream = new DataInputStream(bais);
		try
		{
			Building building;
			this.buildingLeft = 0;
			for (int ix = 0; ix < this.maxBuilding; ix++)
			{
				building = (Building) city.elementAt(ix);
				building.blocks = inputStream.readInt();
				building.color  = inputStream.readInt();
				building.hit    = inputStream.readBoolean();
				this.buildingLeft+=building.blocks;
			}
		}
		catch (IOException ioe)
		{
			throw new IllegalArgumentException("Error in setBuildingData");
		}
	}

    	public void run()
    	{
        	Thread currentThread = Thread.currentThread();
        	try
        	{
            	// This ends when animationThread is set to null, or when
            	// it is subsequently set to a new thread; either way, the
            	// current thread should terminate.
            	while (currentThread == animationThread)
            	{
                		long startTime = System.currentTimeMillis();

                		if (!isPaused)
	                	{
	                  	if (canvas.isShown())
      	            	{
            	            	// We shouldn't use a transition to the 'not shown'
                  	      	// state to pause the game (see below), until we
                        		// are first sure that we have been shown at
                        		// some time.
	                        	if (!hasBeenShown)
      	                  	{
            	            	    hasBeenShown = true;
                  	      	}

	                        	// We only tick if we are shown.
      	                  	tick();
							this.gameEffects.lightsOn();
	                  	}
      	            	else
            	      	{
                  	      	// The game canvas is no longer shown. We should
	                        	// pause the game since it is no longer visible.
      	                  	// An example is when a system pop-up window like a
            	            	// low battery warning, incoming phone call, etc.
                  	      	// occurs during the game.
                        		// (The method keyPress is used to resume after
                        		// such a pause.)
	                        	if (hasBeenShown)
      	                  	{
            	                		pause();
                  	      	}
                    		}
                		}

	                	// After each tick, we request a repaint.
      	          	// We do not request a repaint when we are not shown.
            	    	//
                		// (The repaint below also allows the paint method to print
	                	// the "Press any key to resume" message after a pause
      	          	// caused by the "!isShown()" code just above,
            	    	// when the game canvas becomes shown again (e.g. after
                		// the system pop-up window disappears). The method keyPress
	                	// causes the actual resume.)
      	          	if (canvas.isShown())
            	    	{
                  	  	canvas.repaint(0, 0, gameWidth, gameHeight);
                   		canvas.serviceRepaints();
      	          	}

            	    	long timeTaken = System.currentTimeMillis() - startTime;
				iDEBUG4 = timeTaken;
	                	if (timeTaken < MILLIS_PER_TICK)
      	          	{
            	        	synchronized(this)
                  	  	{
	                        	wait(MILLIS_PER_TICK - timeTaken);
      	              	}
            	    	}
	                	else
      	          	{
	                    	currentThread.yield();
      	          	}
            	}
        	}
        	catch (InterruptedException e)
        	{
        	}
    	}


    	public synchronized void start(boolean bPause)
    	{
		animationThread = new Thread(this);
     	  	animationThread.start();
		if (bPause)
		{
			pause();
			bJustLoaded = true;
		}
		else
		{
			resume();
			bJustLoaded = false;
		}
		this.redrawAll();
    	}


    	public synchronized void stop()
    	{
		{
			ByteArrayOutputStream gameBaos = new ByteArrayOutputStream();
			gameBaos = this.getGameData();
			try
			{
				Settings.setValue(Settings.GM_MAIN_DATA, gameBaos);
			}
			catch(Exception e)
			{
				// We can not do anything to recover from this error,
				// let the game proceed without using saved settings.
				throw new IllegalArgumentException("Error in GameEffects.Pause:try setValue (1)");
			}
		}
        	animationThread = null;
        	gameEffects.pause();
    	}


	public void pause()
	{
		synchronized(this)
		{
      		isPaused = true;
        	}
		{
			ByteArrayOutputStream gameBaos = new ByteArrayOutputStream();
			gameBaos = this.getGameData();
			try
			{
				Settings.setValue(Settings.GM_MAIN_DATA, gameBaos);
			}
			catch(Exception e)
			{
				// We can not do anything to recover from this error,
				// let the game proceed without using saved settings.
				throw new IllegalArgumentException("Error in gameManager.pause:try setValue (1)");
			}
		}
        	gameEffects.pause();
		this.redrawAll();
    	}


    	public synchronized boolean isPaused()
    	{
        	return isPaused;
    	}

	void redrawAll()
	{
		this.redrawBackground = true;
		this.redrawBuilding = true;
		this.redrawScore = true;
	}

	void closePressed()
    	{
        	// The GameManager handles the 'closePressed' event from one
        	// of two possible sources:
        	//  - any softkey press in a NokiaCloseableCanvas (FullCanvas)
        	//    indicates that 'close' was pressed
        	//  - a CloseableCanvas (Canvas) uses its 'Back' command
        	//    to indicate that close was pressed
        	// The 'closed' canvas is suspended but not destroyed.
        	// It can be resumed and shown on the display again later.
        	if (!isPaused)
        	{
            	pause();
        	}

        	midlet.gameManagerMainMenu(isGameOver, hiScore);
    }

    	public void keyReleased(int keyCode)
    	{
        	// The base handles game key releases with gameActionPressed.
        	// 'false' means 'game action released' (i.e. no longer pressed).
       	switch (canvas.getGameAction(keyCode))
        	{
            	case Canvas.FIRE:
				if (this.bomb.status == 1) gameActionFire = 0;
                		break;
        	}
    	}


	public void keyPressed(int keyCode)
	{
        	// When we return to an existing game screen from a 'Continue'
        	// in the MainMenu, the game remains paused though displayed.
        	// This gives the player time to put their fingers back
        	// onto the game keys before the game resumes.
        	// The first game key press resumes the game.
        	if (isPaused && canvas.isShown())
      	{
            	resume();
        	}
		else
		{
	       	switch (canvas.getGameAction(keyCode))
      	  	{
            		case Canvas.FIRE:
	                		gameActionFire = 1;
      	          		break;
        		}
		}
	}


    	public void resume()
    	{
        	synchronized(this)
        	{
            	isPaused = false;
        	}
        	gameEffects.resume();
    	}


	private void tick()
    	{

		switch (this.status)
		{
			case GM_NEW_GAME:
				this.score = 0;
				this.bonus = 0;
				this.ticks = 0;
				this.level = 1;
				this.status = GM_NEW_LEVEL;
				break;

			case GM_NEW_LEVEL:
				if (this.ticks == 0)
				{
					this.initLevel();
					this.redrawAll();
				}
				if (this.inGamePause(50))
				{
					this.redrawAll();
					this.status = GM_PLAY;
				}
				break;

			case GM_PLAY:
				if (this.plane.move())
				{
					// The plane has landed
					this.status = GM_LANDED;
					break;
				}
				if (this.gameActionFire == 1)
				{
					if (this.bomb.status == 0) this.gameActionFire = 0;
					this.bomb.drop(this.plane.getX(), this.plane.getY());
				}
				this.bomb.move();
				if (collision())
				{
					if (this.score > this.hiScore)
					{
						this.hiScore = this.score;
						this.status = GM_HISCORE;
						this.isGameOver = true;
					}
					else
					{
						this.status = GM_CRASH;
						this.isGameOver = true;
					}
//					Settings.setContinue(false);
					break;
				}
				break;

			case GM_LANDED:
				if (this.ticks == 45)
					this.redrawAll();
				if (this.inGamePause(50))
				{
					this.level++;
					this.bonus = 0;
					this.status = GM_NEW_LEVEL;
				}
				break;

			case GM_CRASH:
				if (this.ticks == 0)
 					gameEffects.playCrash();
				if (this.inGamePause(100))
				{
					closePressed();
				}
				break;

			case GM_HISCORE:
				if (this.ticks == 0)

⌨️ 快捷键说明

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