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

📄 tiledscreen.java

📁 一个JAVA的扫雷游戏源代码
💻 JAVA
字号:
/*
 * Siemens AG
 * Mobile Radio Terminals
 * Munich, Germany
 * .AUTHOR      Michael Becker CT SE 2 / Sam Nova (THQ)
 * .PACKAGE		GameAPI_Demo
 * .STATUS         DRAFT
 * .CHANGE_CONTROL
 * Version |  Date  	| Changed by | 	Reason for Change
 * 1.0    	21.05.01  		M.Becker		file created.
 * 1.1      26.06.01        Jan Eichholz Math.randomInt removed 
 * 1.2      16.08.01        S. Gerber    adapted to new TiledBackground constructor
*/

package GameAPI_Demo;


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

import com.siemens.mp.game.*;

class TiledScreen extends MyScreen
{
	private static final int GRAVITY = 0x1000;
	public boolean running;
	private boolean readyToPaint = false;
	private static final int GAME_SCREEN_WIDTH = 96;
	private static final int GAME_SCREEN_HEIGHT = 80;

	private Image gameScreenImage;
	private ExtendedImage gameScreen = null;
	private GraphicObjectManager gfxManager;
	private TiledBackground tiledBack;
	private TiledBackground tiled2Back;

	private byte [] birdPixels, ballPixels;
	private byte [] birdMask, ballMask;
	private Sprite birdSpr, ballSpr;

	private int ballX, ballY, ballXVel, ballYVel, ballDelay, ballFrame = 0;
	private int birdX, birdY, birdXVel, birdYVel, birdDelay, birdFrame = 0;	

	private int nFrameCounter;
	private boolean bFirePressed;

	private byte [] tiledPixels;
	private byte [] tiled2Pixels;
	private byte [] tiled2Mask;
	private int mapX = 0, mapY = 0,speedX = 0x8000, speedY = 0x8000;
	private int map2X = 32 << 16, map2Y = 0,speed2X = -0x8000, speed2Y = 0x8000;

	private byte [] map = {
	         3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,
	         7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10,
	        11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11,12,13,14,
	        15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18,
	        3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,
	        7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10,
	       11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11,12,13,14,
	       15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18,
	        3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,
	        7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10,
	       11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11,12,13,14,
	       15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18,
	        3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,
	        7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10,
	       11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11,12,13,14,
	       15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18,
	        3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,
	        7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10,
	       11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11,12,13,14,
	       15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18,
	        3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,  3, 4, 5, 6,
	        7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10,
	       11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11, 12,13,14,11,12,13,14,
	       15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18, 15,16,17,18
	        };
	private static final int mapXSize = 24;
	private static final int mapYSize = 24;

	private byte [] map2 = { // 18 x 18
                 4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 3,
                 9, 10, 11, 12,13,14, 9, 10, 11, 12,13,14, 9, 10, 11, 12,13,14,
                15,16,17,18,19,20,15,16,17,18,19,20,15,16,17,18,19,20,
                21,22,23,24,25,26,21,22,23,24,25,26,21,22,23,24,25,26,
                27,28,29,30,31,32,27,28,29,30,31,32,27,28,29,30,31,32,
                3, 33,34,35,36, 3,3, 33,34,35,36, 3,3, 33,34,35,36, 3,

                4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 3,
                9, 10, 11, 12,13,14, 9, 10, 11, 12,13,14, 9, 10, 11, 12,13,14,
               15,16,17,18,19,20,15,16,17,18,19,20,15,16,17,18,19,20,
               21,22,23,24,25,26,21,22,23,24,25,26,21,22,23,24,25,26,
               27,28,29,30,31,32,27,28,29,30,31,32,27,28,29,30,31,32,
               3, 33,34,35,36, 3,3, 33,34,35,36, 3,3, 33,34,35,36, 3,

	        4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 3,
	        9, 10, 11, 12,13,14, 9, 10, 11, 12,13,14, 9, 10, 11, 12,13,14,
	       15,16,17,18,19,20,15,16,17,18,19,20,15,16,17,18,19,20,
	       21,22,23,24,25,26,21,22,23,24,25,26,21,22,23,24,25,26,
	       27,28,29,30,31,32,27,28,29,30,31,32,27,28,29,30,31,32,
	       3, 33,34,35,36, 3,3, 33,34,35,36, 3,3, 33,34,35,36, 3,
	        };

	private static final int map2XSize = 18;
	private static final int map2YSize = 18;



	public TiledScreen()
	{
		// new gameScreen
		try
		{
			gameScreenImage = Image.createImage(GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT);
			gameScreen = new ExtendedImage(gameScreenImage);
			gameScreen.clear((byte)0);
		}catch(Exception e)
		{
			System.out.println("Exception: "+e);
		}

		gfxManager= new GraphicObjectManager();

		try
		{
			// Load the sprites
			birdPixels = ReadByteArray("res\\bird.bin", 480, 0);
			birdMask = ReadByteArray("res\\bird.mask", 480, 0);

			ballPixels = ReadByteArray("res\\ball.bin", 640, 0);
			ballMask = ReadByteArray("res\\ball.mask", 640, 0);

			tiledPixels = ReadByteArray("res\\tiles.bin", 128, 0);
			tiledBack = new TiledBackground(tiledPixels, null, map, mapXSize,mapYSize);
			gfxManager.addObject(tiledBack);

			birdSpr = new Sprite(birdPixels, 0, 32,24, birdMask, 0, 5);
			birdX = 32 << 16;
			birdY = -32 << 16;
			birdSpr.setPosition(birdX>>16, birdY>>16);
			birdSpr.setVisible(true);
			birdYVel = 0x4000;
			birdXVel = 0x1000;
			if ((Math.abs(GameAPI_Demo.rand.nextInt()) & 1) == 1)
				birdXVel = -birdXVel;

			birdDelay = 0;

			// Add the ball
			ballSpr = new Sprite(ballPixels, 0, 32,32, ballMask, 0, 5);
			ballX = 42 << 16;
			ballY = 20 << 16;
			ballYVel = 0;
			ballXVel = 0x8000;
			ballDelay = 0;
			ballSpr.setPosition(ballX>>16, ballY>>16);
			ballSpr.setVisible(true);
			gfxManager.addObject(ballSpr);			

			tiled2Pixels = ReadByteArray("res\\tiles2.bin", 280, 0);
			tiled2Mask = ReadByteArray("res\\tiles2.mask", 280, 0);
			tiled2Back = new TiledBackground(tiled2Pixels, tiled2Mask, map2, map2XSize,map2YSize);
			gfxManager.addObject(tiled2Back);


			gfxManager.addObject(birdSpr);


		}catch(Exception e)
		{
			System.out.println("Exception: "+e);
		}
		System.gc();
		//		System.out.println("Done : free memory " + Runtime.getRuntime().freeMemory());

		readyToPaint = true;
	}


	public void Dispose()
	{
		gameScreen = null;

		// We should remove all objects from the gfx manager here
		gfxManager = null;
	}


	private void updateSprite()
	{
		mapX += speedX;
		if (mapX > (( ((mapXSize - 12) << 3) << 16) + 65535))
		{
			mapX = ( ((mapXSize - 12) << 3)  << 16) + 65535;
			speedX = -speedX;
		}
		else if (mapX < 0)
		{
			mapX = 0;
			speedX = -speedX;
		}

		mapY += speedY;
		if (mapY > (( ((mapYSize - 10) << 3) << 16) + 65535))
		{
			mapY = ( ((mapYSize - 10) << 3) << 16) + 65535;
			speedY = -speedY;
		}
		else if (mapY < 0)
		{
			mapY = 0;
			speedY = -speedY;
		}


		map2X += speed2X;
		if (map2X > (( ((map2XSize - 12) << 3) << 16) + 65535))
		{
			map2X = ( ((map2XSize - 12) << 3)  << 16) + 65535;
			speed2X = -speed2X;
		}
		else if (map2X < 0)
		{
			map2X = 0;
			speed2X = -speed2X;
		}

		map2Y += speed2Y;
		if (map2Y > (( ((map2YSize - 10) << 3) << 16) + 65535))
		{
			map2Y = ( ((map2YSize - 10) << 3) << 16) + 65535;
			speed2Y = -speed2Y;
		}
		else if (map2Y < 0)
		{
			map2Y = 0;
			speed2Y = -speed2Y;
		}

		tiledBack.setPositionInMap(mapX >> 16, mapY >> 16);
		tiled2Back.setPositionInMap(map2X >> 16, map2Y >> 16);

		// Update the ball
		ballYVel += GRAVITY;

		ballX += ballXVel;
		if (ballX < (-3 << 16) || ballX > (96-32 + 3) << 16)
		{
			ballX -= ballXVel;
			ballXVel = - ballXVel;
		}

		ballY += ballYVel;
		if (ballY > (80-24) << 16)
		{
			ballY -= ballYVel;
			ballYVel = - ballYVel;
		}

		if (ballDelay == 0)
		{
			ballDelay = 5;

			if (ballXVel > 0)
				ballFrame--;
			else    
				ballFrame++;

			if (ballFrame < 0)
				ballFrame = 4;
			else if (ballFrame == 5)
				ballFrame = 0;

			ballSpr.setFrame(ballFrame);
		}
		else
			ballDelay--;
		ballSpr.setPosition(ballX>>16, ballY>>16);

		// Bird control..
		birdX += birdXVel;
		birdY += birdYVel;

		if (birdY > 80 << 16)
		{
			birdY = -32 << 16;
			birdXVel = -birdXVel;
		}
		birdSpr.setPosition(birdX>>16, birdY>>16);

		if (birdDelay == 0)
		{
			birdDelay = 5;

			birdFrame++;
			if (birdFrame == 5)
				birdFrame = 0;

			birdSpr.setFrame(birdFrame);
		}
		else
			birdDelay--;

	}


	/**
	* Method declaration
	*
	*
	* @param g
	*
	* @see
	*/
	public void paint(Graphics g)
	{
		//no normal painting used!
		//paint all Sprites (no repaint necessary!)
		if (gameScreen != null && readyToPaint)
		{
			gameScreen.clear((byte)0);
			try
			{
				gfxManager.paint(gameScreen, 0, 0);
				gameScreen.blitToScreen(0,0);
			}catch(Exception e)
			{
				System.out.println("Exception: "+e);
			}

		}
	}

	public int getReturnValue()
	{
		return 0;
	}

	protected void keyPressed(int keyCode)
	{
		int key = getGameAction(keyCode);

		switch(key)
		{
		case Canvas.FIRE :
			bFirePressed = true;
			break;
		}
	}


	public void run()
	{
		running = true;
		nFrameCounter = 0;

		while (running)
		{
			try
			{
				Thread.yield();
				//Thread.sleep(20);
			}
			catch (Exception exc)
			{
			}

			//move the sprites
			nFrameCounter++;
			updateSprite();

			//clear the doublebuffer
			//spriteManager.clearGameScreenWithBackground(gameScreen, Background96x80.image); // Rather slow...
			//System.arraycopy(clearScreen, 0 , gameScreen, 0, (GAME_SCREEN_WIDTH >> 3) * GAME_SCREEN_HEIGHT);
			gameScreen.clear((byte)0);

			//paint all Sprites (no repaint necessary!)
			try
			{
				gfxManager.paint(gameScreen, 0, 0);
				gameScreen.blitToScreen(0,0);
			}catch(Exception e)
			{
				System.out.println("Exception: "+e);
			}

			if (bFirePressed)
				running = false;
		}
	}
}

⌨️ 快捷键说明

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