patternpythonMinor
Can my Python maze game be better in any way?
Viewed 0 times
canmazeanywaybettergamepython
Problem
How could I improve or make this program more user friendly? I have not been programming very long, so please don't give me a task that is too complicated.
```
from random import randint
import time
global word
word = "a"
shield = 5
#The main function
def main():
#This will trigger the introduction
intro()
while TakeTurn() == False:
if shield == 1:
word1 = "shield"
else:
word1 = "shields"
print ("You have", shield, word1,)
if shield < 1:
print ("Sorry! You ran out of shields! You lose!")
else:
print ("You win")
#This function is the introduction to the program
def intro():
time.sleep(2)
print ("You are lost in a maze, it is dark and you are lost")
time.sleep(2)
print ("Im afraid there are monsters here...")
time.sleep(1.5)
print ("BOO!")
time.sleep(1)
print ("You have five shields to protect you...")
time.sleep(2)
print ("Use them well!")
time.sleep(1)
print ("Okay, let's go!")
#This function is the actual 'game' and will deterine what happens to the character
def TakeTurn():
global word
global shield
time.sleep(1.5)
#This means that when the user reaches 0 shields, they lose.
if shield < 1:
return True
#Whatever the user inputs will not actually affect the outcome
print ("You have reached", word ,"junction.\nDo you want to turn left (L), turn right (R) or go straight ahead(S)?")
turning = input()
#This is a simple instruction that means that the first time you come to a junction, it will say 'a junction' but the second time it will say 'another junction'
word = "another"
#This 'if' statement means that the program will only continue if the user has inputed the letters 'L', 'R' or 'S'
if turning not in ["L","R","S","l","r","s"] :
time.sleep (0.7)
print ("Sorry, I didn't understand that")
TakeTurn()
else:
choice = randint (1
```
from random import randint
import time
global word
word = "a"
shield = 5
#The main function
def main():
#This will trigger the introduction
intro()
while TakeTurn() == False:
if shield == 1:
word1 = "shield"
else:
word1 = "shields"
print ("You have", shield, word1,)
if shield < 1:
print ("Sorry! You ran out of shields! You lose!")
else:
print ("You win")
#This function is the introduction to the program
def intro():
time.sleep(2)
print ("You are lost in a maze, it is dark and you are lost")
time.sleep(2)
print ("Im afraid there are monsters here...")
time.sleep(1.5)
print ("BOO!")
time.sleep(1)
print ("You have five shields to protect you...")
time.sleep(2)
print ("Use them well!")
time.sleep(1)
print ("Okay, let's go!")
#This function is the actual 'game' and will deterine what happens to the character
def TakeTurn():
global word
global shield
time.sleep(1.5)
#This means that when the user reaches 0 shields, they lose.
if shield < 1:
return True
#Whatever the user inputs will not actually affect the outcome
print ("You have reached", word ,"junction.\nDo you want to turn left (L), turn right (R) or go straight ahead(S)?")
turning = input()
#This is a simple instruction that means that the first time you come to a junction, it will say 'a junction' but the second time it will say 'another junction'
word = "another"
#This 'if' statement means that the program will only continue if the user has inputed the letters 'L', 'R' or 'S'
if turning not in ["L","R","S","l","r","s"] :
time.sleep (0.7)
print ("Sorry, I didn't understand that")
TakeTurn()
else:
choice = randint (1
Solution
Welcome to programming and Python. I have a few suggestions. There are a lot of suggestions here in this answer. To improve the style of your code beyond the things I have spoken to here, have a look at the official Python style guide.
-
This code would be simpler if you used classes. However, you said you are new to programming so I won't push this idea too hard. A simple class skeleton that could fit your needs might be:
-
Try not to use
-
You have a lot of
Using this function in your
-
When you are getting
-
In your
-
Your choice if-elses could be simplified if they were placed into a
There are other things I can point out. However, I have already given you a lot of information. Once you have updated your code, feel free to comment and I'll update my answer with more suggestions.
-
This code would be simpler if you used classes. However, you said you are new to programming so I won't push this idea too hard. A simple class skeleton that could fit your needs might be:
class Player(object):
def __init__(self, shield, treasure):
self.shield = shield
self.treasure = treasure
def take_turn(self):
# Do stuff-
Try not to use
global. This should only be used if you HAVE to. Implementing classes as I suggested above would help remove the use for global. Also you could pass the variables as parameters to the functions:def TakeTurn(word, shield):
...-
You have a lot of
time.sleep(...) then print('stuff'). This is fine, however it results in a lot of repeated code. You could bring these lines into a single function:def print_on_a_timer(times, lines):
for time, line in zip(times, lines):
time.sleep(time)
print(line)Using this function in your
intro function for example:def intro():
times = [2, 2, 1.5, 1, 2, 1]
lines = ["You are lost in a maze, it is dark and you are lost",
"Im afraid there are monsters here...", "BOO!",
"You have five shields to protect you...","Use them well!",
"Okay, let's go!"]
print_on_a_timer(times, lines)-
When you are getting
input from the user, you are checking for both lowercase and uppercase letters. This is good, however instead of doing choice not in ["Y","N","y","n"] you could use the lower function:choice = input().lower()
if choice not in ['y', 'n']:-
In your
TakeTurn function you restart the function if the user didn't provide good input. Instead of calling the function again, you can use a while loop:print ("You have reached", word ,"junction.\nDo you want to turn left (L),\
turn right (R) or go straight ahead(S)?")
turning = input().lower()
while turning not in ["l", "r", "s"]:
time.sleep (0.7)
print ("Sorry, I didn't understand that")
turning = input().lower()-
Your choice if-elses could be simplified if they were placed into a
dict and the results were their own functions:choices = {'1':lambda: update_shields(1),
'2':lambda: update_shields(2),
'3':lambda: update_shields(3), ...}
choice = input()
choices[choice]()
. . .
# Updates the current shield count and prints an appropriate message.
def update_shield(num_shields):
shields += num_shields
multiple = num_shields > 1 or num_shields 0 else 'lost',
num_shields, 's' if multiple else ''))There are other things I can point out. However, I have already given you a lot of information. Once you have updated your code, feel free to comment and I'll update my answer with more suggestions.
Code Snippets
class Player(object):
def __init__(self, shield, treasure):
self.shield = shield
self.treasure = treasure
def take_turn(self):
# Do stuffdef TakeTurn(word, shield):
...def print_on_a_timer(times, lines):
for time, line in zip(times, lines):
time.sleep(time)
print(line)def intro():
times = [2, 2, 1.5, 1, 2, 1]
lines = ["You are lost in a maze, it is dark and you are lost",
"Im afraid there are monsters here...", "BOO!",
"You have five shields to protect you...","Use them well!",
"Okay, let's go!"]
print_on_a_timer(times, lines)choice = input().lower()
if choice not in ['y', 'n']:Context
StackExchange Code Review Q#52083, answer score: 8
Revisions (0)
No revisions yet.