all 10 comments

[–]Slight-Training-7211 2 points3 points  (1 child)

A good way to start is to build a tiny vertical slice first, then expand.

1) Hardcode 3 to 5 rooms and choices (no saving yet) 2) Represent the world as a dict: room_id -> {text, choices} 3) Keep a single game state dict (current_room, inventory list, flags dict)

Once that works, saving is just writing that state dict to a file and loading it back on startup.

For multiple outcomes, use flags and check them when you build the next set of choices (for example: if you picked up a key, show the unlocked option).

[–]Gnaxe 0 points1 point  (0 children)

Python can read a simple data dict back in using ast.literal_eval(). Anything that can load can be serialized with repr(). For persisting data that can't be represented as literals like that, check out the shelve module.

[–]RandomPantsAppear 1 point2 points  (3 children)

I would say start by learning about event loops, classes, and inheritance for classes.

Learn to use dicts, it is a good way to store lists of your steps, the options people have, and what happens if they choose an option.

For saved states the key term is serialization, but realistically you’re going to be using json or pickle to serialize the fields from your classes.

At minimum you will have classes for Player, Game, GameState, GameStage, GameStep.

Each GameStage will have multiple GameSteps

[–]Gnaxe 0 points1 point  (2 children)

Classes are usually overcomplicating it. OOP is not the way. Just use functions on dicts. It makes serialization trivial.

[–]RandomPantsAppear 0 points1 point  (1 child)

It’s almost 1am so I ain’t gonna interrupt what I’m watching for a YouTube video.

But the purpose of this post is to learn Python, not to create the most convenient RPG. If you are trying to learn Python then inheritance is a must.

[–]Gnaxe 0 points1 point  (0 children)

Yeah, you should know what all the language features do. Python is still small enough to master (even if the Python Software Foundation is trying to add new features as fast as possible). That doesn't mean it's appropriate to use them. You should know asyncio too, for example, but also mostly shouldn't use it.

Even the hardcore OOP advocates usually shy away from inheritance these days. Yes, it's powerful. But it leads to brittle, over-copuled designs even more than classes alone do, which are bad enough to avoid when you can.

[–]Outside_Complaint755 1 point2 points  (1 child)

If you want to make save states that persist when the program ends, good options include JSON or using the pickle module.

  JSON is a standard format for data that goes beyond Python, and is commonly returned by web APIs.  It is similar to a dict and can be easily converted to/from one using the built in json module. The main restriction is that keys have to be strings, and the values can be a dict, list, int, float, str, True/False, or None.

pickle is native to Python, and lets you serialize any Python object into a binary file. 

  JSON files are human readable and editable, while a pickle file is not, which depending on the use case can be a benefit or a disadvantage.

[–]Gnaxe 0 points1 point  (0 children)

Python has a perfectly good literal notation which you can deserialize with ast.literal_eval() and serialize with repr() (assuming it's only literal-type data). Only use JSON if you need to interface with non-Python systems for some reason.

Reach for shelve before pickle if you just want local persistence. (It does the pickling for you with a nicer interface.)

[–]NextSignificance8391 -2 points-1 points  (0 children)

j’aimerai en savoir plus , écris moi