[Site map]
You are here ---
> Newbies Paradise
> Tutorials
> Unofficial
> Website
> PHP & MySQL
> Reading a tutorial
A simple PHP game
You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
Hi folks
Some of you may have the dream of opening you own games-website. The problem is, that you don't know how to create a game to put inside it.
Here is the solution! I will teach you how to program an easy-to-do game, which despite its simpleness can hold you for hours. It is a perfect example to start off with in the games business.
This tutorial is mainly written for beginners, but it can be fun to read if you are an advanced programmer or if you just wonder what the game is
I know you are dying to know what the game is.
Here is what it will be:
The player has to pick a number between 1 and 10. Then he has either won or lost.
Isn't that called a lottery?
Indeed it is! but the problem is, PHP can't draw coloured balls rolling around. So we have to find a way to make him do it.
This is how we will proceed:
- Firstly, the player chooses a number.
- Secondly, the number is transmitted to the computer.
- Thirdly, the computer generates one number too.
- Fourthly, the computer compares the two number (no cheating
)
- Finally, the computer says whether you have won or lost.
Easy as pie? We will see
Since we are for the protection of the environment, we do not like to waste stuff.
What the hell is he on about?
OK, here is what it was supposed to mean:
We want to be as light and simple as possible. Therefore, we shall use only one single page for the whole game. You will see, it is not that complicated
This is our blank page:
Code: PHP 1
2
3
4
5
6
7
8
9
10
11 | <html>
<head>
<title>The Secret Number</title>
</head>
<body>
<div align="center">
This is where we will insert the text.
</div>
</body>
</html>
|
First thing to do is determine whether we want to display the page where you choose your number or the one that tells you you have lost

or won

.
Easy Peasy Lemon Squeezy!
Just think a minute (at least try

). What will be the varying factor that determines what the computer should do?
Spoiler (click to show)THE CHOSEN NUMBER!
If a number has been chosen, then it means you have already been on the first page, so the computer knows he has to do the rest of the work. If no number has been chosen, then you need to or the game won't work properly.
Let's assume we will use a form to send the chosen number (which we will). The number will be stored in the following variable:
$_POST['number']
We therefore need to look for that variable. Here is the code to do so:
Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | <?php
//if the player has sent a number
if (isset ($_POST['number']))
{
//2nd page:
//the computer does its part of the job
}
//if no number was sent
else
{
//1st page
//we display the form to play
}
?>
|
Notice the second page comes before the first. You can as well invert the two pages, but I personally prefer and isset than a !isset.
Now let's get into the more interesting work
We will begin with the first page, that is the one containing the form.
We have the choice between different styles. It can either be a simple text input where you type your number (practical when the choice of numbers is greater), or a group of radio inputs (the little circular ones), or also a scroll-down list of all the numbers.
Personally, I prefer the second option, which I think looks more professional. Moreover, we will only have the choice between numbers 1 to 10, so this will be the most intuitive method in our case.
Here is our page:
Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | //we display the form to play
?>
<h1><font color="red"><u>The Secret Number</u></font></h1>
<p>
<form action="number.php" method="post">
<label><input type="radio" name="number" value="1" />1</label> |
<label><input type="radio" name="number" value="2" />2</label> |
<label><input type="radio" name="number" value="3" />3</label> |
<label><input type="radio" name="number" value="4" />4</label> |
<label><input type="radio" name="number" value="5" />5</label> |
<label><input type="radio" name="number" value="6" />6</label> |
<label><input type="radio" name="number" value="7" />7</label> |
<label><input type="radio" name="number" value="8" />8</label> |
<label><input type="radio" name="number" value="9" />9</label> |
<label><input type="radio" name="number" value="10" />10</label> |
<br /><br /><input type="submit" value="Play" />
</form>
</p>
<?php
|
This works fine!
But, as I said earlier, we want to save space. This long list of inputs looks ugly. And what if we want to choose between 10 000 different numbers? This code is rubbish. We need something more efficient.
This is where loops come in handy. We will have an occasion for using
for loops (yes, the ones you never understood what the point was!).
We will use PHP to display all the inputs for us using a loop, thus saving time and space. And we will add a useful function to this: the start and end numbers between which you have to choose.
Let's set the variables for the starting and ending number:
Code: PHP1
2
3 | //set start and end numbers
$start_number = 1;
$end_number = 10;
|
Now we will build our loop for the display of the form.
The first parameter of the loop is the starting number for the loop.
We will call the variable
$loop_number. This is where the
$start_number will be used.
Code: PHP1 | for ($loop_number = $start_number
|
The second parameter is the condition.
Here, we want the loop to continue working until the number reaches the
$end_number value.
Code: PHP1 | for ($loop_number = $start_number, $loop_number <= $end_number
|
And the third parameter is the operation done on the variable every time the loop goes round. Here, we need to increment
$loop_number.
Code: PHP1 | for ($loop_number = $start_number, $loop_number <= $end_number, $loop_number++)
|
Now our loop is ready, we just have to display a radio input with value="$loop_number" and display $loop_number just next to it.
Here is the final code:
Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | <html>
<head>
<title>The Secret Number</title>
</head>
<body>
<div align="center">
<?php
//set start and end numbers
$start_number = 1;
$end_number = 10;
//if the player has sent a number
if (isset ($_POST['number']))
{
//to do
}
//if no number was sent
else
{
//we display the form to play
?>
<h1><font color="red"><u>The Secret Number</u></font></h1>
<p>
<form action="number.php" method="post">
<?php
for ($loop_number = 1, $loop_number <= 10, $loop_number++)
{
echo '<label><input type="radio" name="number" value="'.$loop_number.'" />'.$loop_number.'</label> | ';
}
?>
<br /><br /><input type="submit" value="Play" />
</form>
</p>
<?php
}
?>
</div>
</body>
</html>
|
Now doesn't that look cleaner? The
for loop is, in this case much more handy than the
while.
Our page is ready, now we need to program the second page that determines whether you have lost or won.
Now this is the most interesting part of all.
The player has sent a number using the form; the script has jumped to the first part of the condition.
First reaction to have is secure the variable (it could have been sent from another server, who knows?)
Code: PHP1 | $number = htmlentities(addslashes($_POST['number']));
|
Now we want the computer to generate his own number.
To do that, we need the rand() function. It will generate an
int number (that means -5, 0, 2, 5486, ... it's just a number without a dot in the middle). It only requires two parameters, and guess what... they are the starting number and ending number. Exactly what we needed! So we won't have to bother creating our own function to do it.
Code: PHP1 | $winning_number = rand($start_number, $end_number);
|
We are almost finished. We must now compare the two numbers to see if they are different or not.
Code: PHP 1
2
3
4
5
6
7
8
9
10
11 | if ($number == $winning_number) //if it is correct
{
//then we write a small message telling the player he has won
echo '<p>Congratulations! You have picked number <font color="red"><b>'.stripslashes($number).'</b></font>, and that was the right choice to make!</p>';
}
else //if it is not the correct number
{
//then we display a message telling the player he was wrong, and we show the number he should have picked
echo '<p>Bad luck! You chose number <font color="red"><b>'.stripslashes($number).'</b></font>, and number <font color="red"><b>'.$winning_number.'</b></font> was picked!</p>';
}
|
That's it, our game works. Last thing to do, is put a link for people to play again:
Code: PHP1 | <a href="number.php">Play again</a>
|
Now just put everything together (your brains first, then the pieces of script

)
You can't be bothered? All right, I've done it for you (just because I'm a very very nice and pleasant person

)
Here is the final game which I know will occupy your long hours of loneliness
Spoiler (click to show)Code: PHP 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 | <html>
<head>
<title>The Secret Number</title>
</head>
<body>
<div align="center">
<?php
//set start and end numbers
$start_number = 1;
$end_number = 10;
//if the player has sent a number
if (isset ($_POST['number']))
{
//secure variables as always
$number = htmlentities(addslashes($_POST['number']));
//we generate a random number
$winning_number = rand($start_number,$end_number);
//now we must check if the player-chosen number is correct:
if ($number == $winning_number) //if it is correct
{
//then we write a small message telling the player he has won
echo '<p>Congratulations! You have picked number <font color="red"><b>'.stripslashes($number).'</b></font>, and that was the right choice to make!</p>';
}
else //if it is not the correct number
{
//then we display a message telling the player he was wrong, and we show the number he should have picked
echo '<p>Bad luck! You chose number <font color="red"><b>'.stripslashes($number).'</b></font>, and number <font color="red"><b>'.$winning_number.'</b></font> was picked!</p>';
}
//then we display a "retry" link
echo '<p><a href="number.php">Play again</a></p>';
}
//if no number was sent
else
{
//we display the form to play
?>
<h1><font color="red"><u>The Secret Number</u></font></h1>
<p>
<form action="number.php" method="post">
<?php
//we don't want to waste space typing 10 times the same tag, so we will use a loop:
for ($loop_number = $start_number; $loop_number <= $end_number; $loop_number++)
{
echo '<label><input type="radio" name="number" value="'.$loop_number.'" />'.$loop_number.'</label> | ';
}
?>
<br /><br /><input type="submit" value="Play" />
</form>
</p>
<?php
}
?>
</div>
</body>
</html>
|
That's it! you can launch your own game website.
Since the game is a bit repetitive, you can customize it with your own ideas.
If you really can't find some, here are a few that you might want to consider:
- vary the number of possible choices
- use sessions to add virtual money to the game
- create a statistics page
- of course, add some design to it (I deliberately put non, otherwise you would have laughed at me; my CSS is crappy
- insert more forms (pick a $number1, $number2 ...) to recreate a real lotto (and harder to win too)
- save all the numbers picked by everyone in a database and generate the computer's number only once every day. Then you can share the money between all the winners of the day.
- can't think of anything else
Good luck and keep programming
==<CmOiRomaiN>==
Change your template |
Learn more about NP |
Site map |
Terms of use |
Rules |
RSS feed |
XHTML 1.0 |
CSS 2.0
Powered by Simple IT SARL:
Contact us
There is nothing else to read, you have to go up now !
Do you want to be published here? Contact us.
2 Newbies Connected |
7 Requests |
0.2213s (0.2082s)