Python Forum

Full Version: How do I make a game save?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I am making a game with python and the content is expanding really fast. It's beginning to reach the point that it can't be played in one sitting. How do I make a game save?
The data is stored in variables. For example, I have a variable named AmountOfWood.
How do I make these save?
Several options exist, you could use a "shelve" like in this blog post.
TinyDB is also an easy option for this.
Example.
from tinydb import TinyDB, Query

db = TinyDB('score.json')
# Insert score
score = dict(AmountOfWood=33)
db.insert(score)
# Overwrite score
#db.update(score)
# Clean all
#db.truncate()
Test it out.
>>> db.all()
[{'AmountOfWood': 33}, {'AmountOfWood': 55}, {'AmountOfWood': 99}]
>>> # Find max score
>>> max(item['AmountOfWood'] for item in db.all())
99
If only want last score to be saved use db.update
(Oct-04-2022, 11:53 AM)Gribouillis Wrote: [ -> ]... you could use a "shelve" like in this blog post.

Thank you for the link. I had been reading about how to create a text based adventure game, which Al Sweigart also has on his site, which is one of the best guides I've seen. The system that he's developed is very good and makes the process very easy to maintain: easy to test, easy to add rooms and objects; just an excellent system.

What I now have is not verbatim, but it's 90% based on what I found at The Invent with Python Blog -- highly recommended.
(Oct-04-2022, 01:14 PM)snippsat Wrote: [ -> ]TinyDB is also an easy option for this.
Example.
from tinydb import TinyDB, Query

db = TinyDB('score.json')
# Insert score
score = dict(AmountOfWood=33)
db.insert(score)
# Overwrite score
#db.update(score)
# Clean all
#db.truncate()
Test it out.
>>> db.all()
[{'AmountOfWood': 33}, {'AmountOfWood': 55}, {'AmountOfWood': 99}]
>>> # Find max score
>>> max(item['AmountOfWood'] for item in db.all())
99
If only want last score to be saved use db.update
I tried to use that, it just says
Traceback (most recent call last):
File "C:\Users\Wannes\survival spel 1.5 klad.py", line 2, in <module>
from tinydb import TinyDB, Query
ModuleNotFoundError: No module named 'tinydb'
Which states there is no tinydb. How do I fix this?
(Oct-06-2022, 08:29 AM)BliepMonster Wrote: [ -> ]ModuleNotFoundError: No module named 'tinydb'
Which states there is no tinydb. How do I fix this?

The first question has to be: did you pip install tinydb?
(Oct-05-2022, 09:43 AM)rob101 Wrote: [ -> ]90% based on what I found at The Invent with Python Blog
I like the blog post about how to use tomllib the TOML parser that is about to enter the standard library. Can be very nice to write configuration files for a game too.