How To Make An Infinite Game In Python 3
Hey there! In this tutorial, I’ll show you how to make an infinite game.You will need to know some things about Python before you get started, though. So, without further ado, let’s get started!
(oh god this phrase is used too much)
For this tutorial, we’ll only import one thing: random. Type
import random in the program code and you should be good to go!
Now, make a list storing two values: the strings left and right. Then, create a variable storing the score of your player, and set it to zero.
left_or_right = [“left”, “right”] score = 0
The REAL Code
Ok, so now we can get on to the actual code. First, we make a while loop
that runs forever using
while True:. Then we put an
input so the user can type left or right in, and we assign a variable which holds a
random.choice value to pick left or right randomly.
while True: inpt = input(“Choose left or right: “) choosed = random.choice(left_or_right)
Now we add a few
if statements to finish.
The code that I’m going to show you means:
if choosed is the same as as the input given by the player, but not always having the same capitalization, then the hallway is safe and your score goes up by one,
else, say Game Over, display your score, and break from the loop.
while True: inpt = input(“Choose left or right: “) choosed = random.choice(left_or_right) if input.lower() == choosed: print(“The hallway is safe!”) score += 1 else: print(“Game over!”) print(“Score: “ + str(score)) break
str() is VERY important. If you don’t wrap it around the score variable in the last
If you combine all these code together, you get a simple infinite game! You can always add more things to your infinite game program.
Thanks for reading!
(The completed code is down below.)