patternpythonMinor
Writing the "Camel Game" with Python
Viewed 0 times
thewithwritingcamelgamepython
Problem
I'm currently on Ch 4 Lab 4 in Programming Arcade Games with Python and Pygame.
Any feedback on how I could improve the code would be awesome. I'm using Python IDLE 3.4.3.
```
import random
print("Welcome to Camel!")
print("""You have stolen a camel to make your way across the great Mobi desert.
The natives want their camel back and are chasing you down! Survive your desert trek and out run the natives.""")
print()
#variables
milesTraveled = 0
thirst = 0
camelFatigue = 0
nativesTraveled = -20
canteen = 3
done = False
#start main loop
while not done:
nativesBehind = milesTraveled - nativesTraveled
fullSpeed = random.randrange(10, 21)
moderateSpeed = random.randrange(5, 13)
print("""
A. Drink from your canteen.
B. Ahead at moderate speed.
C. Ahead full speed.
D. Stop for the night.
E. Status check
Q. Quit.""")
print()
userInput = input("Your choice? ")
if userInput.lower() == "q":
done = True
#status
elif userInput.lower() == "e":
print("Miles traveled: ",milesTraveled)
print("Drinks in canteen: ",canteen)
print("Your camel has ",camelFatigue,"amount of fatigue.")
print("The natives are ",nativesBehind,"miles behind you.")
#stop for night
elif userInput.lower() == "d":
camelFatigue *= 0
print("Your camel feels refreshed and happy his fatigue is now ",camelFatigue)
nativesTraveled += random.randrange(7, 15)
#move full speed
elif userInput.lower() == "c":
print("You traveled ",fullSpeed,"miles!")
milesTraveled += fullSpeed
thirst += 1
camelFatigue += random.randrange(1, 4)
nativesTraveled += random.randrange(7, 15)
oasis = random.randrange(1, 21)
#move moderate speed
elif userInput.lower() == "b":
print("You traveled ",moderateSpeed,"miles!")
milesTraveled += moderateSpeed
thirst += 1
camelFatigue += 1
nativesTraveled += random.randrange(7, 15
Any feedback on how I could improve the code would be awesome. I'm using Python IDLE 3.4.3.
```
import random
print("Welcome to Camel!")
print("""You have stolen a camel to make your way across the great Mobi desert.
The natives want their camel back and are chasing you down! Survive your desert trek and out run the natives.""")
print()
#variables
milesTraveled = 0
thirst = 0
camelFatigue = 0
nativesTraveled = -20
canteen = 3
done = False
#start main loop
while not done:
nativesBehind = milesTraveled - nativesTraveled
fullSpeed = random.randrange(10, 21)
moderateSpeed = random.randrange(5, 13)
print("""
A. Drink from your canteen.
B. Ahead at moderate speed.
C. Ahead full speed.
D. Stop for the night.
E. Status check
Q. Quit.""")
print()
userInput = input("Your choice? ")
if userInput.lower() == "q":
done = True
#status
elif userInput.lower() == "e":
print("Miles traveled: ",milesTraveled)
print("Drinks in canteen: ",canteen)
print("Your camel has ",camelFatigue,"amount of fatigue.")
print("The natives are ",nativesBehind,"miles behind you.")
#stop for night
elif userInput.lower() == "d":
camelFatigue *= 0
print("Your camel feels refreshed and happy his fatigue is now ",camelFatigue)
nativesTraveled += random.randrange(7, 15)
#move full speed
elif userInput.lower() == "c":
print("You traveled ",fullSpeed,"miles!")
milesTraveled += fullSpeed
thirst += 1
camelFatigue += random.randrange(1, 4)
nativesTraveled += random.randrange(7, 15)
oasis = random.randrange(1, 21)
#move moderate speed
elif userInput.lower() == "b":
print("You traveled ",moderateSpeed,"miles!")
milesTraveled += moderateSpeed
thirst += 1
camelFatigue += 1
nativesTraveled += random.randrange(7, 15
Solution
Bad instructions
Perhaps the lab is instructing you specifically to write code in a certain way just for learning purposes, but there is some questionable advice in the instructions that I would recommend disregarding.
Printing the game instructions using multiple
Introducing a
Logic
The input is case-insensitive. To avoid code repetition, you should immediately normalize the input to lowercase (or uppercase, as the instructions suggest):
There are five menu options, plus the "Quit" option. Of those five, two are "instantaneous", and three involve the passage of time. Of the three options that involve passage of time, two cause you to cover some distance. The code should therefore be structured such that similar code is placed together. Also, you should avoid generating and discarding random numbers that you aren't going to use (for example,
It's odd how you write
By my interpretation, it should not be possible to stumble across an oasis while resting for the night. It would be even more realistic (and easier to code) if the probability of finding an oasis were proportional to the distance covered, but I don't get to make the rules.
When evaluating the situation at the end of the loop, I would place the position checks first, then the health checks, then any applicable warnings. You don't really care about the natives drawing near or finding an oasis if you've already crossed the finish line. You can also simplify the thirst and fatigue checks if you test for death first. (By the way, in Python, you can also write double-ended inequalities like
Perhaps the lab is instructing you specifically to write code in a certain way just for learning purposes, but there is some questionable advice in the instructions that I would recommend disregarding.
Printing the game instructions using multiple
print statements is poor practice. Combining the instructions into one long triple-quoted string is more readable and efficient.Introducing a
done flag variable is a bad idea in general. If your goal is to direct the execution of code in a loop, use statements like continue, break, return, or raise. Setting a variable just to have it tested later is needlessly indirect. In fact, I would say that for this exercise, a done flag fails to capture enough state information for a realistic game. (More on that below.)Logic
The input is case-insensitive. To avoid code repetition, you should immediately normalize the input to lowercase (or uppercase, as the instructions suggest):
user_choice = input("Your choice? ").upper()There are five menu options, plus the "Quit" option. Of those five, two are "instantaneous", and three involve the passage of time. Of the three options that involve passage of time, two cause you to cover some distance. The code should therefore be structured such that similar code is placed together. Also, you should avoid generating and discarding random numbers that you aren't going to use (for example,
fullSpeed = random.randrange(10, 21) when the user hasn't even decided to make a move).# Quit
if user_choice == "Q":
break
# Status check
elif user_choice == "E":
print(…)
# Drink from canteen
elif user_choice == "A":
if canteen == 0:
print("You're out of water.")
else:
canteen -= 1
thirst = 0
print(…)
# Stop for the night
elif user_choice == "D":
natives_traveled += random.randrange(7, 15)
camel_fatigue = 0
print(…)
# Moderate speed
elif user_choice == "B":
natives_traveled += random.randrange(7, 15)
miles_traveled += random.randrange(5, 13)
camel_fatigue += 1
thirst += 1
# Full speed
elif user_choice == "C":
natives_traveled += random.randrange(7, 15)
miles_traveled += random.randrange(10, 21)
camel_fatigue += random.randrange(1, 4)
thirst += 1It's odd how you write
variable *= 0 when you could just set variable = 0.By my interpretation, it should not be possible to stumble across an oasis while resting for the night. It would be even more realistic (and easier to code) if the probability of finding an oasis were proportional to the distance covered, but I don't get to make the rules.
When evaluating the situation at the end of the loop, I would place the position checks first, then the health checks, then any applicable warnings. You don't really care about the natives drawing near or finding an oasis if you've already crossed the finish line. You can also simplify the thirst and fatigue checks if you test for death first. (By the way, in Python, you can also write double-ended inequalities like
5 < camelFatigue <= 8.)# Position checks
if miles_traveled >= 200:
print("You made it across the desert!")
break
elif natives_traveled >= miles_traveled:
print("The natives caught and beheaded you.\nYou're dead!")
break
# Health checks
# 1 in 20 chance of finding an oasis
if user_choice != "D" and random.randrange(20) == 0:
camel_fatigue, thirst, canteen = 0, 0, 3
print("You have found…")
elif thirst > 6:
print("You died of dehydration!")
break
elif camel_fatigue > 8:
print("Your camel is dead.")
break
# Warnings
if natives_traveled >= miles_traveled - 15:
print("The natives are drawing near!")
if thirst > 4:
print("You are thirsty")
if camel_fatigue > 5:
print("Your camel is getting tired.")Code Snippets
user_choice = input("Your choice? ").upper()# Quit
if user_choice == "Q":
break
# Status check
elif user_choice == "E":
print(…)
# Drink from canteen
elif user_choice == "A":
if canteen == 0:
print("You're out of water.")
else:
canteen -= 1
thirst = 0
print(…)
# Stop for the night
elif user_choice == "D":
natives_traveled += random.randrange(7, 15)
camel_fatigue = 0
print(…)
# Moderate speed
elif user_choice == "B":
natives_traveled += random.randrange(7, 15)
miles_traveled += random.randrange(5, 13)
camel_fatigue += 1
thirst += 1
# Full speed
elif user_choice == "C":
natives_traveled += random.randrange(7, 15)
miles_traveled += random.randrange(10, 21)
camel_fatigue += random.randrange(1, 4)
thirst += 1# Position checks
if miles_traveled >= 200:
print("You made it across the desert!")
break
elif natives_traveled >= miles_traveled:
print("The natives caught and beheaded you.\nYou're dead!")
break
# Health checks
# 1 in 20 chance of finding an oasis
if user_choice != "D" and random.randrange(20) == 0:
camel_fatigue, thirst, canteen = 0, 0, 3
print("You have found…")
elif thirst > 6:
print("You died of dehydration!")
break
elif camel_fatigue > 8:
print("Your camel is dead.")
break
# Warnings
if natives_traveled >= miles_traveled - 15:
print("The natives are drawing near!")
if thirst > 4:
print("You are thirsty")
if camel_fatigue > 5:
print("Your camel is getting tired.")Context
StackExchange Code Review Q#106486, answer score: 5
Revisions (0)
No revisions yet.