Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jrcode.followme;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class FollowMe extends Activity implements OnClickListener {
- /** Called when the activity is first created. */
- Button UpLeftButton, UpRightButton, DownLeftButton, DownRightButton;
- Button StartButton, CheckButton, RepeatButton, ClearButton;
- TextView ScoreTextView, LevelTextView, InputTextView;
- MediaPlayer myMediaPlayer;
- int Music[], Input[];
- int level, score, stage;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ScoreTextView = (TextView)findViewById(R.id.Tv_score);
- LevelTextView = (TextView)findViewById(R.id.Tv_level);
- InputTextView = (TextView)findViewById(R.id.Tv_input);
- UpLeftButton = (Button)findViewById(R.id.upleft);
- UpRightButton = (Button)findViewById(R.id.upright);
- DownLeftButton = (Button)findViewById(R.id.downleft);
- DownRightButton = (Button)findViewById(R.id.downright);
- StartButton = (Button)findViewById(R.id.start);
- CheckButton = (Button)findViewById(R.id.check);
- RepeatButton = (Button)findViewById(R.id.repeat);
- ClearButton = (Button)findViewById(R.id.clear);
- StartButton.setOnClickListener(Button_Listener);
- CheckButton.setOnClickListener(Button_Listener);
- RepeatButton.setOnClickListener(Button_Listener);
- ClearButton.setOnClickListener(Button_Listener);
- UpLeftButton.setOnClickListener(this);
- UpRightButton.setOnClickListener(this);
- DownLeftButton.setOnClickListener(this);
- DownRightButton.setOnClickListener(this);
- Initiparameter();
- }
- public void UpdateScoreLevel(int gain) // 更新 關卡 分數 觸控的次數
- {
- score += gain;
- ScoreTextView.setText(String.valueOf(score));
- LevelTextView.setText(String.valueOf(level));
- InputTextView.setText(String.valueOf(stage));
- }
- public void Initiparameter() // 初始化所有參數
- {
- Music = new int[300]; // 初始共有 300 個關卡
- Input = new int[300]; // 初始共可以按 300 下
- level = 1; // 初始從第一關開始
- score = 0; // 初始分數為 0 分
- stage = 0; // 初始都沒被 Touch 過
- myMediaPlayer = MediaPlayer.create(this, R.raw.piano_do); // 初始播放器為沒有音樂播
- ScoreTextView.setText(String.valueOf(score));
- LevelTextView.setText(String.valueOf(level));
- InputTextView.setText(String.valueOf(stage));
- }
- public void RandMusic() // 亂數產生音階順序
- {
- for (int i=0 ; i<level ; i++)
- Music[i] = (int)Math.ceil((Math.random()*4));
- }
- public void PlayMusic() // 播放音階音樂
- {
- MediaPlayer play = null;
- for (int i=0 ; i<level ; i++)
- {
- if (Music[i] == 0) // 當 Music[i] 裡沒有音階時不要播放
- return;
- else if (Music[i] == 1)
- {
- UpLeftButton.setBackgroundColor(0x99f0f000);
- play = MediaPlayer.create(this, R.raw.piano_do);
- }
- else if (Music[i] == 2)
- {
- UpRightButton.setBackgroundColor(0x9900f000);
- play = MediaPlayer.create(this, R.raw.piano_re);
- }
- else if (Music[i] == 3)
- {
- DownLeftButton.setBackgroundColor(0x990000f0);
- play = MediaPlayer.create(this, R.raw.piano_mi);
- }
- else if (Music[i] == 4)
- {
- DownRightButton.setBackgroundColor(0x99f00000);
- play = MediaPlayer.create(this, R.raw.piano_fa);
- }
- play.start(); // 播放音樂
- try
- {
- Thread.sleep(1000); // 讓時間 waiting 1 秒中
- }
- catch (InterruptedException e)
- {
- Log.i("Info", e.toString());
- }
- play.release(); // 釋放掉存在memory裡的檔案
- SetBackground();
- }
- }
- public void Match() // 與 Answer 比對
- {
- if (level != stage) // 判斷按的次數是否有符合這麼多
- {
- Toast.makeText(this, "Not match step!!", Toast.LENGTH_SHORT).show();
- return;
- }
- int gain = level; // 這場所獲得的分數
- for (int i=0 ; i<level ; i++) // 比對 User 按的跟 Music 是否有 match
- if (Music[i] != Input[i]) // 若不符合就扣分
- gain--;
- if (level > gain) // 分數少於此關關卡,表示輸了這關,要重新開始
- {
- gain = 0; // 沒過關為 0 分
- Toast.makeText(this, "Wrong answer !! Please retry again...", Toast.LENGTH_SHORT).show();
- }
- else // 表示過關,可以進入下一關
- {
- for (int i=0 ; i<level ; i++) // Release 掉原本的音,不 Release 掉會發生 Check 過關後按 Repeat 會發生 crash 的狀況
- Music[i] = 0; // 會 crash 是因為進入 next level 按 Repeat 會有 MediaPlayer 沒有音可播,但又要 play 而造成
- Toast.makeText(this, "Right , Go next level", Toast.LENGTH_SHORT).show();
- level++; // 進入下一個 level
- }
- Clear(); // 刷掉按過的記錄
- UpdateScoreLevel(gain); // 更新關卡及分數
- }
- public void Clear() // 刷掉按過的記錄
- {
- for (int i=0 ; i < stage ; i++) // 把之前按過的記錄都清成 0
- Input[i] = 0;
- stage = 0;
- }
- public void SetBackground() // 設定回原來的按鈕顏色
- {
- UpLeftButton.setBackgroundColor(0xfff0f000);
- UpRightButton.setBackgroundColor(0xff00f000);
- DownLeftButton.setBackgroundColor(0xff0000f0);
- DownRightButton.setBackgroundColor(0xfff00000);
- }
- private OnClickListener Button_Listener = new OnClickListener() // Start Check Repeat Stop Button 的功能 Listener
- {
- public void onClick(View v)
- {
- SetBackground(); // 設定 Do Re Mi Fa 回原色
- switch (v.getId())
- {
- case R.id.start:
- RandMusic(); // 亂數產生音階
- PlayMusic(); // 播放音樂
- break;
- case R.id.check:
- Match(); // 與 Answer 比對看有無錯誤
- break;
- case R.id.repeat:
- PlayMusic(); // 播放音樂
- break;
- case R.id.clear:
- Clear(); // 刷掉按過的記錄
- UpdateScoreLevel(0); // 更新 Touch 次數顯示
- Toast.makeText(FollowMe.this, "Clear the touch...", Toast.LENGTH_SHORT).show();
- break;
- }
- }
- };
- public void onClick(View v) // 點擊 Do Re Mi Fa 的 Button 所回應的程式碼
- {
- myMediaPlayer.release(); // 釋放掉存在memory裡的檔案
- SetBackground(); // 設定 Do Re Mi Fa 回原色
- switch (v.getId())
- {
- case R.id.upleft:
- UpLeftButton.setBackgroundColor(0x99f0f000);
- Input[stage] = 1;
- myMediaPlayer = MediaPlayer.create(this, R.raw.piano_do);
- break;
- case R.id.upright:
- UpRightButton.setBackgroundColor(0x9900f000);
- Input[stage] = 2;
- myMediaPlayer = MediaPlayer.create(this, R.raw.piano_re);
- break;
- case R.id.downleft:
- DownLeftButton.setBackgroundColor(0x990000f0);
- Input[stage] = 3;
- myMediaPlayer = MediaPlayer.create(this, R.raw.piano_mi);
- break;
- case R.id.downright:
- DownRightButton.setBackgroundColor(0x99f00000);
- Input[stage] = 4;
- myMediaPlayer = MediaPlayer.create(this, R.raw.piano_fa);
- break;
- }
- myMediaPlayer.start();
- stage++; // 步驟數加一次
- UpdateScoreLevel(0);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) // 建立選單 Menu
- {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu, menu);
- return true;
- }
- public boolean onOptionsItemSelected(MenuItem item) // 設定選單 Menu 的動作
- {
- switch(item.getItemId())
- {
- case R.id.author:
- AlertDialog.Builder ad = new AlertDialog.Builder(this); // 宣告一個彈出視窗
- ad.setTitle("About the Program");
- ad.setMessage("Author : JR Jiang");
- DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() // 彈出視窗的 Listener
- {
- public void onClick(DialogInterface dialog, int i) // 當按下時要執行的 Code
- {}
- };
- ad.setPositiveButton("OK", listener); // 建立 Listener 去監聽 , 幫對話框加上一個按鈕
- ad.show(); // 把對話框顯示出來
- break;
- case R.id.restart:
- Toast.makeText(this, "Already Start 1 Level", Toast.LENGTH_LONG).show();
- break;
- case R.id.quit:
- finish(); // 結束 Activity 這個頁面
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement