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

📄 gmcanvas.java

📁 蓝牙技术 本游戏来源于网络
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
//	GMCanvas
//
//	Main canvas class for game
//


// unnamed package

import java.io.*;

//#ifdef nokia
//import com.nokia.mid.ui.FullCanvas;
//import javax.microedition.lcdui.*;
//#elifdef midp1
//import javax.microedition.lcdui.*;
//#else
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
//#endif


class GMCanvas
//#ifdef nokia
//    extends FullCanvas
//#elifdef midp1
//    extends Canvas
//#else
    extends GameCanvas
//#endif
    implements Runnable
{
    private static int MILLIS_PER_TICK = 44;
    private	static final int LAST_LEVEL = 30;

	// -------------------------------------------------------------------------------------------
	// List of game status
	// -------------------------------------------------------------------------------------------
	private static final int GM_SPLASH = 0;
	private static final int GM_FIRST = 1;
	private static final int GM_INIT = 2;
	private static final int GM_PLAY = 3;
	private static final int GM_LEVEL_COMP = 4;
	private static final int GM_GAME_OVER = 5;
	private static final int GM_INIT_LEVEL = 6;
	private static final int GM_LOOSE_LIFE = 7;
	private static final int GM_TITLE = 8;
	private static final int GM_TITLE2 = 9;
	private static final int GM_ENTER_SCORE = 10;
	private static final int GM_GAME_COMPLETE = 11;
	private static final int GM_EXIT = 14;


	// -------------------------------------------------------------------------------------------
	// Odds & Sods
	// -------------------------------------------------------------------------------------------
    private final	GameMIDlet midlet;
    private Thread 	animationThread = null;
	private boolean	isPaused		= false;
	private boolean	forceMenu 		= false;
	public 	static	Dictionary	dict;
	public	static  GameEffects ge;
	public	static	Standard stan = new Standard();
	public	long	timing = 0;
	public	boolean quit = false;
	public	static boolean bContinue = false;	// Used to identify a continued game
	public	Canvas canvas = null;				// Used in startApp to see if we have a canvas
	public	static boolean noSound = true;

	// -------------------------------------------------------------------------------------------
	// Used for scrolling
	// -------------------------------------------------------------------------------------------
	// Constants
    public 	static final int TILE_WIDTH = 16;
    public 	static final int TILE_HEIGHT = 16;
    public 	static final int MAP_TILE_WIDTH = 16;
	// Variables
	public	static int xDisplay = 0;
	public	static int yDisplay = 0;
    public	static int DISPLAY_WIDTH;
    public 	static int DISPLAY_HEIGHT;
    public	static int WIDTH_IN_TILES;
    public	static int HEIGHT_IN_TILES;
    public	static int MAP_TILE_HEIGHT;
    public  static int MAP_PIXEL_WIDTH = 0;
    public  static int MAP_PIXEL_HEIGHT = 0;
    public	static BGLayer bg;
	// Animate tiles
	public static int[] animTile = new int[30];


	// -------------------------------------------------------------------------------------------
	// Visible screen (actual screen size?)
	// -------------------------------------------------------------------------------------------
    public	static int VIS_WIDTH;
    public 	static int VIS_HEIGHT;


	// -------------------------------------------------------------------------------------------
	// List of images
	// -------------------------------------------------------------------------------------------
    public	static Image	imageMenu;
    public	static Image	imageTitle;
	/* EG
    public	static Image	imageCloud;
    public	static Image[]	imageDude = new Image[50];
    public	static Image	imageNumber;
	*/

	// -------------------------------------------------------------------------------------------
    // Keys & stuff
	// -------------------------------------------------------------------------------------------
    private static boolean[] upPressed = new boolean[3];	// array of 3:- 2 = menu input, 0&1 are for you and bluetooth
    private static boolean[] upHeld = new boolean[3];
    private static boolean[] downPressed = new boolean[3];
    private static boolean[] downHeld = new boolean[3];
    private static boolean[] leftPressed = new boolean[3];
    private static boolean[] leftHeld = new boolean[3];
    private static boolean[] rightPressed = new boolean[3];
    private static boolean[] rightHeld = new boolean[3];
    private static boolean[] firePressed = new boolean[3];
    private static boolean[] fireHeld = new boolean[3];
    private static boolean[] firing = new boolean[3];
	public	static boolean[] bLeftPressed = new boolean[3];
	public	static boolean[] bRightPressed = new boolean[3];
	public static int keyStates = 0;


	// -------------------------------------------------------------------------------------------
    // Bluetooth stuff
	// -------------------------------------------------------------------------------------------
	public static final int BT_SETUP_HOST = 1;
	public static final int BT_AWAIT_CLIENT = 2;
	public static final int BT_SETUP_JOIN = 3;
	public static final int BT_AWAIT_HOST = 4;
	public static final int BT_AWAIT_RECEIVE = 5;
	public static final int GM_CONNECTION_LOST = 50;
	public static final int BT_DELAY = 5;
	public boolean 		bBTSetup = false;
	public int			btStatus = 0;
    public BlueTooth 	disc;
    public boolean		bHost = false;
    public boolean		bMultiplayer = false;
    public boolean		updateTicks = true;
    public boolean		updateDraw = true;
    public int 			btMissCount = 0;
    public int 			btResent = 0;
    public boolean		btIn = false;
    public int 			btTick = 0;

	// -------------------------------------------------------------------------------------------
    // Kind of Bluetooth
	// -------------------------------------------------------------------------------------------
	public	static int thisDude = 0;
	public	static int otherDude = 1;
	public	static int looseLife;			// The Dude who is to loose a life because of a collision


	// -------------------------------------------------------------------------------------------
    // General game variables
	// -------------------------------------------------------------------------------------------
	public	static	int 		tickCount = 0;
	public 	static	int			gameMode = GM_SPLASH;
	public	static	int			gameLevel;
	public	static	boolean		clearSoftKey;
	// -------------------------------------------------------------------------------------------
	// Font stuff
	// -------------------------------------------------------------------------------------------
	public	final 	static Font GAME_FONT = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	public	final 	static int 	fontHeight = GAME_FONT.getHeight();
	// -------------------------------------------------------------------------------------------
	// Options
	// -------------------------------------------------------------------------------------------
    public static final byte[] MAX_OPTIONS = {6,3,5};
	public static int 		option = 1;
	public static int		optionPage = 0;
	public static boolean[]	options = new boolean[7];
	public static int[] menuS = {0,9630,15582,15582,9630,0,0,8192,14188,16384,14188,8192,0}; 	// Y offset
	public static int[] menuC = {16384,13255,5063,-5063,-13255,-16384,16384,14188,8192,0,-8192,-14188,-16384};	// X offset


	// -------------------------------------------------------------------------------------------
	// Debug
	// -------------------------------------------------------------------------------------------
	public long fps = 0;
	public long fpsCount = 0;




//#ifdef midp1
	// -------------------------------------------------------------------------------------------
    // MIDP1 specific bollocks
	// -------------------------------------------------------------------------------------------
//    public static final int LEFT_PRESSED = 0x004;
//    public static final int RIGHT_PRESSED = 0x020;
//    public static final int DOWN_PRESSED = 0x040;
//    public static final int UP_PRESSED = 0x002;
//    public static final int FIRE_PRESSED = 0x100;
//#endif



	// -------------------------------------------------------------------------------------------
	// At last:- the constructor
	// -------------------------------------------------------------------------------------------
    GMCanvas(GameMIDlet midlet)
    {
//#ifdef midp2
        super(false);   // true = suppress key events for game keys
//#endif
        this.midlet = midlet;

		// Do as little as possible in here, and do the bulk of the setup in the GM_SPLASH & GM_FIRST gameModes
		gameMode = GM_SPLASH;
	}

    public void keyReleased(int keyCode)
    {
    	switch (getGameAction(keyCode))
		{
    		case Canvas.UP:
    			keyStates &= -UP_PRESSED-1;
    			break;
    		case Canvas.DOWN:
    			keyStates &= -DOWN_PRESSED-1;
    			break;
    		case Canvas.LEFT:
    			keyStates &= -LEFT_PRESSED-1;
    			break;
    		case Canvas.RIGHT:
    			keyStates &= -RIGHT_PRESSED-1;
    			break;
    		case Canvas.FIRE:
    			keyStates &= -FIRE_PRESSED-1;
    			break;
		}
    }

    public void keyPressed(int keyCode)
    {
		// used for entering highscore name
    	Standard.theKey = keyCode;

		// Handle softkeys. For the most part this is menu navigation code
        if ((keyCode == Standard.LEFT_SOFTKEY1 || keyCode == Standard.RIGHT_SOFTKEY1
        || keyCode == Standard.LEFT_SOFTKEY2 || keyCode == Standard.RIGHT_SOFTKEY2))
        {
			// If a menu is in play then handle navigation etc
        	if (gameMode == GM_TITLE || gameMode == GM_TITLE2 || forceMenu)
        	{
				// Left key set firePressed
        		if (keyCode == Standard.LEFT_SOFTKEY1 && (btStatus < 1 || btStatus > 4 || gameMode == GM_PLAY))
        		{
	    			if (forceMenu) firePressed[2] = true;
	    			else firePressed[0] = true;
        		}
        		// Right key manipulates menu when not establishing a bluetooth connection
        		else if ((keyCode == Standard.RIGHT_SOFTKEY1 || keyCode == Standard.RIGHT_SOFTKEY2) && (btStatus < 1 || btStatus > 4 || gameMode == GM_PLAY))
        		{
					// If right key is pressed on forced menu or main menu then exit game
        			if (optionPage == 0)
        			{
						if (disc != null) disc.close();
				    	bMultiplayer = false;
				    	if (gameMode >= GM_PLAY && gameMode <= GM_LOOSE_LIFE) storeGame();
						gameMode = GM_EXIT;
						forceMenu = false;
						isPaused = false;
						tickCount = 0;
        			}
        			else
        			{
						// Back from option page 1
        				if (optionPage == 1)
        				{
        					optionPage = 0;
        					option = 2;
    						clearSoftKey = true;
        				}
						// Back from option page 2
        				else if (optionPage == 2)
        				{
            				optionPage = 0;
        					option = 3;
    						clearSoftKey = true;
        				}
						// Back from option page 3
        				else if (optionPage == 3)
        				{
            				optionPage = 2;
        					option = 2;
    						clearSoftKey = true;
        				}
						// Back from option page 4
        				else if (optionPage == 4)
        				{
            				optionPage = 2;
        					option = 3;
    						clearSoftKey = true;
        				}
        			}
        		}

        	}
        	else
        	{
        		changePause();
        	}
        }
        else
        {
        	switch (getGameAction(keyCode))
			{
        		case Canvas.UP:
        			keyStates |= UP_PRESSED;
        			break;
        		case Canvas.DOWN:
        			keyStates |= DOWN_PRESSED;
        			break;
        		case Canvas.LEFT:
        			keyStates |= LEFT_PRESSED;
        			break;
        		case Canvas.RIGHT:
        			keyStates |= RIGHT_PRESSED;
        			break;
        		case Canvas.FIRE:
        			keyStates |= FIRE_PRESSED;
        			break;
			}
        }
    }

	// Change pause state when possible
    public void changePause()
    {

    	if (gameMode != GM_FIRST && gameMode != GM_SPLASH) isPaused = !isPaused;

    	if (isPaused)
    	{
    		if (ge != null) ge.stopSound();
    		forceMenu = (gameMode != GM_TITLE && gameMode != GM_TITLE2 && gameMode != GM_FIRST && gameMode != GM_SPLASH);
			if (forceMenu) option = 0;
			else isPaused = false;
    		btResent = 0;
    	}
    	else
    	{
			if (bMultiplayer) BlueTooth.resend();
			if (forceMenu == true) clearSoftKey = true;
    		forceMenu = false;
    	}
    }
    void init()
    {
    	imageMenu   = GameMIDlet.createImage("/i2.png");
    }

    public synchronized void start()
    {
		if (canvas == null)
		{
//#ifdef midp2
	        setFullScreenMode(true);
//#endif
	        canvas = this;
		}

	    animationThread = new Thread(this);
        animationThread.start();
		changePause();
    }


    public synchronized void stop()
    {
		if (ge != null) ge.stopSound();
		if (disc != null) disc.close();
        animationThread = null;
    }

    public void run()
    {
        Thread currentThread = Thread.currentThread();
        long timecounter = 0,
			 fpsStart = System.currentTimeMillis(),
			 endTime = 0,
			 delay = 0;

⌨️ 快捷键说明

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