Example 45 Progress

I thought it might be helpful for myself and others to go over some steps I took to modify my program from one that used a dictionary to pass/store data, and one that used a class to pass/store data.

The program is similar to the bear room program from examples 35 and 36. I wanted to create a player that had an inventory, and a way to keep track of how many times the player biffed up their selection. The idea being that certain rooms needed certain items, and if the player mistyped too many times he would be eaten by monsters.

I tried to do this with a class, but was unable to figure it out and repeatedly banging my head on a wall. Instead I decided to “cheat”. I understand how dictionaries and writing to text files work a hole lot better than I understand classes so I thought of a way to do that without using classes. Either would work, so being lazy, I chose to try it with dictionaries first. That resulted in code that had an inventory defined right after my import variable.

I would then call the dictionary and write to it whenever I wanted to override a key in the dictionary in other classes like so:

This allowed me to debug a lot of other code without getting hung up on how to implement my player class. I came back to the program the next day after reading some online resources about classes with a fresh idea and came up with a solution for using classes. You can see both my initial program and my second program here, game_b.py being my second attempt: GitHub - fordkj0/Game.py: First take at creating my own game with the LPTH ex.45

The solution might not be perfect or elegant, but it’s a step in the direction I wanted my program to take. There’s more I want to do with this program, but I thought this might be a good example for others on how to not get discouraged when you can’t figure something out. Try it a different way and come back to it later. If anyone wants to comment on my class solution, or point out a different method that would be great.

1 Like

Ok, that’s a good progression but I have a question:

Are you totally rewriting the game engine from scratch or basing it on the one I had?

I was basing it off the engine you created, and trying to make that engine do different things like let the player choose rooms and store items in inventory. Then I had to go on a work trip all last week and wasn’t able to work on it at all.

Awesome, that’s the best strategy. Until you can craft your own stuff, copy other people’s.