Commit bf19f601 by BellCodeEditor

auto save

parent 545793fe
Showing with 47 additions and 3 deletions
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
......@@ -17,6 +18,8 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
basic_font = pygame.font.Font('STKAITI.TTF',18)
score_audio=pygame.mixer.Sound('score.wav')
index = 0
y = 400
jumpState = "runing"
......@@ -25,6 +28,9 @@ road_x=0
bg_x=0
time=0
gamestate=True
score=0
speed=8
old_score=0
class Block(pygame.sprite.Sprite):
......@@ -34,6 +40,7 @@ class Block(pygame.sprite.Sprite):
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__()
......@@ -43,6 +50,14 @@ class Player(pygame.sprite.Sprite):
self.rect.y=400
block_list=pygame.sprite.Group()
with open('record.txt','r',encoding='utf-8')as f:
content = f.read()
record=json.loads(content)
print(record)
one=record["第1名"]
two = record["第2名"]
three = record["第3名"]
while True:
......@@ -54,7 +69,7 @@ while True:
if jumpState == "runing":
if event.key == locals.K_SPACE:
jumpState = "up"
speed=8+score//3
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
......@@ -84,7 +99,7 @@ while True:
if bg_x <=-1000:
bg_x=0
screen.blit(background, (bg_x, 0)) # 远处背景
road_x-=8
road_x-=speed
if road_x<=-1000:
road_x=0
screen.blit(road, (road_x, 500)) # 路
......@@ -100,7 +115,7 @@ while True:
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x-= 8
sprite.rect.x-= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width:
sprite.kill()
......@@ -108,10 +123,38 @@ while True:
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
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
else:
if sprite.rect.x+sprite.rect.width<wukong.rect.x:
score+=sprite.score
sprite.score=0
print(score)
# if obstacle.rect.x <= 0-obstacle.rect.width: # 障碍物消失
# # 创建障碍物对象
# obstacle = Block(bush, stone, cacti)
# obstacle.rect.x -= 8
if score > old_score:
score_audio.play()
old_score = score
scoreSure=basic_font.render("分数:"+str(score),True,(255,255,255))
screen.blit(scoreSure,(800,20))
scoreSure=basic_font.render("第1名:"+str(score),True,(255,255,255))
screen.blit(scoreSure,(800,50))
scoreSure=basic_font.render("第2名:"+str(score),True,(255,255,255))
screen.blit(scoreSure,(800,80))
scoreSure=basic_font.render("第3名:"+str(score),True,(255,255,255))
screen.blit(scoreSure,(800,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