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

📄 nutty.java

📁 一个推箱子游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
游戏名:松鼠推箱子
开发者:关文柏
适用机型:Nokia S40 Motorola C650


*/




import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;


public class Nutty extends Canvas implements Runnable
{

	private Main ma;
	private Thread thread;
	private static int scrW=128,scrH = 128 ; 
	private boolean pause;
	//--------------------------Game delay  control-------------------//
	private static int frame = 100;
	private static long start = 0 ;
	private static long end = 0 ;
	//----------------------------------------------------------------//

	//---------------------------Game display effect------------------//	
	
	private static  Font[] font = {(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_LARGE)),(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_MEDIUM)),(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL))} ;
	
	private static int BLUE = 0x0000FF; 
	private static int WHITE = 0xFFFFFF;
	private static int BLACK = 0x000000;
	private static int GREEN = 0x00FF00;
	private static int RED = 0xFF0000;
	private static int GRAY = 0xCCCCCC;
	private static int YELLOW = 0xFFFF00 ; 
	//private int[] color = {WHITE,BLACK,RED,YELLOW,BLUE,GREEN,GRAY} ;
	//----------------------------------------------------------------//
	//--------------------------Game mode control--------------------//
	private static int gameMode;
	private static final int MENU = 2;
	private static final int TITLE = 1 ;
	private static final int HELP = 4;
	private static final int ABOUT = 5;
	private static final int PAUSE = 7;
	private static final int GAME = 6;
	private static final int EXIT = 8;
	private static final int SETTING = 3;
	private static final int LOAD = 0;	
	//-----------------------------------------------------------------//


	//----------------------------Game text-----------------------------//
	private static String[] menuTxt = {"继续游戏","开始游戏","最高分","设置","帮助","关于","退出游戏"};
	//private static String[] menuTxt = {"Continue","New game","High score","Setting","Help","About game","Exit"};
	//------------------------------------------------------------------//


	///---------------------------Game Images----------------------------//
	private static Image[] squirrel = new Image[4];	
	private static Image[] nut = new Image[3];
	private static Image[] map = new Image[2];

	private static Image buffer;
	private static Image title;
	//--------------------------------------------------------------------//

	//--------------------------------keyboard value-------------------------------------//
	public static final int KEYSOFT_1 = -6 ;
	public static final int KEYSOFT_2 = -7 ;
	public static final int SELECT = -5;
	public static final int KEY_UP = -1;
	public static final int KEY_DOWN = -2;
	public static final int KEY_LEFT = -3;
	public static final int KEY_RIGHT = -4;
	public static final int KEY_2 = 50;
	public static final int KEY_8 = 56;
	public static final int KEY_4 = 52;
	public static final int KEY_6 = 54;
	public static final int KEY_STAR = 42;
	public static final int KEY_POD = 35 ; 
	//-------------------------------- C650---------------------------------//
//	public static final int KEYSOFT_1 = -21 ;
//	public static final int KEYSOFT_2 = -22 ;
//	public static final int SELECT = -20;
//	public static final int KEY_UP = -1;
//	public static final int KEY_DOWN = -6;
//	public static final int KEY_LEFT = -2;
//	public static final int KEY_RIGHT = -5;
//	public static final int KEY_2 = 50;
//	public static final int KEY_8 = 56;
//	public static final int KEY_4 = 52;
//	public static final int KEY_6 = 54;
//	public static final int KEY_STAR = 42;
//	public static final int KEY_POD = 35 ; 

	//---------------------------------------------------------------------//


	//-----------------------------Game logic data -----------------------//

	private static int level = 1; // Game's level

	private static int mSelect = 0 ; //control menu select

	private static boolean[] moveS = new boolean[4] ; //squirrel move flag;
	
	private static int[][][] map_data = new int[8][8][5];	//first  -  [8] indication map's height separated 8 rows
															//second -  [8] indication map's width separated 8 tiers
															//		  - [0] x coordinate
															//        - [1] y coordinate
															//        - [2] wall flag   1- wall 0 - space
															//        - [3] nut's destination 1/2 -  yes 0 - no		(1 green 2 red)
															//        - [4] nut's original location  1/2 - yes 0 - no  (1 green 2 red)

	private static int[] splace = new int[2] ;		//squirrel's initial location    [0] - x [1] - y   array index
	private static int[][] nplace ;				//nut's initial location			[nut's number][x/y/color]
	private static int[][] ndplace ;			//nut's destination
	private static int sdir ;					//control squirrel direction

	private static boolean win ; 
	//--------------------------------------------------------------------//

	
	Nutty(Main m)
	{
		ma = m ;
		
		setFullScreenMode(true);
		thread = new Thread(this);
		thread.start();

		
	}

	public void run()
	{
		if(gameMode == LOAD)
		{
			loadImages();
			init();
			gameMode = TITLE ; 
		}

		for(;;)
		{
			if(gameMode == EXIT)				
				break ;
			start = System.currentTimeMillis();
			{
				
				moveSqu();
				win = isWin();
				
			}
			end = System.currentTimeMillis();
			if((end - start) < frame)
			{
				try
				{
					thread.sleep(frame-(end-start));
					repaint();
					///System.out.println(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory());
				}catch(InterruptedException ie)
				{
					ie.printStackTrace();
				}

			}else
			{
				thread.yield();
			}
		}

		if(gameMode == EXIT)
			ma.exitApp();


	}

	public void paint(Graphics g)
	{
		
		switch(gameMode)
		{
			case LOAD:
					drawProcess(g,0);
				break;
			case TITLE:
					drawTitle(g);
				break;
			case MENU:
					clearScreen(g);
					drawMenu(g);
				break;
			case SETTING:
					drawSettingScreen(g);
				break;
			case HELP:
					drawHelpScreen(g);
				break;
			case ABOUT:
					drawAboutScreen(g);
				break;
			case GAME:
					g.drawImage(buffer,0,0,0);
					//drawMap(g);
					//drawNutDest(g);
					drawSqu(g);
					drawNuts(g);
					if(win)
						drawWinScreen(g);
				break;
			case PAUSE:
				break;
			case EXIT:
				break;
				
		}
	}
	
	/*main game logic control*/
	private void keyControl(int key)
	{
		switch(gameMode)
		{
			case TITLE:	
			
			if( key == KEYSOFT_1)
			{
					
					gameMode = MENU;
			}

			if( key == KEYSOFT_2 )	
			{
					
					gameMode = EXIT ;
			}
			
				break;
			case MENU:
			{
				if( key == KEYSOFT_1)
				{
					
					switch(mSelect)
					{
						case 0:
							gameMode = GAME;
							break;
						case 2:
							break;
						case 3:
							break;
						case 4:
							break;
						case 5:
							break;
						case 6:
							break;
					}
					
				}

				if( key == KEYSOFT_2)
				{
					
					gameMode = TITLE;
					
				}

				if( key == KEY_UP )	//'up'
				{
					
					if(pause)
					{
							if(mSelect==0)
								mSelect = menuTxt.length;
							else
								mSelect--;
					}else{
							if(mSelect==0){
								mSelect = menuTxt.length-1;
							}else{
								if(mSelect==2)
									mSelect=0;
								else							
									mSelect--;
							}
					}			
					
				}
				if( key == KEY_DOWN )	//'down'
				{
					
					if(pause)
					{
						if(mSelect == menuTxt.length)
							mSelect = 0;
						else
							mSelect++;
					}else
					{
						if(mSelect == menuTxt.length-1){
							mSelect = 0;
						}else{
							if(mSelect == 0)
								mSelect = 2;
							else
								mSelect++;
						}

					}
					
				}

				
			}
				break;
			case SETTING:
			{
				
			}
				break;
			case HELP:
			{
				
			}
				break;
			case ABOUT:
			{
				
			}
				break;
			case GAME:
			{
				if( key == KEYSOFT_1)
				{
					gameMode = MENU;
					pause = true;
				}

				if( key == KEYSOFT_2)
				{
					gameMode = EXIT;
				}

				if( key == KEY_UP)
				{
					moveS[0] = true;
				}
				if( key == KEY_DOWN)
				{
					moveS[1] = true;
				}
				if( key == KEY_LEFT)
				{
					moveS[2] = true;
				}

				if( key == KEY_RIGHT)
				{
					moveS[3] = true ;
				}
			}
				break;
			case PAUSE:
			{
				
			}
				break;
			case EXIT:
			break;
				
		}
	}
	int m= 0;
	/*initialize game data*/
	private void init() 
	{
		/*load map datas*/
		loadMap_data();	

		/*define a nut's counter temp */
		int nutCount = 0;

		/*Find nut's counter*/
		for(int m = 0 ; m < map_data.length ; m ++ )
		{
			for(int n = 0 ; n < map_data[m].length; n++)
			{			

					if(map_data[n][m][4] == 1)
					{
						nutCount++;
					}				
				
			}
			
		}

		/*initialize nut*/		
		nplace = new int[nutCount][3];
		ndplace = new int[nutCount][3];
		


		/*a temporary variable*/
		int temp = 0 ;
		int dtemp = 0 ;

		/*Find nut's coordinates and nut's destination in map,save array index in nplace[] nplace[][2] indication nut's color*/
		for(int m = 0 ; m < map_data.length ; m ++ )
		{
			for(int n = 0 ; n < map_data[m].length; n++)
			{
				
				
					if(map_data[m][n][4] != 0)
					{
						nplace[temp][0] = n;
						nplace[temp][1] = m;
						
						if(map_data[m][n][4] == 1)
						nplace[temp][2] = 1;
						else if(map_data[m][n][4] == 2)
							nplace[temp][2] = 2;

						
						temp++;
					}

					if(map_data[m][n][3] != 0)
					{
						ndplace[dtemp][0] = n;
						ndplace[dtemp][1] = m;
						
						if(map_data[m][n][3] == 1)
						ndplace[dtemp][2] = 1;
						else if(map_data[m][n][3] == 2)
							ndplace[dtemp][2] = 2;

						
						dtemp++;
					}
				
			}
			
		}


		/*Greate a buffer graphic*/
		buffer = Image.createImage(scrW,scrH);
		Graphics buf_g = buffer.getGraphics();

		/*Drawing map on buffer image*/
		drawMap(buf_g);
		drawNutDest(buf_g);
	}
	

	//--------------------Handset keyboard control----------------------//
	protected void keyPressed(int code)
	{
		keyControl(code);
		//debug("key::::::"+code);

	}
	protected void keyReleased(int code)
	{
	}
	///-----------------------------------------------------------------//

	//---------------------------drawing method-------------------------//
	private void drawProcess(Graphics g,int num)
	{

⌨️ 快捷键说明

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