这里我的代码有问题,我想确保将有关变量的信息写入文本文件中的文件,并在下次将其写入变量时。这是我的代码:
import pygame
pygame.init()
bg = pygame.image.load("images/kira.png")
window = pygame.display.set_mode((1300, 500))
screen = pygame.Surface((1300, 600))
start_game = True
print("Log")
score = 0
coins = 0
f = open('whu.txt','r')
coins = f.read()
f.close()
while start_game == True:
for i in pygame.event.get():
if i.type == pygame.QUIT:
start_game = False
if i.type == pygame.KEYDOWN and i.key == pygame.K_w:
coins += 1
print(coins)
if i.type == pygame.KEYDOWN and i.key == pygame.K_d and coins == 1000:
coins -= 1000
score += 1
print("Молодец! Ты заработал 1 робукс! Теперь у тебя: " + str(score) + " робуксов! Кидай скрин Владу и пиши сколько хочешь вывести!")
window.blit(screen, (1,1))
screen.blit(bg, (1,0))
pygame.display.update()
while True:
with open ('whu.txt', 'r') as f:
old_data = f.read()
new_data = old_data.replace('0', str(coins))
with open ('whu.txt', 'w') as f:
f.write(new_data)
break
pygame.quit()