1. Miscellaneous (Working with Touch Input)
Short Version
The canvas offers functionality for tapping on the screen and getting information about the tap:
Touch touch = canvas.waitForTouch(); // app will pause until screen is tapped
double touchX = touch.getX(); // ask the touch point for its x-coordinate
double touchY = touch.getY(); // ask the touch point for its y-coordinate
int taps = touch.getTaps(); // ass the touch point how many times tapped
If we draw a draw a row of circles that represent buttons, we can use the x-coordinate and the
circle radius to compute the index of the circle that was tapped (i.e. 1,2,3,etc) as follows:
index = (int)(touchX / (2*radius)) + 1; // compute index of touched circle
Long Version -- Drawing a keypad
Here is how we can create our own keypad so the player can enter the numbers on the screen.
The goal is to display 10 circles in a row such that:
- the keypad (the row) is centered vertically in the middle of the screen
- the left-most circle is touching the edge of the screen
- the radius of each circle is 15
Here is the code for displaying the keypad on the screen:
double radius = 15;
double midY = canvas.getHeight() / 2;
drawCirclesRow( radius, midY, radius, 10 );
------ ---- ------ --
startX startY size n
Note: Since the first circle has to touch the left edge of the screen
its center is at startX=radius.
The keypad is supposed to work as follows:
- if the player taps a particular circle once, then we assume that
the selected number equals the index of the circle in the line
- if the player taps a particular circle twice, then we assume that
the selected number equals the index of the circle in the line + 10
With this keypad we can enter numbers from 1-20 just with a touch on the screen.
Working with Touch Input
Here is the code for obtaining and manipulating touch input from the player:
double radius = 15;
double midY = canvas.getHeight() / 2;
drawCirclesRow( radius, midY, radius, 10 );
Touch touch = canvas.waitForTouch(); // wait until player touches the screen
double touchX = touch.getX(); // get the x-coord of the touch
double touchY = touch.getY(); // get the y-coord of the touch
int taps = touch.getTaps(); // get the number of taps
guess = (int)(touchX / (2*radius)) + 1; // compute index of touched circle: 1, 2, 3, ...
The last line may seem confusing, but just copy it and know that it will compute
the correct index of the circle that was touched.
Adding Double Tap
The previous version disregarded the number of taps, so the keypad can only enter
numbers from 1-10. We can modify it, so that whenever the player taps a circle twice
we add 10 to the index, which will increase the range of the keypad to 1-20.
double radius = 15;
double midY = canvas.getHeight() / 2;
drawCirclesRow( radius, midY, radius, 10 ); // draw keypad: 10 circles of given radius
Touch touch = canvas.waitForTouch(); // wait until player touches the screen
double touchX = touch.getX(); // get the x-coord of the touch
double touchY = touch.getY(); // get the y-coord of the touch
int taps = touch.getTaps(); // get the number of taps
guess = (int)(touchX / (2*radius)) + 1; // compute index of touched circle
if (taps == 2)
{
guess = guess + 10; // 1 becomes 11, 2 becomes 12, 3 becomes 13, ...
}
Here is the final code:
void playGuessingGame( int low, int high, int maxTrials )
{
double hintX = canvas.getWidth() / 2;
double hintY = canvas.getHeight() / 2;
// int secret = (low + high) / 2;
int secret = canvas.getRandomInt( low, high ) / 2;
int guess = canvas.readInt( "Guess a number" );
int trials = 1;
double totalPenalty = 0;
while( guess != secret && trials < maxTrials )
{
canvas.clear();
if (guess < low || guess > high)
{
canvas.drawText( hintX, hintY, "Guess is out of range!!!", "red" );
}
else
{
// computing the new penalty and updating total
double curPenalty = computePenalty( guess, secret );
totalPenalty = totalPenalty + curPenalty;
// displaying the penalty to the player
canvas.drawText( hintX, hintY,
"Total Penalty so far is " + totalPenalty + " points.", "red" );
}
// ***replaced*** guess = canvas.readInt( "Guess a number" );
double radius = 15;
double midY = canvas.getHeight() / 2;
drawCirclesRow( radius, midY, radius, 10 ); // draw keypad: 10 circles of given radius
Touch touch = canvas.waitForTouch(); // wait until player touches the screen
double touchX = touch.getX(); // get the x-coord of the touch
int taps = touch.getTaps(); // get the number of taps
guess = (int)(touchX / (2*radius)) + 1; // compute index of touched circle
if (touch.getTaps() == 2)
{
guess = guess + 10; // 1 becomes 11, 2 becomes 12, 3 becomes 13, ...
}
trials = trials + 1;
}
canvas.clear();
if (guess == secret)
{
canvas.drawText( hintX, hintY, "Congratulations, you win!!!", "blue" );
}
else
{
canvas.drawText( hintX, hintY, "Sorry, play again!!!", "blue" );
}
}