2

我正在开发一个积木游戏这里是代码

public class Sprite {
    private static final int NO_COLUMNS = 12;
    Bitmap bmp,bar,brick,scalebit;
    GameView gameview;
    int x,speedx=0,speedy=0;
    int no_bricksperline;
    private int currentFrame=0;
    private int width;
    private int height;
    int brwidth;
    int brheight;
    int init=0;
    float tempwidth;
    String[][] bricks;
    GameThread thread;
    private int y;
    Paint paint=new Paint();
    private int barx;
    private int bary;
    @SuppressLint("FloatMath")
    public Sprite(GameView gameview,GameThread thread,Bitmap bmp,Bitmap bar,Bitmap brick)
    {
        this.gameview=gameview;
        this.bmp=bmp;
        this.bar=bar;
        this.brick=brick;
        this.thread=thread;
        this.no_bricksperline=gameview.getWidth()/brick.getWidth()-2;
        bricks=new String[no_bricksperline][4];
        brwidth=brick.getWidth();
        brheight=brick.getHeight();
        paint.setColor(Color.BLACK);
        for (int i = 0; i < no_bricksperline; ++i)
            for (int j = 0; j < 4; ++j)
                bricks[i][j] = "B";

        String strwidth=String.valueOf(((float)(bmp.getWidth())/NO_COLUMNS));
        if(strwidth.contains("."))
        {
            scalebit=Bitmap.createScaledBitmap(bmp, (int)(Math.ceil(((float)bmp.getWidth())/NO_COLUMNS))*NO_COLUMNS, bmp.getHeight(), true);
        }
        else
        {
            scalebit=bmp;
        }
        this.width=scalebit.getWidth()/NO_COLUMNS;
        this.bary=gameview.getHeight()-100;
        this.height=scalebit.getHeight();
        Random random=new Random();
         x = random.nextInt(gameview.getWidth() - width);
         y = random.nextInt(gameview.getHeight() - height)-100;
         if(y<=100)
         {
             y=120;
         }
        speedx=random.nextInt(10)-5;
        speedy=random.nextInt(10)-5;
    }

    public void update()
    {

        if(x>gameview.getWidth()-width-speedx || x+speedx<0)
        {
            speedx=-speedx;
        }
        if(y>gameview.getHeight()-height-speedy || y+speedy<0)
        {
            speedy=-speedy;
        }
        if(y+height >= bary-bar.getHeight()) {

            if((x+width>barx-bar.getWidth()/2 && barx>x) ||  (x<barx+bar.getWidth()/2 && x>barx)) {
                speedy=-speedy;
            }
        }

        if(y>0 && y<=(5*brheight) && init==1)
        {
            if(x!=0){
                int x1=x;
                int y1=y;
                if(x1>(no_bricksperline+1)*brwidth)
                {
                    x1=(no_bricksperline)*brwidth;
                }
                if(x1<=brwidth)
                {
                    x1=brwidth;
                }
                if(y1<=brheight)
                {
                    y1=brheight;
                }
                if(y1>=(4*brheight) && y1<=(5*brheight))
                {
                    y1=4*brheight;
                }
                int posi=(int)Math.floor(x1/brwidth)-1;
                int posj=(int)Math.floor(y1/brheight)-1;
                if(bricks[posi][posj]=="B")
                {
                        bricks[posi][posj]="K";
                        speedy=-speedy;
                }   

            }
        }
        if(y+height>bary+bar.getHeight())
        {
        }
        x=x+speedx;
        y=y+speedy;
        currentFrame=++currentFrame%NO_COLUMNS;
    }
    @SuppressLint("DrawAllocation")
    public void onDraw(Canvas canvas)
    {
        checkFinish();
        update();
        for (int i = 0; i < no_bricksperline; ++i){
            for (int j = 0; j < 4; ++j) {
                // ourHolder.lockCanvas();

                if (bricks[i][j].contains("B"))
                    canvas.drawBitmap(brick, (i+1)*brick.getWidth(),(j+1)*brick.getHeight(),
                            null);
                init=1;
            }
        }

        int srcX=currentFrame*width;
        int srcY=0;
        Rect src=new Rect(srcX, srcY, srcX+width, srcY+height);
        Rect dest=new Rect(x,y,x+width,y+width);
        canvas.drawBitmap(scalebit,src,dest,null);
        canvas.drawBitmap(bar, barx-bar.getWidth()/2, bary-bar.getHeight(),null);
    }

    private void checkFinish() {
        int totalbricks=0;
        for (int i = 0; i < no_bricksperline; ++i){
            for (int j = 0; j < 4; ++j){
                if(bricks[i][j] == "K"){
                    totalbricks++;
                }
            }
        }
        if(totalbricks==(no_bricksperline*4))
        {
            thread.setRunning(false);}
        }

    public void onTouch(MotionEvent event) {
        barx= (int) event.getX();

    }

}

问题是砖正在显示在此处输入图像描述

当我直接使用砖图像时,它们会被像素化,并且只显示 2 块砖。所以我写了一个逻辑来显示最少的砖块

我在我的代码中使用了一些逻辑这里是代码

 String strwidth=String.valueOf(((float)(bmp.getWidth())/NO_COLUMNS));
        if(strwidth.contains("."))
        {
            scalebit=Bitmap.createScaledBitmap(bmp, (int)(Math.ceil(((float)bmp.getWidth())/NO_COLUMNS))*NO_COLUMNS, bmp.getHeight(), true);
        }
        else
        {
            scalebit=bmp;
        }

但是墙和砖之间有一个缝隙。我怎样才能让砖块占据整个屏幕。

4

3 回答 3

2

如果我对您的理解正确,您的问题是要调整砖块的宽度以填充整个屏幕。尝试使用这个

 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);
 dispWidth=metrics.widthPixels;

 //make the dispWidth your gameView width
于 2013-01-21T08:13:40.317 回答
2

用这个

this.no_bricksperline=gameview.getWidth()/brick.getWidth();

代替

this.no_bricksperline=gameview.getWidth()/brick.getWidth()-2;
于 2013-01-24T11:47:16.527 回答
0

使用 imageview 属性

android:scaleType="..." 
于 2013-01-22T12:56:05.870 回答