How to program a text adventure game in python
A string is a data type in Python. When you open with " you have to close with a closing " , same thing with '. Numbers like 1 are called integers, they are whole numbers. For decimals, they are known as floats. It means that the Python script is also a standalone programme which makes it re-usuable. Just take it for now that normally this is the first thing you type. It will autocomplete for you.
We will see an example how we re-use Python scripts, otherwise known as modules at the end of the workshop if we have time. We are going to use another Python function called input. When this is called, it will wait for the user to type something and hit Return.
This comes by default in Python. When you run this script, it will ask for your name, after you type and hit Return, it will print out what you typed in. When you run the script, Python will load it and when it comes across , it will ignore that line of code.
Python treats string and everything else as an object, and Python provides many features and in-built functions associated with each type of object. So anything versions of Python prior to this version is not supported. You should look at something called String formatting instead. The string above is calling format and it requires something to be passed in.
They are a name given to a routine which can return a value, and can be called many times. Functions will keep the script tidy. To create a function, it begins with def followed by name of your function. It should start with alphabetical characters only, try adding a number in front of main and run it again. You should be a syntax error. In this instance, if the input by the player is Note the return call. This is an in-built Python function.
Hint: try a word that has flee in it when you run the script. In this script, create a function that can be called and passing in an argument. This is where you tell the player that they died giving a reason if they selected not to flee. Well, that was tasty! We will have a look at lists and see what happens in the blue room. In other languages, it can also be know as arrays. Lists is basicially a list of items in sequential order. Can describe using the following image below In game We ask what the player wants to do next.
We want to do something with the treasure chest. The code is these functions look very similar, and very repetitive, which is why we can write it once, and change the outcome based on the data. A problem with the way you've written the game is recursion. If you start in the burrow, and you can go down into the basement or north to the front of the house. We keep getting deeper and deeper into the call stack. If the player keeps exploring, eventually they will run into Python's stack limit.
You could fix this by increasing the stack size, but that is just a kludge; the correct solution is to get rid of the recursion. You should start by defining some things. Like rooms.
A room is location the player can be in. It should have a short name "Kitchen", "Burrow's Basement" , and a longer description. The player may or may not have been in the room before. It will have connections exits to other locations. There may be items in the room. Beyond rooms, the world is full of objects, or things. Things will have a name, and perhaps a description.
Some things can be carried photos, pamphlets, jars, Some things can be worn, like a coat or a hat. It might be hidden:. Some things can be placed inside other things. Fish sticks may be found in an ice box. The ice box is usually closed, but occasionally open. Some containers may be locked, like a cabinet. Some containers may be see through, like a china cabinet. You can climb into some containers, like a wardrobe or a car, but most you cannot.
Some things can be placed on other things. You can put a jar on a table. You can put a key on a hook. So perhaps some containers should be considered a supporter, instead. You can even enter some supporters, such as climbing into a bed.
Some things can move around, and have behaviours. You can talk to some of them, such as a shopkeeper, and they will talk back. Other actors, might not talk back, such as a cat.
A shopkeeper an animate thing might be wearing pants with pockets a container which may contain a pocket watch openable. Is the player an animate container??? They can hold things inventory , move around animate , take things container , hold things, wear things. A door sits between two rooms. A door could be open or closed. If it is closed, it may or may not be locked. Moving the rug should reveal the trap door marking it not concealed , allowing travel through the door to the other location it connects to.
Now if your game logic allows you to type "lift rug" or "move rug", you could parse the word "rug", find the object with that name in the location. Something to get you started. The lantern is just a Thing. The rug is actually a Rug a special Thing , which has an action defined in the frog.
This is a "better" way that you're previous approach, but I wouldn't say it is a good way, yet. The framework has a lot of detail to flush out, and reworked to include better visibility and touchability.
Items should optionally have a. A room. If you continue down this road, eventually you'll re-invent the Inform 7 interactive fiction framework.
Good luck. If you're going to import them this way all the time, why didn't you just name the modules rooms and char respectively? What's the point of all the extra typing? Btw, it's conventional in text adventures to refer to the player ; I'd go with import player over import char , just to de-confuse the syntax highlighter.
You should just inline it there. A text-based adventure game is a completely text-based and very simple game. In this game, users have options to tackle a situation and with each input provided by the user, the game will continue to increase by putting more situations and more options.
This is what a good task for a beginner to get their hands on. In the section below, you will learn how to create a very basic text-based game with Python. Here I will show you the basic idea of how you can create this game and then you can modify or increase the size of this game with more situations and user inputs to suit you. Enter Your Name: Aman Aman you are stuck at work You are still working and suddenly you you saw a ghost, Now you have two options 1.
0コメント