Commit cf71cb5f by BellCodeEditor

auto save

parent eadc569e
Showing with 54 additions and 5 deletions
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类
def __init__(self,image1,image2,image3):
super().__init__()
......@@ -31,7 +33,7 @@ screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑")
# 载入图片
s_audio=pygame.mixer.Sound('score.wav')
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
......@@ -53,6 +55,19 @@ time = 0
gamestate = True
score=0
block_list =pygame.sprite.Group() # 创建精灵组
speed=0
list=[]
with open(r"C:\Users\makcoo\Desktop\record.txt","r",encoding="utf-8") as f:
content=f.read()
record=json.loads(content)
list.append(record["1"])
list.append(record["2"])
list.append(record["3"])
print(list)
first=0
second=0
third=0
while True:
for event in pygame.event.get():
......@@ -76,14 +91,14 @@ while True:
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
t -= 1.5
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
t += 1.5
else:
jumpState = "runing"
t =30
......@@ -109,21 +124,55 @@ while True:
obstacle = Block(bush,cacti,stone)
block_list.add(obstacle)
for sprite in block_list: # 遍历、展示障碍物精灵
sprite.rect.x -= 8
sprite.rect.x -= 15+speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width:
sprite.kill()
if sprite.checked==False:
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
gameover = pygame.image.load('gameover.png') # 游戏结束
if score>list[0]:
list.insert(0,score)
print(1)
list.pop(-1)
elif score<list[0] and score>list[1]:
list.insert(1,score)
print(2)
list.pop(-1)
elif score<list[1] and score>list[2]:
list.insert(2,score)
print(3)
list.pop(-1)
with open(r"C:\Users\makcoo\Desktop\record.txt","w",encoding="utf-8") as f:
record["1"]=list[0]
record["2"]=list[1]
record["3"]=list[2]
print(list)
print(record)
record=json.dumps(record,ensure_ascii=False)
f.write(record)
screen.blit(gameover, (400, 200))
gamestate = False
else:
if wukong.rect.x>sprite.rect.x:
score=score+1
sprite.checked=True
s_audio.play()
speed=score//3
scoreSurf = basic_font.render("score:"+str(score),True,(255,220,125))
first="first:"+str(list[0])
second="second:"+str(list[1])
third="third:"+str(list[2])
screen.blit(scoreSurf,(830,20))
scoreSurf = basic_font.render(first,True,(255,220,125))
screen.blit(scoreSurf,(830,60))
scoreSurf = basic_font.render(second,True,(255,220,125))
screen.blit(scoreSurf,(800,90))
scoreSurf = basic_font.render(third,True,(255,220,125))
screen.blit(scoreSurf,(830,120))
# 刷新画面
pygame.display.update()
......
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