patternpythonMinorCanonical
Small text-based RPG (v2)
Viewed 0 times
textsmallbasedrpg
Problem
This is my first text-based game I made. It's very small because I just learned Python and wanted to start small, and then code bigger programs. I added some stuff since the last time. Please give your honest opinion.
Here is my original question about this game.
Here is my original question about this game.
import random
import time
import sys
def fight_enemy_3(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)
if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
you_killed_him()
else:
print("You walk away unscathed, but the {enemy_name} still lives.".format(enemy_name=enemy_name))
you_killed_him()
def you_killed_him():
print("You keep walking")
def friends():
print("You and your friend combined powers")
intro()Solution
Design
As it stands, in order for you to create "stages" you create multiple functions, each containing many
I've actually created a library, along with the help of a few other users, which makes creating adventure games like this much easier. It's called Cactus, and you can find the repository here, along with the download links. You can also visit the website.
From the official docs:
Imports
It’s super easy to import Cactus into your project. Once you’ve added
Creating a cactus.Position instance
A
a
the reference key to the other position in the flowchart.
Here’s how a simple
Creating a
Flowcharts are essential to Cactus. Flowcharts are how you stucture
the "map" that the player of your game will traverse. For reference,
here’s a list of the
Here’s how a simple Flowchart instance is structured.
Creating a
The
relevant to flowcharts or positions. The
where event handlers and global commands are stored. For reference,
here’s a list of the
none of that happens.
Here’s how a simple
Running your game
Once you’ve done all of the above, you’re ready to play your game.
Assuming you’ve created an instance of
and everything checks out, all you need to do is the below:
Removing duplication
Rather than (again) recreating the
```
def fight_enemy(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage, function_to_run):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)
if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
function_to_run()
else:
print("You walk away uns
As it stands, in order for you to create "stages" you create multiple functions, each containing many
if statements, each determining which function or "stage" is going to be run next. This isn't a great way to do this, and it can make code very hard to read.I've actually created a library, along with the help of a few other users, which makes creating adventure games like this much easier. It's called Cactus, and you can find the repository here, along with the download links. You can also visit the website.
From the official docs:
Imports
It’s super easy to import Cactus into your project. Once you’ve added
Cactus/cactus to your PYTHONPATH, this is all you need to do:import cactusCreating a cactus.Position instance
A
cactus.Position instance stores relevant data about a position ona
cactus.Flowchart. For reference, here’s a list of thecactus.Position attributes."name"- The position’s name.
"desc_enter"- The description to display when the player “enters” the position.
"desc_exit"- The description to display when the player “exits” the position.
"choices"- A dictionary of choices representing other positions on the flowchart. The key is what the user enters, and the value is
the reference key to the other position in the flowchart.
Here’s how a simple
cactus.Position instance is structured:cactus.Position({
"name": "start",
"desc_enter": "Description on enter.",
"desc_exit": "Description on exit.",
"choices": {
"choice1": "1",
"choice2": "2"
}
})Creating a
cactus.Flowchart instanceFlowcharts are essential to Cactus. Flowcharts are how you stucture
the "map" that the player of your game will traverse. For reference,
here’s a list of the
cactus.Flowchart attributes."data"- Stores the actual flowchart data as a dictionary.
Here’s how a simple Flowchart instance is structured.
FLOWCHART = cactus.Flowchart({
"data": {
"position_name": cactus.Position(
...
)
}
})Creating a
cactus.Game instanceThe
cactus.Game class stores general data about your game that isn’trelevant to flowcharts or positions. The
cactus.Game class is alsowhere event handlers and global commands are stored. For reference,
here’s a list of the
cactus.Game attributes."name"- The game’s name.
"desc"- The game’s description.
"prompt"- The prompt to be used for entering commands.
"invalid_input_msg"- The message to be displayed when the user enters invalid input.
"flowchart"- The flowchart instance that has been created.
"case_sensitive"- If this is False, then all user input, and certain attribute values a converted to lowercase. If it’s True, then
none of that happens.
"allow_help"- If this is True, then the user can obtain help.
"about_text"- The game’s about text, including the reqiuired attribution text.
"event_handlers"- A dictionary of the event handlers and their corresponding functions.
"global_commands"- A dictionary of commands that can be executed anywhere, and their corresponding functions.
Here’s how a simple
cactus.Game instance is structured.GAME = cactus.Game({
"name": "Game Name",
"desc": "Game Description",
"prompt": "> ",
"invalid_input_msg": "Invalid input",
"flowchart": FLOWCHART,
"case_sensitive": False,
"allow_help": True,
"about_text": "Write about your game here.",
"event_handlers": {
"position.Name Goes Here 1.enter.after": exit,
"position.Name Goes Here 2.enter.after": exit
},
"global_commands": {
"exit": exit
}
})Running your game
Once you’ve done all of the above, you’re ready to play your game.
Assuming you’ve created an instance of
cactus.Game, named it GAME,and everything checks out, all you need to do is the below:
GAME.play_game()Removing duplication
Rather than (again) recreating the
fight_enemy function to run a different function each time, you can add an additional argument to the end of the function signature, function_to_run. You fight_enemy function would then become this:```
def fight_enemy(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage, function_to_run):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)
if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
function_to_run()
else:
print("You walk away uns
Code Snippets
import cactuscactus.Position({
"name": "start",
"desc_enter": "Description on enter.",
"desc_exit": "Description on exit.",
"choices": {
"choice1": "1",
"choice2": "2"
}
})FLOWCHART = cactus.Flowchart({
"data": {
"position_name": cactus.Position(
...
)
}
})GAME = cactus.Game({
"name": "Game Name",
"desc": "Game Description",
"prompt": "> ",
"invalid_input_msg": "Invalid input",
"flowchart": FLOWCHART,
"case_sensitive": False,
"allow_help": True,
"about_text": "Write about your game here.",
"event_handlers": {
"position.Name Goes Here 1.enter.after": exit,
"position.Name Goes Here 2.enter.after": exit
},
"global_commands": {
"exit": exit
}
})GAME.play_game()Context
StackExchange Code Review Q#100644, answer score: 5
Revisions (0)
No revisions yet.