用户:
苏联盟军团万岁查看:0 回复:0 评论:0 创建时间:2024-08-13T11:46:13
import pgzrun
import random
WIDTH = 400
HEIGHT = 600
TITLE = '训练飞行员'
bird = Actor('机械鸟1', (75, 200))
block_above = Actor('上方障碍物', anchor=('left', 'bottom'))
block_below = Actor('下方障碍物', anchor=('left', 'top'))
bird.step = 1
status = 0
block_above.step = 3
block_below.step = 3
score = 0
def reset_block():
rand_y = random.randint(200, 400)
block_above.pos = (WIDTH, rand_y - 75)
block_below.pos = (WIDTH, rand_y + 75)
reset_block()
def update_block():
global score
block_above.x -= block_above.step
block_below.x -= block_below.step
if block_above.right < 0:
reset_block()
if status == 1:
score += 1
def update_bird():
global status, score
bird.step += 0.3
bird.y += bird.step
if status == 1:
if bird.step < 0:
bird.image = '机械鸟2'
else:
bird.image = '机械鸟1'
if bird.top < 0 or bird.bottom > HEIGHT:
bird.image = '机械鸟1'
bird.y = 200
bird.step = 1
status = 0
reset_block()
score = 0
if bird.colliderect(block_above) or bird.colliderect(block_below):
status = 2
bird.image = '机械鸟坠毁'
def draw():
screen.blit('背景', (0, 0))
bird.draw()
block_above.draw()
block_below.draw()
screen.draw.text(str(score), (20, 10), color='white', fontsize=50)
def update():
if status == 0:
pass
else:
update_bird()
update_block()
def on_key_down(key):
global status
if key == keys.SPACE and status == 0:
status = 1
if key == keys.UP and status == 1:
bird.step = -5
music.play_once('flap')
sounds.bgm.play(-1)
pgzrun.go()