Hi Friends here is the complete source code of Brick Game in our Android Gaming Tutorial .
Hope you'll understand.. Any Queries can contact me on my mail pragnani@gmail.com
Home. java
package com.example.simplegame;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class Home extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
}
}
------------- GameView. java------------------
package com.example.simplegame;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class GameView extends SurfaceView {
Bitmap bmp,bar,brick;
SurfaceHolder holder;
int bary;
float barx;
private GameThread thread;
Sprite sprite;
public GameView(Context context) {
super(context);
thread=new GameThread(this);
holder=getHolder();
holder.addCallback(new Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
thread.setRunning(false);
boolean retry=true;
while(retry)
{
try {
thread.join();
retry=false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
bar=BitmapFactory.decodeResource(getResources(), R.drawable.bar);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.ball_animation);
brick=BitmapFactory.decodeResource(getResources(), R.drawable.brick);
// bricksprite=new BrickSprite(GameView.this, brick);
sprite=new Sprite(GameView.this,thread,bmp,bar,brick);
bary=getHeight()-100;
thread.setRunning(true);
thread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
});
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
sprite.onDraw(canvas);
// bricksprite.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
sprite.onTouch(event);
return true;
}
}
-------------Game Thread.java---------------
package com.example.simplegame;
import android.graphics.Canvas;
public class GameThread extends Thread {
GameView view;
boolean running = false;
int fps = 10;
public GameThread(GameView view) {
this.view = view;
}
public void setRunning(boolean run) {
running = run;
}
@Override
public void run() {
Canvas canvas = null;
while (running) {
try {
canvas = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(canvas);
}
} finally {
if (canvas != null) {
view.getHolder().unlockCanvasAndPost(canvas);
}
try {
sleep(10);
} catch (Exception e) {
}
}
}
}
}
-------------------Sprite .java ------------------
package com.example.simplegame;
import java.util.Random;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
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;
speedx=10;
speedy=10;
}
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();
}
}
Complete Source Code:
Brick Game Source Code
Thanks
Your's Pragnani