Commit a811868c by BellCodeEditor

auto save

parent 478e478b
Showing with 27 additions and 4 deletions
import pygame
from pygame import locals
import random
#导入json模块
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类
......@@ -42,6 +43,8 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
#加载游戏音效score_audio
score_audio=pygame.mixer.Sound("score.wav")
basic_font = pygame.font.Font('STKAITI.TTF',32)
index = 0
y = 400
......@@ -52,9 +55,18 @@ bg_x = 0
time = 0
gamestate = True
#定义一个变量表示分数score
score=0
#定义一个旧分数
old_score=score
block_list =pygame.sprite.Group() # 创建精灵组
with open("record.txt","r",encoding="utf-8") as file:#读取文件的分数with open() as 变量名file
content=file.read()#读取文件内容,使用变量存储下来content
content_1=json.load(content)#把json字符串转换为字典
one=content_1["第1名"]
two=content_1["第2名"]
three=content_1["第3名"]
#分别读取第一名、第二名、第三名的分数one、two、three
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
......@@ -64,7 +76,8 @@ while True:
if jumpState == "runing":
if event.key == locals.K_SPACE:
jumpState = "up"
#分数每增加3分速度speed增加1
speed=8+score//3
# 悟空造型
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
......@@ -122,10 +135,19 @@ while True:
if (sprite.rect.x+sprite.rect.width) < wukong.rect.x:#判断障碍物是否移到悟空后面
score+=sprite.score#分数增加1
sprite.score=0#把精灵的分值设为0
if score>old_score: #判断分数是否增加
score_audio.play() #如果增加了则播放游戏音效
old_score=score #重新设置旧的分数值
scoreSurf = basic_font.render("分数:"+str(score),True,(255,0,0))
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()
FPS.tick(60)
\ 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