Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Hack-n-Slash The Final Push

Can someone post the full game.py code?

I've finished all the videos and followed the code precisely (except instead of monster.py I used creatures.py and I changed all the enemies to be based off the game BioShock, otherwise the code is exactly the same) and I can't seem to get it to work. It keeps printing syntax errors involving the if, else, and elif statements. Here's my code (game.py):

import sys

from character import Character
from creatures import Creature
from creatures import BigDaddy
from creatures import Splicer
from creatures import Turrent

class Game:
  def setup(self):
    self.player = Character()
    self.creatures = [BigDaddy(), Splicer(), Turret()]

  def creature_turn(self):
    if slef.monster.attack():
      print("{} is attacking!".format(self.monster))

      if input("Dodge? Y/N").lower() == 'y':
        if self.player.dodge():
          print("You dodged the attack!")
        else:
          print("You got hit anyway, you clumsy clut!")
          swelf.player.hitpoints -= 1
      else:
        print("{} isn't attacking this turn.".format(self.monster))

    #Check if creature attacks
    #If so, tell player
      #Check if player wants to dodge
      #If so, check if dodge is successful
        #If it is, move on
      #If it isn't, the player recieves a hit. A virtual hit, not a real one. Thaty would be mean.
    #If the creature doesn't attack, tell the player and move on.

  def player_turn(self):
    player_choice = input("[A]ttack, [R]est, [Q]uit? ").lower()
    if player_choice == 'a':
      print("You're attacking {}!".format(self.monster))

      if self.player.attack():
        if self.monster.dodge():
          print("{} dodged your attack!".format(self.monster()))
      else:
        if self.player.leveled_up():
          self.monster.hit_points -= 2
        else:
          self.monster.hit_points -= 1
          print("You hit {} with your {}!".format(self.monster, self.player.weapon))
          else:
            print("You missed!")

    elif (player_choice == 'r'):
      self.player.rest()
    elif (player_choice == 'q'):
      sys.exit()
    else:
      self.player_turn()

    #Let the player attack, rest, or quit
    #If they attack:
      #See if the attack is successful
        #If so, see if the creature dodges
          #If dodged, notify player
          #If not dodged, deal the right amount of damage to the creature
        #If not a good hit, tell the player
    #If they rest:
      #Call the player.rest() method
    #If they quit, exit the game
    #If they pick anything else, re-run this method and try to ignore their lack of ability to follow directions.

  def cleanup(self):
    if self.creature.hit_points <= 0:
      self.player.experience += self.monster.experience
      print("You killed {}!".format(self.monster))
      self.monster = self.get_next_monster()
    #If the monster has no more hit points left:
      #Give the player more experience
      #Print a message
      #Get a new monster, bring it on and on and on.
  def __init__(self):
    self.setup()

    while self.player.hitpoints:
      print("\n" + "="*20)
      print(self.player)
      self.monster_turn()
      print("-"*20)
      self.player_turn()
      self.cleanup()
      print("\n"+"="*20)

    if self.player.hit_points:
      print("You win! You defeated the monster!")
    elif self.monsters or self.monster:
      print("You lose! The monsters defeated you!")
    sys.exit()

Game()

I haven't done the OOP course yet, sorry! As soon as I'll do, I'll try to help :)

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Your

else:
    print("You missed!")

seems to be badly indented. I think the else needs to come back out one indentation level.

On the last video, there's a Download link that has all of the code if you can't get yours to work.

Natan Becker
Natan Becker
7,044 Points

Kenneth could you please post the entire code of character.py something is wrong with mine:

import random from combat import Combat

class Character(Combat):

attack_limit = 10
experience = 0
base_hit_points = 10

def attack(self):
    roll = random.randint(1 , self.attack_limit)
    if self.weapon == 'sword':
        roll += 1
    elif self.weapon == 'axe':
        roll += 2
    return roll > 4


def get_weapon(self):
    weapon_choice = input("Weapon 1([S]word, [A]xe, [B]ow): ").lower()

    if weapon_choice in 'sab':
        if weapon_choice == 's':
            return 'sword'

        elif weapon_choice == 'a':
            return 'axe'

        else:
            return 'bow'
    else:
        return self.get_weapon()

def __init__(self, **kwargs):

    self.name = input("Name: ")
    self.weapon = self.get_weapon()
    self.hit_points = self.base_hit_points


    for key, value in kwargs.items():
        setattr(self, key, value)

def __str__(self):
    return '{}. HP: {}, XP: {}'.format(self.name, self.hit_points,                                                        self.experience)

def rest(self):
    if self.hit_points < self.base_hit_points:
        self.hit_points += 1

def leveled_up(self):
    return self.experience >= 5
William Higgins
William Higgins
5,904 Points

These lines have a typo each:

if slef.monster.attack():
      swelf.player.hitpoints -= 1