Commit 3db53a74 by BellCodeEditor

auto save

parent 3bb2e58c
Showing with 42 additions and 3 deletions
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
......@@ -51,6 +52,16 @@ gamestate = True
block_list =pygame.sprite.Group() # 创建精灵组
font = pygame.font.Font('STKAITI.TTF',20)
score = 0
with open('score.txt','r',encoding='utf-8')as file:
content = file.read()
recode = json.loads(content)
one = recode['第1名']
two = recode['第2名']
three = recode['第3名']
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
......@@ -109,12 +120,39 @@ while True:
sprite.rect.x -= 8
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width:
score += 1
sprite.kill()
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200))
gamestate = False
gamestate = False
if score > three:
if score > one:
recode['第1名'] = score
recode['第2名'] = one
recode['第3名'] = two
elif score > two:
recode['第2名'] = score
recode['第3名'] = two
else:
recode['第3名'] = score
recode = json.dumps(recode,ensure_ascii=False)
with open('score.txt','w',encoding='utf-8')as file:
file.write(recode)
text = font.render('分数:'+str(score),True,(0,0,0))
screen.blit(text,(900,20))
text1 = font.render('第1名:'+str(one),True,(0,0,0))
screen.blit(text1,(900,50))
text2 = font.render('第2名:'+str(two),True,(0,0,0))
screen.blit(text2,(900,80))
text3 = font.render('第3名:'+str(three),True,(0,0,0))
screen.blit(text3,(900,110))
# 刷新画面
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名": 4, "第2名": 4, "第3名": 1}
\ 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