maingame.java

来自「j2me 连连看源代码」· Java 代码 · 共 2,475 行 · 第 1/5 页

JAVA
2,475
字号
	//# 
	//# private int getThePoint(int hit_x ,int hit_y,int[][] area)
	//# {
		//# int iLength = area.length;
		//# int[][] temp_area = area;
		//# for(int i = 0; i < iLength; i++)
		//# {
			//# if(hit_x >= temp_area[i][0] && hit_x <= temp_area[i][2]
			 //# && hit_y >= temp_area[i][1] && hit_y <= temp_area[i][3])
			//# {
				//# return i;
			//# }	 
		//# }
		//# return -1;	
	//# }
	//#endif
	
    /**
	 * handle the movement of the hand in the game
	 * @param key
	 */
	private void keyAction(int key)
	{
		if(key == KEY_NONE)
			return;
		if(key == KEY_LEFT)
		{
			hand_x--;
			if(hand_x < 1)
				hand_x = line_count - 2;
		}
		else if(key == KEY_RIGHT)
		{
			hand_x++;
			if(hand_x > line_count - 2)
				hand_x = 1;
		}
		else if(key == KEY_UP)
		{
			hand_y--;
			if(hand_y < 1)
				hand_y = row_count - 2;
		}
		else if(key == KEY_DOWN)
		{
			hand_y++;
			if(hand_y > row_count - 2)
				hand_y = 1;
		}
	}
	
	/**
	 * TODO: process the logic of the game's loop 
	 */
	public final void run()
	{
		
		long start_time ,taken_time;
		
		while(game_stage != STAGE_EXIT)
		{
			if(!thread_flag)
			{
				start_time = System.currentTimeMillis();
				
				//this method process the main logic of the game
				switch(game_stage)
				{  
				
				case STAGE_LOADING:
					flow_loading();
					try{
						repaint();
						//#if !NOKIA
						serviceRepaints();
						//#endif
						Thread.sleep(20);
					}catch(Exception e){}
					continue;
				case STAGE_SETTING:
					flow_setting();
					break;
				case STAGE_PAY:
					flow_pay();
					break;
				case STAGE_TITLE:
					flow_title();
					break;
				case STAGE_INIT:
					flow_init();
					break;
				case STAGE_GAME:
					response_key = m_iKey;
					m_iKey = KEY_NONE;
					flow_game();
					break;
				case STAGE_HELP:
					flow_help();
					break;
				case STAGE_ABOUT:
					flow_about();
					break;
				case STAGE_RANKING:
					flow_ranking();
					break;
				case STAGE_GAMEOVER:
					flow_gameover();
					break;
				case STAGE_LEVEL:
					flow_level();
					break;	
				case STAGE_SYSTEM:
					flow_system();
					break;
				}
				taken_time = System.currentTimeMillis() - start_time;
				//here we show the images
				repaint();
				
				//#if !NOKIA
				serviceRepaints();
				//#endif
				//=============================================//
				// test the interval of the loop here
				// System.out.
				//=============================================//
				
				// let the thread have a short sleep, 
				// maybe it is good for the game to 
				// response to the key hittings;
				try
				{
					Thread.sleep( 5L );
				}
				catch(InterruptedException e)
				{
					
				}
				
				//make the thread sleep the left time 		    
				if(taken_time < TIME_PER_TICKER){
					try
					{
						Thread.sleep( TIME_PER_TICKER - taken_time);
					}
					catch(InterruptedException e)
					{
						
					}
				}
			}
		}
		// clear the thread
		stop();
		// exit the game
		game_midlet.exitGame();
	}
	
	//锟侥憋拷锟皆讹拷锟斤拷锟斤拷
	public Vector getSubsection(String strSource,int width,String strSplit){
	     Vector vector = new Vector();
	     String temp=strSource;
	     int i,j;
	     int LastLength = 1;
	     int step = 0;
	     try{
	         while (!temp.equals("")) {
	           i=temp.indexOf("\n");
	           if(i>0){
	             if(font.stringWidth(temp.substring(0,i-1)) >= width){
	               i = -1;
	             }
	           }
	           if(i==-1){
	             if(LastLength>temp.length()){
	               i = temp.length();
	             }else{
	               i = LastLength;
	               step = font.stringWidth(temp.substring(0, i)) > width ? -1 : 1;
	               if(i<temp.length()){
	                 while (! (font.stringWidth(temp.substring(0, i)) <= width
	                           && font.stringWidth(temp.substring(0, i + 1)) > width)) {
	                   i = i + step;
	                   if (i == temp.length())
	                     break;
	                 }
	               }
	             }
	             if(!strSplit.equals("")){
	               j = i;
	               if (i < temp.length()) {
	                 while (strSplit.indexOf(temp.substring(i-1,i))==-1) {
	                   i--;
	                   if (i == 0) {
	                     i = j;
	                     break;
	                   }
	                 }
	               }
	             }
	           }
	           LastLength = i;
	           vector.addElement(temp.substring(0, i));
	           if (i == temp.length()) {
	             temp = "";
	           }
	           else{
	             temp = temp.substring(i);
	             if (temp.substring(0, 1).equals("\n")) {
	               temp = temp.substring(1);
	             }
	           }
	         }
	     }catch(Exception e)
	     {
	       System.out.println("getSubsection:"+e);
	     }
	     return vector;
	  }
	
	private final void flow_pay(){
		if(m_iKey == KEY_L){
			MsgPay msgPay = new MsgPay(GameMIDlet.instance);
			byte value = msgPay.payOneYuan(payType);	
			if(value == MsgPay.SEND_SUCCESS){
				errorTime = 0;
				nowTime++;
				Rms.savePayRecord(payType,nowTime);
				if(nowTime>=payTime){
					game_stage = tmpState;
					if(payType == MsgPay.PM_PERLEVEL){
						Rms.savePayRecord(MsgPay.PM_THING_SKILL, 2);
					}
				}else{
					Vector v = getSubsection("您总共发送成功了"+nowTime+"次,还需发送"+(payTime-nowTime)+"条短信(每条"+money+"元,不含通信费)", screen_width - 20,"");
					payMsg = new String[v.size()];
					v.copyInto(payMsg);
				}
			}else{
				errorTime++;
				Vector v = null;
				if(errorTime % 3 == 0){							
					v = getSubsection(MsgPay.strPayFailMore, screen_width - 6,"");							
				}else{
					v = getSubsection("发送失败"+(errorTime)+"次,还需发送"+(payTime-nowTime)+"条短信(每条"+money+"元),是否再试试?", screen_width - 20,"");								
				}
				payMsg = new String[v.size()];
				v.copyInto(payMsg);
			}
		}else if(m_iKey == KEY_R){
			if(tmpState == STAGE_LEVEL){
				game_stage = STAGE_EXIT;
			}else{
				//#if COMMAND
				//# AddCommand(tmpState);
				//#endif
				game_stage = tmpState;
			}
		}
		m_iKey = KEY_NONE;
	}
	
	private final void flow_system()
	{
		if(m_iKey != KEY_NONE)
		{
			if(m_iKey == KEY_UP)
			{
				myWin.keyUp();
                //#ifdef A760 || POINTER_PRESSED
				//# if(hasPointerEvents())
				//# {
					//# old_pointer--;
					//# if(myWin.selectedChoice == 3)
						//# old_pointer = 3;
					//# else 
					//# {
						//# if(old_pointer < 0)
							//# old_pointer = 0;
					//# }
				//# }
				//#endif
			}
			else if(m_iKey == KEY_DOWN)
			{
				myWin.keyDown();
				
                //#ifdef A760 || POINTER_PRESSED
				//# if(hasPointerEvents())
				//# {
					//# old_pointer++;
					//# if(myWin.selectedChoice == 0)
						//# old_pointer = 0;
					//# else 
					//# {
						//# if(old_pointer > 3)
							//# old_pointer = 3;
					//# }
				//# }
				//#endif
			}

			else if(m_iKey == KEY_FIRE || m_iKey == KEY_L)
			{
				int option = myWin.selectedChoice;
				// back to the game
				if(option == 0)
				{
					//#ifdef COMMAND
                    //# AddCommand(STAGE_GAME);
					//#endif
                    
					game_stage = STAGE_GAME;
					deleteWindow();
					m_pEffect.StopSound() ;
				}
				//sound
				else if(option ==1)
				{
					bSound = !bSound;
					if(bSound)
					{
						Text.system[1] = Text.on_off[0];
						m_pEffect.PlaySound("/title.mid",true );
					}
					else
					{
						Text.system[1] = Text.on_off [1];
						m_pEffect.StopSound() ;
					}
					saveSound();
				}
				// to the help
				else if(option == 2)
				{
					if(help_ui != null)
						help_ui = null;
					help_ui = Common.splitString(Text.rule_string,font,screen_width - 30 );
					initWindow(help_ui,SHOW_NUM);
					help_pointer = 0;
					deleteWindow();
                    //#ifdef COMMAND
                    //# AddCommand(STAGE_HELP);
					//#endif
					
					last_stage = STAGE_SYSTEM;
					game_stage = STAGE_HELP;
				}
				// to the main menu
				else if(option == 3)
				{
					//#ifdef S40
					//# loadTitleImage();
					//#endif
					
					m_pImage[GAME] = null;
					System.gc();
					
					deleteWindow();
					CreateTitleWindow();
					if(bSound)
					{
						m_pEffect.StopSound() ;
						m_pEffect.PlaySound( "/title.mid",true);
					}
                    //#ifdef COMMAND
                    //# AddCommand(STAGE_TITLE);
					//#endif
					game_stage = STAGE_TITLE;
				}
			}
			m_iKey = KEY_NONE;
		}
	}
	
	/**
	 * show the score of the plyer ,and then go into next level
	 */
	private final void flow_level()
	{
		wait_counter++;
		if(m_iKey != KEY_NONE)
		{
			//#ifdef COMMAND
			//# AddCommand(STAGE_GAME);
			//#endif
			
			wait_counter = 0;
			game_level++;
			setGameLevel(game_level);
			//calculate the game_score
			game_score += combo_score;
//			saveScoreAndLevel();
//			byte scoreAndLevel[] = new byte[8];
//			scoreAndLevel = readScoreAndLevel();
			newgame();
//            if(!isHaveRecord)
//            {
//            	isHaveRecord = true;
//            	menu_ui = Text.menu_item;
//				bSound = readSound();
//				if(bSound)
//					menu_ui[3] = Text.on_off[0];
//				else
//					menu_ui[3] = Text.on_off [1];
//            }
			game_stage = STAGE_GAME;
		}
	}
	
	/**
	 * wait for a minute in this stage ,then go the title
	 */
	private final void flow_gameover()
	{
		wait_counter++;
		if(wait_counter > 40)
		{
			wait_counter = 0;
			m_iKey = KEY_NONE;
			boolean isNewRecord = false;
			for(int i = 0 ; i< MAX_RANK; i++)
	        {               	           
	            if( game_score > record_score[i])
	            {
	                isNewRecord = true;
	                break; 
	            }    	
	        }
						
			if(isNewRecord)
			{
                //#ifdef COMMAND
				//# AddCommand(STAGE_RANKING);
				//#endif
			    game_rank = new pRank();
			    game_score += combo_score;
			    game_rank.UpdateUI( GetRank(),"",game_score);
			    game_stage = STAGE_NONE;
			    Display.getDisplay(game_midlet).setCurrent(game_rank);
			}
			else
			{
				//#ifdef S40
				//# loadTitleImage();
				//#endif
				CreateTitleWindow();
				if(bSound)
				{
					m_pEffect.StopSound() ;
					m_pEffect.PlaySound("/title.mid",true);
				}
                //#ifdef COMMAND
				//# AddCommand(STAGE_TITLE);
				//#endif				
				game_stage = STAGE_TITLE;
			}
		}
	}
	
	/**
	 * when enter the ranking stage , the keypressed event is processed here
	 */
	//variable for ranking
	//items showed in a page
	private static int page_pointer;
	private final void flow_ranking()
	{
		if(m_iKey != KEY_NONE)
		{
			if(m_iKey == KEY_UP)
			{
				page_pointer--;
				hasDownArrow = true;
				if(page_pointer<=0)
				{
					hasUpArrow = false;
					page_pointer = 0;
				}
			}
			else if(m_iKey == KEY_DOWN)
			{
				page_pointer++;
				hasUpArrow = true;
				if(page_pointer >= MAX_RANK - LINE_A_PAGE)
				{
					hasDownArrow = false;
					page_pointer = MAX_RANK - LINE_A_PAGE;
				}
			}
            if(m_iKey == KEY_L)
			{
            	
				CreateTitleWindow();
				if(last_stage == STAGE_GAMEOVER)

⌨️ 快捷键说明

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