maingame.java

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

JAVA
2,475
字号
	
	 /**
     * initialize the RMS
     */
    private final void initRMS()
    {
    	int len; 
    	byte[] rec = {0,0,0,0,0,0,0,0};
     	try{
     	
     	    rs = RecordStore.openRecordStore(RMS_NAME,true);
     	    len = rs.getNumRecords();
     	    if(len == 3)
     	    {   
     	    	if(rs.getRecord(2).length <= 1)
     	     	{
     	     	    isHaveRecord = false;	
     	     	}
     	     	else
     	     	    isHaveRecord = true;
     	     }
     	     else
     	     {
     	     	 isHaveRecord = false;
     	     	 if(len > 0)
     	     	 {
     	     	     rs.closeRecordStore();
     	     	     RecordStore.deleteRecordStore(RMS_NAME);
     	     	     rs = RecordStore.openRecordStore(RMS_NAME,true);	
     	     	 }
     	     	 
     	     	 //sound
     	     	 rs.addRecord( rec,0,1);
     	     	 //stage , score
     	     	 rs.addRecord(rec,0,rec.length );
     	     	 // ranking initialize
     	     	 initRanking();
     	     }
     	     rs.closeRecordStore();
         }catch(Exception ex) {
            if (rs != null) {
                try {rs.closeRecordStore(); }
                catch (Exception e) {}
            }
            print("initRMS:" + ex.toString());
        }
     }
    
    private boolean readSound()
    {
    	byte rec[] = {0};
    	try
		{
    		rs = RecordStore.openRecordStore(RMS_NAME, false);
    		rec = rs.getRecord(1);		
    		rs.closeRecordStore();		
    	}
    	catch(Exception ex)
		{
    		if (rs != null) 
    		{
    			try
				{
    		        rs.closeRecordStore();
				}
    			catch(Exception e)
				{}
    		}
    	}
    	return (rec[0] == 0)? false:true;
    }
    
    private void saveSound()
    {
    	byte rec[] = new byte[1];
    	if(bSound)
    		rec[0] = 1;
    	else 
    		rec[0] = 0;
    	try{
    		rs = RecordStore.openRecordStore(RMS_NAME, false);
    		rs.setRecord( 1,rec,0,rec.length );
    		rs.closeRecordStore();		
    	}
    	catch(Exception ex)
		{
    		if (rs != null) 
    		{
    			try
				{
    		        rs.closeRecordStore();
				}catch(Exception e){}
    		}
    	}
    }
    
    private void initRanking(){
    	ByteArrayOutputStream bos  =new ByteArrayOutputStream();
		DataOutputStream      dos  =new DataOutputStream(bos);
		try{
			for (byte i = 0; i < MAX_RANK; i++) {
				dos.writeUTF(record_name[i]);
				dos.writeInt(record_score[i]);
			}
			bos.close();		
		}catch(Exception ex){
			System.out.println("BrickEngine saveRanking write error!!");
			ex.printStackTrace();
		}	
		byte[] _data = bos.toByteArray();
		
		try{				
			rs.addRecord(_data,0,_data.length);		
		}catch(Exception e2){
			if (rs != null) {
				try {
					rs.closeRecordStore();
				} catch (Exception e) {
				}
			}
			System.out.println("Sudoku saveRanking error!!");
			e2.printStackTrace();
		}
    }
    
    /**
     *  read A ranking from rms and Extract
     */
    private static void readRanking()
    {    
    	byte rec[] = null;
		try{
			rs = RecordStore.openRecordStore(RMS_NAME, false);
			rec = rs.getRecord(3);		
			rs.closeRecordStore();		
		}catch(Exception ex){
			if (rs != null) {
				try {
					rs.closeRecordStore();
				} catch (Exception e) {
				}
			}
		}	
		if (rec == null) {
			System.out.println("readRanking data===null!");
		}			
          
		ByteArrayInputStream bis = new ByteArrayInputStream(rec);     
		DataInputStream     dis  =new DataInputStream(bis);
		
		try {
			for (byte i = 0; i < MAX_RANK; i++) {
			    record_name[i] =dis.readUTF();
				record_score[i] =dis.readInt();
			}
			bis.close();
		} catch (Exception e) {}
		bis=null;
		System.gc();
    }
    
    public  static void BackFromUI(boolean bUpdate,int iRank,String name) {
        if (bUpdate == true)
        {   
        	String myName="";
        	int k = 0; 
        	for(int i = 0; i < name.length();i++)
        	{
        		if(font.stringWidth( name ) < NAME_LENGTH)
        		{
        			k = name.length();
        			break;
        		}
        		else if(font.stringWidth( name.substring( 0,i+1)) > NAME_LENGTH)
        		{
        			k = i;
        			break;
        		}
        	}
        	myName = name.substring( 0,k);
        	
        	sortRanking(iRank,myName);
        	saveRanking();
        	        	
            //set 
			page_pointer = 0;
			if(MAX_RANK <= LINE_A_PAGE)
			{
				hasArrow = false;
			}
			else
			{
				hasUpArrow = false;
				hasDownArrow = true;
				hasArrow = true;
			}
			readRanking();
			game_stage = STAGE_RANKING;
			last_stage = STAGE_GAMEOVER;
			Display.getDisplay( game_midlet).setCurrent( main_game );
			if(game_rank!=null)
				game_rank = null;
        }
        else
        {
        	game_stage = STAGE_TITLE;
                	
        	//#ifdef S40
        	//# loadTitleImage();
        	//#endif
        	if(bSound)
        	{
        		m_pEffect.StopSound() ;
        		m_pEffect.PlaySound("/title.mid",true);
        	}
        	CreateTitleWindow();
        	Display.getDisplay( game_midlet).setCurrent( main_game );
        	if(game_rank!=null)
				game_rank = null;
        }
    }
    
    private int GetRank(){
    	for(int i = 0; i < MAX_RANK; i++){
    		if( game_score > record_score[i] )
    		{
    			return (i+1);
    		} 	      	
    	}
    	return 0;
    }
    
    /**
     * sort the ranking
     */
   private static void sortRanking(int temp,String name){
    	    for(int j = MAX_RANK-1; j >= temp ;j--){
    	        record_name[j] = record_name[j-1];
    	        record_score[j] = record_score[j-1];	
    	    }
    	    temp--;
    	    record_name[temp] = name; //name
    	    record_score[temp] = game_score; //score
   } 
   
   /**
    *  save a ranking
    */
   private static void saveRanking(){
   	ByteArrayOutputStream bos  =new ByteArrayOutputStream();
		DataOutputStream      dos  =new DataOutputStream(bos);
		
		try{
			for (byte i = 0; i < MAX_RANK; i++) {
				dos.writeUTF(record_name[i]);
				dos.writeInt(record_score[i]);
			}
			bos.close();		
		}catch(Exception ex){
			System.out.println("BrickEngine saveRanking write error!!");
			ex.printStackTrace();
		
		}	
		byte[] _data = bos.toByteArray();

			try{				
				
				rs = RecordStore.openRecordStore(RMS_NAME,false);	
				rs.setRecord(3,_data,0,_data.length);
		    	rs.closeRecordStore();
			}catch(Exception e2){
				
				if (rs != null) {
					try {
						rs.closeRecordStore();
					} catch (Exception e) {
					}
				}
				System.out.println("Sudoku saveRanking error!!");
				e2.printStackTrace();
			}
    } 
   
   public int getInt(byte rez[],int i) {
   	int score =  (0x000000FF & rez[i])<<24 |
	(0x000000FF & rez[i+1])<<16 |
	(0x000000FF & rez[i+2])<<8  |
	(0x000000FF & rez[i+3]);
   	return score;
   }
   
   public void setInt(int score,byte rez[],int i)  {
   	rez[i] = (byte)((score&0xFF000000)>>24);
   	rez[i+1] = (byte)((score&0x00FF0000)>>16);
   	rez[i+2] = (byte)((score&0x0000FF00)>>8);
   	rez[i+3] = (byte) (score&0x000000FF);		
   }
      
	/**
	 * TODO handle the key pressing
	 */
	public final void keyPressed(int iKeyCode)  {
		
		switch (iKeyCode){
		
		//#if NOKIA || K700 || S40 || OTHER || SE || LG || SAMSUNG
		case -6:
		    m_iKey = KEY_L;  return;
		case -7:
		    m_iKey = KEY_R;  return;
		//#endif
		
		//#if E398 || MOTO
		//# case -21:
		    //# m_iKey = KEY_L;  return;
		//# case -22:
		    //# m_iKey = KEY_R;  return;
		//#endif    
				
		//#ifdef V600
	    //# case 21:
	        //# m_iKey = KEY_L;  return;
	    //# case 22:
	        //# m_iKey = KEY_R;  return;
		//#endif
		}
		
		m_iKey = mergeKey(iKeyCode);
		
		key_counter = 0;
		tempKey = KEY_NONE;
		isKeyReleased = false;
		
		//#ifdef A760 || POINTER_PRESSED
		//# //mask the key when it is in game
		//# if(game_stage == STAGE_GAME)
		//# {
		    //# if(hasPointerEvents())
		    	//# m_iKey = KEY_NONE;
		//# }	
		//#endif
	}
	
	private byte mergeKey(int keyCode)
	{
		byte key = KEY_NONE;
		int gAction = getGameAction(keyCode);
		if(keyCode == KEY_NUM2 || gAction ==UP)
			return KEY_UP;
		else if(keyCode == KEY_NUM4 || gAction == LEFT)
			return KEY_LEFT;
		else if(keyCode == KEY_NUM6 || gAction == RIGHT)
			return KEY_RIGHT;
		else if(keyCode == KEY_NUM8 || gAction == DOWN)
			return KEY_DOWN;
		else if(keyCode == KEY_NUM5 || gAction == FIRE)
			return KEY_FIRE;
		return key;
	}
	
	public final void keyReleased(int iKeyCode){
		    response_key = KEY_NONE;
		    tempKey = KEY_NONE;
		    isKeyReleased = true;
	}
	
	/**
	 *  for touch-screen
	 */
	//#ifdef  A760 || POINTER_PRESSED
	//# private int pointer_x,pointer_y;
	//# private int old_pointer;
	//# private static int[][] title_area = 
	//# {
			//# {80,MENU_Y - 1,160,MENU_Y - 1 + Common.FONT_LINE_H },
			//# {80,MENU_Y - 1 + Common.FONT_LINE_H,160,MENU_Y - 1 + (2*Common.FONT_LINE_H)},
			//# {80,MENU_Y - 1 +(2*Common.FONT_LINE_H),160,MENU_Y - 1 +(3*Common.FONT_LINE_H)}
	//# };
	//# 
	//# private static final int SYSTEM_Y = (240 - Common.FONT_LINE_H*4)>>1;
	//# private static int[][] system_area =
	//# {
			//# {80,  SYSTEM_Y,160,SYSTEM_Y+Common.FONT_LINE_H},
			//# {80,  SYSTEM_Y+Common.FONT_LINE_H,160,SYSTEM_Y+Common.FONT_LINE_H*2},
			//# {80,  SYSTEM_Y+Common.FONT_LINE_H*2,160,SYSTEM_Y+Common.FONT_LINE_H*3},
	        //# {80,  SYSTEM_Y+Common.FONT_LINE_H*3,160,SYSTEM_Y+Common.FONT_LINE_H*4}
	//# };
	//# 
	//# private static int temp_y =  240 - ((240 - SHOW_NUM * item_height)>>1);
	//# private static int[][] help_area = 
	//# {
			//# {55,0,80,19},  //left arrow 
			//# {160,0,185,19},  //right arrow   
			//# {120 - 25 ,temp_y,120,temp_y + 25},  //up arrow
			//# {120,temp_y,120 + 25,temp_y + 25}   //down arrow
	//# };
	//# 
	//# private static int temp_y1 = RANK_Y+ 2 + LINE_A_PAGE * (Common.FONT_LINE_H + 4);
	//# private static int title_rank_area[][] = 
	//# {
			//# {165,MENU_Y,165+25,MENU_Y + 25},
			//# {165,MENU_Y +(3*Common.FONT_LINE_H) - 25,165+25,MENU_Y +(3*Common.FONT_LINE_H)},
			//# {120 - 25 ,temp_y1,120,temp_y1 + 25},  //up arrow
			//# {120,temp_y1,120 + 25,temp_y1 + 25}   //down arrow
	//# };
	//# 
	//# protected void pointerPressed(int arg0, int arg1) {
		//# pointer_x = arg0;
		//# pointer_y = arg1;
		//# 
		//# if(game_stage == STAGE_GAME)
		//# {
			//# //get the position pointed
			//# int temp_x = (pointer_x - OFF_X - view_x);
			//# int temp_y = (pointer_y - OFF_Y - view_y);
			//# if(temp_x < 0 || temp_y < 0 || temp_x > tile_width*10 || temp_y > tile_height*9)
				//# return;
			//# else
			//# {
				//# m_iKey = KEY_FIRE;
				//# hand_x = temp_x / tile_width +1;
				//# hand_y = temp_y / tile_height + 1;
			//# }
		//# }
		//# else if(game_stage == STAGE_TITLE)
		//# {
			//# int current_pointer = getThePoint(pointer_x,pointer_y,title_area);
			//# if(current_pointer != -1)
			//# {
				//# if(old_pointer == current_pointer)
				//# {
					//# m_iKey = KEY_FIRE;
					//# return;
				//# }
				//# else
				//# {
					//# myWin.selectedChoice =myWin.selectedChoice + (current_pointer - old_pointer);
					//# old_pointer = current_pointer;
				//# }
			//# }
			//# else
			//# {
				//# current_pointer = getThePoint(pointer_x,pointer_y,title_rank_area);
				//# if(current_pointer == -1)
					//# return;
				//# if(current_pointer ==0)
					//# m_iKey = KEY_UP;
				//# else if(current_pointer ==1)
					//# m_iKey = KEY_DOWN;
			//# }
		//# }
		//# else if(game_stage == STAGE_SYSTEM)
		//# {
			//# int current_pointer = getThePoint(pointer_x,pointer_y,system_area);
    		//# if(current_pointer == -1)
    		   //# return;
			//# if(old_pointer == current_pointer)
			//# {
				//# m_iKey = KEY_FIRE;
				//# return;
			//# }
			//# else
			//# {
				//# myWin.selectedChoice =myWin.selectedChoice + (current_pointer - old_pointer);
    			//# old_pointer = current_pointer;
			//# }
		//# }
		//# else if(game_stage == STAGE_RANKING)
		//# {
			//# int value = getThePoint(pointer_x,pointer_y,title_rank_area);
			//# if(value == 3)
				//# m_iKey = KEY_UP;
			//# else if(value == 2)
				//# m_iKey = KEY_DOWN;
		//# }
		//# else if(game_stage == STAGE_LEVEL)
		//# {
			//# m_iKey = KEY_UP;
		//# }
		//# else if(game_stage == STAGE_HELP || game_stage == STAGE_ABOUT)
		//# {   
			//# int value = getThePoint(pointer_x,pointer_y,help_area);
			//# if(value == 2)
				//# m_iKey = KEY_UP;
			//# else if(value == 3)
				//# m_iKey = KEY_DOWN;
			//# else if(value == 0)
				//# m_iKey = KEY_LEFT;
			//# else if(value == 1)
				//# m_iKey = KEY_RIGHT;
		//# }
	//# }
	//# 
	//# protected void pointerReleased(int arg0, int arg1) {
	//# }

⌨️ 快捷键说明

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