How to develop android games?
Developing android games is simpler than you think.
Check out this android game. Which is built using this technique.
Save Super Penguins
https://play.google.com/store/apps/details?id=com.positivesthinking.parachutepenguinsfree
Before starting the tutorial familiar yourself with android.
1. Set up the Android Workspace
https://developer.android.com/training/basics/firstapp/index.html
2. Create your Hello World Android application
https://developer.android.com/training/basics/firstapp/creating-project.html
Lets start the game development.
Just follow this Four Steps to start your first simple android game.
Step 1: Create a Game Loop Thread that renders the Frames.
Step 2: Create a SurfaceView class that will show the game UI and controls the game.
Step 3: Create a Image handler class that will help you in collision detection and loading bitmap images.
Step 4: Wire up your MainActivity.
Post your comments in case of any difficulty and help.
Step 1: Create a Game Loop Thread that renders the Frames.
MainThread.java.
package com.coolcomputerpctricks.tutorialgame;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
/**
* www.coolcomputerpctricks.com
* Android Game development tutorial *
*/
public class MainThread extends Thread {
private static final String TAG = MainThread.class.getSimpleName();
// Frames Per seconds
public int MAX_FPS = 150;
private int FRAME_PERIOD = 1000 / MAX_FPS;
private SurfaceHolder surfaceHolder;
private ViewGamePlay gamePanel;
private boolean running;
public void setRunning(boolean running) {
this.running = running;
}
public MainThread(SurfaceHolder surfaceHolder, ViewGamePlay gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
@Override
public void run() {
Canvas canvas;
long beginTime;
long timeDiff;
int sleepTime;
sleepTime = 0;
while (running) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
beginTime = System.currentTimeMillis();
this.gamePanel.render(canvas);
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {}
}
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
Step 2: Create a SurfaceView class that will show the game UI and controls the game.
ViewGamePlay.java
package com.coolcomputerpctricks.tutorialgame;
import java.util.Random;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* www.coolcomputerpctricks.com
* Android Game development tutorial *
*/
public class ViewGamePlay extends SurfaceView{
MainThread thread;
ItemImages bgImage,parachute;
int max,min;
public ViewGamePlay(Context context) {
super(context);
thread = new MainThread(getHolder(), this);
getHolder().addCallback(new SurfaceHolder.Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.setRunning(false);
thread.join();
retry = false;
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
Canvas c = holder.lockCanvas(null);
initializeGame(c);
drawGame(c);
holder.unlockCanvasAndPost(c);
thread.setRunning(true);
try{
thread.start();
}catch(Exception e){
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//Surface changed
}
});
}
public void initializeGame(Canvas canvas){
bgImage = new ItemImages(BitmapFactory.decodeResource(getResources(), R.drawable.sky),0,0);
parachute = new ItemImages(BitmapFactory.decodeResource(getResources(), R.drawable.parachute),0,0);
//Random left position for the parachute
max=(int) (canvas.getWidth()-parachute.getWidth());
min=0;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
parachute.setLeft(randomNum);
}
protected void drawGame(Canvas canvas) {
bgImage.drawBMP(canvas);
parachute.drawBMP(canvas);
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if(parachute.isCollition(x, y))
{
// If player touched, reset the parachute location
parachute.setTop(0);
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
parachute.setLeft(randomNum);
}
return true;
}
public void render(Canvas canvas) {
parachute.setTop(parachute.getTop()+2);
if(parachute.getTop()>canvas.getHeight()){
parachute.setTop(0);
// Reset the parachute location
parachute.setTop(0);
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
parachute.setLeft(randomNum);
}
drawGame(canvas);
}
}
Step 3: Create a Image handler class that will help you in collision detection and loading bitmap images.
ItemImages.java
package com.coolcomputerpctricks.tutorialgame;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
/**
* www.coolcomputerpctricks.com
* Android Game development tutorial *
*/
public class ItemImages {
private float width;
private float height;
private float left;
private float top;
private Bitmap bmp;
private boolean visible;
public ItemImages(Bitmap bmp,float left, float top)
{
this.width = bmp.getWidth();
this.height = bmp.getHeight();
this.bmp=bmp;
this.left=left;
this.top=top;
this.setVisible(true);
}
public void setBmp(Bitmap bmp) {
this.bmp = bmp;
}
public void drawBMP(Canvas canvas) {
if(canvas!=null && bmp!=null)
canvas.drawBitmap(bmp, left, top, null);
}
public void drawBMP(Canvas canvas,Rect src,Rect dst) {
if(canvas!=null && bmp!=null)
canvas.drawBitmap(bmp, src, dst, null);
}
public boolean isCollition(float x2, float y2) {
return x2 > left && x2 < left + width && y2 > top && y2 < top + height;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getTop() {
return top;
}
public void setTop(float top) {
this.top = top;
}
public float getLeft() {
return left;
}
public void setLeft(float left) {
this.left = left;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
Step 4: Wire up your MainActivity.
MainActivity.java
package com.coolcomputerpctricks.tutorialgame;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
/**
* www.coolcomputerpctricks.com
* Android Game development tutorial *
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( new ViewGamePlay(this));
}
}
Here is the two images used in this tutorial
Sky.png
parachute.png
Download Project here
GitHub -
https://github.com/karthikeyan98/SimpleGame-AndroidExample
Post your comments for any doubts and helps.
Google's Fun Facts