Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method for Saving / Loading Game Levels in Python

I'm writing a game using Python and PyGame. (This is perscribed in the assignment, so it's no use suggesting another game dev. library that has built-in level parsing)

I'm at the stage now where the game physics etc. are complete, but I am yet to work out a method for saving and loading levels into the game. Here's what I've thought about it so far:

1. Method Brainstorm

  • Have been suggested YAML (Therefore, PyYAML) for handling level input / output.
    • This stores as plain text, but in a structured mannar.
    • Easy to edit etc. - one of YAML's major features is that it is easy to be humanly readable.
  • Using pickle
    • Which I have used in the past - save a list as individual items with a splitter item to differentiate them.
    • Again, saves as plain text, but using a splitter item (for example, {><} as with the Learner Driver Logbook makes it harder to understand.

2. Data Representation

  • Each level needs the follow data to be parsed. The data types are in bold next to it.
    • The starting positions and characteristics of balls and circles [Two lists]
    • The limit on circles [Integer]
    • Permitted colours [Definition List Colour:True / Colour:False]
    • The Obsessive Completion Distinction criteria [Integer]
    • Any storyline or tutorial text that must appear in level [List of strings]
    • Level name and number [List of strings]

All I'm looking for are suggestions of the best method for achieving this.

TL;DR - Best way to parse game levels from file into Python / PyGame.

like image 301
nchpmn Avatar asked Apr 25 '26 17:04

nchpmn


2 Answers

If you don't need to edit by hand, or read the levels in any other program, just use pickle.

Store your level data in a single Python object, and it's (nearly) a one-liner in your code to read and write.

like image 163
payne Avatar answered Apr 27 '26 07:04

payne


Pickle is made for this sort of thing. You don't need any delimiter to differentiate the items in a list; just pickle the whole list.

like image 24
nmichaels Avatar answered Apr 27 '26 07:04

nmichaels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!