How to create a Timer for the Bomb in Android

December 2nd, 2011 by aabhaanjan

Basically we are going to get an idea abot using timer for a bomb game. In this article I have
used just a simple piece of displaying text for the timer. later on we will use this widely in our
complete game.

In this app we are going to learn :
1.How to create a GameLoop.
2.How to use surfaceView.
3.How to display the text and images.
4.How to implementing two classes in same java file.

Step 1: Lets create a new motodev project :
1

Step 2: In your res/drawble folder put bomb.png file.
Step 3: in your res/layout/main.xml put following code :
[xml]


[/xml]

Step 4: In the MainActivity.java put following code :
[xml]
package com.bombtimer;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
[/xml]

Step 5: Create a GameLoop.java class and put following code :

[xml]
package com.bombtimer;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class GameLoop extends SurfaceView implements SurfaceHolder.Callback {
class CounterThread extends Thread {
private boolean isRunning;
private int counter = 1000;
private SurfaceHolder refSurfaceHolder;
private Paint textPaint;

public CounterThread(SurfaceHolder surfaceHolder) {
refSurfaceHolder = surfaceHolder;
textPaint = new Paint();
textPaint.setARGB(255,255,255,255);
textPaint.setTextSize(32);
}

@Override
public void run() {
while (isRunning) {
Canvas c = null;
try {
c = refSurfaceHolder.lockCanvas(null);
synchronized (refSurfaceHolder) {
updateTimer();
doDraw(c);
}
}finally {
if (c != null) {
refSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}

private void updateTimer() {
counter -= 1;
}

private void doDraw(Canvas canvas) {
canvas.drawColor(Color.RED);
bombInstance.draw(canvas);
canvas.drawText(counter +” : Left Time “, getWidth() / 2, getHeight() / 2, textPaint);
canvas.restore();
}

public void setRunning(boolean b) {
isRunning = b;
}

}

private CounterThread thread;
private Bomb bombInstance;

public GameLoop(Context context, AttributeSet attrs) {
super(context, attrs);
bombInstance = new Bomb(BitmapFactory.decodeResource(getResources(), R.drawable.bomb), 300, 300);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
thread = new CounterThread(holder);
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}

public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}

public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
[/xml]

Step 7: Create a Bomb.java class and put following code :

[xml]
package com.bombtimer;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Bomb {

private Bitmap bitmap;
private int x;
private int y;
private boolean touched;

public Bomb(Bitmap bitmap, int x, int y) {
this.bitmap = bitmap;
this.x = x;
this.y = y;
}

public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isTouched() {
return touched;
}

public void setTouched(boolean touched) {
this.touched = touched;
}
public void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, x – (bitmap.getWidth() / 2), y – (bitmap.getHeight() / 2), null);
}

}
[/xml]

Step 8: Run your app it will look like this :

2

Tags: , , , , ,