Daniel Palme

Daniel Palme

.NET consultant from Germany.

Blog > Android - Yahtzee application with shake detection

Android - Yahtzee application with shake detection

During my experiments with Android, I have created a Yahtzee application with shake detection.
In this post I will give you an overview of the application and show you how to implement shake detection.
I also provide the full source code and an APK file, so you could install the game on your phone.

Features

The application has the following features:

  • 1-4 players
  • Shake to roll
  • Highscores stored in SQLite database

Yahtzee Yahtzee

Shake detection

The dices can be rolled by either pressing a button or by shaking the phone. For shake detection you need to create a SensorEventListener to monitor the accelerometer:

this.sensorMgr = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);

List<Sensor> sensors = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);

if (sensors != null && sensors.size() > 0) {
  sensorMgr.registerListener(this,
    sensors.get(0),
    SensorManager.SENSOR_DELAY_GAME);
}

Now you have to implement the onSensorChanged method to react on shaking:

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
  long curTime = SystemClock.uptimeMillis();
  // Ignore events that have occurred too early to get better speed approximation.
  if ((curTime - lastUpdate) > 200) {
    long diffTime = (curTime - lastUpdate);
    lastUpdate = curTime;

    x = sensorEvent.values[SensorManager.DATA_X];
    y = sensorEvent.values[SensorManager.DATA_Y];
    z = sensorEvent.values[SensorManager.DATA_Z];

    float speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
  
    if (speed > SHAKE_THRESHOLD) {
      // Shake detected
      // TODO: react on shake
    }
  
    last_x = x;
    last_y = y;
    last_z = z;
  }
}

Installation

The download contains the full source code and the file bin/Yahtzee.apk. To install the game on your Android device, place the file on your SD card or phone memory and use the Apps Installer.

Downloads

Yahtzee.zip


Subscribe to RSS Feed

Tags: Android
 

Related posts

 

New comment

:

:

:

:

 

Comments

#1
Tugume

Tugume

04/24/2012

Hello Palme,I am impressed with your application and wanted to implement it.I would also wish to add in more features as well.Do you have any other accelerometer related applications? I would like to know how to use the motion sensor event to create a shuffle media player like for and Ipod.any ways. thanks alot brother for sharing.wish you all the best.
 
#2
Daniel

Daniel

04/24/2012

@Tugume:
I've only created this single application using the accelerometer.
 
#3
Nanito

Nanito

09/08/2012

Hi Daniel,
I am new to apps, can I use Eclipse to edit the source code?
or there is another development tool I should use?
How do I open the project in Eclipse?
Thank you in advance.
 
#4
Daniel

Daniel

09/08/2012

@Nanito:
Please have a look at:
http://developer.android.com/sdk/installing/index.html

There you will find detailed instructions.
 
#5
Safvan

Safvan

05/07/2014

Excellent.. I appreciate your willingness to make it open... well done.
 
#6
Android Example

Android Example

06/30/2017 | http://androidexample.com/

Hi dude, i have also find out one good example
<a href="http://androidexample.com/Accelerometer_Basic_Example_-_Detect_Phone_Shake_Motion/index.php?view=article_discription&aid=109">
Accelerometer Basic Example - Detect Phone Shake Motion </a>