How to Create a Python Text-Based Game

by jorflo439 in Circuits > Software

19943 Views, 7 Favorites, 0 Comments

How to Create a Python Text-Based Game

header.PNG

By Joel Flores

***Before we start***

Do you have python installed?

If you do not have Python installed onto your personal device, then here is a video that demonstrates how to install python, either on Windows or mac OS. Python Tutorial for Beginners 1: https://www.youtube.com/watch?v=YYXdXT2l-Gg&ab_channel=CoreySchafer

Introduction

The following instruction set is aimed at computer science/ game programming students, or intermediate programmers that are interested in learning how to program a text-based game using python. Python is a programming language that can tackle many problems, because of this it has risen in demand over the past years now. It is critical for computer science majors, and game programming majors to learn how to use python and creating a text-based game is a fun and easy way to learn Python in around 25-30 minutes.

Disclaimer

This instruction set will be using python 3.9, so make sure you are up to date with your python software on your personal device. The story in my game can be wildly different in your game, the thing that you should pay attention to are the methods I use to execute my code.

Supplies

Python 3.9

Basic-intermediate level of Python understanding

Python IDLE

step1.PNG

1.Open up your python IDLE, this will open up a python shell.

Create New File

step2.PNG

2. Go to file on the upper right corner of the python shell, and press new File. This will open up a new file to program our text-based game.

Function Game

step3.PNG

3. Type in a function named game, and type in the game method bellow the function. We will be writing the rest of the code inside the game function, and we write in the game method so that the program runs whenever we open the python file.

Create an If Statement

step4.PNG

4. Create an If statement. Above of the if statement write an answer input that takes a yes or no response, inside of the parameters that says if the player wants to play or not. Type in the if statement's parameters to determine if the answer input is yes to write out a print statement welcoming the player (use lower () with answer to avoid case sensitivity), then Create a start Boolean that is true, then create an empty inventory list.

Else Statement

step5.PNG

5. Create an else statement that prints out something else when the players decide to type in the ‘no’ input

If Statement

step6.PNG

6. Create an if statement that takes the input of our start Boolean that sees if it is True or False. Write in an introduction that greets the player into your game. Create a choice input that takes up their next yes or no response.

If Statement

Step7.PNG

7. Create an if statement: the if statement’s parameters should determine the ‘yes’ case of the previous choice input. Then, create a choiceY1 Boolean that is true. Then write some story of how you want this decision to impact the player. Then add something to the empty list by using append (), to reward the player for this yes decision.

Create Else Statement

Step8.PNG

8. Create an else statement, and inside it, create a choiceY1 Boolean that is False. Write some story that shows the player that their decisions have consequences. Add something to the list using append to reward the player for their no response.

Create If Statement

step9.PNG

9. Create an if statement: the if statement accounts for the False ChoiceY1 variable and the item that is in the inventory to progress the story. Add some story, and add an input decide of what the player should do next.

Create an Elif Statement

Step10.PNG

10. Add an elif statement, add some story that rewards the player for picking the ‘Yes’ option for choiceY1, and then add an input statement approach that progresses the story into a battle for the player. We will return to elif statement later.

Create an If Statement

Step11.PNG

11.Add an if statement that is underneath the last if statement. The if statement should determine a negative decision from the decide input, like ending the game.

Create Elif Statement

step12.PNG

12. Add an input in the elif statement that should contain an input that continues the story.

Create an If Statement

step13.PNG

13. Create an if statement: the if statement ‘s parameters should contain the no value of approach1 input, it should print out a game over screen.

Create an If Statement

step14.PNG

14. Create an if statement, in the if statement write multiple(at least more than two) options that the player can use, with certain percentage points as their success rate. Write a minpoint variable that equals 1. Write a max point variable that equal 100. Write a Chx option variable that is equal to a random object that takes the random ints minpoint and maxpoint, and multiply it with the percentage you assigned it previously, do this for any number of choices you want to add, I added three, this should allow for an authentic role playing experience to be had.

Scroll to the Top

step15.PNG

15. Scroll all the way to the top, and underneath the def game () line, add import random. This will allow us to activate the random effect we added the previous step.

Scroll All the Way Down

step16.PNG

16.Scroll all the way down to the previous If statement on step 10. create an if-elif-else statement: in the if statement’s parameters, write the first user input value, then write and Boolean where it is greater than some value, I put down 25. Then write that their option was successful. Do the same with elif statement, except write in that their decision worked out. Do this for how many choices you have. Write an else statement where it shows that the player failed and shows game over. Remember, that if an item you added to the list is a weapon, use 'in' to determine if the player can use that move in the first place.

Scroll Back

step17.PNG

17: Scroll back to the if statement from step 7.

Write If Statement

step18.PNG

18. Write an if statement that evaluates the no option of the approach input. Print out a game over screen.

Create If Statement

step19.PNG

19. Create an if statement that takes the yes option. Print out the success rates for the player options. Create a decision input that tells the player what to choose. Create a min point equal to 1, create a max point equal to 100, Write the ch variables that are random ints that take the range of the min and max points, then multiply each by their success rate respectively.

Create an If-elif-else Statement

step20.PNG

20. Create an if-elif-else statement: the if statement should take the first option of the decision, and should be greater than a certain value, I chose 25. Do the same with the elif statement, depending on how many choices you want to add. Add an else statement that ends the game.

-----------------------------------------------------Celebrate! ------------------------------------------------------------

Pat yourselves on the back, you just completed your first python text game! Throughout the instruction set we have learned how to create complex if else statements, import random and use its built in functions, and create a systematic story where the player’s decision have meaning. It is vital that you take the lessons you learned from this instruction set and expand on them for your future projects.

If you are serious about expanding your current programming knowledge please check out r/ computerscience on reddit, as they will help you expand your programming skills and answer any question you might have. Also check out Stack Overflow for any problems or errors you might come across in your programming projects, either this one or any other project, they will be happy to lend a hand.

Thank you for joining me, now go on and control the world with python.