Getting graphical

Although text-based games can be exciting as long as you have really good playability in there, the easiest way to grab a player's attention is to wow them with graphics - something that is notoriously hard to do using plain old text!

However, help is at hand through a special extension to PHP that uses the SDL library. SDL, if you have never heard of it, stands for the Simple Directmedia Layer, and is essentially a cross-platform multimedia toolkit. If you have ever played DirectX games you will have an idea of how SDL works - DirectX unifies graphics, sound, and input devices all under one header that games can work with, and SDL does pretty much the same except it is cross platform to boot.

Creating graphical games, whether 2D or 3D, is not terribly easy. SDL takes a lot of the pain away - much more so than DirectX or even OpenGL - which means we can focus more on making the game work as we want as opposed to spending hours getting the graphics to display properly. SDL is capable of using OpenGL for its graphics, which means you can create 3D games if you really want to, but that would require far too much space to explain here, so we will be sticking firmly to 2D. However, the PHP SDL extension is basically a remapped version of SDL for C, so you can read the SDL manual and figure it out from there if you are really committed.

There is one fairly big downside to the PHP SDL extension, and that is the fact that it is not official in the least - it is not bundled with PHP, it is not in PEAR at the time of writing, and it hasn't even been updated by the authors since September 2002. Basically, it is not supported, and there is not a manual to go along with it either, which explains why it is not all that popular. Having said that, it does work remarkably well if you know what you are doing. Of course, if you knew what you were doing, you would not be reading this, so enough waffle and on with the good stuff!

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Getting it to work >>

Previous chapter: Text game v1

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.