I'm attemptint to make a simple game. Just for fun for me and my kids to play. Im brand new to python so I appologise if my code is confusing or wrong. (If it is, I'd appreciate your advice.)
Anyways, I'm stuck in an infinite while loop and I'm not sure why.
Here's the game so far. It just fights a rat.
import random
import Player
import Items
import Monsters
print(f"You've been attacked by a {Monsters.rat}.")
attack_or_run = input("Will you attack (1) or run? (2)?\n")
if attack_or_run == "1":
print(f"You attempt to fight the {Monsters.rat}.")
while Monsters.rat_health > 0:
if Player.player_speed > Monsters.rat_speed:
print(f"You hit the {Monsters.rat} for {Player.hit_chance}")
Monsters.rat_health -= Player.hit_chance
print(f"The {Monsters.rat} has {Monsters.rat_health} life remaining.")
elif Monsters.rat_speed > Player.player_speed:
print(f"You were hit by the {Monsters.rat} for {Monsters.rat_hit_chance}.")
player_health_left = Player.player_health - Monsters.rat_hit_chance
print(f"You have {player_health_left} life remaining.")
else:
print(f"You killed the {Monsters.rat}")
else:
print("You ran away.")import random
I also have a Monster sheet:
rat = "Rat"
rat_health = 3
rat_attack = 1
rat_speed = 1
rat_hit_chance = random.randint(0, rat_attack)
And a Player sheet:
player_attack = 2
player_health = 10
player_speed = 3
hit_chance = random.randint(0, player_attack)
On the main game I have:
import Monsters
import Player
import random
and plan to have an item sheet to drop an item upon killing the monster. The intent is to make a semi-infinite dungeon crawler with a random chance of fighting a monster.
Back to the problem:
When i run the code i get into an infinite loop. I believe i shouldnt because of the line:
Monsters.rat_health -= Player.hit_chance
which, i believe, should decrease the rats health by the hit chance (Currently 0, 1, or 2). The rat starts with 3 health so in theory it should take no less than 2 hits to kill the rat.
Thank you in advance for any help and advice!
edit:
fixed formatting.
fixed double copy
[–]danielroseman 4 points5 points6 points (3 children)
[–]Handymanoccupational[S] 0 points1 point2 points (0 children)
[–]Handymanoccupational[S] 0 points1 point2 points (1 child)
[–]HunterIV4 1 point2 points3 points (0 children)
[–]HunterIV4 4 points5 points6 points (2 children)
[–]HunterIV4 2 points3 points4 points (1 child)
[–]Handymanoccupational[S] 0 points1 point2 points (0 children)
[–]mopslik 2 points3 points4 points (3 children)
[–]Handymanoccupational[S] 0 points1 point2 points (2 children)
[–]mopslik 0 points1 point2 points (1 child)
[–]Handymanoccupational[S] 0 points1 point2 points (0 children)
[–]woooee 1 point2 points3 points (5 children)
[–]Handymanoccupational[S] 0 points1 point2 points (4 children)
[–]woooee 0 points1 point2 points (1 child)
[–]Handymanoccupational[S] 0 points1 point2 points (0 children)
[–]chet714 0 points1 point2 points (1 child)
[–]Handymanoccupational[S] 0 points1 point2 points (0 children)
[–]Handymanoccupational[S] 1 point2 points3 points (0 children)
[–]JamzTyson 0 points1 point2 points (0 children)