I'm making a text based game, and I want players to be able to do battle with monsters throughout the game. I have player stats stored in a dictionary, and for attack I am using the random.randint(a,b). The problem is that this is stored outside of the while loop for the battle. So the attack is always the same, but I want it to change throughout the loop.
The only thing that I could think of so far was to store it in the dictionary as a string and then use slice to take the quotation marks off and then use int() so that it's not an integer anymore. Here's the last thing I tried. Spells have been commented out until I can figure out
import random
player = {"attack":"(random.randint(2,4))", "health": 50}
goblin = {"name": "goblin", "attack": "(random.randint(1,3))", "health": 30}
def battle(user, monster):
for key in player:
if key != "health":
print(key)
while goblin["health"] > 0:
user["attack"] = int(user["attack"][1:-1])
monster["attack"] = int(monster["attack"][1:-1])
print(user["attack"])
print(monster["attack"])
select = input("Would you like to attack or use a spell?")
if select == "attack":
print(user["attack"])
goblin["health"] -= user["attack"]
print("The", (monster["name"]), "is at", (monster["health"]),"health")
user["health"] -= goblin["attack"]
print("The goblin hit you for", goblin["attack"], "damage, you are at", user["health"])
if user["health"] < 0:
print("You were killed by the ")
print("You killed the goblin!")
battle(player, goblin)
I know it's a weird question. Any help would be greatly appreciated.
You won't be able to use an int()
, you'd have to eval()
, which normally is recommended against—security issues may not be a huge issue for a game, but it'd also be, subjectively, a somewhat clunky and inelegant design.
Disregarding the bigger design picture, is there anything preventing you from simply defining attacks as anonymous functions in your dictionaries?
player = {"attack": lambda: random.randint(2, 4), "health": 50}
This way later you can do, for example,
goblin["health"] -= player["attack"]()
You can also have damage calculation defined as independent function:
player = {"attack_skill_factor": 2, "health": 50, "defense_skill_factor": 2}
goblin = {"attack_skill_factor": 1, "health": 30, "defense_skill_factor": 1}
def calculate_damage(aggressor, victim):
return (
random.randint(1, 3) *
aggressor['attack_skill_factor'] /
victim['defense_skill_factor'])
# Action
damage = calculate_damage(goblin, player)
player['health'] -= damage
print(
"The goblin hit you for {damage} damage, "
"you are at {health}".
format(damage=damage, health=player['health'])
Next logical step would probably be using classes to define players and NPCs in an OOP manner.
Why not use:
player = {"attack":"2-4", "health": 50}
goblin = {"name": "goblin", "attack": "1-3", "health": 30}
then use a split (using '-' as the delimiter string) to get the upper and lower bounds of the attack, then you can calculate a random value inside the loop:
user["attack"] = random.randint(int(player["attack"].split('-')[0], int(player["attack"].split('-')[1])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.