Using classes and random together in a game? Worth it?

Hi, I have been stumped and extremely stressed about this for several weeks! I’m new to programming. I was able to create a small game (linked here) that would allow the user to randomly get a story genre (Mystery or Horror). Then, based on the genre selected, a character, setting and plot (from the chosen genre) would be randomly selected for them.

I was able to create this game quite easily using functions but a friend told me it would be so much better if I used classes and objects. I am able to put everything into classes (makes sense) but my confusion is how could I implement the random selection part? I still want users to get randomly selected characters, settings and plots.

Trying to convert this program into one that uses OOP has been a rough experience - I can’t easily see a way to do it and I question if it’s worth it?! Help! Here’s the revised one trying to use classes.

One of the benefits would be to get rid of all the global variables as you could hold those variables in the instance of a player class.

But look at it this way, OOP is merely a coding approach to try to represent the world in parallel to how humans perceive it, that is, through seeing objects and their interactions and behaviours.

We look at a game player (class) and recognise they might have actions (like roll for choice) and attributes (like the chosen genre). We can relate to this as a layer of abstraction above the code.

This can help in writing the game in pseudocode as we create a structure before any functioning code is written.

It can also be helpful in avoiding repetition which is very important for maintaining your software. The code similarity between hor_roll() and mys_roll() could be refactored into a more generic roll() function. What would happen if you decided to add a comedy and romantic genres in your game?

One key observation I would make about the code examples provided is that you are using functions in a linear manner to control flow. Each function is a block of code that you stack on top of each other in sequence. But these functions could be simplified to do a specific task and return something specific and control the flow elsewhere.

Take count() for example, really this is a code block for text. If I was also maintaining this programme with you, I’d expect a function called count() to do some mathematics and return a result:

def count():
    # do something like player_health / player_injury
    return player_health

OK maybe this is just being a little critical of your function titles, and you could remove all of the program flow into a main() and just call the functions given choices that are made.

I’m looking for a distraction from my current project/learning so I’ll see if I can refactor today as it’s a good practice example for me too.

1 Like

Wow I don’t even know how to begin to thank you for your time! I’m really struggling and am trying to learn as much as possible before school starts again in September (I’m a teacher). You gave me a lot to think about and I had not considered a player class. I have SO much to learn but I don’t want to quit!

Thank for your feedback!

An apology from me too. I didn’t actually execute the working script until now so wasn’t 100% clear on what it did. I assumed falsely that it had a lot more user interaction to the selection process.

Anyway I will have a play as it seems a fun script. And definitely don’t quit. Programming can be fun as well as a total PITA!!

OK @bcrivera have a look at this file: tools/ at master · PlutoniumProphet/tools · GitHub

I created a Story class as that is the object you are actually creating, not a player. The story has its own methods for generating the genre, character, plot and setting.

I’ve reused your calculating function as it’s fun. But it sits outside the Story class along with the roll_a_story function (basically the program flow) and a roll_again function to shortcut the process for subsequent tries.

I think this is in a similar direction to what you were aiming for?


This revision is still pretty poor. I’ve started ‘modelling’ the idea of a story and things a story may need. But you’ll notice the class functions still have a lot of data in lists. (By the way I opted for lists as a dictionary wasn’t needed with random.choice() builtin method use.) You could easily have lots more genres or characters, and certainly many, many more plots and settings.

If you remove the unnecessary print statements I added in the main roll_a_story function for fun, you’ll see the script is quite short now, and more maintainable. But there is lots more you could do to simplify it.

Why not look into how you could separate the data from the class methods? Perhaps in one lists of lists? Or if you prefer, a list of dictionaries? Perhaps in another file completely? Could you return all story elements at once in one method? :wink:

Or bin it completely. But the idea was so show how you can create multiple objects (story class) for each story creation and reuse the function calls. And more importantly, that you are modelling an object and its behaviours to use as a blueprint in instances, not writing a script to just procedurally execute from top to bottom.

If you’re tool got much bigger, you might just have the roll_a_story function in the main file. Your Story class might be in its own module with many more methods, like change character, or something… food for thought.

Hope that helps!

Edit: it’s also pointless in initialising the story instance with name. The use of the name variable is managed outside of the class so it’s never used in the class methods. I forgot to remove it! :yum:

Omg you are an absolute angel! I’m going to scrutinize your work so I can learn as much as possible from it! I only work with kids grades 5-8 and I don’t think OOP will come up but I still want to be comfortable enough to answer questions in case it does! Thank you sooo much for your time! How did you learn so much? Just from this course alone or using additional materials (or are you a CS student?)?

You are very welcome. I hope it helps. There is nothing in that code that isn’t covered in Learn Python The Hard Way course. In fact, there lots more in the course.

I mostly learned from this site and a little group we had working with Zed when this was being created a few years back. Since then, just writing code, reading code and co to using to learn new stuff as I can.

It’s kind of why I respond on here a fair bit. I like to give something back if I can.

I spent hours today going through the code and then ripping it apart so I could redo it myself. I made tweaks and “made it my own”. I can’t thank you enough and I learned so much just by reviewing your interpretation of things. It’s so frustrating that I couldn’t think of all that myself. Seeing your work, it pretty much made perfect sense.

I hope I can get to that point one day! Have a great day!

1 Like

Awesome. Thanks @gpkesley for helping @bcrivera out. You are a gem of a friend on this forum.

1 Like