
376 MAKING THINGS TALK
if (xPos < 0) {
rightScore++;
resetBall();
}
// if the ball goes off the screen right:
if (xPos > width) {
leftScore++;
resetBall();
}
// stop the ball going off the top or the bottom of the screen:
if ((yPos - ballSize/2 <= 0) || (yPos +ballSize/2 >=height)) {
// reverse the y direction of the ball:
yDirection = -yDirection;
}
// update the ball position:
xPos = xPos + xDirection;
yPos = yPos + yDirection;
// Draw the ball:
rect(xPos, yPos, ballSize, ballSize);
}
void resetBall() {
// put the ball back in the center
xPos = width/2;
yPos = height/2;
}
Chapter 3
Modified Date page
Language: PHP
Prints the date. But no HTML.
<?php
// Get the date, and format it:
$date = date("Y-m-d h:i:s\t");
// Include the date:
echo "< $date >\n";
?>
Parameter reader
Language: PHP
Prints any parameters sent in using an HTTP GET command.
<?php
// print the beginning of an HTML page:
echo "<html><head></head><body>\n";
// print out all the variables:
foreach ($_REQUEST as $key => $value)
{
echo "$key: $value<br>\n";
}
// finish the HTML:
echo "</body></html>\n";
?>
Age checker
Language: PHP
Expects two parameters from the HTTP request:
• name (a text string)
• age (an integer)
Prints a personalized greeting based on the name and age.
<?php
// print the beginning of an HTML page:
echo "<html><head></head><body>\n";
// read all the parameters and assign the