Commit 550fef4a by BellCodeEditor

save project

parent 4cd18c00
import json
j='{"第一名":122,"第二名":119,"第三名":100}'
text=json.loads(j)
print("第一名",text["第一名"])
print("第二名",text["第二名"])
print("第三名",text["第三名"])
\ No newline at end of file
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
import json
score=0 score=0
pygame.init() # 初始化 pygame.init() # 初始化
speed=8 speed=20
class Block(pygame.sprite.Sprite): # 障碍物精灵类 class Block(pygame.sprite.Sprite): # 障碍物精灵类
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__() super().__init__()
...@@ -53,7 +53,14 @@ road_x = 0 ...@@ -53,7 +53,14 @@ road_x = 0
bg_x = 0 bg_x = 0
time = 0 time = 0
gamestate = True gamestate = True
block_list =pygame.sprite.Group() # 创建精灵组 block_list =pygame.sprite.Group()
# 创建精灵组
with open("record.txt","r",encoding="utf-8") as f:
content=f.read()
recond=json.loads(content)
one=recond["第1名"]
two=recond["第2名"]
three=recond["第3名"]
p=0 p=0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -90,7 +97,7 @@ while True: ...@@ -90,7 +97,7 @@ while True:
t =30 t =30
# 将背景图画上去 # 将背景图画上去
bg_x -= 1 bg_x -= 10
if bg_x<=-1000: if bg_x<=-1000:
bg_x = 0 bg_x = 0
screen.blit(background, (bg_x, 0)) # 远景 screen.blit(background, (bg_x, 0)) # 远景
...@@ -118,16 +125,38 @@ while True: ...@@ -118,16 +125,38 @@ while True:
gameover = pygame.image.load('gameover.png') # 游戏结束 gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200)) screen.blit(gameover, (400, 200))
gamestate = False gamestate = False
if score > one:
recond["第1名"]=score
recond["第2名"]=one
recond["第3名"]=two
elif score>two:
recond["第2名"]=score
recond["第3名"]=two
else:
recond["第3名"]=score
text=json.dumps(recond,ensure_ascii=False)
with open('score.txt','w',encoding='utf-8')as f:
f.write(text)
else: else:
if sprite.rect.x+sprite.rect.width<wukong.rect.x: if sprite.rect.x+sprite.rect.width<wukong.rect.x:
score+=sprite.score score+=sprite.score
sprite.score=0 sprite.score=0
speed=8+score//3 speed=15+score//3
if p<score: if p<score:
score_audio.play() score_audio.play()
p=score p=score
scoreSurf = basic_font.render("分数:"+str(score),True,(255,0,0)) scoreSurf = basic_font.render("分数:"+str(score),True,(255,0,0))
screen.blit(scoreSurf,(850,20)) screen.blit(scoreSurf,(850,20))
scoreSurf = basic_font.render("第1名:"+str(one),True,(255,0,0))
screen.blit(scoreSurf,(850,50))
scoreSurf = basic_font.render("第2名:"+str(two),True,(255,0,0))
screen.blit(scoreSurf,(850,80))
scoreSurf = basic_font.render("第3名:"+str(three),True,(255,0,0))
screen.blit(scoreSurf,(850,110))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
......
{"第1名": 8, "第2名": 0, "第3名": 0}
\ 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