Advertisement
Guest User

followme

a guest
Mar 14th, 2011
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. package jrcode.followme;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.media.MediaPlayer;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.Menu;
  10. import android.view.MenuInflater;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. public class FollowMe extends Activity implements OnClickListener {
  19. /** Called when the activity is first created. */
  20.  
  21. Button UpLeftButton, UpRightButton, DownLeftButton, DownRightButton;
  22. Button StartButton, CheckButton, RepeatButton, ClearButton;
  23. TextView ScoreTextView, LevelTextView, InputTextView;
  24. MediaPlayer myMediaPlayer;
  25.  
  26. int Music[], Input[];
  27. int level, score, stage;
  28.  
  29. @Override
  30. public void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33.  
  34. ScoreTextView = (TextView)findViewById(R.id.Tv_score);
  35. LevelTextView = (TextView)findViewById(R.id.Tv_level);
  36. InputTextView = (TextView)findViewById(R.id.Tv_input);
  37.  
  38. UpLeftButton = (Button)findViewById(R.id.upleft);
  39. UpRightButton = (Button)findViewById(R.id.upright);
  40. DownLeftButton = (Button)findViewById(R.id.downleft);
  41. DownRightButton = (Button)findViewById(R.id.downright);
  42.  
  43. StartButton = (Button)findViewById(R.id.start);
  44. CheckButton = (Button)findViewById(R.id.check);
  45. RepeatButton = (Button)findViewById(R.id.repeat);
  46. ClearButton = (Button)findViewById(R.id.clear);
  47.  
  48. StartButton.setOnClickListener(Button_Listener);
  49. CheckButton.setOnClickListener(Button_Listener);
  50. RepeatButton.setOnClickListener(Button_Listener);
  51. ClearButton.setOnClickListener(Button_Listener);
  52. UpLeftButton.setOnClickListener(this);
  53. UpRightButton.setOnClickListener(this);
  54. DownLeftButton.setOnClickListener(this);
  55. DownRightButton.setOnClickListener(this);
  56.  
  57. Initiparameter();
  58. }
  59.  
  60. public void UpdateScoreLevel(int gain) // 更新 關卡 分數 觸控的次數
  61. {
  62. score += gain;
  63. ScoreTextView.setText(String.valueOf(score));
  64. LevelTextView.setText(String.valueOf(level));
  65. InputTextView.setText(String.valueOf(stage));
  66. }
  67.  
  68. public void Initiparameter() // 初始化所有參數
  69. {
  70. Music = new int[300]; // 初始共有 300 個關卡
  71. Input = new int[300]; // 初始共可以按 300 下
  72. level = 1; // 初始從第一關開始
  73. score = 0; // 初始分數為 0 分
  74. stage = 0; // 初始都沒被 Touch 過
  75. myMediaPlayer = MediaPlayer.create(this, R.raw.piano_do); // 初始播放器為沒有音樂播
  76. ScoreTextView.setText(String.valueOf(score));
  77. LevelTextView.setText(String.valueOf(level));
  78. InputTextView.setText(String.valueOf(stage));
  79. }
  80.  
  81. public void RandMusic() // 亂數產生音階順序
  82. {
  83. for (int i=0 ; i<level ; i++)
  84. Music[i] = (int)Math.ceil((Math.random()*4));
  85. }
  86.  
  87. public void PlayMusic() // 播放音階音樂
  88. {
  89. MediaPlayer play = null;
  90. for (int i=0 ; i<level ; i++)
  91. {
  92. if (Music[i] == 0) // 當 Music[i] 裡沒有音階時不要播放
  93. return;
  94. else if (Music[i] == 1)
  95. {
  96. UpLeftButton.setBackgroundColor(0x99f0f000);
  97. play = MediaPlayer.create(this, R.raw.piano_do);
  98. }
  99. else if (Music[i] == 2)
  100. {
  101. UpRightButton.setBackgroundColor(0x9900f000);
  102. play = MediaPlayer.create(this, R.raw.piano_re);
  103. }
  104. else if (Music[i] == 3)
  105. {
  106. DownLeftButton.setBackgroundColor(0x990000f0);
  107. play = MediaPlayer.create(this, R.raw.piano_mi);
  108. }
  109. else if (Music[i] == 4)
  110. {
  111. DownRightButton.setBackgroundColor(0x99f00000);
  112. play = MediaPlayer.create(this, R.raw.piano_fa);
  113. }
  114. play.start(); // 播放音樂
  115. try
  116. {
  117. Thread.sleep(1000); // 讓時間 waiting 1 秒中
  118. }
  119. catch (InterruptedException e)
  120. {
  121. Log.i("Info", e.toString());
  122. }
  123. play.release(); // 釋放掉存在memory裡的檔案
  124. SetBackground();
  125. }
  126. }
  127.  
  128. public void Match() // 與 Answer 比對
  129. {
  130. if (level != stage) // 判斷按的次數是否有符合這麼多
  131. {
  132. Toast.makeText(this, "Not match step!!", Toast.LENGTH_SHORT).show();
  133. return;
  134. }
  135. int gain = level; // 這場所獲得的分數
  136. for (int i=0 ; i<level ; i++) // 比對 User 按的跟 Music 是否有 match
  137. if (Music[i] != Input[i]) // 若不符合就扣分
  138. gain--;
  139. if (level > gain) // 分數少於此關關卡,表示輸了這關,要重新開始
  140. {
  141. gain = 0; // 沒過關為 0 分
  142. Toast.makeText(this, "Wrong answer !! Please retry again...", Toast.LENGTH_SHORT).show();
  143. }
  144. else // 表示過關,可以進入下一關
  145. {
  146. for (int i=0 ; i<level ; i++) // Release 掉原本的音,不 Release 掉會發生 Check 過關後按 Repeat 會發生 crash 的狀況
  147. Music[i] = 0; // 會 crash 是因為進入 next level 按 Repeat 會有 MediaPlayer 沒有音可播,但又要 play 而造成
  148. Toast.makeText(this, "Right , Go next level", Toast.LENGTH_SHORT).show();
  149. level++; // 進入下一個 level
  150. }
  151. Clear(); // 刷掉按過的記錄
  152. UpdateScoreLevel(gain); // 更新關卡及分數
  153. }
  154.  
  155. public void Clear() // 刷掉按過的記錄
  156. {
  157. for (int i=0 ; i < stage ; i++) // 把之前按過的記錄都清成 0
  158. Input[i] = 0;
  159. stage = 0;
  160. }
  161.  
  162. public void SetBackground() // 設定回原來的按鈕顏色
  163. {
  164. UpLeftButton.setBackgroundColor(0xfff0f000);
  165. UpRightButton.setBackgroundColor(0xff00f000);
  166. DownLeftButton.setBackgroundColor(0xff0000f0);
  167. DownRightButton.setBackgroundColor(0xfff00000);
  168. }
  169.  
  170. private OnClickListener Button_Listener = new OnClickListener() // Start Check Repeat Stop Button 的功能 Listener
  171. {
  172. public void onClick(View v)
  173. {
  174. SetBackground(); // 設定 Do Re Mi Fa 回原色
  175. switch (v.getId())
  176. {
  177. case R.id.start:
  178. RandMusic(); // 亂數產生音階
  179. PlayMusic(); // 播放音樂
  180. break;
  181. case R.id.check:
  182. Match(); // 與 Answer 比對看有無錯誤
  183. break;
  184. case R.id.repeat:
  185. PlayMusic(); // 播放音樂
  186. break;
  187. case R.id.clear:
  188. Clear(); // 刷掉按過的記錄
  189. UpdateScoreLevel(0); // 更新 Touch 次數顯示
  190. Toast.makeText(FollowMe.this, "Clear the touch...", Toast.LENGTH_SHORT).show();
  191. break;
  192. }
  193. }
  194. };
  195.  
  196. public void onClick(View v) // 點擊 Do Re Mi Fa 的 Button 所回應的程式碼
  197. {
  198. myMediaPlayer.release(); // 釋放掉存在memory裡的檔案
  199. SetBackground(); // 設定 Do Re Mi Fa 回原色
  200.  
  201. switch (v.getId())
  202. {
  203. case R.id.upleft:
  204. UpLeftButton.setBackgroundColor(0x99f0f000);
  205. Input[stage] = 1;
  206. myMediaPlayer = MediaPlayer.create(this, R.raw.piano_do);
  207. break;
  208. case R.id.upright:
  209. UpRightButton.setBackgroundColor(0x9900f000);
  210. Input[stage] = 2;
  211. myMediaPlayer = MediaPlayer.create(this, R.raw.piano_re);
  212. break;
  213. case R.id.downleft:
  214. DownLeftButton.setBackgroundColor(0x990000f0);
  215. Input[stage] = 3;
  216. myMediaPlayer = MediaPlayer.create(this, R.raw.piano_mi);
  217. break;
  218. case R.id.downright:
  219. DownRightButton.setBackgroundColor(0x99f00000);
  220. Input[stage] = 4;
  221. myMediaPlayer = MediaPlayer.create(this, R.raw.piano_fa);
  222. break;
  223. }
  224. myMediaPlayer.start();
  225. stage++; // 步驟數加一次
  226. UpdateScoreLevel(0);
  227. }
  228.  
  229. @Override
  230. public boolean onCreateOptionsMenu(Menu menu) // 建立選單 Menu
  231. {
  232. MenuInflater inflater = getMenuInflater();
  233. inflater.inflate(R.menu.menu, menu);
  234. return true;
  235. }
  236.  
  237. public boolean onOptionsItemSelected(MenuItem item) // 設定選單 Menu 的動作
  238. {
  239. switch(item.getItemId())
  240. {
  241. case R.id.author:
  242. AlertDialog.Builder ad = new AlertDialog.Builder(this); // 宣告一個彈出視窗
  243. ad.setTitle("About the Program");
  244. ad.setMessage("Author : JR Jiang");
  245. DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() // 彈出視窗的 Listener
  246. {
  247. public void onClick(DialogInterface dialog, int i) // 當按下時要執行的 Code
  248. {}
  249. };
  250. ad.setPositiveButton("OK", listener); // 建立 Listener 去監聽 , 幫對話框加上一個按鈕
  251. ad.show(); // 把對話框顯示出來
  252. break;
  253. case R.id.restart:
  254. Toast.makeText(this, "Already Start 1 Level", Toast.LENGTH_LONG).show();
  255. break;
  256. case R.id.quit:
  257. finish(); // 結束 Activity 這個頁面
  258. break;
  259. }
  260. return super.onOptionsItemSelected(item);
  261. }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement