Venus__ 2014-05-22 08:56 采纳率: 0%
浏览 1603

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用来完成对绘制的画布进行裁剪,控制其大小
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;
// int action;

//创建图片对象
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 fire = 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);

// sHolder = this.getHolder();
// sHolder.addCallback(this);
paint=new Paint();
tm = new showPage();
cm = new contro();
a = new Store();
c = new Store();
// action=0;
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);
// fire = this.getBitmapById(R.drawable.fire);
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() {

//// Canvas c = null;//声明画布
// c = sHolder.lockCanvas();//锁定画布
//// Paint p;//声明画笔
// p = new Paint();
//

// c.drawBitmap(bbk, 0, 0, p);
// c.drawBitmap(border1, 2, 1, p);//绘制游戏主背景
// c.drawBitmap(border2, 240, 20, p);//绘制产生下一个方块的背景
// c.drawBitmap(record, 240, 270, p);//绘制计分榜背景
// showFace(c,p);
// showC(c,p);
// showT(c,p);
// showP(c,p);
// sHolder.unlockCanvasAndPost(c);//解锁画布
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;

// boolean back=false;

public void run() {
    if(onlyone){

// int x = -2;
// int y = 4;
// boolean next = false;
// Store a = new Store();
// Store b = new Store();
c.Sttore(-1);//随机为下一个方块区域生成一个方块
a.Sttore(-1);//随机为游戏区域生成一个方块
tm.putS(c);//在下一个方块区域产生一个方块
onlyone=false;
// int kk = 0; //用来控制方块的变形次数,在一行内
}
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(back){
// Intent intent1 = new Intent();
// intent1.setClass( ActivityGame.acGame, TTetris.class);
// ActivityGame.acGame.startActivity(intent1);
// ActivityGame.acGame.finish();
// break;
// }
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) { //每种方块有1,2,3,4这四种状态,如果处于第4种状态,按上就变回第一种状态
tv = a.kind -3; //否则把没按一次上,1变成2,2变成3,3变成4
}
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);//消行,并返回消除的行数,不可以消行返回0;

// putface(t);
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) {//通过face控制人物的表情
    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 putface(int fa) { //生成face的值
// if(fa == 0) {
// face = (int)(Math.random()*2);
// }
// else {
// face = fa+1;
// }
// }

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;
}

}

main()的代码:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation = "vertical"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

  • 写回答

1条回答

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 23:57
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题