![]() You may also be interested in our free online course Object-oriented Programming in Python: Create Your Own Adventure Game. The author has also written a condensed version of the same guide. If you’re keen to learn more about making a text adventure in Python, you could check out Phillip Johnson’s guide to the subject, Make Your Own Python Text Adventure. Despite the poor design of my code, I’d learned the essentials of programming. I moved on from the ZX Spectrum eight years after my dad first unpacked it. When the main loop restarts, the description of the new location is displayed. ![]() When a direction is entered, we check to make sure it’s a valid direction from the current location, then update currentLocation to the new location. In this version of the code, the only valid commands are directions: for example, type ‘north’ at the starting location to go to the lake. Then we wait for the player to input a command. We first display the description of the current location, along with the available directions in which the player can move. We then reach the main game loop, which will repeat indefinitely. The final step of the setup phase is to create the variable currentLocation, specifying where the player will start the game. Next, we call the addLink method on each of the locations we’ve just created, so that the player will be able to walk between them. This has two entries, with the keys being woods and lake, and the values being instances of the Location class. The class includes a method named addLink, which adds entries to the linked locations dictionary after checking that the specified direction and destination exist.įollowing the class definition, we then create a dictionary named locations. For example, if you go north from the woods, you’ll reach the lake. We store a name, a description, and a dictionary data structure which stores the other locations that the current location is linked to. We then create the class Location which specifies each location’s properties. We first create a list of directions in which the player can potentially move. This is a more convenient option which wasn’t available in Spectrum BASIC. Instead of numeric IDs and arrays, the code uses string IDs and dictionary data structures, where each piece of data is associated with an ID or ‘key’. ![]() The code below shows how to implement the beginnings of a text adventure game in Python. With that data, it can then take the appropriate action based on the command the player typed. You’d then have the main game code which keeps track of the room the player is currently in, and looks up the data for that room. Each room would have had several pieces of data associated with it, such as an ID number, the description of the room (“You are in a small cave”), an array of objects which can be found in the room, and an array of room numbers indicating where the player should end up if they try to move in a particular direction – for example, the first number could indicate which room to go to if the player enters ‘NORTH’. The correct way would have been to separate my code and data. This ‘hard-coding’ led to the code being much longer than necessary, and more difficult to maintain. Each room had its own set of code, which would print out a description of the room and then check to see what the player typed. The code was essentially a huge list of IF statements. You can see a door,” from 1982’s The Hobbit) and the player enters commands such as “Go through door” or “Kill goblin with sword.” Although this type of game is comparatively easy to write, I implemented it in the worst way possible. Instead, I focused on writing text adventures, where the game describes scenes to the player (“You are in a comfortable, tunnel-like hall. ![]() The Hobbit, released in 1982, was widely praised for its intuitive parser. I didn’t even know how to display a sprite on the screen. I didn’t have any idea how to create a graphical game more complex than Pooh. There were no obstacles, no enemies, and if you tried to walk off the screen, the program would stop with an error message. In my first game, Pooh (the title was a misspelling), the player controlled a baby, represented by a pound sign, and had to guide it to a potty, represented by the letter O. It was incredible enough that the Spectrum let you play games on the TV, but like most home computers of the time, it also came with a built-in language called BASIC, and a manual which explained how to program it. In September 1982, I was four years old, and the ZX Spectrum home computer had just been released. The first game I ever wrote was named Pooh. Game developer Andrew Gillett explains how to make a simple text adventure in Python - and what pitfalls to avoid while doing so - in the latest issue of Wireframe magazine, out now.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |