How to make a text based game in python

how to make a text based game in python

Derek Shidler

Feb 06,  · How to Create a Python Text-Based Game Step 1: Python IDLE. Step 2: Create New File. Go to file on the upper right corner of the python shell, and press new File. This will open up Step 3: Function Game. Type in a function named game, and type in the game method bellow the function. We will. Sep 18,  · It's pretty self-explanatory. First, we are giving some messages to the player to describe the current situation. Then we take the player's choice as input (). Then we check if the player typed "1" or "2" or anything else. If he/she typed "1", then the game is over. So call the game_over (). Else.

Lesson Plan. Type print "hello" and hit Return. You are now back in command line. We will cover this in a couple of sections. Absed string is a data type in Python. When you open with " you have to close with a closing " ro, 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 gae 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 bxsed 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 ni you typed in.

When you run the script, Python will load it and when it comes acrossit will ignore that line of code. Maks 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 mkae 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 How to treat a hamstring injury 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 Gme game We ask what the player wants to do next.

We want to do something with the treasure chest. After asking the player what they want to do, the if statement is another way of matching with user input. Notice lowerhandy to change to lowercase and match it with given list of strings. Go to line Remove the double quotes and save the script and run it. The for loop what is the euro exchange rate in spain used to iterate through a given list.

Try getting the 2nd, 3rd and 4th item in the list. You are still in the Python interpretor. Run the following to get find out how many items are in a given list You start from the inside and work x.

You can find how many items in a list through your Python interpreter as well. You can remove an yow from a list using remove. Try the pytgon in the Python interpretor:.

This is not as simple when looping through yext list and trying to remove the items from the same list. You will need to create a copy of the list, gake the copy to loop around pyton items in the list, and remove the item you want from the original list. Reason, when you open quotes, you should close with the quotes you opened with.

In above example, it was closed but Python encountered more double quotes on the same line. To resolve this, you can do the following:. We will be focused on the guard function. This will be called Just like a dictionary in real life, you have a word that you look up and find the value associated z it.

When user types in check and hits return, it should print out the value from the dictionary:. What are you waiting for? When user types in sneak and hits return, it should print out the baxed from the dictionary:. Reaching for the door, you open it slowly and slip out. Tet return is important as it breaks out of the While loop which otherwise will keep looping forever and the maake will not end. If the while loop is not used properly, you might end up with an infinite loop.

Which means the program will keep going until you force it to quit basde when computer runs out of memory and crashes. In the while loopwe ask tetx player what they want to do, and actually give the two options: run or door.

The output should just be Hello! If you have other ideas and want to share you game with us, send them on to contact codinggrace. So many if statements, this can be made simpler and easier to maintain by using Finite State Machine FSMYou can find info about it, but it will mainly be touching object-orient programming, which is another lesson for another day.

Navigation index next amke Coding Grace 1. Quick search. You just ran your first Baseed script via command line. Run the script. Other things to do with strings. Ask class to try the following: print "Hello! Numbers are pytuon datatype integers, whole numbers.

It will look like the following: print input "What's your name? We are going to do some string formatting, this is recommended on offical Python.

Remove the number in front of the function name. But we need to do something with the player. Run game Encounter 2 doors, red and blue. We will see more data structures later. The options are: Player types fleethey are back in the room with red and blue doors previous room Game Over. You can use string formatting as before with the why variable. Can describe using the following image below:- In game We set t the if statement to print out the two options.

We will be looking more at lists and introduce the for loop. If it matches, we print treasure statement, or we head to the guard.

In this instance, every time it loops, it prints out the treasure variable. To resolve this, you can do the following: Use single quotes: 'I said "Hello". This will be called:- After checking treasure chest first Skip checking the treasure chest Dealing with the guard Checking the guard and what the guard is doing.

Sneak around the guard and escape to freedom. Attack the guard. When a player escapes through the door, you return to the previous function which called this function. How to add friends on playstation network on computer What function called guard? And also thanking the player, its variable was set when main was called.

What you will learn?

Creating a Text-Based Adventure and Quiz Game in Python Text-Base Adventure Game. Press the play button to begin. Things to Know about the Code Above. Using web emulators, especially when developing a story driven application, can be Quiz Game . Learn Python by making a text-based adventure game ¶ Introduction ¶. Open terminal/cmd, and type python. Note >>>, this means you are in the “Python interpretor”. Type print game_py ¶. In game_py, we encounter say_hello (), this is called a function. We will cover this in a couple of. Jan 15,  · Text-Based Adventure Game with Python. Now let’s see how to create a text-based adventure game with Python programming language: name = str (input ("Enter Your Name: ")) print (f"{name} you are stuck at work") print (" You are still working and suddenly you you saw a ghost, Now you have two options").

Howdy folks welcome to another fun tutorial! In this tutorial, you will learn how to make a text-based choose your own adventure game in python3 using functions. This tutorial is intended for beginners in python, if you are a python beginner then take this tutorial.

As I said we are going to build a text-based choose your own adventure game. This is how the game works:. Now as you can see above in the game map, we first start the game. Then tell the player a story like "You are standing in a dark room.

There is a door to your left and right, which one do you take? You can easily guess how the game works by looking at the map alone. Then lead the player according to his inputs.

To make our work again simple, we are going to use functions in python3. This part is for people who don't know the concept of functions in programming. If you are familiar with the concept of functions, then feel free to skip this part. We are not going to start making the game in this section though.

If you are an absolute beginner to programming, then you may ask what is a function? If I give a formal definition, then it may bore you. So I am going to teach it in a practical way! Now Imagine you have a cake-making robot! For the robot to make a cake, you should give certain commands. And assume that the following codes are those "certain commands".

If you want to type this, feel free to do so inside the adventure. Ok, by using the above commands, the robot will make only one cake of vanilla flavor , right? What would you do if you want 5 cakes? By seeing this alone you would agree that this is not the proper way to do it.

So what we are going to do is that we are going to extract the pieces of code that needs repetition and put it under a particular name like below:. This is what's called a function. To define a function in python, we use the def keyword, and following that we give the name of our function and the brackets -. Then after semicolumn : , we give the function body from the next line with indentation.

Now if we want 5 cakes , then you don't have to hard code them. Instead, you can call the name of the function 5 times! But now you may say that we can do this exact thing using loops then why should I do it using a function? That's a great question! Yes, we may have done it using loops, but here's the tricky part. What would you do if you want 5 cakes with 5 different flavors like vanilla , chocolate , orange , banana , and strawberry? If you are using functions , then you can easily do it like this:.

The thing inside the brackets is what's called an argument. Then we can use it inside the function body where ever you want. Phew, that was a lot. Now you understand what is a function , and how to create it, and many more things. So let's start working on our game. Just clear whatever you typed up to this point inside the adventure. If you take a look at it, you can see there are so many boxes.

Think of each box as a room and also as a function except the win box. So let's start by creating a function called start. We are not going to run this game yet. If we do so, we will get a bunch of errors. Type the following code above the start function definition:. The following function is for creating the monster room:.

Type this above the previous function:. Now that's it you have just created an awesome yet simple text-based choose your own adventure game in python3 using functions!

Hooray, It's time to run the game! But before that just make sure whatever you have typed up to this point inside the adventure. CLI which stands for Command Line Interface or the classic terminal window is where we are going to run our game. I hope you enjoyed this tutorial. In some places, I intentionally skipped the explanation part. Because those codes were simple and self-explanatory. That's why I left it to you to decode it on your own. True learning takes place when you try things on your own.

By simply following a tutorial won't make you a better programmer. You have to use your brain. Tutorials are made to only kickstart your journey. The rest is in your hands. Keep trying new things and never stop learning. Hey folks, my name is Aravind, and I am the man behind this website.

To know more about me, check out the About Me page. If you like and enjoy my content, then please consider supporting what I do through - Buy Me a coffee. We use cookies to ensure you have the best browsing experience on our website. Sep 18, The Game Plan As I said we are going to build a text-based choose your own adventure game.

See its simple! What you will learn? By doing this simple project, you will learn the following How to work with functions in python3. How to take input. How to print output. And much much more. Things you will need The very first thing you will need is the python itself. I am going to use python3.

Yes, of course, you can do this simple project inside the default python IDLE or things like that, but the benefit of using a modern code editor like visual studio code is that it comes pre-baked with all the things you will ever need during your programming journey. It has a pre-built terminal, Integrated Git, debugging tools, extensions, and much much more. And more than anything else, it is completely free and open-source. With these things set up, now you are good to go.

Now just open visual studio code. Now click on this icon like below to create a new file and name it as adventure. Wait What are functions? Take the honey. Taunt the bear. You can go through it now! What would you do? Go through the door silently. Kill the monster and show your courage! Take some diamonds and go through the door. Just go through the door. The moment you touched it, the building collapsed, and you die!

Congrats you win the game! About Me Hey folks, my name is Aravind, and I am the man behind this website. Tags: python css html javascript web-development machine-learning flutter nodejs reactjs.

0 thoughts on “How to make a text based game in python

Add a comment

Your email will not be published. Required fields are marked *