Commit 736c6932 by BellCodeEditor

auto save

parent 3bb2e58c
Showing with 47 additions and 6 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
import json
pygame.init() # 初始化 pygame.init() # 初始化
...@@ -22,7 +23,7 @@ class Player(pygame.sprite.Sprite): # 悟空 ...@@ -22,7 +23,7 @@ class Player(pygame.sprite.Sprite): # 悟空
self.image = image self.image = image
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象 # image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.x = 150 self.rect.x = 70
self.rect.y = 400 self.rect.y = 400
# 创建一个窗口 # 创建一个窗口
...@@ -50,7 +51,16 @@ time = 0 ...@@ -50,7 +51,16 @@ time = 0
gamestate = True gamestate = True
block_list =pygame.sprite.Group() # 创建精灵组 block_list =pygame.sprite.Group() # 创建精灵组
score=0 #分数
music=pygame.mixer.Sound("score.wav")
with open('record.txt','r',encoding='utf-8')as file:
dwe= file.read()
record=json.loads(dwe)
one=record["第1名"]
two=record["第2名"]
three=record["第3名"]
print(one)
myfont=pygame.font.Font('STKAITI.TTF',25)
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:
...@@ -60,6 +70,7 @@ while True: ...@@ -60,6 +70,7 @@ while True:
if jumpState == "runing": if jumpState == "runing":
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpState = "up" jumpState = "up"
speed=8+score//3
# 悟空造型 # 悟空造型
wukong = Player(hero[index]) wukong = Player(hero[index])
...@@ -96,7 +107,7 @@ while True: ...@@ -96,7 +107,7 @@ while True:
road_x = 0 road_x = 0
screen.blit(road, (road_x, 500)) # 道路 screen.blit(road, (road_x, 500)) # 道路
screen.blit(wukong.image, (150, y)) # 悟空 screen.blit(wukong.image, (70, y)) # 悟空
time += 1 time += 1
if time >= 60: # 创建障碍物精灵 if time >= 60: # 创建障碍物精灵
...@@ -106,14 +117,39 @@ while True: ...@@ -106,14 +117,39 @@ while True:
obstacle = Block(bush,cacti,stone) obstacle = Block(bush,cacti,stone)
block_list.add(obstacle) block_list.add(obstacle)
for sprite in block_list: # 遍历、展示障碍物精灵 for sprite in block_list: # 遍历、展示障碍物精灵
sprite.rect.x -= 8 sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y)) screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width: if sprite.rect.x <= 0-sprite.rect.width:
sprite.kill() sprite.kill()
score+=1
music.play()
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测 if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
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 > three:
if score > one:
record["第1名"]=score
record["第2名"]=one
record["第3名"]=two
elif score > two:
record["第2名"]=score
record["第3名"]=two
else:
record["第3名"]=score
record=json.dumps(record,ensure_ascii=False)
with open("record.txt","w",encoding="UTF-8")as flw:
flw.write(record)
crd=myfont.render("分数:"+str(score),True,(6,6,6))
screen.blit(crd,(850,20))
text=myfont.render('第一名:'+str(one),True,(6,6,6))
screen.blit(text,(850,45))
text1=myfont.render('第二名:'+str(two),True,(6,6,6))
screen.blit(text1,(850,70))
text2=myfont.render('第三名:'+str(three),True,(6,6,6))
screen.blit(text2,(850,95))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
......
import json
jsonData='{"第一名":122,"第二名":100,"第三名":88}'
text=json.loads(jsonData)
print(text["第一名"])
\ No newline at end of file
{"第1名": 0, "第2名": 0, "第3名": 0} {"第1名": 107, "第2名": 23, "第3名": 11}
\ No newline at end of file \ 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