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 Game Planning

Jeff Hartl
seal-mask
.a{fill-rule:evenodd;}techdegree
Jeff Hartl
Python Web Development Techdegree Student 8,420 Points

Why define Game as a class?

In game.py, Game is defined as a class. Why did Kenneth make Game into a class? Is this standard practice in Python OOP? I'm not challenging it, I'm just wondering.

That is, why not just write game.py as a plain old python script, with something like a play_game() function that does pretty much the same as Game.__init__(self) -- it would call, in the proper order, the other functions that are defined in game.py?

I tried rewriting game.py without defining a class within it (deleted the word "self" heaven knows how many times), and the game ran okay but for some difficulty I had in coding the conditions of the while loop that was originally within the __init__ method.

Here's what I wrote:

import blah blah blah

monsters = [Goblin(), Dragon(), Troll()]

# deleted the setup(self) method, put its code within play_game()

def other methods...
...

def play_game():
    player = Character()
    monster = get_next_monster(monsters) 

    while player.hit_points and (monster.hit_points > 0 or len(monsters) != 0):
        print('\n'+'='*20)
        print(player)
        monster_turn(monster, player)
        print('-'*20)
        player_turn(player, monster)
        cleanup(monster, player)
        print('\n'+'='*20)

    if player.hit_points > 0:
        print("You win!")
    else:
        print("You lose!")
    sys.exit()

I suppose with some more tweaking it could be made workable.

But my question is, in general: should a python file that imports several other class-defining files in order to run a bunch of statements and commands to do something (as game.py does) itself also be a class object?

Steven Parker
Steven Parker
231,271 Points

Great question. Other than just giving you more practice with the declarative programming paradigm, I'm not sure what the advantage of a class is at the uppermost level.

I'm going to tag Kenneth Love here and see if he might notice and respond directly.

1 Answer

Stone Preston
Stone Preston
42,016 Points

object oriented programming attempts to model real world and abstract objects, ideas, and functionality as reusable and easily extendable modules

I think in this case it makes sense to model the game as an object itself, rather than a script, because in doing so you give yourself the ability to easily reuse and extend the functionality of that game in the future. Maybe you wanted to make a different game with similar attributes and actions. You could simply inherit from the game class.

It can be difficult to see the advantages of object oriented programming when you first get started, but as you learn more you will see why these concepts and ideas are in place

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I don't have anything to add to this answer.

Jeff Hartl
seal-mask
.a{fill-rule:evenodd;}techdegree
Jeff Hartl
Python Web Development Techdegree Student 8,420 Points

Thanks for the explanation, Stone. I can sort of see that as one reason why, but like you guessed, I am just starting out (with OOP, not Python scripting). So the concept is a wee bit less fuzzy now.

Steven Parker
Steven Parker
231,271 Points

Remember to mark this as the "best answer", Jeff. Even if it wasn't the only answer, based on KL's comment, it's a clear winner. :+1: