Commit 58f57977 by BellCodeEditor

auto save

parent 31ba4600
Showing with 76 additions and 7 deletions
import pygame
from pygame import locals
import random
#注意导入json模块
import json
pygame.init() # 初始化
......@@ -14,6 +16,7 @@ class Block(pygame.sprite.Sprite): # 障碍物精灵类
# 障碍物绘制坐标
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite): # 悟空
def __init__(self, image):
......@@ -40,6 +43,8 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
my_font = pygame.font.Font("STKAITI.TTF",18) #加载文字对象
score_sound = pygame.mixer.Sound("score.wav") #加载分数音效
index = 0
y = 400
jumpState = "runing"
......@@ -51,6 +56,23 @@ gamestate = True
block_list =pygame.sprite.Group() # 创建精灵组
#设置游戏分数
score = 0
#设置分数音效
old_score = 0
#当前障碍物移动速度
speed = 8
#读取txt文件
with open("record.txt","r",encoding="utf-8") as file:
content = file.read()
#json.loads()将json数据转化为python中的字典
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:
......@@ -60,7 +82,8 @@ while True:
if jumpState == "runing":
if event.key == locals.K_SPACE:
jumpState = "up"
#实现分数+3,速度+1
speed = 8+score//3
# 悟空造型
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
......@@ -73,14 +96,14 @@ while True:
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
t -= 1
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
t += 1
else:
jumpState = "runing"
t =30
......@@ -91,7 +114,7 @@ while True:
bg_x = 0
screen.blit(background, (bg_x, 0)) # 远景
road_x -= 8
road_x -=speed #用变量speed运算
if road_x<=-1000:
road_x = 0
screen.blit(road, (road_x, 500)) # 道路
......@@ -105,8 +128,9 @@ while True:
if num > 20:
obstacle = Block(bush,cacti,stone)
block_list.add(obstacle)
for sprite in block_list: # 遍历、展示障碍物精灵
sprite.rect.x -= 8
sprite.rect.x -= speed #用变量speed运算
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width:
sprite.kill()
......@@ -114,6 +138,51 @@ while True:
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200))
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
#将python字典转化为json数据,注意第二个参数时不转换为ASCII码
recode = json.dumps(recode,ensure_ascii=False)
with open("record.txt","w",encoding="utf-8") as file:
file.write(recode)
#如果没有碰到障碍物:也就是跨过障碍物了
else:
if sprite.rect.x + sprite.rect.width < wukong.rect.x:
score+=sprite.score
sprite.score = 0
#新的分数比旧的分数高,就说明分数增加了,就播放音效
if score>old_score:
score_sound.play()
old_score = score
#将分数渲染在右上角
text = my_font.render("分数:"+str(score),True,(0,0,0))
screen.blit(text,(900,20))
#将前三名渲染到分数的下面
text = my_font.render("第1名:"+str(one),True,(0,0,0))
screen.blit(text,(900,50))
text = my_font.render("第2名:"+str(two),True,(0,0,0))
screen.blit(text,(900,80))
text = my_font.render("第3名:"+str(three),True,(0,0,0))
screen.blit(text,(900,110))
# 刷新画面
pygame.display.update()
......
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
{"第1名": 23, "第2名": 3, "第3名": 3}
\ 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