Commit b0c0bb4c by BellCodeEditor

auto save

parent 022ec8df
Showing with 151 additions and 18 deletions
import pygame
from pygame import locals
import time
import random
import json
with open("score.txt",'r',encoding='utf-8') as f:
txt=f.read()
dtxt=json.loads(txt)
one=dtxt["第1名"]
two=dtxt["第2名"]
three=dtxt["第3名"]
pygame.init() # 初始化
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
fps = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
apple = pygame.image.load('bush.png') # 灌木丛
hero = pygame.image.load('hero1.png')
index = 0
bush = pygame.image.load('bush.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')]
pygame.display.set_caption('悟空酷跑 -(@>_<@)-')
index = 0
y=410
jumpstate='r'
t=20
road_x=0
bg_x=0
time=0
gamestate=True
score=0
my_font=pygame.font.Font('STKAITI.TTF',25)
speed=8
bg_speed=1
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,self.rect.y=1000,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,self.rect.y=150,400
Block_list=pygame.sprite.Group()
while True:
speed=8+score//5
bg_speed=speed//5
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
# 将背景图画上去
screen.blit(background, (0, 0))
screen.blit(road, (0, 500))
screen.blit(hero, (150, 400))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
if event.type==locals.QUIT:
exit()# 接收到退出事件后退出程序
if event.type==locals.KEYDOWN:
if (event.key==locals.K_SPACE or event.key==locals.K_UP or event.key==locals.K_w) and jumpstate=='r':
jumpstate='u'
if event.key==locals.K_0:
if gamestate==False:
gamestate=True
# prop.kill()
score=0
if event.key==locals.K_z:
score=500
if gamestate==True:
wukong=Player(hero
[
index])
road_x-=speed
if road_x<-999:
road_x=0
bg_x-=bg_speed
if bg_x<-999:
bg_x=0
# 将背景图画上去
screen.blit(background, (bg_x, 0))
screen.blit(road, (road_x, 500))
screen.blit(wukong.image, (170,y))
if jumpstate=='r':
index+=1
y=410
if index==5:
index=0
if jumpstate=='u':
if t>0:
y-=t
wukong.rect.y=y
t-=0.99
else:
jumpstate='d'
if jumpstate=='d':
if t<=20:
y+=t
wukong.rect.y=y
t+=0.99
else:
jumpstate='r'
t=20
time+=1
if time>=60:
time=0
r=random.randint(0,50)
if r>16:
obstacle=Block(stone,cacti,bush)
Block_list.add(obstacle)
for prop in Block_list:
prop.rect.x-=speed
if prop.rect.x<0-prop.rect.width:
prop.kill()
if pygame.sprite.collide_rect(wukong,prop):
gameover=pygame.image.load('gameover.png')
gamestate=False
screen.blit(gameover,(400,200))
print('哈哈!…………………………………………………………………………………………………………………………')
if score>three:
if score>one:
dtxt['第1名']=score
dtxt['第2名']=one
dtxt['第3名']=two
elif score>two:
dtxt['第2名']=score
dtxt['第3名']=two
else:
dtxt['第3名']=score
txt=json.dumps(dtxt,ensure_ascii=False)
with open("score.txt",'w',encoding='utf-8') as f:
f.write(txt)
else:
if wukong.rect.x>prop.rect.x+prop.rect.width:
score+=prop.score
prop.score=0
screen.blit(prop.image,(prop.rect.x,prop.rect.y))
# 刷新画面
font=my_font.render('score: '+str(score),True,(255,255,255))
screen.blit(font,(880,15))
font=my_font.render('one: '+str(one),True,(255,255,255))
screen.blit(font,(880,45))
font=my_font.render('two: '+str(two),True,(255,255,255))
screen.blit(font,(880,75))
font=my_font.render('three: '+str(three),True,(255,255,255))
screen.blit(font,(880,105))
pygame.display.update()
fps.tick(60)
\ No newline at end of file
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
{"第1名": 4, "第2名": 2, "第3名": 1}
\ 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