Commit 39682630 by BellCodeEditor

auto save

parent 700e420b
Showing with 46 additions and 6 deletions
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
......@@ -54,6 +55,14 @@ gamestate = True
block_list =pygame.sprite.Group() # 创建精灵组
my_font=pygame.font.Font("STKAITI.TTF",30)
score_audio=pygame.mixer.Sound("score.wav")
old_score=0#旧分数
with open("record.txt",mode="r",encoding="utf-8") as file:
content=file.read()
dict=json.loads(content)#转换为字典
one=dict["第1名"]
two=dict["第2名"]
three=dict["第3名"]
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
......@@ -118,15 +127,36 @@ while True:
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200))
gamestate = False
if score> three:
if score>one:
dict["第1名"]=score
dict["第2名"]=one
dict["第3名"]=two
elif score>two:
dict["第2名"] =score
dict["第3名"]=two
else:
dict["第3名"]=score
content=json.dumps(dict,ensure_ascii=False)
with open("record.txt","w",encoding="utf-8") as file:
file.write(content)
else:
if sprite.rect.x + sprite.rect.width <wukong.rect.x:
score += sprite.score
sprite.score=0
text=my_font.render("分数:"+str(score),True,(0,0,0))
screen.blit(text,(900,20))
text1=my_font.render("第1名:"+str(one),True,(0,0,0))
text2=my_font.render("第2名:"+str(two),True,(0,0,0))
text3=my_font.render("第3名:"+str(three),True,(0,0,0))
screen.blit(text,(870,20))
screen.blit(text1,(870,80))
screen.blit(text2,(870,140))
screen.blit(text3,(870,200))
if score>old_score:
score_audio.play()
old_score=score
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
{"第1名": 9, "第2名": 8, "第3名": 7}
\ No newline at end of file
import json
jsonData='{"第一名":122,"第二名":109,"第三名":88}'
text=json.loads(jsonData)
print(text["第一名"])
print(text["第二名"])
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment