After the login part of the game,
the user can now play. When the page loads, there are additional items which
were not there before. The new features are the welcome message at the top left
of the screen, below which we find the current score. At the top right of the game there is a
counter going down. When the player hits the ship, the value of the counter
going down is added to the current score at the right of the screen. The game
can end in two ways. The first method is when the counter goes down to 0 and
the other method is when the ship reaches the cannon.
Figure 1: The Layout
of the Game
Also, on page load, there is a
table with the high scores called the hall of fame. The hall of fame is loaded
into a div called highscores to the right of the game.
<div
id="highscores"></br>Hall of Fame</br></br>
<table>
<thead>
<tr>
<th>Username</th><th>Highest
Score</th>
</tr>
</thead>
<tbody>
<?php
getHighScores();?>
</tbody>
</table>
</div>
The code above shows how the hall of fame is
generated. The body of the table is generated from the PHP function
getHighScores(). The code below shows the code generating the body of the
table:
$sql = mysql_query("SELECT username,
highscore FROM cannon_game.login order by
highscore desc, username limit 10;");
while (($row = mysql_fetch_array($sql))) {
print("<tr><td>".(ucwords($row[0]))."</td><td>".$row[1]."</td></tr>");
}
The code above prints the HTML
code together with the values returned from the query. It truncates the result
to the top 10 scores. Here we are using the function ucwords() to capitalize
the username.
Update the Score
The next part of this assignment
is to update the high score when the game is over. When the game is over, the
file comparehighscore.php is called passing the playerscore. This can be seen
in the image below:
Figure 2: Passing the
score to the PHP function
The username is obtained from the
session. The comparehighscore.php then calls a function with the user and the
score. The functions gets the score for the current user. Then, it compares the
current score to the score obtained from the database. If the current score is
larger than the score in the database, the score of the user is inserted into
the database. This is shown in the code below:
$sql = mysql_query("SELECT username,
highscore FROM cannon_game.login where
username='".mysql_real_escape_string($username)."';");
if
(($row = mysql_fetch_array($sql))) {
$oldscore
= $row[1]+0;
if
($score > $oldscore) {
mysql_query("UPDATE
login SET highscore='".mysql_real_escape_string($score)."' where
username='".mysql_real_escape_string($username)."';");
}
}
Logout Function
Another part is the logout function. This is a
very simple function that unsets the session and redirects the page to the
login.
function logout() {
unset($_SESSION['user']);
header("Location: ./login.php");
}
Hall of Fame
When the game is over, the game redirects to
the Hall of Fame Screen. The style is similar to the login screen, except for the
size. The hall of fame lists the top 10 high scores, along with the Username.
The screen has two buttons, Return to Game and Logout. Return to the game
redirects to the game, while keeping the session. This means that the player
can try again. By pressing the logout button, the player can logout and the
game redirects to the login screen. This will allow another player to play the
game. Below is a shot of the Hall of Fame with the sample data.
Figure 3: Hall of Fame with the sample data.
In
the next part of the blog I will talk about some more features.
No comments:
Post a Comment