Due: Tuesday June 2, 2015, end of day.
In this assignment, you will program a very simple game, called The Drain.
Make a drawing on the screen (via a View, obviously) as in the drawing below. Some small variation / improvement on the drawing is possible; just keep the general idea.
This represents a drain with a drain-hole (below, gray); two dirty objects left inattentively in the sink by your house-mates, and a bright red cherry tomato. The goal of the game is to send the cherry tomato down the drain. Once the cherry tomato falls down the drain, a new cherry tomato materializes at the top of the screen (and the monster living in the sink piping emits a burping noise -- this is optional though). Note: my daughter actually suggests that the monster makes a disgusted noise and spits the tomato back up in the game to its original position, but this again is optional.
The motion of the tomato is governed by the following rules. Let vx, vy be the velocity of the tomato in the x and y directions.
Note: to detect whether the tomato hits one of the partitions, it helps to consider the partition as a partition line from (0, py) to (px, py), and of the point (px, py). Then, to check whether a tomato of center (x, y) and radius r is hitting the partition, you can do:
If you are hitting the flat partition, simply bounce along the horizontal axis of the partition. If you are hitting the endpoint... just model what would happen to an infinitely elastic and rigid sphere that has no friction.
You don't need to worry about modeling the angular rotation of the tomato, in particular, and the friction effects on the walls. Although it would definitely be fun if you did.
To do this assignment, study carefully how they do in the tutorial to do the animation (as described in class as well): basically:
You need to read the inclination sensors. You can do it by simply reading the accelerometer, which gives you the direction of the gravity acceleration (plus any additional shaking you do). Here is sample code. Please check I got the signs right for the acceleration (it will be pretty obvious in practice). This code updates activity variables accelx
and accely
. In the tomato object, you can just look at the value of these variables, and update the state accordingly. The point is that you cannot get the acceleration from the sensors when you need it. You need to register a listener (as done below), that will give you the acceleration when it has it, and stores the values. Then the tomato reads the acceleration when it needs.
Note that you need to add the permission in the AndroidManifest.xml
in order to use the sensors.
((SensorManager) getSystemService(Context.SENSOR_SERVICE)).registerListener(
new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// I hope I got the signs right. If not, experiment with this.
accelx = -event.values[0];
accely = event.values[1];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {} //ignore
},
((SensorManager)getSystemService(Context.SENSOR_SERVICE))
.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0) SensorManager.SENSOR_DELAY_GAME);