Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make a game save?
#1
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?
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#2
Several options exist, you could use a "shelve" like in this blog post.
rob101 likes this post
Reply
#3
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
Reply
#4
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
(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?
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#6
(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?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  RimWorld Save Game Manager- Help! LastStopDEVS 2 1,269 Jul-29-2024, 03:36 AM
Last Post: LastStopDEVS
  how to save to multiple locations during save cubangt 1 1,272 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  how to make a hotkey for text adventure game myn2018 2 2,615 Jan-06-2021, 10:39 PM
Last Post: myn2018
  so im trying to make a question game for school and im trying to make it when some on dertder25t 1 2,094 Jan-27-2020, 10:44 PM
Last Post: woooee
  I am trying to make a Colour Wars game, "Winning code" missing Ondrejoda 0 2,718 Feb-18-2018, 11:06 AM
Last Post: Ondrejoda

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020