|
|
|
@ -0,0 +1,284 @@
|
|
|
|
|
package com.example.musicwork.chuangguan;
|
|
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
|
import android.app.Dialog;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
|
|
|
|
import com.example.musicwork.Data;
|
|
|
|
|
import com.example.musicwork.R;
|
|
|
|
|
import com.example.musicwork.SignIn;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
public class GameActivity extends AppCompatActivity {
|
|
|
|
|
TextView curgrade;
|
|
|
|
|
Button startgame, gradeinfo;
|
|
|
|
|
|
|
|
|
|
String mode;//记录用户选择的模式
|
|
|
|
|
Dialog dialog;//选择模式的对话框
|
|
|
|
|
|
|
|
|
|
ArrayList<Integer> testKeys = new ArrayList<Integer>();//记录测试音乐集中的键
|
|
|
|
|
int count = 0;//记录被点击的按钮数
|
|
|
|
|
int grade = 0;//记录用户成绩
|
|
|
|
|
int num;//记录当前模式每组会播放的音符数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//所有音符对应的资源文件(c2-低音阶段1-7,c3-中音阶段8-14,c4-高音阶段15-21
|
|
|
|
|
HashMap<Integer, Integer> musicMap = new HashMap<Integer, Integer>() {{
|
|
|
|
|
put(1, R.raw.c2);
|
|
|
|
|
put(2, R.raw.d2);
|
|
|
|
|
put(3, R.raw.e2);
|
|
|
|
|
put(4, R.raw.f2);
|
|
|
|
|
put(5, R.raw.g2);
|
|
|
|
|
put(6, R.raw.a2);
|
|
|
|
|
put(7, R.raw.b2);
|
|
|
|
|
put(8, R.raw.c3);
|
|
|
|
|
put(9, R.raw.d3);
|
|
|
|
|
put(10, R.raw.e3);
|
|
|
|
|
put(11, R.raw.f3);
|
|
|
|
|
put(12, R.raw.g3);
|
|
|
|
|
put(13, R.raw.a3);
|
|
|
|
|
put(14, R.raw.b3);
|
|
|
|
|
put(15, R.raw.c4);
|
|
|
|
|
put(16, R.raw.d4);
|
|
|
|
|
put(17, R.raw.e4);
|
|
|
|
|
put(18, R.raw.f4);
|
|
|
|
|
put(19, R.raw.g4);
|
|
|
|
|
put(20, R.raw.a4);
|
|
|
|
|
put(21, R.raw.b4);
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
PianoMusic music;//播放音乐对象
|
|
|
|
|
|
|
|
|
|
boolean finishFlag = false;
|
|
|
|
|
|
|
|
|
|
String username ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
setContentView(R.layout.activity_game);
|
|
|
|
|
init();//初始化按钮
|
|
|
|
|
music = new PianoMusic(getApplicationContext());//初始化音乐播放器
|
|
|
|
|
final TextView text_username = (TextView) this.findViewById(R.id.text_username);
|
|
|
|
|
|
|
|
|
|
final Data data = (Data)getApplication();
|
|
|
|
|
username = data.getUsername();
|
|
|
|
|
text_username.setText("当前用户: "+username);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void init() {
|
|
|
|
|
// 获取按钮ID
|
|
|
|
|
startgame = (Button) this.findViewById(R.id.startgame);
|
|
|
|
|
gradeinfo = (Button) this.findViewById(R.id.gradeinfo);
|
|
|
|
|
curgrade = (TextView) this.findViewById(R.id.curgrade);
|
|
|
|
|
gradeinfo.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
try {
|
|
|
|
|
GameService.CheckScore(username);
|
|
|
|
|
Intent intent = new Intent(GameActivity.this, GradeInfoActivity.class);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
startgame.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
ModeDialog();//弹出选择模式的对话框
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//计算被点击的按钮数(在开始闯关之前不允许点击?
|
|
|
|
|
public void countPressedBtn(View v) {
|
|
|
|
|
//pressedFlag = true;
|
|
|
|
|
if(count>=num){
|
|
|
|
|
Log.e("map","不允许多点");
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
countGrade(v);//计算分数
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//显示分数
|
|
|
|
|
TextView gradeView = (TextView) findViewById(R.id.curgrade);
|
|
|
|
|
gradeView.setText(" "+grade);
|
|
|
|
|
|
|
|
|
|
//闯关完成
|
|
|
|
|
if(finishFlag){
|
|
|
|
|
//创建一个弹出框提示闯关结束,显示成功或失败
|
|
|
|
|
TextView successinfo = new TextView(this);
|
|
|
|
|
if (grade>=60){
|
|
|
|
|
successinfo.setText("闯关成功!");
|
|
|
|
|
}else {
|
|
|
|
|
successinfo.setText("成绩低于60分,继续努力~");
|
|
|
|
|
}
|
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
|
.setView(successinfo)
|
|
|
|
|
.create().show();
|
|
|
|
|
try{
|
|
|
|
|
if (GameService.SaveScore(username, grade))
|
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
Toast.makeText(GameActivity.this, "保存成绩成功", Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
Log.e("GA","e");
|
|
|
|
|
}
|
|
|
|
|
grade = 0;//闯关结束,成绩清零
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断用户是否选择正确并计分(点击按钮时则开始计分
|
|
|
|
|
public void countGrade(View v) {
|
|
|
|
|
|
|
|
|
|
Button musicbtn = (Button) v;
|
|
|
|
|
int answer = Integer.parseInt(musicbtn.getText().toString());
|
|
|
|
|
if (answer == testKeys.get(count)) {//判断答案是否正确
|
|
|
|
|
switch (mode) {//不同模式
|
|
|
|
|
case "简单模式":
|
|
|
|
|
grade += 10;
|
|
|
|
|
break;
|
|
|
|
|
case "普通模式":
|
|
|
|
|
grade += 5;
|
|
|
|
|
break;
|
|
|
|
|
case "困难模式":
|
|
|
|
|
grade += 2;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//随机选择每组测试要播放的音效
|
|
|
|
|
public ArrayList<Integer> randomChooseMusic() {
|
|
|
|
|
Integer[] keys = musicMap.keySet().toArray(new Integer[0]);//键的集合
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
int randomKey;
|
|
|
|
|
|
|
|
|
|
ArrayList<Integer> tempKey = new ArrayList<Integer>();//存储随机抽出的key
|
|
|
|
|
//HashMap<Integer, Integer> testMusic = new HashMap<>();//存储抽取出的测试音乐集
|
|
|
|
|
// HashMap<Integer,Object> testMusicMap = new HashMap<>();
|
|
|
|
|
while(tempKey.size()<num) {
|
|
|
|
|
randomKey = keys[random.nextInt(keys.length-1)];//随机抽取
|
|
|
|
|
if (!tempKey.contains(randomKey)){//不允许重复
|
|
|
|
|
tempKey.add(randomKey);
|
|
|
|
|
//testMusic.put(randomKey, musicMap.get(randomKey));//将抽到的音符及其音效放入,构建测试音乐集
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tempKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//播放音效
|
|
|
|
|
public void playMusic() {
|
|
|
|
|
testKeys = randomChooseMusic();
|
|
|
|
|
//testKeys = testMusic.keySet().toArray(new Integer[0]);//测试音乐集中键的集合(有序的?
|
|
|
|
|
//Log.e("map","playmusic");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < testKeys.size(); i++) {
|
|
|
|
|
Log.e("map","answer:"+testKeys.get(i).toString());
|
|
|
|
|
//Log.e("map", "testkey" +i);
|
|
|
|
|
music.soundPlay(testKeys.get(i));
|
|
|
|
|
//music.soundPlay(0);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//内部类,播放音乐的线程
|
|
|
|
|
class playMusicRunnable implements Runnable {
|
|
|
|
|
Context context;
|
|
|
|
|
|
|
|
|
|
public playMusicRunnable(Context context) {
|
|
|
|
|
this.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
//播放五组
|
|
|
|
|
for (int i = 1; i <= 5; i++) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
count = 0;//计数清零
|
|
|
|
|
playMusic();
|
|
|
|
|
//判断用户点击的按钮数,数量不够不继续执行播放操作
|
|
|
|
|
while (count < num) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finishFlag = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//模式按钮的监听
|
|
|
|
|
public void modeBtnClick(View v) {
|
|
|
|
|
count = 0;//切换模式清空计数
|
|
|
|
|
finishFlag = false;//初始化结束标志
|
|
|
|
|
Button modeBtn = (Button) v;
|
|
|
|
|
mode = modeBtn.getText().toString();
|
|
|
|
|
dialog.cancel();
|
|
|
|
|
//记录每组测试播放的音符数
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case "简单模式":
|
|
|
|
|
num = 2;//每组播放两个音符
|
|
|
|
|
break;
|
|
|
|
|
case "普通模式":
|
|
|
|
|
num = 4;
|
|
|
|
|
break;
|
|
|
|
|
case "困难模式":
|
|
|
|
|
num = 10;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//在新的线程上播放音乐
|
|
|
|
|
playMusicRunnable runnable = new playMusicRunnable(v.getContext());
|
|
|
|
|
Thread thread = new Thread(runnable);
|
|
|
|
|
//启动线程
|
|
|
|
|
thread.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//选择模式的对话框的创建
|
|
|
|
|
public void ModeDialog() {
|
|
|
|
|
dialog = new Dialog(GameActivity.this);//可以在此设置对话框的风格R.style.dialog
|
|
|
|
|
dialog.setTitle("请选择模式:");
|
|
|
|
|
dialog.setContentView(R.layout.mode_layout);
|
|
|
|
|
dialog.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|