Android俄罗斯方块键盘操控变为手势识别
小弟最近做毕业设计,之前有上过java课程,刚刚接触android开发,网上下了个模板发现是键盘操控方块的,但是现在要做成触摸屏手势识别的,还请各位指点迷津~这个是要修改的控制代码:
package com.comon; //启动游戏
import java.io.InputStream;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class StarSurfaceView extends SurfaceView implements Runnable,SurfaceHolder.Callback,View.OnKeyListener {
SurfaceHolder sHolder=null;
Canvas canvas;
Paint paint;
showPage tm;
contro cm;
int face;
boolean flag;
boolean onlyone=true;
boolean next = false;
Store a;
Store c;
int x;
int y;
int kk;
Bitmap store1 = null;
Bitmap store2 = null;
Bitmap store3 = null;
Bitmap store4 = null;
Bitmap store5 = null;
Bitmap store6 = null;
Bitmap store7 = null;
Bitmap border1 = null;
Bitmap border2 = null;
Bitmap record = null;
Bitmap num0 = null;
Bitmap num1 = null;
Bitmap num2 = null;
Bitmap num3 = null;
Bitmap num4 = null;
Bitmap num5 = null;
Bitmap num6 = null;
Bitmap num7 = null;
Bitmap num8 = null;
Bitmap num9 = null;
Bitmap blg = null;
Bitmap conti=null;
Bitmap goon = null;
Bitmap good = null;
Bitmap happy = null;
Bitmap bbk = null;
public StarSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
//实例化sHolder
sHolder = this.getHolder();
//addCallback:给SurfaceView添加一个回调函数
sHolder.addCallback(this);
this.setFocusable(true);
paint=new Paint();
tm = new showPage();
cm = new contro();
a = new Store();
c = new Store();
face = 0;
x=-2;
y=4;
kk=0;
//获得对象
bbk = this.getBitmapById(R.drawable.bbk);
blg = this.getBitmapById(R.drawable.tetris);
store1 = this.getBitmapById(R.drawable.block0);
store2 = this.getBitmapById(R.drawable.block1);
store3 = this.getBitmapById(R.drawable.block2);
store4 = this.getBitmapById(R.drawable.block3);
store5 = this.getBitmapById(R.drawable.block4);
store6 = this.getBitmapById(R.drawable.block5);
store7 = this.getBitmapById(R.drawable.block6);
border1 = this.getBitmapById(R.drawable.border);
border2 = this.getBitmapById(R.drawable.border_sm);
num0 = this.getBitmapById(R.drawable.a0);
num1 = this.getBitmapById(R.drawable.a1);
num2 = this.getBitmapById(R.drawable.a2);
num3 = this.getBitmapById(R.drawable.a3);
num4 = this.getBitmapById(R.drawable.a4);
num5 = this.getBitmapById(R.drawable.a5);
num6 = this.getBitmapById(R.drawable.a6);
num7 = this.getBitmapById(R.drawable.a7);
num8 = this.getBitmapById(R.drawable.a8);
num9 = this.getBitmapById(R.drawable.a9);
record = this.getBitmapById(R.drawable.record);
conti=this.getBitmapById(R.drawable.conti);
goon = this.getBitmapById(R.drawable.goon);
good = this.getBitmapById(R.drawable.good);
happy = this.getBitmapById(R.drawable.happy);
}
//从资源中装载图片
public Bitmap getBitmapById(int id) {
Bitmap image = null;
Context currentCtx = this.getContext();//得到当前设备环境
Resources res = currentCtx.getResources();//得到图片资源
InputStream is = res.openRawResource(id);//打开图片资源
image = BitmapFactory.decodeStream(is);//获取图片资源
return image;
}
//绘制方法
public void draw() {
try {
canvas= sHolder.lockCanvas();
if (canvas!= null) {
canvas.drawBitmap(bbk,0,0,paint);
canvas.drawBitmap(border1, 2,1 ,paint);
canvas.drawBitmap(border2,240,20, paint);
canvas.drawBitmap(record,240, 270,paint);
showFace(canvas,paint);
showC(canvas,paint);
showT(canvas,paint);
showP(canvas,paint);
// sHolder.unlockCanvasAndPost(c);
}
} catch (Exception e) {
System.out.println("error");
} finally {
if (canvas!= null)
sHolder.unlockCanvasAndPost(canvas);
}
}
boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;
public void run() {
if(onlyone){
c.Sttore(-1);//随机为下一个方块区域生成一个方块
a.Sttore(-1);//随机为游戏区域生成一个方块
tm.putS(c);//在下一个方块区域产生一个方块
onlyone=false;
}
while(flag){
if(next) {
tm.delS(c);//删除下一个方块区域的方块
a.Sttore(c.kind);//把存储下一个方块区域的方块的数组传递给游戏区域的方块数组
c.Sttore(-1);//为下一个方块区域随机生成一个方块
x = -2;
y = 4;
next = false;
tm.putS(c);
}
draw();//绘制方块
//控制方块下降速度
try {
Thread.sleep(400 - cm.speed);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if(left) {
tm.delStore(a, x, y);//删除游戏区域的方块
if(tm.leftM(a, x, y)) {
y--;
}
tm.putStore(a, x, y);
left = false;
kk++;
if(kk < 3) {
continue;
}
}
//方块右移
else if(right) {
tm.delStore(a, x, y);
if(tm.rightM(a, x, y)) {
y++;
}
tm.putStore(a, x, y);
right = false;
kk++;
if(kk < 3) {
continue;
}
}
//方块变形
else if(up) {
int tv;
if( (a.kind+1)%4 == 0) {
tv = a.kind -3;
}
else {
tv = a.kind + 1;
}
Store b = new Store();
b.Sttore(tv);
tm.delStore(a, x, y);
if(tm.fit(b, x, y)) {
a = b;
tm.putStore(a, x, y);
up = false;
kk++;
if(kk < 3) {
continue;
}
}
else {
tm.putStore(a, x, y);
up = false;
}
}
else if(down) {
tm.delStore(a, x, y);
while(tm.fit(a, x, y)) {
x++;
}
tm.putStore(a, x, y);
down = false;
}
tm.delStore(a, x, y);
kk = 0;
if(tm.fit(a, x, y)) {
x++;
tm.putStore(a, x, y);
}
else {
int t;
tm.putStore(a, x, y);
if(tm.outM(a, x, y)) {
Intent intent = new Intent();
intent.setClass( ActivityGame.acGame, ActGameover.class);
ActivityGame.acGame.startActivity(intent);
ActivityGame.acGame.finish();
break;
}
t = tm.flood(a, x, y);
face=t;
cm.tscore(t); //计分
cm.passWar();
System.out.println(cm.score);
tm.pSco(cm.sco);
next = true;
}
}
}
public void showFace(Canvas canvas,Paint paint) {
if(face == 0) {
canvas.drawBitmap(goon, 240,120, paint);
}
else if(face == 1) {
canvas.drawBitmap(conti, 240,120, paint);
}
else if(face == 2||face==3) {
canvas.drawBitmap(good, 240,120, paint);
}
else if(face==4) {
canvas.drawBitmap(happy, 240,120, paint);
}
}
public void showC(Canvas canvas,Paint paint) {
for(int i = 0; i <4; i++) {
if(tm.co[i] == 0) {
canvas.drawBitmap(num0, i*20+240, 330, paint);
}
else if(tm.co[i] == 1) {
canvas.drawBitmap(num1, i*20+240, 330, paint);
}
else if(tm.co[i] == 2) {
canvas.drawBitmap(num2, i*20+240, 330, paint);
}
else if(tm.co[i] == 3) {
canvas.drawBitmap(num3, i*20+240, 330, paint);
}
else if(tm.co[i] == 4) {
canvas.drawBitmap(num4, i*20+240, 330, paint);
}
else if(tm.co[i] == 5) {
canvas.drawBitmap(num5, i*20+240, 330, paint);
}
else if(tm.co[i] == 6) {
canvas.drawBitmap(num6, i*20+240, 330, paint);
}
else if(tm.co[i] == 7) {
canvas.drawBitmap(num7, i*20+240, 330, paint);
}
else if(tm.co[i] == 8) {
canvas.drawBitmap(num8, i*20+240, 330, paint);
}
else if(tm.co[i] == 9) {
canvas.drawBitmap(num9, i*20+240, 330, paint);
}
}
}
public void showT(Canvas canvas,Paint paint) {
for(int i = 0 ;i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(tm.brray[i][j] == 1){
canvas.drawBitmap(store1, j*tm.w+240, i*tm.w+20, paint);
}
else if(tm.brray[i][j] == 2){
canvas.drawBitmap(store2, j*tm.w+240, i*tm.w+20, paint);
}
else if(tm.brray[i][j] == 3){
canvas.drawBitmap(store3, j*tm.w+240, i*tm.w+20, paint);
}
else if(tm.brray[i][j] == 4){
canvas.drawBitmap(store4, j*tm.w+240, i*tm.w+20, paint);
}
else if(tm.brray[i][j] == 5){
canvas.drawBitmap(store5, j*tm.w+240, i*tm.w+20, paint);
}
else if(tm.brray[i][j] == 6){
canvas.drawBitmap(store6, j*tm.w+240, i*tm.w+20, paint);
}
else if(tm.brray[i][j] == 7){
canvas.drawBitmap(store7, j*tm.w+240, i*tm.w+20, paint);
}
}
}
}
public void showP(Canvas canvas,Paint paint) { //为游戏区域的方块绘制不同颜色的图片
for(int i = 0 ;i < tm.array.length; i++){
for(int j = 0; j < tm.array[0].length; j++){
if(tm.array[i][j] == 1){
canvas.drawBitmap(store1, j*tm.w-1, i*tm.w-2, paint);
}
else if(tm.array[i][j] == 2){
canvas.drawBitmap(store2, j*tm.w-1, i*tm.w-2, paint);
}
else if(tm.array[i][j] == 3){
canvas.drawBitmap(store3, j*tm.w-1, i*tm.w-2, paint);
}
else if(tm.array[i][j] == 4){
canvas.drawBitmap(store4, j*tm.w-1, i*tm.w-2, paint);
}
else if(tm.array[i][j] == 5){
canvas.drawBitmap(store5, j*tm.w-1, i*tm.w-2, paint);
}
else if(tm.array[i][j] == 6){
canvas.drawBitmap(store6, j*tm.w-1, i*tm.w-2, paint);
}
else if(tm.array[i][j] == 7){
canvas.drawBitmap(store7, j*tm.w-1, i*tm.w-2, paint);
}
}
}
}
public boolean onKeyDown(int k, KeyEvent event) {
return false;
}
public boolean onKeyUp(int k,KeyEvent event){
switch (k) {
case KeyEvent.KEYCODE_DPAD_UP:
up=true;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
down=true;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
left=true;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
right=true;
break;
}
return false;
}
public boolean onKey(View v, int k, KeyEvent e) {
return false;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
flag=true;
Thread th = new Thread(this);
th.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
flag=false;
}
}
Android 俄罗斯方块 手势识别 --------------------编程问答-------------------- http://download.csdn.net/detail/yycaogg/5671697有你想要的,在mainactivity中,我也才接触。
补充:Java , Eclipse