2017-11-02|1023阅|作者:lc|举报 摘要:package com.example.game.gamehit;
import android.annotation.SuppressLint;
import androi
package com.example.game.gamehit;
|
import android.annotation.SuppressLint;
|
import android.app.Activity;
|
import android.app.AlertDialog.Builder;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.media.AudioManager;
|
import android.media.MediaPlayer;
|
import android.media.SoundPool;
|
import android.os.Bundle;
|
import android.view.Gravity;
|
import android.view.KeyEvent;
|
import android.view.Window;
|
import android.view.WindowManager;
|
import android.widget.ImageView;
|
import android.widget.TextView;
|
import com.example.game.gamehit.common.Const;
|
import com.example.game.gamehit.util.MUtils;
|
@SuppressLint("ResourceAsColor")
|
public class MainActivity extends Activity {
|
private MediaPlayer mBgMediaPlayer;
|
private boolean isMusic = true;
|
private Context mContext;
|
private static SoundPool mSoundPool = new SoundPool(1,
|
AudioManager.STREAM_MUSIC, 0);
|
private int[] soundIds = { -1, -1, -1, -1, -1 };
|
public GameView gameView;
|
public void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
// setContentView(R.layout.activity_main);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);// 无标题
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 应用运行时,保持屏幕高亮,不锁屏
|
gameView = new GameView(MainActivity.this);
|
setContentView(gameView);
|
// playBackgroundVoide();
|
mContext = MainActivity.this;
|
mBgMediaPlayer = MediaPlayer.create(mContext, Const.voiceBackground);
|
mBgMediaPlayer.setLooping(true);// 循环
|
soundIds[Const.voiceShoot] = mSoundPool.load(mContext, R.raw.shoot, 1);
|
soundIds[Const.voiceHit] = mSoundPool.load(mContext, R.raw.hit, 1);
|
soundIds[Const.voiceNo] = mSoundPool.load(mContext, R.raw.no, 1);
|
soundIds[Const.voiceNextlevel] = mSoundPool.load(mContext,
|
soundIds[Const.voiceGameover] = mSoundPool.load(mContext,
|
private void adMethod() {
|
MUtils.getInstance(mContext);
|
MUtils.getInstance(mContext);
|
* @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
switch (event.getKeyCode()) {
|
case KeyEvent.KEYCODE_BACK:
|
// super.onKeyDown(keyCode, event)
|
private boolean isGotomenu() {
|
Builder gotoMenu = new Builder(mContext);
|
TextView textView = new TextView(mContext);
|
textView.setText("要返回菜单吗?");
|
textView.setTextSize(24);
|
textView.setTextColor(getResources().getColor(R.color.fontColor));
|
textView.setGravity(Gravity.CENTER);
|
textView.setBackgroundColor(getResources().getColor(
|
android.R.color.darker_gray));
|
gotoMenu.setView(textView);
|
gotoMenu.setPositiveButton("是", new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
gotoMenu.setNegativeButton("否", null);
|
public void nextLevel() {
|
playVoice(Const.voiceNextlevel);
|
TextView textView = new TextView(mContext);
|
textView.setText("成功过关!你真棒!");
|
textView.setTextSize(24);
|
textView.setTextColor(getResources().getColor(R.color.fontColor));
|
textView.setGravity(Gravity.CENTER);
|
textView.setBackgroundColor(getResources().getColor(
|
android.R.color.darker_gray));
|
Builder nextLevel = new Builder(mContext);
|
nextLevel.setView(textView);
|
nextLevel.setPositiveButton("下一关",
|
new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
nextLevel.setNegativeButton("返回菜单",
|
new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
nextLevel.setCancelable(false);
|
playVoice(Const.voiceGameover);
|
ImageView imgView = new ImageView(mContext);
|
imgView.setBackgroundColor(getResources().getColor(
|
android.R.color.darker_gray));
|
imgView.setBackgroundResource(R.drawable.gameover);
|
Builder gameOver = new Builder(mContext);
|
gameOver.setView(imgView);
|
gameOver.setPositiveButton("重新挑战",
|
new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
gameOver.setNegativeButton("返回菜单",
|
new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
gameOver.setCancelable(false);
|
@SuppressLint("ResourceAsColor")
|
public void showTimerGrade(int grade, int killnum) {
|
playVoice(Const.voiceNextlevel);
|
Builder timerGrade = new Builder(mContext);
|
TextView textView = new TextView(mContext);
|
textView.setText("本次" + Const.timeNum + "秒计时\r\n击中:" + killnum
|
+ "个\r\n得分:" + grade + "分");
|
textView.setTextColor(getResources().getColor(R.color.fontColor));
|
textView.setTextSize(24);
|
textView.setGravity(Gravity.CENTER);
|
textView.setBackgroundColor(getResources().getColor(
|
android.R.color.darker_gray));
|
timerGrade.setView(textView);
|
timerGrade.setPositiveButton("重新挑战",
|
new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
timerGrade.setNegativeButton("返回菜单",
|
new DialogInterface.OnClickListener() {
|
public void onClick(DialogInterface dialog, int which) {
|
timerGrade.setCancelable(false);
|
public void initGameMode() {
|
switch (Const.gameMode) {
|
case Const.gameMode_Level:// 闯关模式
|
// Const.backgroundImgResid = R.drawable.maplevel48_001;
|
Const.backgroundImgResid = R.drawable.mapds48_001;
|
Const.gameArrayStrResid = R.string.didong_level;
|
Const.voiceBackground = R.raw.level;
|
case Const.gameMode_Random:// 随机模式
|
Const.backgroundImgResid = R.drawable.mapds48_001;
|
Const.gameArrayStrResid = R.string.didong_001;
|
Const.voiceBackground = R.raw.random;
|
case Const.gameMode_Timer:// 计时模式
|
Const.voiceBackground = R.raw.time;
|
Const.backgroundImgResid = R.drawable.mapds48_001;
|
Const.gameArrayStrResid = R.string.didong_level;
|
case Const.gameMode_Super:// 无尽模式
|
Const.voiceBackground = R.raw.supers;
|
Const.backgroundImgResid = R.drawable.mapds48_001;
|
Const.gameArrayStrResid = R.string.didong_001;
|
public void playBackgroundMusic() {
|
if (Const.backgroundMusicOn && mBgMediaPlayer != null) {
|
if (mBgMediaPlayer.isPlaying()) {
|
// public void playHitVoice(){
|
// if(Const.voiceMusicOn && soundIds[0]!=-1){
|
// mSoundPool.play(soundIds[0], 1.0f, 1.0f, 1, 0, 1.0f);
|
// public void playDishuDeadVoice(){
|
// if(Const.voiceMusicOn && soundIds[1]!=-1){
|
// mSoundPool.play(soundIds[1], 1.0f, 1.0f, 1, 0, 1.0f);
|
public void playVoice(int idx) {
|
if (Const.voiceMusicOn && soundIds[idx] != -1) {
|
mSoundPool.play(soundIds[idx], 1.0f, 1.0f, 1, 0, 1.0f);
|
* @see android.app.Activity#onDestroy()
|
protected void onDestroy() {
|
if (this.mBgMediaPlayer != null) {
|
this.mBgMediaPlayer.stop();
|
this.mBgMediaPlayer.release();
|
this.mBgMediaPlayer = null;
|