Posts Tagged ‘Game Programming’

Blinking Santa Handling alpha feature

December 16th, 2011 by aabhaanjan

In this article we are going to learn how to make an blinking image on touch or click.
There are following steps we need to follow to make Blinking Santa.

Step 1: Create a Project using Motodev. The options will look like this.
1

Step 2: in your res/drwable folder put the image santa.png

Step 3. Open the layout/Main.xml and put following codes

[xml]


[/xml]

Step 4. Open the MainActivity.java and put following codes
[xml]

package com.blinkingsanta;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.Animation.AnimationListener;

import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener, AnimationListener {

private ImageView _santaImage;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

_santaImage = (ImageView) findViewById(R.id.image);

_santaImage.setOnClickListener(this);

}

@Override

public void onClick(View view) {

if (view.getId() == R.id.image) {

Animation animation;

if (_santaImage.getVisibility() == View.VISIBLE) {

animation = new VisibilityClass(3, true);
}

else
{

animation = new VisibilityClass(3, false);

}

animation.setDuration(1000L);

animation.setAnimationListener(this);
_santaImage.startAnimation(animation);

}

}

@Override

public void onAnimationEnd(Animation animation) {

_santaImage.setVisibility(View.VISIBLE);

}

@Override

public void onAnimationRepeat(Animation animation) {

}

@Override

public void onAnimationStart(Animation animation) {

}

}

[/xml]

Step 5. Create a new class VisibilityClass.java and put following codes

[xml]

package com.blinkingsanta;

import android.util.FloatMath;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class VisibilityClass extends Animation {
private int _totalNumber;
private boolean _isAnimFinished;

public VisibilityClass(int _totalNumber, boolean _isAnimFinished) {
this._totalNumber = _totalNumber;
this._isAnimFinished = _isAnimFinished;
}

@Override
protected void applyTransformation(float _interpolatedTime, Transformation _trans) {
float period = _interpolatedTime * _totalNumber * 4.12f + (_isAnimFinished ? 4.12f / 2 : 0);
_trans.setAlpha(Math.abs(FloatMath.cos(period)));
}

}

[/xml]

Step 6. Run the app and Click on Santa . It should look like this 🙂

2

Tags: , , , , , , , , , , , , ,

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: , , , , ,

Move The Apple on TouchEvent

December 2nd, 2011 by aabhaanjan

The most important technical points you are going to learn are :
SurfaceView : The SurfaceView punches a hole in its window to allow its surface to be displayed.
getHolder(): By calling getHolder()we can access to the underlying surface
surfaceCreated(SurfaceHolder): Gives an idea of when the Surface is created.
surfaceDestroyed(SurfaceHolder):Gives an idea of when the Surface is destroyed
SurfaceHolder.addCallback :The Callback is set with SurfaceHolder.addCallback method

Step 1. Create a new android motodev project.

1

Step 2 : Put a apple.png in your res/drwable folder.
Step 3 : In your MainActivity class paste following codes :
[xml]
package com.ballmovementontouch;

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

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
}
}
[/xml]

Step 4: Create a new class GameView and paste following codes :
[xml]
package com.ballmovementontouch;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements
SurfaceHolder.Callback {
private MainThread thread;
private Apple appleInstance;

public GameView(Context context) {
super(context);
getHolder().addCallback(this);
appleInstance = new Apple(BitmapFactory.decodeResource(getResources(), R.drawable.apple), 50, 50);
thread = new MainThread(getHolder(), this);
setFocusable(true);
}

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

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

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {

}
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
appleInstance.handleActionDown((int)event.getX(), (int)event.getY());
if (event.getY() > getHeight() – 50) {
thread.setRunning(false);
((Activity)getContext()).finish();
}
} if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (appleInstance.isTouched()) {
appleInstance.setX((int)event.getX());
appleInstance.setY((int)event.getY());
}
} if (event.getAction() == MotionEvent.ACTION_UP) {
if (appleInstance.isTouched()) {
appleInstance.setTouched(false);
}
}
return true;
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
appleInstance.draw(canvas);
}

}
[/xml]

Step 5: Create an Apple Class and paste following code :
[xml]
package com.ballmovementontouch;

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

public class Apple {
private Bitmap bitmap;
private int x;
private int y;
private boolean touched;

public Apple(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);
}

public void handleActionDown(int eventX, int eventY) {
if (eventX >= (x – bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) { if (eventY >= (y – bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) { setTouched(true); } else { setTouched(false); } } else { setTouched(false); } } } [/xml] Step 6: Now Run your app and move the apple on touching it. It will look like this : 2

Tags: , , , , , , , ,

The Key movement for a particular object

November 30th, 2011 by aabhaanjan

Lets learn today how to handle the the Key movement for a particular object.
Here I have used a ball as object wich will move 10 px according to the key:

There are following steps :
1. Create a new motodev project ‘Ballmovement’.
New_Project

2. Your AndroidManifest.xml will look like this

[xml]










[/xml]

3. Put the ball_1.png in your res/drwable folder

4. Now put following code in ManiActivity.java

[xml]
package com.ballmovent;

import android.app.Activity;

import android.os.Bundle;

import android.view.Window;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(new GameView(this));

}

}
[/xml]

5. Create a new class GameView.java. Now put following code in GameView.java

[xml]

package com.ballmovent;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import com.ballmovent.ball;

public class GameView extends View {

private ball ballInstance;
public static final int UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3;

public GameView(Context context) {
super(context);
ballInstance = new ball(BitmapFactory.decodeResource(context.getResources(),R.drawable.ball_1), 50, 50);
setFocusable(true);
this.setFocusableInTouchMode(true);
}

@Override

protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
ballInstance.draw(canvas);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent evt) {
boolean moved = false;
Log.v(“aks 1″+ moved, “index=” + 1);
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
moved = ballInstance.move(UP);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
moved = ballInstance.move(DOWN);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
moved = ballInstance.move(RIGHT);
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
moved = ballInstance.move(LEFT);
break;
default:
return super.onKeyDown(keyCode,evt);
}
if(moved) {
//the ball was moved so we’ll redraw the view
ballInstance.setX((int)ballInstance.getX());
ballInstance.setY((int)ballInstance.getY());
invalidate();
}
return true;
}

}

[/xml]

7. Create a new class ball.java. Now put following code in ball.java for varous function etc

[xml]
package com.ballmovent;

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

public class ball {

private Bitmap bitmap;
private int x;
private int y;
public static final int UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3;

public ball(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 void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, x – (bitmap.getWidth() / 2), y – (bitmap.getHeight() / 2), null);
}

public boolean move(int direction) {
boolean moved = false;
if(direction == UP) {
y -= 10;
moved = true;
}
if(direction == DOWN) {
y += 10;
moved = true;
}
if(direction == RIGHT) {
x += 10;
moved = true;
}
if(direction == LEFT) {
x -= 10;
moved = true;
}
return moved;
}

}

[/xml]

8. Run your application and check the movement of ball by pressing keys. Your app will look like this :

ball_movement_on_key

Tags: , , , , , , , , , , ,

How to create Christmas Card Gallery using Grid view

November 18th, 2011 by aabhaanjan

Lets develop a Christmas Card Gallery application. We will need to follow these steps :
Step 1: Create a new android project using motodev studios.
Step 2: Choose the name and SDK version for the app. Your AndroidManifest.xml will look
like this :
[xml]










[/xml]

Step 3: Put your app launcher icon image into res/drawable folder.
Step 4: Open your res/layout/main.xml and put following code there :

[xml]


[/xml]

Step 5 : Write your app name into res/value/String.xml

[xml]


Hello World
Christmas Card gallery

[/xml]

Step 6 : write following code in your MainActivity.java file

[xml]
package com.cardGallery;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity
{
//—the images to display—
Integer[] imageIDs = {
R.drawable.card1,
R.drawable.card2,
R.drawable.card3,
R.drawable.card4,
R.drawable.card5,
R.drawable.card6,
R.drawable.card7,
R.drawable.card8,
R.drawable.card9,
R.drawable.card10,
R.drawable.card11,
R.drawable.card12,
R.drawable.card13,
R.drawable.card14,
R.drawable.card15

};

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

GridView gridView = (GridView) findViewById(R.id.cardview);
gridView.setAdapter(new ImageAdapter(this));

gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent,
View v, int position, long id)
{
Toast.makeText(getBaseContext(),
“Card” + (position + 1) + ” selected”,
Toast.LENGTH_SHORT).show();
}
});
}

public class ImageAdapter extends BaseAdapter
{
private Context context;

public ImageAdapter(Context c)
{
context = c;
}

//—returns the number of images—
public int getCount() {
return imageIDs.length;
}

//—returns the ID of an item—
public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

//—returns an ImageView view—
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
return imageView;
}
}
}
[/xml]

Step 7 : Run your application it will look like this.
result

Step 8 : In main menu your app icon will be shown like this :
app_icon

Tags: , , , , , , , , , , , , , ,