For Learn Python The Hard Way exercise 45 I made a small text game, that took a few hours. Since classes were used and classes were a recently brought up concept in the book, I was wondering how this piece of code could be made better. It should be able to work in terminal as far as I know.
from sys import exit from random import randint from Scene import * class Main(object): def __init__(self): self.stage = DoorScene() self.stage_2 = FightScene() self.stage_3 = EndingScene() def play(self): boss_door = self.stage.enter_boss_room() if boss_door == True: defeated_boss = self.stage_2.the_fight() if defeated_boss == True: self.stage_3.ending() else: exit(0) game_try = Main() game_try.play()
from random import randint class DoorScene(object): def enter_boss_room(self): print "You walk and come across a door. It requires you to guess the" print "right number to enter the room to fight the final boss. You only" print "have 5 guesses so make good use of your guesses." code = randint(1, 10) choice = raw_input("> ") attempts = 0 while int(choice) != code: attempts += 1 if attempts == 5: break else: pass print "Wrong number, try again." choice = raw_input("> ") if int(choice) == code: print "That's the correct number! You pass through the door and see" print "a huge beast laying in the room. Prepare yourself to fight the beast!\n" return True else: print "You failed more than 5 times. Game Over." return False class FightScene(object): def the_fight(self): print "Your character will now fight the boss. If your health drops" print "below 100, you can use a health potion to restore your" print "health by 50. Note that you only have 3 health potions.\n" current_boss_health = 200 current_hero_health = 150 health_potion = 3 while True: print "It is your turn to perform an action. Would you like" print "to attack the boss, use a health potion or run away?" choice = raw_input("> ") if choice == 'attack': damage_hero = randint(25, 35) current_boss_health -= damage_hero print "You hit the boss for %d damage!" % (damage_hero) if current_boss_health <= 0: print "The boss has 0/200 health left!\n" print "Well done, you defeated the boss!\n" break print "The boss has %d/200 health left!\n" % (current_boss_health) elif choice == 'use health potion': if current_hero_health <= 100 and health_potion > 0: print "You restored your health by 50, you now have %d/150 health" % (current_hero_health + 50) current_hero_health = current_hero_health + 50 health_potion = health_potion - 1 print "You have %d health potion(s) left.\n" % (health_potion) continue else: print "Sorry your health is not below 100 or you don't have any" print "health potions left anymore!\n" continue elif choice == 'run away': print "Coward!" exit(0) else: continue damage_boss = randint(30, 50) current_hero_health -= damage_boss print "The boss hits you for %d damage!" % (damage_boss) if current_hero_health <= 0: print "You have 0/150 health left!\n" print "You got defeated...." exit(0) print "You have %d/150 health left!\n" % (current_hero_health) return True class EndingScene(object): def ending(self): print "You manage to escape through an opening you find in the boss' lair" print "You return home, lay in your bed and deeply fall asleep...." exit(0)