Showing posts with label Game Development. Show all posts
Showing posts with label Game Development. Show all posts

Monday, December 3, 2012

Android game development : Moving or scrolling background


In game development in android, before we go further let have a look what we had achieved

    1) Basic science of game programming
    2) Creating surface to draw
    3) Creating dynamic object on surface and moving them

But in general , every game does not move their character much . They moves their background. But when a person plays, its seems everything is moving around. In android we draw an background with one bitmap.  And we start slowly moving background. This is important part game development.

Suppose one's want to develop a game in which, one's have one actor who's running all the way. Then real game scenerio will be like that

  • Animate actor on same place
  • Move the background in opposite direction

Moving background is quite simple, we scroll one image to some position and same image's another instance to respective direction to cover the space left by first one. and so on.......
This process need same image to draw continueously two times to fill the gap and never ending scrolling or moving of background

Look at the respective code in which backGround bitmap is background of canvas.....

 /**
* Draws current state of the game Canvas.
*/
private int mBGFarMoveX = 0;
private int mBGNearMoveX = 0;

private void doDrawRunning(Canvas canvas) {
// decrement the far background
mBGFarMoveX = mBGFarMoveX - 1;
// decrement the near background
mBGNearMoveX = mBGNearMoveX - 4;
// calculate the wrap factor for matching image draw
int newFarX = backGround.getWidth() - (-mBGFarMoveX);
// if we have scrolled all the way, reset to start
if (newFarX <= 0) {
mBGFarMoveX = 0;
// only need one draw
canvas.drawBitmap(backGround, mBGFarMoveX, 0, null);
} else {
// need to draw original and wrap
canvas.drawBitmap(backGround, mBGFarMoveX, 0, null);
canvas.drawBitmap(backGround, newFarX, 0, null);
}
}


                 

Download Source

Saturday, July 7, 2012

Android mobile game development part III : drawing and moving image on android surface view

Now we come closer to be able to make a simple 2D game in android. Before you start this new article which include

1) Create one canvas to draw bitmap or images on android surface view

2) Create dynamically unlimited images and move them on surface

Please see previous article to get more idea

Coordinate system in mobile gaming and creating some simple figure Like Square and Handle Touch event in android canvas 

I have created same thing in two ways First using Surface View and View.First make one User Interface to select an option , how you want to draw?  you can see the difference between performance by increasing number of images. So take one activity and make view like this.You need not worry as i posted full source code at the end


Now  we will need two separate class one to draw in SurfaceView and another using view.
Here we go with surface view and simple draw some bitmap inside canvas . I have taken one ArrauList to save information of coordinate where bitmap need to be draw that are created when you touch the surface.

    private Vector<Integer> xCoordinate = new Vector<Integer>();
private Vector<Integer> yCoordinate = new Vector<Integer>();
private ArrayList<Integer> speed = new ArrayList<Integer>();

speed array is to decide of bitmap. here is code to draw bitmap


package com.ahmad;

import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MoveImageUsingSurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
private Bitmap mBitmapMove;

public MoveImageUsingSurfaceView(Context context) {
super(context);
mBitmapMove = BitmapFactory.decodeResource(context.getResources(),
R.drawable.football);
setFocusableInTouchMode(true);
setWillNotDraw(false);
}

private int moving_speed = 5;
private int left = 0, right = 0;

@Override
protected void onDraw(Canvas canvas) {
setBackgroundColor(Color.GREEN);
CheckCorner(canvas);
super.onDraw(canvas);
}

/**
* <P>
* It will check corner and it will then reverse back
* </P>
*
* @param canvas
*/
private void CheckCorner(Canvas canvas) {
if (right > canvas.getHeight())
moving_speed = -moving_speed;

if (right < 0)
moving_speed = -moving_speed;

right = right + moving_speed;
canvas.drawBitmap(mBitmapMove, left, right, new Paint());

/**
* <html><B> Drawing bitmap on touching screen<B></html>
*/
if (xCoordinate.isEmpty()) {

} else {
int size = xCoordinate.size();
for (int i = 0; i < size; i++) {
int speedTemp = speed.get(i);
canvas.drawBitmap(mBitmapMove, xCoordinate.get(i),
yCoordinate.get(i), new Paint());
yCoordinate.set(i, (yCoordinate.get(i) + speedTemp));
if (yCoordinate.get(i) > canvas.getHeight()) {
speedTemp = -speedTemp;
}
if (yCoordinate.get(i) < 0) {
speedTemp = -speedTemp;
}
speed.set(i, speedTemp);

}
}
invalidate();
}

private Vector<Integer> xCoordinate = new Vector<Integer>();
private Vector<Integer> yCoordinate = new Vector<Integer>();

private ArrayList<Integer> speed = new ArrayList<Integer>();
private int speedLimit = 30;

@Override
public boolean onTouchEvent(MotionEvent event) {
Random random = new Random();
int Speed = random.nextInt(speedLimit);
xCoordinate.add(Math.round(event.getX()));
yCoordinate.add(Math.round(event.getY()));
speed.add(Speed);
invalidate();
return super.onTouchEvent(event);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
right = 0;
left = width / 2;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
}


I am not able to post Video here, only screen shot will be here .In screen shot, image are blur because of motions.and obviously it will not show movement just import this project and run



                                                      Download Source Code

        Article May you like on this site

Bitmap operation like re sizing , rotating etc ,

Displaying Bitmap efficiently without getting Error of memory overflow

Next article will be on how to detect collision and create one simple game of enemy killing

Sunday, June 17, 2012

Android Mobile Game Development Part II, Creating surface using android view and drawing some object on touch

This is next step in game development after  Game Development Basic Part I is to creating some real action.
Mobile game development is some what difficult as you need to consider its limitation of graphics and screen size but we will take it as challenge
There are three way to create surface and drawing object like circle, Rectangle and image

1) Using View  this is simplest way to design surface and drawing object on this. But its some what slow in comparison of next two option. Highly useful when you are creating game like Chess which does not require large number frame per second to draw

2) Using surface view Second option and say better option in comparison of first. But notable thing is that it is not much complex to step first but improve our game performance

3) Using Open GL or say GLSurface View  that is derived from c and c++. Highly recommend when your game include so many graphics and animation. And your game need huge amount of memory

So i will start from option at the end of this article we will learn  with source code of project and video

I. How to create simple surface
II. How draw some object
III. How to implement Touch event on View
IV.Drawing shape on touch dynamically

Start with basic step create one project Creating Surface.

Step 2). create another class called  OnsurfaceToDraw.java 

in this class , we create rectangle and circle and line. read little bit about Paint , View , Touch event


package com.ahmad;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;

public class OurSurfaceTodraw extends View {
private Context context;

public OurSurfaceTodraw(Context context) {
super(context);
this.context = context;
setWillNotDraw(false);
setFocusableInTouchMode(true);

}
long timedelay = SystemClock.currentThreadTimeMillis();
@Override
protected void onDraw(Canvas canvas) {
// Setting canvas background
setBackgroundColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.RED);
// Drawing Line on canvas
canvas.drawLine(50, 200, 0, 10, paint);
if ((SystemClock.currentThreadTimeMillis() + 3) > timedelay) {
paint.setColor(Color.BLACK);
canvas.drawRect(100, 100, 140, 140, paint);
}
// Drawing Rectangle or square
if ((SystemClock.currentThreadTimeMillis() + 6) > timedelay) {
paint.setColor(Color.BLACK);
canvas.drawRect(100, 100, 140, 140, paint);
}
// Drawing Rectangle or square
if ((SystemClock.currentThreadTimeMillis() + 6) > timedelay) {
paint.setColor(Color.BLACK);
canvas.drawCircle(100, 250, 20, paint);
}
if (mDynamic.isEmpty()) {

} else {
paint.setColor(Color.RED);
for (int i = 0; i < mDynamic.size(); i++) {
canvas.drawRect(mDynamic.get(i), paint);
}
}
super.onDraw(canvas);
}

private ArrayList<Rect> mDynamic = new ArrayList<Rect>();

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = Math.round(event.getX());
int y = Math.round(event.getY());
Rect rect = new Rect(x, y, x + 30, y + 20);
mDynamic.add(rect);
invalidate();
return super.onTouchEvent(event);
}
}

Notable Line in above code are

 Drawing a circle,Rectangle and Line

 canvas.drawCircle(100, 250, 20, paint);
 canvas.drawRect(100, 100, 140, 140, paint);
 canvas.drawLine(50, 200, 0, 10, paint);

If we want to draw Rectangle dynamically then store all rectangle into one ArrayList and then draw by getting each element.

invalidate(); This will ensure that while you change something on touch it will draw to canvas

 Video will show how this out put will work


                             

 You can download complete source from below given link


                                  Download Source Code


Saturday, June 16, 2012

Game Development Part I - Basic of Mobile Game Development

Game development is tricky task to start with. Specially if we are doing game development in mobile then we have to deal with some constraint like

1) Dealing with limited system configuration
2) Dealing with limited and small screen size and graphics

So these two constraint will applied for every mobile game developer . We will take it as  a challenge not a constraint.I am going to post a complete series including some game demo at last until you become a mobile game developer. Initially article are not related to any specific platform like iPhone, Android, Window but in later article i will focus on Android Game Development

Before start making a killer game in android we will focus what basic we need to learn first. Game development is much different from business application.
To be a game developer we need to be good -

1)Physics                         2) Coordinate

Today we will discuss about coordinate system of phone and how it differ from plane system to which we are more aware.

Plane coordinate system that we study in our education system contain origin, and two side other then origin. So we can go into positive direction or in negative direction. Look at figure

 
In plane we can move in three direction, if  we are moving along any axis, then following will be the cases

1) Move on x axis -> increase/decrease value of x , y will remain same
2)Move on y axis  -> increase/decrease value of y,  x will remain same
3)Move on both axises -> increase/decrease value of both axis as required

distance between two point on plane..if we have one initial point from where we start movement and final point where we stop our movement then simply we used -

                                                        http://www.mathopenref.com/images/coord/dist.gif
where dx=x2-x1; and dy=y2-y1; where (x1,y1) and (x2,y2) are two points
this is how plane work.

But while we are handling plane or say mobile screen then we have way to move only in two way although we can move in every direction but it will not be visible so our above image will change to following



So if we move our object from above the screen then it will be invisible.
Let an example of racing game, We kept our Car object always static and move our background track accordingly, its so fast and smooth that user feel that his/her car is moving to avoid obstacle .
If we are making racing game then our movement will like this



For Creating simple shape read this Basic Shape in coordinate


Ready for next article  how to create surface to draw some shape using view. Please never forget to keep your feedback here
Posting Lama ►
 

Copyright 2013 Tutorials For Newbie: Game Development Template by CB Blogger Template. Powered by Blogger