Commit 63255ade by BellCodeEditor

save project

parent 9ae0b404
Showing with 9 additions and 3 deletions
...@@ -3,7 +3,7 @@ from pygame import locals ...@@ -3,7 +3,7 @@ from pygame import locals
import random import random
speed = 8 speed = 8
pygame.init() # 初始化 pygame.init() # 初始化
score_music = pygame.music.sound('score.wav') score_music = pygame.mixer.Sound('score.wav')
class Block(pygame.sprite.Sprite): # 障碍物精灵类 class Block(pygame.sprite.Sprite): # 障碍物精灵类
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__()#继承父类属性 super().__init__()#继承父类属性
...@@ -50,7 +50,7 @@ time = 0 ...@@ -50,7 +50,7 @@ time = 0
gamestate = True gamestate = True
score = 0 score = 0
block_list =pygame.sprite.Group() # 创建精灵组 block_list =pygame.sprite.Group() # 创建精灵组
old_score = 0
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:# 接收到退出事件后退出程序
...@@ -100,11 +100,13 @@ while True: ...@@ -100,11 +100,13 @@ while True:
time += 1 time += 1
if time >= 60: # 每隔一秒创建障碍物精灵 if time >= 60: # 每隔一秒创建障碍物精灵
time = 0 time = 0
speed =speed +score //3
print(speed)
num = random.randint(0,50) num = random.randint(0,50)
if num > 10: if num > 10:
obstacle = Block(bush,cacti,stone) obstacle = Block(bush,cacti,stone)
block_list.add(obstacle) block_list.add(obstacle)
speed =speed +score//3
for sprite in block_list: # 遍历、展示障碍物精灵 for sprite in block_list: # 遍历、展示障碍物精灵
sprite.rect.x -= speed #障碍物精灵向左移动(x坐标减小) sprite.rect.x -= speed #障碍物精灵向左移动(x坐标减小)
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y)) #渲染障碍物 screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y)) #渲染障碍物
...@@ -116,9 +118,13 @@ while True: ...@@ -116,9 +118,13 @@ while True:
gamestate = False gamestate = False
else: else:
if sprite.rect.x+sprite.rect.width<wukong.rect.x: if sprite.rect.x+sprite.rect.width<wukong.rect.x:
score += sprite.score#加1分 score += sprite.score#加1分
sprite.score = 0 sprite.score = 0
if score>old_score:
score_music.play() score_music.play()
old_score = score
# 刷新画面 # 刷新画面
myfont = pygame.font.Font('STKAITI.TTF',15)#设置字体 myfont = pygame.font.Font('STKAITI.TTF',15)#设置字体
info1 = myfont.render("分数:"+str(score),True,(0,0,0))#设置语句 info1 = myfont.render("分数:"+str(score),True,(0,0,0))#设置语句
......
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