Commit 232153ff by BellCodeEditor

auto save

parent 022ec8df
Showing with 154 additions and 2 deletions
File added
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
{"第1名": 6, "第2名": 4, "第3名": 4}
\ No newline at end of file
import pygame
import random
import json
from pygame import locals
pygame.init()
screen=pygame.display.set_mode((1000, 600))
pygame.display.set_caption("悟空跑酷")#设置窗口标题
fps=pygame.time.Clock()
bg=pygame.image.load('bg.png')
bush=pygame.image.load('bush.png')
cacti=pygame.image.load('cacti.png')
gameover=pygame.image.load('gameover.png')
road=pygame.image.load('road.png')
stone=pygame.image.load('stone.png')
hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
basic_font=pygame.font.Font('STKAITI.TTF',18)#字体对象
#sound=pygame.mixer.Sound('Heaven.ogg')#音效对象
with open('score.txt','r',encoding='utf-8') as f:
content=f.read()
record=json.loads(content)
one=record['第1名']
two=record['第2名']
three=record['第3名']
index=0
y=400
state="running" #悟空运动状态
t=30 #悟空速度变量
speed=0 #障碍物速度
gamestate=True#游戏状态
score=0#分数
#创建障碍物类
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
self.image=random.choice([image1,image2,image3])
self.rect=self.image.get_rect()
self.rect.x=1000
self.rect.y=500-self.rect.height
self.score=1#障碍物分数
#创建悟空类
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
self.image=image
self.rect=self.image.get_rect()
self.rect.x=150
self.rect.y=400
blocklist=pygame.sprite.Group() #创建精灵组
road_x=0
bg_x=0
time=0 #制作时间间隔
while 1 :
for event in pygame.event.get():
if event.type==locals.QUIT:
exit()
if event.type==locals.KEYDOWN:
if state=="running":
if event.key==locals.K_SPACE:
state="up"
speed=8+score//3 #每次增加3分,速度加1
wukong=Player(hero[index])#悟空造型
if state=="running":#悟空切换造型
index+=1
if index>=5:
index=0
if gamestate==True:
if state=="up":#向上模式
if t>0:
y-=t
wukong.rect.y=y
t-=2
else:
state="down"#向下模式
if state=="down":
if t<=30:
y+=t
wukong.rect.y=y
t+=2
else:
state="running"#跑步模式
t=30
bg_x-=1
if bg_x<=-1000:
bg_x=0
screen.blit(bg,(bg_x,0))#背景
road_x-=speed
if road_x<=-1000:
road_x=0
screen.blit(road,(road_x,500))#道路
screen.blit(wukong.image,(150,y))#悟空
time+=1
if time>=60:#1秒产生一个随机数,如果随机数>20,就创建一个精灵
time=0
num=random.randint(0,50)
if num>20:
zhangai=Block(bush,cacti,stone)#创建障碍物精灵
blocklist.add(zhangai)
for sprite in blocklist:#遍历、展示障碍物精灵
sprite.rect.x-=speed
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x<=0-sprite.rect.width:
sprite.kill()#删除精灵
if pygame.sprite.collide_rect(sprite,wukong):
gameover=pygame.image.load('gameover.png')
screen.blit(gameover,(450,250))
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('score.txt','w',encoding='utf-8') as s:
s.write(record)
else:
if sprite.rect.x+sprite.rect.width<wukong.rect.x:
score+=sprite.score
sprite.score=0
#sound.play()
#文字绘制
scoretext=basic_font.render("分数:"+str(score),1,(255,255,255))
screen.blit(scoretext,(880,20))
scoretext=basic_font.render("第1名:"+str(one),1,(255,255,255))
screen.blit(scoretext,(880,50))
scoretext=basic_font.render("第2名:"+str(two),1,(255,255,255))
screen.blit(scoretext,(880,80))
scoretext=basic_font.render("第3名:"+str(three),1,(255,255,255))
screen.blit(scoretext,(880,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