'Use a texfile for the save data but it just blank when rewrite [duplicate]
So the level and the players score is saved in a textfile but now in order to rewrite the data so that when it loads again it is saved where the player is i use this code
#Exit button
for event in pygame.event.get():
if event.type == pygame.QUIT:
Game_data = open('Gamedata.txt','w')
Game_data.write(level)
Game_data.write(str(Score))
Game_data.close
game_over = True
But now the texfile data just gets removed and stays empty
#Load in text file data
Game_data = open('Gamedata.txt','r')
level = Game_data.readline().split("\n")
Score = Game_data.readline().split("\n")
lives = Game_data.readline().split("\n")
this is the code to read the data
Solution 1:[1]
It looks like you have an issue with your string parsing, or perhaps not closing the file. I've created a minimal example that demonstrates loading and saving your game data to a file. Some minor event handling to change the values saved.
import pygame
def load_game_data():
"Load save data, minimal error checking"
try:
with open("game_data.txt", "r") as save_file:
level = save_file.readline().strip() # remove newline
score = int(save_file.readline())
lives = int(save_file.readline())
return level, score, lives
except FileNotFoundError: # no save file, return defaults
return "First", 0, 0
def save_game_data(level, score, lives):
"Save game data, no error checking"
with open("game_data.txt", "w") as save_file:
save_file.write(f"{level}\n")
save_file.write(f"{score}\n")
save_file.write(f"{lives}\n")
WIDTH = 640
HEIGHT = 480
FPS = 30
pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
level, score, lives = load_game_data()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
score += 1
elif event.type == pygame.MOUSEBUTTONUP:
lives += 1
# draw surface - fill background
window.fill(pygame.color.Color("grey"))
# update window title to show score
pygame.display.set_caption(f"Level: {level:10} Lives: {lives:5} Score: {score:5}")
# show surface
pygame.display.update()
# limit frames
clock.tick(FPS)
# frames += 1
pygame.quit()
save_game_data(level, score, lives)
I think format strings are clearer and more explicit. The only thing that tripped me up was stripping the newline when reading the level. The int()
function ignores trailing newlines/spaces.
Solution 2:[2]
Maybe because you are not closing the file? you are not using the close method close()
Also as a general better practice you should use:
with open('Gamedata.txt','r') as file:
file.write(level)
file.write(str(Score))
Solution 3:[3]
Turns out I missed the () by the .close and then i had to add the \n for the new line and it fixed the problem
Game_data = open('Gamedata.txt','w')
Game_data.write(f"{level}\n")
Game_data.write(str(f"{Score}\n"))
Game_data.write(str(f"{lives}\n"))
Game_data.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | import random |
Solution 2 | Alfonso |
Solution 3 | Uncle_batty |