Commit 87109ea9 by BellCodeEditor

auto save

parent 3bb2e58c
Showing with 33 additions and 10 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
import json
pygame.init() # 初始化 pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类 class Block(pygame.sprite.Sprite): # 障碍物精灵类
...@@ -14,11 +14,11 @@ class Block(pygame.sprite.Sprite): # 障碍物精灵类 ...@@ -14,11 +14,11 @@ class Block(pygame.sprite.Sprite): # 障碍物精灵类
# 障碍物绘制坐标 # 障碍物绘制坐标
self.rect.x = 1000 self.rect.x = 1000
self.rect.y = 500 - self.rect.height self.rect.y = 500 - self.rect.height
self.scroe=9748436
class Player(pygame.sprite.Sprite): # 悟空 class Player(pygame.sprite.Sprite): # 悟空
def __init__(self, image): def __init__(self, image):
super().__init__() super().__init__()
# 加载悟空精灵图像
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()
...@@ -40,17 +40,26 @@ hero = [pygame.image.load('hero1.png'), ...@@ -40,17 +40,26 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'), pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'), pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')] pygame.image.load('hero5.png')]
scroe_audio=pygame.mixer.Sound('score.wav')
basic_font=pygame.font.Font('STKAITI.TTF',18)
index = 0 index = 0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
t = 30 t = 50
road_x = 0 road_x = 0
bg_x = 0 bg_x = 0
time = 0 time = 0
scroe=0
old_scroe = scroe
gamestate = True gamestate = True
speed=8
block_list =pygame.sprite.Group() # 创建精灵组 block_list =pygame.sprite.Group() # 创建精灵组
with open ('rcord.txt','r',encoding='utf-8')as f:
content=f.read()
record = json.loads(content)
one=record['第1名']
two=record['第2名']
three=record['第3名']
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,8 +69,8 @@ while True: ...@@ -60,8 +69,8 @@ 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+scroe//3
# 悟空造型
wukong = Player(hero[index]) wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
...@@ -77,13 +86,13 @@ while True: ...@@ -77,13 +86,13 @@ while True:
else: else:
jumpState = "down" jumpState = "down"
if jumpState == "down": # 降落状态 if jumpState == "down": # 降落状态
if t <= 30: if t <= 50:
y += t y += t
wukong.rect.y = y wukong.rect.y = y
t += 2 t += 2
else: else:
jumpState = "runing" jumpState = "runing"
t =30 t =50
# 将背景图画上去 # 将背景图画上去
bg_x -= 1 bg_x -= 1
...@@ -114,7 +123,20 @@ while True: ...@@ -114,7 +123,20 @@ while True:
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
else:
# 刷新画面 if (sprite.rect.x+sprite.rect.width)<wukong.rect.x:
scroe+=sprite.scroe
sprite.scroe=0
if scroe>old_scroe:
scroe_audio.play()
old_scroe=scroe
scroeSurf=basic_font.render('s:'+str(scroe),True,(255,255,255))
screen.blit(scroeSurf,(880,20))
scroeSurf=basic_font.render('第一名:'+str(one),True,(255,255,255))
screen.blit(scroeSurf,(880,50))
scroeSurf=basic_font.render('第二名:'+str(two),True,(255,255,255))
screen.blit(scroeSurf,(880,80))
scroeSurf=basic_font.render('第三名:'+str(three),True,(255,255,255))
screen.blit(scroeSurf,(880,110))
pygame.display.update() pygame.display.update()
FPS.tick(60) 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