Think of each box as a room and also as a function except the win box. If you take a look at it, you can see there are so many boxes. We are going to make our game according to the game map: Just clear whatever you typed up to this point inside the adventure.py file and make it blank.
Now you understand what is a function, and how to create it, and many more things. Making use of the argument inside function bodyĪnd of course, you can give more than one argument like this - make_cake(flavor, baking_time, something_else, something_else). Then we can use it inside the function body where ever you want. At the time of calling the function make_cake(), we can supply that argument like make_cake(argument). The thing inside the brackets is what's called an argument.
If you want to type this, feel free to do so inside the adventure.py file we just created:ĭef make_cake (flavor ) : print ( "Mix Ingredients for one cake" ) print ( "Add " + flavor + " flavor" ) print ( "Bake the cake" ) print ( "Serve the cake" ) And assume that the following codes are those "certain commands". Now Imagine you have a cake-making robot! For the robot to make a cake, you should give certain commands. So I am going to teach it in a practical way! 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. We are not going to start making the game in this section though. If you are familiar with the concept of functions, then feel free to skip this part. This part is for people who don't know the concept of functions in programming. That's it now you are good to write the python code.
New file creating icon in visual studio code
You can download visual studio code from here according to your Operating system. And more than anything else, it is completely free and open-source. It has a pre-built terminal, Integrated Git, debugging tools, extensions, and much much more. 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. After setting up python3, you will need a modern code editor like visual studio code.You can read this post to learn how to setup python3 on any operating system. The very first thing you will need is the python itself.lower() function to convert the string into a lower case.To make our work again simple, we are going to use functions in python3.īy doing this simple project, you will learn the following Then lead the player according to his inputs. To build this game in python, we need to take the input() from the user after showing some prompts like "you are in a _ room". You can easily guess how the game works by looking at the map alone. If the player types "l", then we lead him to the bear_room, or if he/she types "r", then we lead him to the monster_room like that. There is a door to your left and right, which one do you take? (l or r)". Then tell the player a story like "You are standing in a dark room. Now as you can see above in the game map, we first start the game. This is how the game works:īy the way, some part of the game story is taken from the book Learn Python The Hard Way written by Zed Shaw because I suck at storytelling ?. The Game PlanĪs I said we are going to build a text-based choose your own adventure game.
Now, without further ado, let's get started. You can download the final complete project code here from my Github repo. This tutorial is intended for beginners in python, if you are a python beginner then take this tutorial. 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.