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

Why is the game set up as class Game: ? Why isn't game just a function that is called?

Is this just so that you can make another instance of the class Game? I guess you'd also have to explicitly run the setup() code at the top of the game function then. If game was a function then you can't just use init to automatically run setup. Do I understand these things right? Thanks everyone for clarification.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

There are a few projects that use "class Game", which video are your referring to?

Ah! Chris. Thanks for responding. I wasn't sure if my question automatically went under the hack n slash game by Kenneth Love. This was my first forum question. Next time I'll say what challenge the question relates to.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Your understanding seems correct. The Hack 'n' Slash section is in the Object Oriented Programming introduction so the thrust of all these code example will be how to solve this using classes. The game could clearly have been designed as a series of functions. As a teaching point, the development of Game as a class shows the built-in constructor or start-up __init__() usage. It also shows how methods are bound to an instance of the class and have the advantage of knowing which instance the method is operating on. The state of the game is held in class attributes which saves having to pass state between functions or keep the state in global variables. In the newly revised Python Basics, a letter guessing game an a number guessing game are developed using functions. It's an interesting contrast to see how developing a game using classes provides a very different approach.