package com.example.beathamsterapp;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.beathamsterapp.music.VolumsPlayer;
import com.google.gson.Gson;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class PlayGameActivity extends AppCompatActivity implements View.OnClickListener{
SharedPreferences sp;//内置数据库保存历史分数
public static List<Map<String,String>> scoreMap=new ArrayList<>();
/**
* 线程睡眠时间
*/
public static final int THREAD_SLEEP_TIME = 1000;
/**
* 游戏时间
*/
private ImageButton one;
private ImageButton two;
private ImageButton three;
private ImageButton four;
private ImageButton five;
private ImageButton six;
private ImageButton seven;
private ImageButton eight;
private ImageButton nine;
private ImageButton ten;
private ImageButton eleven;
private ImageButton twleve;
// 显示时间
private TextView showTime;
// 显示分数
private TextView score;
private Random random;
// 游戏总时间
private int Time=60;
// 老鼠下一次出现位置
private int next;
// 游戏当前分数
private int nowScore;
// 游戏线程
private Thread t;
// 存放按钮和next的映射
HashMap<ImageButton, Integer> battle;
HashMap<Integer, ImageButton> nextMap;
public Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==1)
{
changeUI();
}
else if(msg.what==2)
{
gameOver();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playgame);
//获取本地数据库
sp = getSharedPreferences("HistoryInfo",0x0000);
//全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
battle = new HashMap<ImageButton, Integer>();
nextMap = new HashMap<Integer, ImageButton>();
inittoobar();
initImageButton();
initbattleMap();
initNextMap();
next = -1;
random = new Random();
nowScore = 0;
showTime.setText(Time + "s");
}
private void inittoobar() {
ImageView back=findViewById(R.id.iv_back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(PlayGameActivity.this);
dialog.setTitle("你确定要退出该局游戏吗?");
dialog.setMessage("");
dialog.setCancelable(true);
dialog.setNeutralButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
dialog.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
}
});
}
// 初始化按钮
private void initImageButton() {
one = findViewById(R.id.first);
two = findViewById(R.id.second);
three =findViewById(R.id.three);
four = findViewById(R.id.four);
five = findViewById(R.id.five);
six = findViewById(R.id.six);
seven = findViewById(R.id.seven);
eight = findViewById(R.id.eight);
nine = findViewById(R.id.nine);
ten = findViewById(R.id.ten);
eleven = findViewById(R.id.eleven);
twleve = findViewById(R.id.twelve);
showTime = findViewById(R.id.showtime);
score = findViewById(R.id.score);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
ten.setOnClickListener(this);
eleven.setOnClickListener(this);
twleve.setOnClickListener(this);
}
// 按钮id和next映射关系
private void initbattleMap() {
battle.put(one, 1);
battle.put(two, 2);
battle.put(three, 3);
battle.put(four, 4);
battle.put(five, 5);
battle.put(six, 6);
battle.put(seven, 7);
battle.put(eight, 8);
battle.put(nine, 9);
battle.put(ten, 10);
battle.put(eleven, 11);
battle.put(twleve, 12);
}
// next和按钮id的映射关系
private void initNextMap() {
nextMap.put(1, one);
nextMap.put(2, two);
nextMap.put(3, three);
nextMap.put(4, four);
nextMap.put(5, five);
nextMap.put(6, six);
nextMap.put(7, seven);
nextMap.put(8, eight);
nextMap.put(9, nine);
nextMap.put(10, ten);
nextMap.put(11, eleven);
nextMap.put(12, twleve);
}
/**
* 更新小老鼠出现位置和倒计时
*/
private void changeUI() {
// 更新显倒计时
showTime.setText(Time + "s");
if (next == -1) {
return;
}
// 全部设置为空洞图片
reImageButton();
// 获得next对应的洞
ImageButton bt = nextMap.get(next);
// 老鼠出洞
bt.setBackground(getDrawable(R.drawable.end));
}
//背景初始化
private void reImageButton() {
one.setBackground(getDrawable(R.drawable.start));
two.setBackground(getDrawable(R.drawable.start));
three.setBackground(getDrawable(R.drawable.start));
four.setBackground(getDrawable(R.drawable.start));
five.setBackground(getDrawable(R.drawable.start));
six.setBackground(getDrawable(R.drawable.start));
seven.setBackground(getDrawable(R.drawable.start));
eight.setBackground(getDrawable(R.drawable.start));
nine.setBackground(getDrawable(R.drawable.start));
ten.setBackground(getDrawable(R.drawable.start));
eleven.setBackground(getDrawable(R.drawable.start));
twleve.setBackground(getDrawable(R.drawable.start));
}
@Override
protected void onResume() {
super.onResume();
if(t == null){
// 控制游戏时间
t = new Thread(new Runnable() {
@Override
public void run() {
try {
while (Time >= 0) {
if (Time==0)
{
handler.sendEmptyMessage(2);
break;
}
Thread.sleep(THREAD_SLEEP_TIME)