Jump to content

Making a text adventure in PHP-very basic/beginner


DeadlyAzn

Recommended Posts

Build a couple of multidimensional arrays (you can either hand-code them or pull the data from a database). One array is for locations on the map (I call them 'squares'), and another array for inventory (items). The first index of each array is a square number, so on a 10X10 grid, you would have 0 - 99 for square numbers. I actually print out a grid with the numbers in them, 0 - 99, upper-let to lower-right. So now every location has a number which is associated with the first index of the two-dimensional array. So $square[0][0] would refer to the upper-left-most square, and $square[99][0] would refer to the bottom-right-most square.

 

The second dimension of the array is for meta information. I use [zero] for the square name ("City Hall"), [1] for the description ("You are standing in the center of City Hall. There is a pathway to the east and more of the city to the south"), [2],[3],[4],[5] are all used for North, South, East, and West, respectively. So $square[0][3] would be the square number you would get to if you went SOUTH of that square, so $square[0][3] = 10, and if the player went south, it would load up $square[10][0] (think of the squares on a 10X10 grid, starting at zero, the square to the south of square zero would be square 10. If you can't go that direction from a particular square, you just have zero as its index. So in the City Hall example, City Hall would be $square[0][2] = 0 (can't go north), $square[0][3] = 10 (you can go south, and it will take you to square 10), $square[0][4] = 1, $square[0][5] = 0 (can't go west either).

 

Use this information as a starting point. If I have time in the future, I'll tell you how to implement the inventory and items array. But I'm going to assume, for now, that this information may be a little beyond where you are right now. Take heed of the others and DO (not just read) some more PHP tutorials.

 

Instead of using numbers for the second dimension, why not a string?  $rooms[0]['name'] is a lot more descriptive than $rooms[0][0], which implies a classic 2D array setup.

Link to comment
Share on other sites

Type this in google hallsofvallhalla and go to youtube page or go to the site http://indie-resource.com/forums/index.php

 

There you can find lot of game making tutorial for php, with database setup and design that can answer to lot of your questions. They are on beginer level but are very good for learning php and for game making, I have learned php from them and started making my own web php rpg game.

 

Link to comment
Share on other sites

Thanks.

 

Do you guys have any way of sending variables from one file to another? Like if I press a button, it opens up a file and also sends a variable along with it that can be used in the file just opened.

 

Yes,

$_GET(URI), $_POST(header), $_SESSION(server) can all be used to pass variables from one page to the other.

Link to comment
Share on other sites

I have around 2 weeks for a computer science project and I want to do this in PHP. However, I have just started learning PHP, and since I'm so short on time I'm going to see if I can learn how to code this while learning basics of PHP.

 

Did 'Computer Science' not teach you how to use Google?

 

So far in this thread you've pretty much been pushing people to 'help' you by doing the work for you. And getting sour when they wont. Perhaps you should ask your teacher if they can suggest any good resources that may help you.

 

What you're asking from us is very hard for us to provide to you seeing as you don't understand the concepts and basic principles of PHP/MYSQL/etc.. Us giving you adivce on how to program this is like a mechanic giving head gasket replacement advice to a dentist.

 

Have a look at what you said in the quote above.. Then let me pretend to be someone who wants to learn pyrotechnics and explosives.

 

Here we go:

 

(Warning! May contain a multitude of sarcasm)

Hi guys. Now I understand you all have a lot of experience in dealing with explosives and most of you here haven't killed yourself or blown up too many people yet. Now I have about two weeks to complete a fireworks and special effects show for a blockbuster Hollywood movie and I want to use things I have no idea how to handle, which is exciting and very dangerous. However I have have only previously played with sparklers when I was child and that was a lot of fun! However since I'm so short on time I'm going to see if I can put together all these explosives, which are pretty much bombs and missiles together for this amazing show while I learn how to do whatever it is you guys took years, and years to learn.. My first idea was to put some of 'this' in with some of 'that' and compress it so that *BOOM* *CRACKLE*..

 

I'm hoping you can identify the problem here.

 

And sure, yes you can learn PHP while you go, but start off with something a bit less complex..

- Create a script, learn loops and conditionals.

- Make a form that 'sends' data (variables) to another page.

- Connect to a database, add/modify and delete records.

 

Then start combining them.

 

Then if you get stuck, come and ask us for help with code you already have and not with non-existent code you haven't bothered to even try to write.. Even if it's pseudocode.

 

Follow through some tutorials, don't just copy/paste and think "Yay, it works!" understand what it is you're doing witht he code, look at each line and think to yourself "What does that do?"

 

example:

 

This is all fine and dandy when copy/pasted..

<?php
$str = "cat,dog,apple,peach,train";
$strarray = explode(",",$str);

foreach ($strarray as $value) {
    echo $value;
}
?>

 

But you should be able to look at each line like this..

<?php
$str = "cat,dog,apple,peach,train";  // I'm going to build a comma seperated string of items..
$strarray = explode(",",$str); // ..then explode that sting into an array..

foreach ($strarray as $value) { // ..and then loop through that array..
    echo $value; // ..and echo each value (or item) out to the page.
}
?>

 

And when you can do the you may realise you want each item on a new line..

<?php
...
    echo $value . "<br>"; // I need to add a line break after each item!
...
?>

 

And that's all it takes..

 

That and time..

 

And experience..

 

Maybe more than the two weeks you had to start with.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.