Need help starting on a browser-based game

Hey folks,

I’m an experienced programmer and have extensive knowledge in java and visual basics, I’ve recently been learning how to make websites using Dreamweaver, PHP, and MySQL through Lynda.com. I’m trying to make a browser-based game similar to TribalWars, Travian, and Castle Battle. Here are the links to these games if you don’t know what these games are:

http://www.tribalwars.net/


http://www.the-crusades.org/

I obviously don’t expect to make my game nearly as professional as these games. My question is, what do I need to know before I can attempt such a project? I know that the main tools will be PHP and MySQL. I already have the game mechanics planned out and programmed in visual basic. I just need to start coding it in PHP. I’m looking for any helpful input that will help me get this started. Are there any resources I could use for coding such a game? Any input is welcome, thanks in advance.

Well, you are talking about a turn-based game, correct? Or, a 2D animated version?

Either way, you might want to learn about 2D gaming using a 3D programming game system.
The one I suggest is Unity3D. They have a great system that has a free web plugin similar to
Flash. ALL of it is totally free. I am currently playing with it and found there are a lot of tutorials
online on how to program games thru it.

Also, the reason that I mentioned the above is that PHP is not really orientated to gaming. It does
do just about everything you want, except in the graphics areas. PHP does do enormous image
manipulations and has a ton of useful tools for graphics. But, it is limited when it comes to actual
animation. (I could be wrong here, but, nobody has said otherwise…)

So, if your game includes a lot of graphics, I might go with a web plugin. There are lots out there.
Adobe Flash, or Shockwave, Unity3D webplayer, Sliverlight (I forget the game part name) and many
others. All are easy to learn if you know VB and general programming. Each has it’s own area that
is better than others. I like Flash, although it is tricky to learn and I really like the Unity webplayer.
The best part of Unity is that is it free… So, if you do not have that much graphics and is just a map
or grid game, you could use PHP as it can piece together a map with avatars, scenery and others easily
merged together in a timely manor. Not too complicated depending on the decision codes needed…

Well, doubt this helped, but, some items to think about. Perhaps a little more info on YOUR game might
help us to help you… LOL… Good luck!

Thankfully I will only be using still images as graphics so the programming task shouldn’t be too complicated. But thanks for the input! I will definitely look into Unity3D.

My game will not be turn-based but time-based instead. Other than that it will also feature a grid map and a minimap to show land that each player owns. It will be very similar to those games I put up links for, take a look at them if you have the time.

PS any other input you guys have is always appreciated thank you. I’m pretty much a beginner PHP coder so this task will probably take me the whole summer to accomplish but I’m determined to learn and do this thing.

Well, PHP is a web programming system. It can handle limited graphics very very well. It can also handle inputs from users and responses in a timely manor. It runs a TON of gaming sites of different types of games. The sites you posted are just sites with graphics and code. We can help! But, you have not asked any PHP questions… It sounds, at first, like PHP will do your job. But, what can we help you with? Ask away…

Alright one of the first things I need to do is populate a MySQL database with the information on the map that the users will play on. I’m new to PHP and MySQL so I would appreciate getting some help with this.

I have a MySQL table which will hold the information on each tile of the 1000x1000 map grid in the game. The columns in this table will hold the amount of resources available on that tile, the X and Y coordinates of that tile, the owner of that tile, and the type of building built on that tile (if there is any).

So the columns in this table are TileID, X coord, Y coord, %Iron, %Gold, %Fertility, %Wood, %Stone, Owner, and Building.

So far this table is empty but it will hold 1000x1000 number of values for each column. What I need to do is populate this table and essentially create the playing field which the players will play on. I’m assuming that it’s possible to do this through PHP code. I would really appreciate if you guys could help me code this first bit.

The tricky bit is that the values for the resources on each tile (aka the %Iron, %Gold, %Fertility, %Wood, %Stone columns) are generated randomly by picking a value between 50 and 100. I accomplish this in my VB version of the game by using the following code:

For X = 0 To UNISPAN
For Y = 0 To UNISPAN
intTIron(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTGold(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTFert(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTWood(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTSton(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
Next
Next

In VB this is simply assigning the resource values into variables inside a 2D array. I need to use PHP code to populate the columns in the same way.

I hope I was clear on what needs to get done, let me know if anything needs more clarification. Again, I appreciate any help I can get with this, thanks in advance.

PS the value ‘UNISPAN’ in my VB code is simply an integer value of 1000.

Well, if you want to use 2D arrays this is done nearly the same way as VB. The difference is the way you access them.
In VB it’s array-name(dimension1, dimension2), in PHP, it’s array-name[dimension1][dimension2]…
And, for the random number, you use rand(min, max), like TIron=rand(1,100)…

So, your code:
For X = 0 To UNISPAN
For Y = 0 To UNISPAN
intTIron(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTGold(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTFert(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTWood(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTSton(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
Next
Next
Would become something like this:
[php]

<?PHP for ($x=1; $x<=1000; $x++); for ($y=1; $y<=1000; $y++); TIron[x][y] = rand(1,100); TGold[x][y] = rand(1,100); TFert[x][y] = rand(1,100); TWood[x][y] = rand(1,100); TSton[x][y] = rand(1,100); } } ?>

[/php]
Note: You do not have to set up the array, just start using it. PHP will auto-type the values. So, you may wish to set the values assigned in this loop as integers. Use intval(rand(1,100)) to make sure it is an integer.
Lastly, make sure you create these values OUTSIDE of any functions. The creation of the arrays should be done in the main program not inside a function. The reason being the “scope” of the array would not be available if created inside a function. Variables and arrays created inside a function are gone when the function is done, unless they have been made global variables.
Hope that helps… Good luck with your game!

Thanks that does help me get the coding done for generating the values, however, I’m having trouble with the actual writing of the values into my MySQL database. Every time I generate a resource value for a tile, I need to write that value into my MySQL database. Is it possible to do this through PHP code? Do I need to do some sort of MySQL query through PHP in order to write the values into the database?

Since my database is so large (I have 1,000,000 tiles in total to enter values for) I simply can’t populate the database by hand through PHPmyadmin or through MySQL code. I was wondering if it’s possible to automate this through PHP instead. Thanks again.

you would use something like:

$query = “INSERT INTO table_name (column1, column2, column3,…) VALUES (value1, value2, value3,…)”;
mysql_query($query);

after initializing a connection to the database of course. You’d set this in the loop as when you establish the values it would insert them.

More information on that here: http://www.w3schools.com/php/php_mysql_insert.asp
Hope thats what you were asking. :slight_smile:

ShieldHeart,
You didn’t mention you were going to load it into a database. I was hoping so… But, I answered you…

Suggestions, I would not load arrays as I mentioned in my previous note. It would be a resource hog…
I would do the loop and just create the values directly in your SQL query. Then, let the query store the values as Sabmin mentioned. Also, it is very easy to read one location out of the database, simple and quick.

A couple quick questions: The 1000x1000 map is going to be the only map used? Each player will use it in general? If so, that should be easy enough for you to handle. Also, think about the format of the fields in your database. Use INT’s if possible as they will load quicker.

I am interested in seeing this project develope… good luck with it…

I’m interested as well, this is something I’ve wanted to do for a while but could never get any graphic designers to join me in it. :frowning:

Well, count me in! LOL…

I have learned a TON about graphics processing in PHP. It is very easy. Lots of tools and functions!
It isn’t designed for animation, but, great for turn-based games, so, that info might help!

Thanks Sabmin, that’s exactly what I was looking for. I feel like I have what I need to figure this out now.

I’m glad you guys are showing interest in this project, the great thing is I already have the entire game programmed in VB, along with the graphics I will be using. Now it’s just a matter of learning the PHP and MySQL and putting the game online. I’m glad there are forums like this with lots of people willing to offer help, so thank you to you both :slight_smile:

As for your questions Ernie, yes, the 1000x1000 map will the only map used. Essentially players will start off by placing a castle on this map which will allow them to claim a 10x10 section of the map. They will then be able to place resource buildings in their land and generate resources. They can then use the resources to train troops. The fun will begin when players will be able to attack each other, form alliances, and try to conquer the entire map. Whichever alliance can conquer the map first wins the game.

So it’s a pretty big project, but thankfully I’m finally done with my exams and have to entire summer to program this sucker haha.

So, your project doesn’t sound all that hard. At least the web and database file goes. Reading and writing data to a MySQL database is all easy these days that is why everyone is doing it. Displaying in a browser is very easy, too. You might use tables depending on what you are displaying. Or, DIV’s which can be made dynamic using Javascript and JQuery. Do you have a link to a picture of your game?

Anyway, when you get things started we are here to help! Good luck…

Oh, PS: Be a “CARPENTER”!!! Measure twice, cut once! LOL… Plan ahead!

yep got a picture, it will essentially look like this:

Nice! When you get down to the graphics, I have a bunch of routines and some experience there…

Good luck…

I just wrote the code to populate my database, the MySQL query seems to be working but I’m only getting 1 row inserted, when instead, it should be inserting 25. I put the INSERT statement into a loop like you guys said so that it inserts a new row at each iteration, but still it only seems to insert 1 row. Anyone know what the problem is?

[php]<?php

// 1. Create a database connection
$connection = mysql_connect(“localhost”,“root”,"");
if (!$connection) {
die("Database connection failed: " . mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db(“ca”,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}

for ($x=1; $x<=5; $x++) {
for ($y=1; $y<=5; $y++) {
$TFert = intval(rand(50,100));
$TLumb = intval(rand(50,100));
$TSton = intval(rand(50,100));
$TIron = intval(rand(50,100));
$TGold = intval(rand(50,100));

	// 3. Insert values into database
	mysql_query("INSERT INTO TileData (X, Y, Fert, Lumb, Ston, Iron, Gold, Building, Owner)
	VALUES ($x, $y, $TFert, $TLumb, $TSton, $TIron, $TGold, '0', 'None')");		
}

}

// 4. Close connection
mysql_close($connection);

?>[/php]

Your code looks good! You can add this after the query to check for an error.
I will print the error and then die so you can read it. Might help…

After the INSERT add these lines:

[php]
if (mysql_errno())
echo “MySQL error “.mysql_errno().”: “.mysql_error().”\n
When executing:
\n$query\n
”;
[/php]
By the way did you put an index or key into your database table for the map data? You may have to add an index which often can help with database speed. Sometimes there are timing issues. But, not normally with this small amount of data… Try this and see if errors pop up…

Oh, one more thing, did you check the spelling of your fields in the database compared to the INSERT Query? Perhaps one is spelled wrong. And, lastly did you look at the actual data in the database to see if it actually wrote anything. Ooooh, AND, if you already have posted square 1,1 you can NOT INSERT it again. You may have to alter your code to check for the square and then INSERT if empty only… Let me know…

Thanks your error code helped me debug it. My TileID index wasn’t set the auto increment haha. Fixed. :slight_smile:

Great! But, I will not mark this topic solved as I am sure we will be talking about more items…

Glad I could help. Good luck with the project!

Sponsor our Newsletter | Privacy Policy | Terms of Service