fork download
  1. import random
  2.  
  3. class Player:
  4. def __init__(self):
  5. self.health = 100
  6. self.ammo = 10
  7.  
  8. def is_alive(self):
  9. return self.health > 0
  10.  
  11. class Zombie:
  12. def __init__(self):
  13. self.health = 50
  14.  
  15. def is_alive(self):
  16. return self.health > 0
  17.  
  18. def game_loop():
  19. player = Player()
  20. zombies = [Zombie() for _ in range(5)]
  21.  
  22. while player.is_alive():
  23. print "\nPlayer Health: " + str(player.health)
  24. print "Ammo: " + str(player.ammo)
  25. print "Zombies:"
  26. for i, zombie in enumerate(zombies):
  27. print str(i+1) + ". Health: " + str(zombie.health)
  28.  
  29. action = raw_input("\nWhat do you want to do? (shoot, run, quit): ")
  30.  
  31. if action.lower() == "shoot":
  32. zombie_index = int(raw_input("Which zombie do you want to shoot? (1-5): ")) - 1
  33. if zombies[zombie_index].is_alive():
  34. zombies[zombie_index].health -= 20
  35. player.ammo -= 1
  36. print "You shot zombie " + str(zombie_index+1) + "!"
  37. else:
  38. print "That zombie is already dead!"
  39.  
  40. elif action.lower() == "run":
  41. if random.random() < 0.5:
  42. print "You successfully ran away!"
  43. zombies = [Zombie() for _ in range(5)]
  44. else:
  45. print "You failed to run away!"
  46. player.health -= 10
  47.  
  48. elif action.lower() == "quit":
  49. print "Game over!"
  50. break
  51.  
  52. else:
  53. print "Invalid action!"
  54.  
  55. for zombie in zombies:
  56. if zombie.is_alive():
  57. player.health -= 5
  58. print "A zombie attacked you!"
  59.  
  60. if not player.is_alive():
  61. print "Game over! You died!"
  62. break
  63.  
  64. game_loop()
Success #stdin #stdout 0.02s 9788KB
stdin
Forward
Backward
Left
Right
stdout
Player Health: 100
Ammo: 10
Zombies:
1. Health: 50
2. Health: 50
3. Health: 50
4. Health: 50
5. Health: 50

What do you want to do? (shoot, run, quit): Invalid action!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!

Player Health: 75
Ammo: 10
Zombies:
1. Health: 50
2. Health: 50
3. Health: 50
4. Health: 50
5. Health: 50

What do you want to do? (shoot, run, quit): Invalid action!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!

Player Health: 50
Ammo: 10
Zombies:
1. Health: 50
2. Health: 50
3. Health: 50
4. Health: 50
5. Health: 50

What do you want to do? (shoot, run, quit): Invalid action!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!

Player Health: 25
Ammo: 10
Zombies:
1. Health: 50
2. Health: 50
3. Health: 50
4. Health: 50
5. Health: 50

What do you want to do? (shoot, run, quit): Invalid action!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
A zombie attacked you!
Game over! You died!