2

I am developing a brick game here is the code

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();

    }

}

the problem is bricks are displayingenter image description here

When I use the brick image directly their are pixlated and also only 2 bricks are displaying. So I have write a logic to display min no of bricks

I have used some logic in my code here is the code

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

but there is a gap between walls and bricks. How can I make the bricks to occupy the entire screen.

Pragnani
  • 20,075
  • 6
  • 49
  • 74

3 Answers3

2

If i have understood you correctly, your problem is the width of the bricks to be adjusted to fill the entire screen. try to use this one

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

 //make the dispWidth your gameView width
She Smile GM
  • 1,322
  • 1
  • 11
  • 33
  • I want min of 8 bricks in a row and the brick image should be scaled so that 8 bricks should occupy entire screen. – Pragnani Jan 21 '13 at 10:09
  • maybe you just have to use the width in the right place. `//change bmp.getWidth to dispWidth` `scalebit=Bitmap.createScaledBitmap(bmp, (int) (Math.ceil(((float)bmp.getWidth())/NO_COLUMNS))*NO_COLUMNS, bmp.getHeight(), true);` – She Smile GM Jan 21 '13 at 10:29
  • try to replace `//gameView.width to dispWidth` and `//bmp.getWidth to dispWidth` – She Smile GM Jan 21 '13 at 10:41
  • I have uploaded my app there check that once – Pragnani Jan 21 '13 at 11:01
2

use this

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

instead of

this.no_bricksperline=gameview.getWidth()/brick.getWidth()-2;
Ramu Pasupuleti
  • 888
  • 2
  • 15
  • 35
0

use the imageview property

android:scaleType="..." 
demongolem
  • 9,474
  • 36
  • 90
  • 105
Rishi Gautam
  • 1,948
  • 3
  • 21
  • 31