Archive for June, 2008

MySQL FIX: Row handle referred to a deleted row or a row marked for deletion.

Okay, so this could be a few things. Usually this means mysql is trying to access a row that you’ve already deleted. However I happened to get this error just by doing a select statement (and I hadn’t updated or deleted anything — go figure). I believe SQL didn’t like order in which I was selecting the fields. To diagnose the problem I broke the SQL down and gradually added the fields I’d selected back in piece by piece. Once I’d done this everything was fine and dandy.

When I was looking for help on this I found someone else who has having the same problem but the reason for their error message was because the database was in 65 compatability mode. Heh, sounds like a lot of things can give you this problem.

5 comments June 27, 2008

Lesson 4: Pits of Doom — And Array We Go

In the last lesson we covered how to create a function, how to use some of php’s built in functions, and how to deal with sessions. Now we’ll take it even a step further.

If you’ve tried the little game we have setup right now you’ll agree with me that’s its not only boring, it’s really REALLY boring. Right now you can click one button and either win or loose the game. In the big scheme of things we want our character to walk around different maps, moving from place to place as well as fighting or falling into pits before they die.

But frankly you’re not at that point yet. So again we’ll take a step back and go at from an easier approach — letting the character move around a larger area before they run into the possibility of either finding the treasure or falling into a pit. So let’s say we have a map like this:

Map Name: Tutorial Map

Map Level: 1 (lower numbers are closer to the surface, higher numbers are deeper down)

W    W    W    W    W    W    W
W    S    E    E    E    E    W
W    W    E    X    E    E    W
W    E    E    E    T    X    W
W    W    W    W    W    W    W

S marks the starting place of our character on the map. The number E is an empty space, meaning the character can move there. The number W is a wall, the character can’t move onto that spot. X is a pit and T is our treasure. Now we already have a much more interesting game already. In order to give our character more movement we can use a database or an array. Since a database is in a way a really huge array we’re going to start with an array so you don’t have to worry about all the finicky problems that pop up with the database and then move on to the database later.

Lesson Concepts

Arrays

In short an array is a way to hold lots information at one place. There are two types of arrays, single dimension arrays and multi-dimension arrays. We’ll cover single dimensional arrays first.

The easiest way to think of an array is a list of items. The list always starts at zero and grows up in it’s number. Let’s take a look at a simple array I’ve made called path. In each of the fields in the path I’ve stored information about what kind of item you might find there (treasure, walls or empty spaces)

<?php

$path[0] = “S”;
$path[1] = “E”;
$path[2] = “E”;
$path[3] = “E”;
$path[4] = “W”;

?>

As you can see, out path has a starting position of a character, three empty spaces, and then a wall. If we were to try and build any kind of character movement using this array it would be pretty difficult. The character would start at position zero, be able to move up to 3 spaces forward, and then get stuck when it hits the wall at position 4. The only other option this character would have would be to turn around and go back to it’s starting position. How boring would something like this be? We want our character to be able to move in multiple directions, not just forward and back. That’s where multi-dimensional arrays come in. Take a look at this…

<?php

$path[0][0] = “W”; $path[0][1] = “W”; $path[0][2] = “W”; $path[0][3] = “W”;

$path[1][0] = “W”; $path[1][1] = “S”; $path[1][2] = “E”; $path[1][3] = “W”;

$path[2][0] = “W”; $path[2][1] = “E”; $path[2][2] = “E”; $path[1][3] = “W”;

$path[3][0] = “W”; $path[3][1] = “E”; $path[3][2] = “T”; $path[3][3] = “W”;

$path[4][0] = “W”; $path[4][1] = “W”; $path[4][2] = “W”; $path[4][3] = “W”;

?>

Does this look familiar? Good! It should! This is a multi-dimensional array. I’ve tried to write it out so it looks similar to the map we have planned out above, only this is a much smaller version. With a multi-dimensional array you can store more then 1 coordinate at a time. Now we can have a left and a right direction along with an up and a down.

In the multi-dimensional array our character starts out where we’ve marked it with an S, at (1,1). This notation (1,1) is what we’ll call a coordinate. It notes the x and y location (x,y) of the character.

If this character were to move up, we’d subtract one from the x coordinate. If they want to move down we add 1 to the x coordinate. If they want to move left or right, we add or subtract one to the y coordinate. Now what happens if the character starts at (1,1) and they move to the right? They are given the position (1,(1+1)) or (1,2). Scroll back up and look at the array. Is there a free space at (1,2)? If there is, then we’d want to allow this move. If there’s a wall in position (1,2) then we’d give the character an error message.

Now it’s your turn to try. Write out the coordinates you’d need to get from the character’s starting position to the treasure. There are actually several ways to do it, can you find more then one way?

Once we have a larger array we have to always test both coordinates when the character moves from place to place, otherwise we won’t be pulling accurate information from the maze. Here’s how you’d test to see what value is in an array at a certain position.

<?php

$x = 0; //first coordinate

$y = 0; //second coordinate

echo $path[$x][$y]; //now we show the value of the path for the given coordinates

?>

I’m sure you can see where this is going. You can test an array for a value just like you can test any other variable.

<?php

$x = 0;

$y = 0;

if ($path[$x][$y] == “E“)

{

echoYou’re on an empty space!“;

}

else if ($path[$x][$y] == “W“)

{

echoHow did you get stuck in a wall?!?“;

}

?>

That’s it! Seems pretty easy doesn’t it?

Summary

In this lesson we covered single and multi-dimensional arrays. In order to complete this lesson you’ll need to know how to assign values to a multi-dimensional array, access the values in the array, display the values in an array, and then write if-else statements to respond differently based on what’s in the given position in the array.

To make this lesson easier I’ve included a custom function that draws the map to the screen and shows you the character’s position as it moves around. The character is drawn in red until you find the treasure, then it turns green!

Game Files

Working Version: character.php – we have enough of a functioning game that you can start playing it for yourself!

Source Code: character.txt

Try it yourself: character2.txt

Lesson Reasoning

This lesson helps us work up to getting character movement based off of values from another data source. Right now we’re using data we’ve entered ourselves, but pretty soon we’ll be pulling this information from our database. This is only one level below reading from a database and it’ll help you understand character movement on a baser level before we add the complication that is SQL.

Now comes the question, can we change the map as we play? Let’s say, instead of the game being over when the character finds the treasure, we instead want them to move onto a new map. Sounds easy, but the more maps you have the more difficult this becomes!

And did you notice how much time it took to make your own custom map? Pff, we don’t want to do that for all the levels in the game. We need something much faster and more powerful, which brings us to the next lesson.

In the next lesson you’ll learn how to dynamically load different map files into the map array from a file instead of having to have it typed on the character’s page. That way we can relocate the character to another map once they’ve found the treasure. Also, we’ll create a map editor so we can quickly and easily make map files.

Once we have those two things done we’ll be ready to move on to putting all the character movement into a database table, creating maps and saving them directly to the database, and letting our character move up and down in the map levels using ladders.

Return to Lesson 3 or you can Go to Lesson 5

1 comment June 26, 2008

Lesson 3: Pits of Doom — Mixing Things Up

In the last lesson we covered how to make a form, check to see if a button has been pressed, and display a message according to whether the user found the treasure or not. Now it’s time to take our very simple navigation page to the next level: random treasure location.

In our final game, the position of the treasure will be pre-determined by data in our database. However, we won’t know what position the treasure is in as the user is navigating their character around the game because this information will be loaded as they play.

In our next lesson we’re going to apply a more advanced concept to figuring out if the treasure is in the correct location. Instead of always knowing where the treasure is, we’re going to make the treasure start in a random direction each time the page is loaded. That way we have to check every time to figure out where the treasure is, and if the player will fall into a pit or not.

Lesson Concepts

Functions

In programming, a function is a great way to break bigger tasks into smaller more manageable pieces, or reduce the need of having to write the same piece of code over and over again. In PHP the syntax for a function is very simple:

<?php

function myfunction ($parameter1, $parameter2)

{

echo $parameter1 . ” ” . $parameter2;
}

?>

This is a really simple function called myfunction. It takes two parameters and simply echoes them on the screen whenever you call it. The periods you see in the echo statement are a way to concatenate or join the two variables together with a space in between them.

Calling a function is just as easy as making one. Anywhere you want myfunction to print something on the screen all you have to do is:

<?php

echo “Hey there, here is what myfunction has to say: “;

myfunction(“Hello”, “World!”);

?>

If you were to run this you’d see: Hey there, here is what myfunction has to say: Hello World! Not only can a function print to the screen, it can return values you can then store into variables. Let’s take a look at this function:

<?php

function Mod5 ($number)

{

return $number % 5;
}

?>

This function takes any number you give it and mods it by 5. If you aren’t familiar with the modulus operator, all you need to know is that it returns the remainder from the division, and not the number you’d get if you divided $number by 5.

Now let’s see what how we’d use a function that returns a value instead of printing it on the screen:

<?php

//declare our function
function Mod5 ($number)

{
return
$number % 5;
}

if ($_POST['number']) //they’ve entered a number
{

echo “Your number is: ” . $_POST['number'] . “<br/>”;

$number = myfunction($_POST['number']); //we store the modulous of what they entered back into the number variable

echo “Your number % 5 is: $number”;
}
else //show them a form to enter a number

{
?>

<form action=”#” method=”post”>

Please Enter a Number: <input type=”text” name=”number”><br/>

<center><input type=”submit” name=”submit” value=”submit” /></center>
</form>
<?php
} //don’t forget to close our ELSE statement
?>

In this example we’ve taken the number a user enters into a text box and then display the number back to them along with the remainder of that number when it’s divided by 5.

One of the great things about PHP is that it comes with a huge collection of pre-defined functions you can use when you program. You can take a look at all the functions by going to php.net. The one function I want to touch on in this lesson is the random function.

As you can probably tell, you don’t always need to know all the little specifics that go on inside of a function as long as it always gives you the results you’re expecting. The same goes with the random function. I could go into details on how it takes the current time on your computer and does a bunch of mathematical stuff to it — but that’s just plain boring!

The short of it is, the rand function takes 2 parameters. The first parameter is the lowest number you want it to generate when it runs. The second number is the highest number you want it to generate when it runs. So, if you wanted a random number between 1 and 4, including the numbers 1 and 4 you’d do this:

<?php

echo “My random number is: “ . rand(1, 4);

?>

View the page with this php code. Now refresh the page. You’ll notice the number is constantly changing.

One other function you’ll need to know is the unset() function. This function is great, you pass it a variable or an object (don’t worry, we’ll get to those later) and it sets the value of that function to null. Think of it as taking a jar of cookies, opening the lid, and dumping all the cookies on the ground. That’s essentially what the unset() function does.

Congratulations, you now know how the rand and unset functions work. You’ll need both of these for this lesson!

Sessions

Now one of the biggest problems we run into is how to keep track of a user’s past responses. Or how to keep track of anything about the user from one page to another. That’s where sessions come in. Sessions are a way to track information about the user as they go from one page to another. Eventually we’ll be using information from the database to track a lot of the moves a user is making with their characters, but we’ll still need to know who the member is as they go from page to page. For right now, we’ll use sessions to keep track of the direction the user picked. And now, instead of letting the user continue to pick a direction after they’ve fallen into the pit we’re going to give them a game over message until they decide to reset the game and try again.

Sessions are very easy to use. They work almost exactly like the $_POST and $_GET variables you can use to access information about what someone has entered on to a page. However, there’s a few major differences:

  1. You can set the values of the $_SESSION variable
  2. Session variables remain with the user until you tell them to expire, they don’t change page by page.
  3. Session variables can be set to expire so they don’t last forever, or you can set them so they never expire and DO last forever.

Using sessions now will get you familiar with them before the programming becomes more complicated.

If you’ve done anything with cookies before then sessions will be a breeze for you. Sessions are a good alternative to cookies because if a browser has cookie functionality turned off you will still be able to keep information about a user from one page to another. If that went right over your head don’t worry, it’s not essential to this lesson.

There’s one thing you have to keep in mind about sessions. In order to use a session you have to call a special function at the top of every php page, BEFORE YOU DO ANY KIND OF OUTPUT!! This last is important. If you try to output anything before calling the session_start() function you will get an error.

Now let’s setup a simple session. Create two pages, one called welcome.php and the other called main.php. Welcome.php is an example of a web page you might see that greets you and asks you what your name is. Main.php is an example of a page you might look at after you’ve visited the welcome.php page.

In welcome.php we have this:

<?php
session_start(); //the first thing we do is start our sessions

if ($_POST['name']) //they entered their name in the form
{
$_SESSION['name'] = $_POST['name'];

//now we’re going automatically to redirect them our next page
//using a function called header() that’s built into PHP

header(“Location: main.php”);
exit;
}

?>
<form action=”#” method=”post”>

Enter your name for our first session variable! <input type=”text” name=”name” />
<center><input type=”submit” name=”submit” value=”All Done!”></center>
</form>

Now, inside of main.php we’re going to put this:

<?php
session_start(); //we have to start our session again to get the data we saved before

echo “Welcome to the main menu “ . $_SESSION['name'] . “!”;
?>

In the main.php file we pull out the information we stored in the session variable for name and print it back out on the screen. Nifty! Now we know, no matter which page this user goes to from now on, as long as we call session_start() we’ll be able to access their name from the variables $_SESSION['name'].

Congratulations! You should now understand the basic concept of sessions.

Summary

In this lesson we learned about how to make our own function and talked about how PHP has it’s own predefined functions we can use to make our life a whole lot easier. I talked about how to make a function print something on the screen versus a function that returns a value. Functions that return a value can also be printed on the screen, but you’d have to put an echo in front of the function call to do that.

Next we talked about sessions. Sessions are a way to store data as the user moves from page to page around your website. Without sessions we wouldn’t be able to tell who was who as multiple people will eventually load this game and try to play it. Right now we’ll be using sessions to keep track of a game being over or not, but eventually we’ll only use sessions to keep track of which user a person is as they play.

Game Files

character.php

character2.php (no source code)

Note: these are text files so you can copy/paste all the source code.

Lesson Reasoning

In this lesson character.php now has the treasure starting at a random location. If the user picks the incorrect location they get a game over message instead of being able to pick a direction again. In order to play again the user has to click on a reset button to start the game all over again. I’ve introduced functions, sessions, and a more abstract checking.

Once we start pulling information from the database we won’t know anything about the character’s current location at runtime other then their x,y,z coordinates and the map they’re on. Information about items in that position will all have to be tested each time the character tries to move somewhere. By introducing the treasure in random locations we start to get into the habit of checking information about our location as we select a particular direction.

Return to Lesson 2 or continue on to Lesson 4

1 comment June 21, 2008

Previous Posts


Cool Sites

Free Code

Game Developer

Game Engines

Game Jobs

Music & Sound

Second Life

Sprites

Lost In Oblivion

June 2008
M T W T F S S
« Apr   Jul »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Top Posts

Recent Posts

Archives

Feeds