Android - Yahtzee application with shake detection

 
10/28/2010
Android
8 Comments

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 &gt; 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

Feedly Feedly Tweet


Related posts


Comments


Daniel

Daniel

2/11/2024

@Ivy: I'm sorry, but I don't have time for that.


Ivy

Ivy

2/10/2024

I know this post is almost 15 years old, but this is my favorite yahtzee game (I stumbled across it in 2020 during covid after playing a switch game like it). Is there any chance this app could get resigned or am I going to have to learn to code?


Android Example

Android Example

6/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>


Safvan

Safvan

5/7/2014

Excellent.. I appreciate your willingness to make it open... well done.


Daniel

Daniel

9/8/2012

@Nanito: Please have a look at: http://developer.android.com/sdk/installing/index.html There you will find detailed instructions.


Nanito

Nanito

9/8/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.


Daniel

Daniel

4/24/2012

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


Tugume

Tugume

4/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.