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

📄 nutty.java

📁 一个推箱子游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		g.setColor(0x000000);
		g.setFont(font[1]);
		g.drawString("Loading...",(scrW-font[1].stringWidth("Loading...")) >> 1,scrH >> 1 , 0) ;
	}
	private void drawTitle(Graphics g)
	{
		clearScreen(g);
		g.drawImage(title,0,0,0);
		g.setColor(0xffffff);
		g.drawString("菜单",2,114,0);
		g.drawString("退出",110,114,0);

	}
	 
	
	private void drawMenu(Graphics g)
	{
		 int mY = 2 ; 
	 	 int offset = 1 ;
		
		
		for(int i=0;i<menuTxt.length;i++)
		{	
			
				if(mSelect == i){
					g.setColor(RED);
					g.setFont(font[0]);
				}else{
					g.setColor(BLACK);
					g.setFont(font[2]);
				}		
				if((!pause)&&(i==0)) i++; 				
				g.drawString(menuTxt[i],( scrW - font[2].stringWidth(menuTxt[i]) ) >> 1 ,mY ,0) ;
				mY += font[2].getHeight() + offset  ; 
		}

		

	}
	private void drawHelpScreen(Graphics g)
	{ 
	}
	private void drawAboutScreen(Graphics g)
	{
	}
	private void drawSettingScreen(Graphics g)
	{
	}
	private void drawWinScreen(Graphics g)
	{
		g.setColor(0xffffff);
		g.drawString("过关",50,30,0);
	}
	/*Draw Background*/
	private void drawMap(Graphics g)
	{
		

		for(int m = 0 ; m < map_data.length ; m ++ )	//// y coordinates
		{
			for(int n = 0 ; n < map_data[m].length; n++)	// x coordinates
			{
				
				
					if(map_data[n][m][2] == 1)
					{
						g.drawImage(map[0],map_data[n][m][0],map_data[n][m][1],0);
						

					}else if(map_data[n][m][2] == 0)
					{
						g.drawImage(map[1],map_data[n][m][0],map_data[n][m][1],0);						
						
					}

			
			}
			
		}

	}
	/*Draw squirrel*/
	private void drawSqu(Graphics g)
	{
		g.drawImage(squirrel[sdir],map_data[( splace[1] )][( splace[0] )][0],map_data[( splace[1] )][( splace[0] )][1],0);
	}

	/*Draw nuts*/
	private void drawNuts(Graphics g)
	{
		for(int i = 0; i < nplace.length; i++)
		{
			g.drawImage(nut[(nplace[i][2]-1)],map_data[(nplace[i][1])][(nplace[i][0])][0],map_data[(nplace[i][1])][(nplace[i][0])][1],0);
		}
	}

	/*Draw nut's destination*/
	private void drawNutDest(Graphics g)
	{
		for(int i = 0; i < ndplace.length; i++)
		{
			g.drawImage(nut[(ndplace[i][2]+1)],map_data[(ndplace[i][1])][(ndplace[i][0])][0],map_data[(ndplace[i][1])][(ndplace[i][0])][1],0);
		}
	}

	//-----------------------------------------move and hit-------------------------------------------//
	
	/*Move squirrel*/
	private static final void moveSqu()
	{	
		/*a temporary variable save which nut beside squirrel*/
		//int p = 0 ;

		/*a flag indication a nut on the direction of squirrel move*/
		boolean hit = false ; 
		
		/*a flag indication the nut can move too*/
		boolean mf = false ; 
		
		/*the move direction is up*/
		if( moveS[0] )
		{	
			//debug("up");
			/*set the direction is up(2),squirrel will show 2sd image*/
			sdir = 2 ;		
			
			/*set the squirrel move flag of the direction  false*/
			moveS[0] = false;
			
			
			/*find whether a nut beside squirrel*/
			for(int r=0; r < nplace.length; r++)	
			{
				/*if have*/
				if( ((splace[1] - 1) == nplace[r][1]) && (splace[0]  == nplace[r][0] )){
					
					/*move nut return flag*/
					mf = moveNuts(0,r);
					//p = r ; 
					
					/*set hit true*/
					hit = true ; 
				}
				
			}
			//debug("p:::::"+p);
			/*if didn't finded nuts,but hava space*/
			if(  (map_data[(splace[1] - 1)][(splace[0])][2] == 0 ) && (!hit ) )  //no nut but have space
			{		
					/*move squirrel*/
					splace[1] -= 1 ;
					
			}
			/*if finded nut*/
			if(hit) 
			{
				/*the nut has moved*/
				if(mf){
					/*move squirrel*/
					splace[1] -= 1 ;
					/*set hit false*/
					hit = false ;
				}
			}
			
		}
		if( moveS[1] )
		{
			//debug("down");
			sdir = 3;
			moveS[1] = false;
			 
			for(int r=0; r < nplace.length; r++)
			{
				if( ((splace[1] + 1) == nplace[r][1] ) && ( splace[0]  == nplace[r][0] )){
					mf = moveNuts(1,r);
					hit = true ; 					
				}
				
			}
			if( (map_data[(splace[1] + 1)][(splace[0])][2] == 0 ) && (!hit))
			{
				splace[1] += 1 ;	
				
			}
			if(hit)
			{
				if(mf)
				{
					splace[1]+=1;
					hit = false ; 
				}
			}
			
			
			
			
		}
		if( moveS[2] )
		{
			//debug("left");
			sdir = 0;
			moveS[2] = false;
			
			for(int r=0; r < nplace.length; r++)
			{
				if( ((splace[0] - 1) == nplace[r][0]) && ( splace[1] == nplace[r][1] )){
					mf = moveNuts(2,r);
					hit = true ; 					
				}				
				
			}
			if( (map_data[(splace[1])][(splace[0] - 1)][2] == 0 ) && (!hit)){
				splace[0] -= 1 ;
				
			}
			if(hit)
			{
				if(mf)
				{
					splace[0] -= 1 ;
					hit = false;
				}
			}
			
			
		}

		if( moveS[3] )
		{
			//debug("right");
			sdir = 1 ;
			moveS[3] = false;
			for(int r=0; r < nplace.length; r++)
			{
				if( ((splace[0] + 1) == nplace[r][0] ) && ( splace[1] == nplace[r][1] )){
					mf = moveNuts(3,r);
					hit = true ; 
				}
				
			}
			if( (map_data[(splace[1])][(splace[0] + 1)][2] == 0) && (!hit)) {
				splace[0] += 1 ;
				
			}
			if(hit)
			{
				if(mf)
				{
					splace[0] += 1 ;
					hit = false ; 
				}
			}
		
			
		}

		

	}

	/*
	*
	*Funcation:Move nuts
	*
	*Parameter: dir:the direction of  move to . nm: which nut should be move.
	*
	*return: whether the nut can moved.
	*/
	private static final boolean moveNuts(int dir,int nm)
	{
		/*flag indication another nut beside current nut*/
		boolean hit = false;
		
		/*temporary flag*/
		boolean mf = false ; 
		switch(dir)
		{
			case 0: //'up'

					/*find another nut*/
					for(int i = 0 ; i < nplace.length ; i++)
					{
						/*use x ,y to coordinate*/
						if( ((nplace[nm][1] - 1) == nplace[i][1] ) && ( nplace[nm][0] == nplace[i][0]))
							hit = true ; 

					}
					/*if have space and not have another nut*/
					if( (map_data[(nplace[nm][1] - 1)][(nplace[nm][0])][2] == 0 ) && (!hit) )
					{
						/*move current nut*/
						nplace[nm][1] -= 1 ;
						mf = true ; 
					}
					
			break;
			case 1:	//'down'
					for(int i = 0 ; i < nplace.length ; i++)
					{
						if( ((nplace[nm][1] + 1) == nplace[i][1] ) && ( nplace[nm][0] == nplace[i][0]))
							hit = true ; 

					}
					if( (map_data[(nplace[nm][1] + 1)][(nplace[nm][0])][2] == 0) && (!hit) )
					{
						nplace[nm][1] += 1 ;						
						mf = true ;
					}
			break;
			case 2: //'left'
					for(int i = 0 ; i < nplace.length ; i++)
					{
						if( ((nplace[nm][1]) == nplace[i][1] ) && ( (nplace[nm][0] - 1) == nplace[i][0]))
							hit = true ; 

					}
					if( (map_data[(nplace[nm][1])][(nplace[nm][0] - 1)][2] == 0) && (!hit))
					{
							nplace[nm][0] -= 1 ;
							mf = true ;
					}
			break;
			case 3: //'right'
					for(int i = 0 ; i < nplace.length ; i++)
					{
						if( ((nplace[nm][1]) == nplace[i][1] ) && ( (nplace[nm][0] + 1) == nplace[i][0]))
							hit = true ; 

					}
					
					if( (map_data[(nplace[nm][1])][(nplace[nm][0] + 1)][2] == 0) && (!hit) )
					{
						nplace[nm][0] += 1 ;						
						mf = true ;
					}
			break;
		}

		return mf ; 
	}


	private static final boolean isWin()
	{
		boolean temp = false;
		int nn = 0;
		for(int r = 0 ; r < nplace.length ; r++)
		{
			for(int m = 0 ; m < ndplace.length ; m++)
			{
				if( (nplace[r][0] == ndplace[m][0] ) && (nplace[r][1] == ndplace[m][1]))
					nn++;
			}
		}

		if(nn == nplace.length)
			temp = true;
		else
			temp = false;

		return temp;
	}

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



	//-----------------------------------------Logic Data--------------------------------------------//
	private void loadMap_data()
	{
		map_data = initMapData(level);
	}

	/*
	*
	*Function: initialization Map 's  data
	*
	*Parameter: level - which map should be used!
	*
	*/
	private int[][][] initMapData(int le)
	{
		int[][][] t = new int[8][8][5]; 
		int[][][] xy = {
					 
					 {  {0,0} , {16,0} ,{32,0}, {48,0},{64,0},{80,0}, {96,0}, {112,0}},
					 {  {0,16}, {16,16},  {32,16},{48,16},{64,16}, {80,16},{96,16},{112,16}  } ,
					 {  {0,32},{16,32},{32,32}, {48,32},{64,32},{80,32},{96,32},{112,32} },
					 {  {0,48}, {16,48}, {32,48}, {48,48},{64,48}, {80,48}, {96,48}, {112,48}  },
					 {  {0,64},{16,64},{32,64},{48,64}, {64,64},{80,64},{96,64},{112,64} },
					 {  {0,80},{16,80},{32,80}, {48,80}, {64,80},{80,80},{96,80},{112,80}},
					 {  {0,96},{16,96}, {32,96},{48,96},{64,96},{80,96},{96,96},{112,96}},
					 {  {0,112},{16,112},{32,112},{48,112},{64,112},{80,112},{96,112},{112,112} }				
			
					 } ; 

		switch(le)
		{
			case 1:
			int[][][] temp = {
					 
					 {  {xy[0][0][0],xy[0][0][1],1,0,0} , /*wall*/ {xy[0][1][0],xy[0][1][1],1,0,0} , /*wall*/ {xy[0][2][0],xy[0][2][1],1,0,0}, /*wall*/ {xy[0][3][0],xy[0][3][1],1,0,0},/*wall*/  {xy[0][4][0],xy[0][4][1],1,0,0},/*wall*/    {xy[0][5][0],xy[0][5][1],1,0,0},/*wall*/      {xy[0][6][0],xy[0][6][1],1,0,0},/*wall*/    {xy[0][7][0],xy[0][7][1],1,0,0} /*wall*/	 },	//[0][0]~[0][7]	 tiers	
					 {  {xy[1][0][0],xy[1][0][1],1,0,0}, /*wall*/  {xy[1][1][0],xy[1][1][1],0,0,0},         {xy[1][2][0],xy[1][2][1],0,0,0},          {xy[1][3][0],xy[1][3][1],0,0,0},         {xy[1][4][0],xy[1][4][1],0,0,0},           {xy[1][5][0],xy[1][5][1],0,0,0},             {xy[1][6][0],xy[1][6][1],0,0,0},           {xy[1][7][0],xy[1][7][1],1,0,0} /*wall*/    } ,
					 {  {xy[2][0][0],xy[2][0][1],1,0,0}, /*wall*/  {xy[2][1][0],xy[2][1][1],0,0,0},         {xy[2][2][0],xy[2][2][1],0,0,0},          {xy[2][3][0],xy[2][3][1],0,0,0},          {xy[2][4][0],xy[2][4][1],0,0,0},           {xy[2][5][0],xy[2][5][1],0,0,1},/*nut org*/ {xy[2][6][0],xy[2][6][1],1,0,0},/*walll*/    {xy[2][7][0],xy[2][7][1],1,0,0}/*wall*/   },
					 {  {xy[3][0][0],xy[3][0][1],1,0,0}, /*wall*/  {xy[3][1][0],xy[3][1][1],0,0,0},         {xy[3][2][0],xy[3][2][1],1,0,0},/*wall*/  {xy[3][3][0],xy[3][3][1],0,0,0},          {xy[3][4][0],xy[3][4][1],1,0,0}, /*wall*/  {xy[3][5][0],xy[3][5][1],0,0,0},             {xy[3][6][0],xy[3][6][1],0,1,0},/*nut des*/ {xy[3][7][0],xy[3][7][1],1,0,0}  /*wall*/ },
					 {  {xy[4][0][0],xy[4][0][1],1,0,0}, /*wall*/  {xy[4][1][0],xy[4][1][1],0,0,0},         {xy[4][2][0],xy[4][2][1],1,0,0},/*wall*/  {xy[4][3][0],xy[4][3][1],0,0,0},          {xy[4][4][0],xy[4][4][1],0,0,1},/*nut org*/ {xy[4][5][0],xy[4][5][1],0,0,0},            {xy[4][6][0],xy[4][6][1],0,1,0},/*nutdes*/ {xy[4][7][0],xy[4][7][1],1,0,0} /*wall*/   },
					 {  {xy[5][0][0],xy[5][0][1],1,0,0}, /*wall*/  {xy[5][1][0],xy[5][1][1],0,0,0},         {xy[5][2][0],xy[5][2][1],1,0,0},/*wall*/  {xy[5][3][0],xy[5][3][1],0,0,0},           {xy[5][4][0],xy[5][4][1],0,0,0},           {xy[5][5][0],xy[5][5][1],0,0,1},/*nut org*/ {xy[5][6][0],xy[5][6][1],0,1,0}, /*nutdes*/{xy[5][7][0],xy[5][7][1],1,0,0} /*wall*/   },
					 {  {xy[6][0][0],xy[6][0][1],1,0,0}, /*wall*/  {xy[6][1][0],xy[6][1][1],0,0,0},         {xy[6][2][0],xy[6][2][1],0,0,0},          {xy[6][3][0],xy[6][3][1],0,0,0},           {xy[6][4][0],xy[6][4][1],0,0,0},           {xy[6][5][0],xy[6][5][1],0,0,0},             {xy[6][6][0],xy[6][6][1],0,0,0},           {xy[6][7][0],xy[6][7][1],1,0,0} /*wall*/  },
					 {  {xy[7][0][0],xy[7][0][1],1,0,0},/*wall*/   {xy[7][1][0],xy[7][1][1],1,0,0},/*wall*/ {xy[7][2][0],xy[7][2][1],1,0,0},/*wall*/ {xy[7][3][0],xy[7][3][1],1,0,0}, /*wall*/{xy[7][4][0],xy[7][4][1],1,0,0},/*wall*/  {xy[7][5][0],xy[7][5][1],1,0,0},/*wall*/    {xy[7][6][0],xy[7][6][1],1,0,0},/*wall*/ {xy[7][7][0],xy[7][7][1],1,0,0}/*wall*/   }				
			
					 } ; 

			 t = temp ;
			 splace[0] = 2;		//squirrel's coordinates   array index in temp
			 splace[1] = 6;

				break ; 
			case 2:
				break ; 
			case 3:
				break ; 
			case 4:
				break ; 
			case 5:
				break ; 
			case 6:
				break ; 
			case 7:
				break ;

		}

		return t;

		
		
	}


	//---------------------------load resource--------------------------//

	private int loadImages()
	{
		try{
		
		for(int i = 0 ; i < squirrel.length ; i ++)
		{
			squirrel[i] = Image.createImage("/img/s_"+i+".png");
			
		}

		nut[0] = Image.createImage("/img/n_0.png");
		nut[1] = Image.createImage("/img/n_1.png");
		nut[2] = Image.createImage("/img/n_2.png");
		map[0] = Image.createImage("/img/m_0.png");
		map[1] = Image.createImage("/img/m_1.png");

		title = Image.createImage("/img/tit.png");

		}catch(Exception ex){ ex.printStackTrace() ; }

		return 0 ;
	}


	//---------------------------tool methods--------------------------//

	public void clearScreen(Graphics g)
	{
		g.setColor(WHITE);
		g.fillRect(0,0,scrW,scrH);
	}


	void debug(String s)
	{
		System.out.println(s);
	}
	

}

⌨️ 快捷键说明

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