Commit a811868c by BellCodeEditor

auto save

parent 478e478b
Showing with 27 additions and 4 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
#导入json模块
import json
pygame.init() # 初始化 pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类 class Block(pygame.sprite.Sprite): # 障碍物精灵类
...@@ -42,6 +43,8 @@ hero = [pygame.image.load('hero1.png'), ...@@ -42,6 +43,8 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'), pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'), pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')] pygame.image.load('hero5.png')]
#加载游戏音效score_audio
score_audio=pygame.mixer.Sound("score.wav")
basic_font = pygame.font.Font('STKAITI.TTF',32) basic_font = pygame.font.Font('STKAITI.TTF',32)
index = 0 index = 0
y = 400 y = 400
...@@ -52,9 +55,18 @@ bg_x = 0 ...@@ -52,9 +55,18 @@ bg_x = 0
time = 0 time = 0
gamestate = True gamestate = True
#定义一个变量表示分数score #定义一个变量表示分数score
score=0
#定义一个旧分数
old_score=score
block_list =pygame.sprite.Group() # 创建精灵组 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: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
...@@ -64,7 +76,8 @@ while True: ...@@ -64,7 +76,8 @@ while True:
if jumpState == "runing": if jumpState == "runing":
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpState = "up" jumpState = "up"
#分数每增加3分速度speed增加1
speed=8+score//3
# 悟空造型 # 悟空造型
wukong = Player(hero[index]) wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
...@@ -122,10 +135,19 @@ while True: ...@@ -122,10 +135,19 @@ while True:
if (sprite.rect.x+sprite.rect.width) < wukong.rect.x:#判断障碍物是否移到悟空后面 if (sprite.rect.x+sprite.rect.width) < wukong.rect.x:#判断障碍物是否移到悟空后面
score+=sprite.score#分数增加1 score+=sprite.score#分数增加1
sprite.score=0#把精灵的分值设为0 sprite.score=0#把精灵的分值设为0
if score>old_score: #判断分数是否增加
score_audio.play() #如果增加了则播放游戏音效
old_score=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()
FPS.tick(60) 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