patternpythonMinor
Text adventure game
Viewed 0 times
textgameadventure
Problem
I just downloaded Python for the first time today, and I had a lot of fun playing around with it for like five hours.
I watched one YouTube video on how to make a text adventure, and then I decided to try my hand at it.
It's my first day, so I'm sure there are a million things that I could do to make my coding more efficient. I basically copied and pasted the majority of the code into every room of the adventure, so mainly I'm looking at shortcuts that would have allowed me to not be so repetitive, but any advice/feedback is appreciated.
```
def start():
print('''
Welcome to Aaron's Text Adventure!
''')
global gold
gold = 2
global health
health = 10
global energy
energy = 10
global melee
melee = 1
global ranged
ranged = 0
global ammo
ammo = 0
global fullhealth
fullhealth = 10
global attacknum
attacknum = 0
cottage()
## Game functions
def checkstats():
print('''
****''')
print("Gold:", gold)
print("Health:", health,"/",fullhealth)
print("Melee:", melee)
print ("Energy:", energy)
global ranged
if ranged > 0:
print("Ranged:", ranged)
print("Ammo:", ammo)
def prompt():
x = input("Type a command: ")
return x
##Rooms***
## Cottage////
def cottage():
print("")
print('''
You are in your cottage in a small village.
''')
print('''Options:
''')
command = prompt()
if command == "1":
hometown()
elif command == "2":
print("****")
print ("You are back to full health. (",fullhealth,"/",fullhealth,")")
global health
health = fullhealth
cottage()
elif command == "9":
I watched one YouTube video on how to make a text adventure, and then I decided to try my hand at it.
It's my first day, so I'm sure there are a million things that I could do to make my coding more efficient. I basically copied and pasted the majority of the code into every room of the adventure, so mainly I'm looking at shortcuts that would have allowed me to not be so repetitive, but any advice/feedback is appreciated.
```
def start():
print('''
Welcome to Aaron's Text Adventure!
''')
global gold
gold = 2
global health
health = 10
global energy
energy = 10
global melee
melee = 1
global ranged
ranged = 0
global ammo
ammo = 0
global fullhealth
fullhealth = 10
global attacknum
attacknum = 0
cottage()
## Game functions
def checkstats():
print('''
****''')
print("Gold:", gold)
print("Health:", health,"/",fullhealth)
print("Melee:", melee)
print ("Energy:", energy)
global ranged
if ranged > 0:
print("Ranged:", ranged)
print("Ammo:", ammo)
def prompt():
x = input("Type a command: ")
return x
##Rooms***
## Cottage////
def cottage():
print("")
print('''
You are in your cottage in a small village.
''')
print('''Options:
- Go Outside
- Rest
- Stats
''')
command = prompt()
if command == "1":
hometown()
elif command == "2":
print("****")
print ("You are back to full health. (",fullhealth,"/",fullhealth,")")
global health
health = fullhealth
cottage()
elif command == "9":
Solution
As I told you, the best practice is to use a class, I called it
The
The constructor:
The
main()
Adventure (it sounds encouraging).The
start() function is the starting point which do initialisation. So, let's create a constructor for that:import textwrap
class Adventure(object):
def __init__(self):
self.gold = 2
self.health = 10
self.energy = 10
self.melee = 1
self.ranged = 0
self.ammo = 0
self.full_health = 10
self.attack_num = 0
def start(self):
msg = textwrap.dedent('''
****************************************
* *
* Welcome to Aaron's Text Adventure! *
* *
****************************************
''')
print(msg)
self.cottage()
def cottage(self):
# ...
pass
def main():
adventure = Adventure()
adventure.start()
if __name__ == '__main__':
main()The constructor:
- Initialisation is done in the constructor (the
__init__method);
- Usually, I prefer not doing anything in the constructor (except initialisation), so I decided to move the
printin thestartmethod.
- I rename the following variable:
fullhealthandattacknumthat way my editor don't alert me about spelling. If you used to do that, your code will be easier to maintain.
The
start method:- I use
textwrap.dedent()to have good looking messages: it simply remove the indentation;
- Like you, I trigger the
cottage()method, but it will be better to use a Finite State Machine…
main()
- It is better to put the main code in a
main()function (call it as you like). Because, yourAdventureclass could be use as an API, for instance: one can inherit from it and add more, say, locations…
- In Python, you use the
if __name__ == '__main__'construction do call the main function when the program is executed as a script. See the answer to the question What does if__name__ == “__main__”: do?
Code Snippets
import textwrap
class Adventure(object):
def __init__(self):
self.gold = 2
self.health = 10
self.energy = 10
self.melee = 1
self.ranged = 0
self.ammo = 0
self.full_health = 10
self.attack_num = 0
def start(self):
msg = textwrap.dedent('''
****************************************
* *
* Welcome to Aaron's Text Adventure! *
* *
****************************************
''')
print(msg)
self.cottage()
def cottage(self):
# ...
pass
def main():
adventure = Adventure()
adventure.start()
if __name__ == '__main__':
main()Context
StackExchange Code Review Q#155661, answer score: 5
Revisions (0)
No revisions yet.