android平台俄罗斯方块游戏完整代码
2012-12-02 14:31:58 来源:WEB开发网核心提示: 游戏视图类有了,当然需要一个活动去执行它了,android平台俄罗斯方块游戏完整代码(11),定义了一个GameActivity.java 具体代码为: package com.example.eluosifangkuai;import android.app.Activity;import android.app.
游戏视图类有了,当然需要一个活动去执行它了,定义了一个GameActivity.java 具体代码为:
package com.example.eluosifangkuai;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
public class GameActivity extends Activity implements OnTouchListener,OnGestureListener { // 实现手势,触摸监听
GestureDetector m_GestureDetector ; // 定义手势
public GameActivity() //构造函数 完成手势的相关初始化
{
m_GestureDetector = new GestureDetector((OnGestureListener) this);
}
protected static final int ii = 0x101; // UI主线程发送消息匹配代码
protected static final int kk = 0x110;
private int dialog_xx = 0;
private boolean Thread_Log = true; // 线程运行标志,用来暂停游戏
private float log = 0; // 左滑时获取距离存放变量
private int log_num = 0; // 下落在自由下落 和快速下滑是声音选择变量
private int log_log = 0;
public int m_time_a,i=0; // 下落时间变量,和运行次数的相关参数
Vibrator vibrator; // 震动类声明
long[] pattern = {0,10,10,30}; //根据指定的模式进行震动 //第一个参数:该数组中第一个元素是等待多长的时间才启动震动, //之后将会是开启和关闭震动的持续时间,单位为毫秒 //第二个参数:重复震动时在pattern中的索引,如果设置为-1则表示不重复震动
GameView m_GameView = null; // 定义视图类变量
Handler m_Handler = new Handler() // 定义Handler变量 实现更新View视图
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case GameActivity.kk:
Thread_Log = false;
dialog_a();
break ;
case GameActivity.ii:
m_GameView.invalidate();
}
super.handleMessage(msg);
}
};
public void onCreate(Bundle saved)
{
super.onCreate(saved);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置无标题的全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
m_GameView = new GameView(this); // 实例化GameView对象
init(); // 初始化工作,包括声音
new Thread(new GameThread()).start(); // 新建游戏线程
}
private void init() // 初始化
{
setContentView(m_GameView); // 活动绘制GameView的内容
vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); // 获取系统服务震动
m_GameView.setOnTouchListener((OnTouchListener) this); // 设置可以响应触摸事件
m_GameView.setClickable(true);
m_GestureDetector.setIsLongpressEnabled(true); // 可以响应长按
MusicPlay.inItMusicPlay(this); // 初始化播放器及相关操作
MusicPlay.inItSound();
MusicPlay.BgMediaplayer();
MusicPlay.startMusic();
Log.i("aiai", "run begin 111");
}
class GameThread implements Runnable{
public void run()
{
while(!Thread.currentThread().isInterrupted())
{
while(Thread_Log) // 游戏是否暂停标志
{
Message message = new Message(); // 新建消息函数
if(dialog_xx == 0)
message.what = GameActivity.ii; // 赋值消息
else
message.what = GameActivity.kk; // 赋值消息
GameActivity.this.m_Handler.sendMessage(message); //发送消息
Log.i("Thread run","yunxing22222");
try
{
Thread.sleep(100); // 线程休眠时间
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
m_time_a = ++i*100; //循环获取时间的叠加值,满足一定条件是执行下落函数块
if(m_time_a == (10-2*m_GameView.levl+1)*100) //时间满足执行
{
onNotDown(); // 执行下落处理函数
i = 0; // 重置变量i
}
}
}
}
}
public void onNotDown()
{
if(Thread_Log) //暂停标志
{
if(m_GameView.moveDownOn()) // 能够下落
{
m_GameView.Down();
log_num = 0;
}
else
{
if(log_num == 0)
MusicPlay.playSound(R.raw.down,0);
m_GameView.NotDown();
if(m_GameView.GameOver()) //游戏结束
{
dialog_xx = 1;
}
if(dialog_xx == 0)
m_GameView.newState();
if(m_GameView.gradeCourt >= 3000&&m_GameView.timeTask == 1000) //一定分数升级
{
m_GameView.timeTask = 800;
m_GameView.levl+=1;
}
if(m_GameView.gradeCourt >= 6000 && m_GameView.timeTask == 800)
{
m_GameView.timeTask = 500;
m_GameView.levl+=1;
m_GameView.log_X = 0;
}
if(m_GameView.gradeCourt >= 9000&&m_GameView.timeTask == 500)
{
m_GameView.timeTask = 200;
m_GameView.levl+=1;
m_GameView.log_X = 0;
}
m_GameView.fastDown(1);
}
}
}
public boolean onTouch(View v, MotionEvent event) // 重写onTouch函数
{
return m_GestureDetector.onTouchEvent(event); // 可以响应手势
}
public boolean onKeyDown(int key,KeyEvent keyevent) //对按键的响应
{
m_GameView.idKeyDown(key, keyevent);
if(key == KeyEvent.KEYCODE_BACK && keyevent.getRepeatCount() == 0) //返回键 提示对话框
{
Thread_Log = false;
dialog();
return true;
}
return true;//super.onKeyDown(key, keyevent);
}
更多精彩
赞助商链接