顯示廣告
隱藏 ✕
看板 TL
作者 TL (踢欸樓)
標題 [筆記] Alertdialog
時間 2013年01月20日 Sun. AM 09:38:22


Modify Cannon Game App so that it can have three modes (easy, medium, and hard) to play.

 public void newEasyGame()
   {
      // set every element of hitStates to false--restores target pieces
      for (int i = 0; i < TARGET_PIECES; ++i)
         hitStates[i] = false;

      targetPiecesHit = 0; // no target pieces have been hit
      blockerVelocity = initialBlockerVelocity; // set initial velocity
      targetVelocity = initialTargetVelocity; // set initial velocity
      timeLeft = 20; // start the countdown at 10 seconds
      cannonballOnScreen = false; // the cannonball is not on the screen
      shotsFired = 0; // set the initial number of shots fired
      totalElapsedTime = 0.0; // set the time elapsed to zero
      blocker.start.set(blockerDistance, blockerBeginning);
      blocker.end.set(blockerDistance, blockerEnd);
      target.start.set(targetDistance, targetBeginning);
      target.end.set(targetDistance, targetEnd);
     
      if (gameOver)
      {
         gameOver = false; // the game is not over
         cannonThread = new CannonThread(getHolder());
         cannonThread.start();
      } // end if
   } // end method newEasyGame
   public void newMediumGame()
   {
      // set every element of hitStates to false--restores target pieces
      for (int i = 0; i < TARGET_PIECES; ++i)
         hitStates[i] = false;

      targetPiecesHit = 0; // no target pieces have been hit
      blockerVelocity = initialBlockerVelocity; // set initial velocity
      targetVelocity = initialTargetVelocity; // set initial velocity
      timeLeft = 10; // start the countdown at 10 seconds
      cannonballOnScreen = false; // the cannonball is not on the screen
      shotsFired = 0; // set the initial number of shots fired
      totalElapsedTime = 0.0; // set the time elapsed to zero
      blocker.start.set(blockerDistance, blockerBeginning);
      blocker.end.set(blockerDistance, blockerEnd);
      target.start.set(targetDistance, targetBeginning);
      target.end.set(targetDistance, targetEnd);
     
      if (gameOver)
      {
         gameOver = false; // the game is not over
         cannonThread = new CannonThread(getHolder());
         cannonThread.start();
      } // end if
   } // end method newMediumGame
   public void newHardGame()
   {
      // set every element of hitStates to false--restores target pieces
      for (int i = 0; i < TARGET_PIECES; ++i)
         hitStates[i] = false;

      targetPiecesHit = 0; // no target pieces have been hit
      blockerVelocity = initialBlockerVelocity; // set initial velocity
      targetVelocity = initialTargetVelocity; // set initial velocity
      timeLeft = 5; // start the countdown at 10 seconds
      cannonballOnScreen = false; // the cannonball is not on the screen
      shotsFired = 0; // set the initial number of shots fired
      totalElapsedTime = 0.0; // set the time elapsed to zero
      blocker.start.set(blockerDistance, blockerBeginning);
      blocker.end.set(blockerDistance, blockerEnd);
      target.start.set(targetDistance, targetBeginning);
      target.end.set(targetDistance, targetEnd);
     
      if (gameOver)
      {
         gameOver = false; // the game is not over
         cannonThread = new CannonThread(getHolder());
         cannonThread.start();
      } // end if
   } // end method newHardGame




private void showGameOverDialog(int messageId)
   {
      // create a dialog displaying the given String
      final AlertDialog.Builder dialogBuilder = 
         new AlertDialog.Builder(getContext());
      dialogBuilder.setTitle(getResources().getString(messageId));
      dialogBuilder.setCancelable(false);

      // display number of shots fired and total time elapsed
      dialogBuilder.setMessage(getResources().getString(
         R.string.results_format, shotsFired, totalElapsedTime));
      dialogBuilder.setPositiveButton(R.string.reset_easy_game,
         new DialogInterface.OnClickListener()
         {
            // called when "Reset Game" Button is pressed
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
               dialogIsDisplayed = false;
               newEasyGame(); // set up and start a new game
            } // end method onClick
         } // end anonymous inner class
      );
      dialogBuilder.setNeutralButton(R.string.reset_medium_game,
   
	
         
new DialogInterface.OnClickListener()
   
	
         
{
   
	
           
@Override
   
	
           
public void onClick(DialogInterface dialog, int which)
   
	
           
{
   
	
               
dialogIsDisplayed = false;
   
	
               
newMediumGame(); // set up and start a new game
   
	
           
} // end method onClick
   
	
         
}
      );
      dialogBuilder.setNegativeButton(R.string.reset_hard_game,
   
	
         
new DialogInterface.OnClickListener()
   
	
         
{
   
	
           
// called when "Reset Game" Button is pressed
   
	
           
@Override
   
	
           
public void onClick(DialogInterface dialog, int which)
   
	
           
{
   
	
               
dialogIsDisplayed = false;
   
	
               
newHardGame(); // set up and start a new game
   
	
           
} // end method onClick
   
	
         
} // end anonymous inner class
      ); 
--
Shaken, Not Stirred.
--
※ 作者: TL 時間: 2013-01-20 09:38:22
※ 編輯: TL 時間: 2013-01-24 16:53:33
※ 看板: TL 文章推薦值: 0 目前人氣: 0 累積人氣: 39 
分享網址: 複製 已複製
guest
x)推文 r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇