飞翔的小鸟

from Walimaker import *
import random
setup(288, 512)
bgpic('./Flappy Bird/bg_day.png')
# 地面 小鸟 管道
# 创建地面
land1 = Character('./Flappy Bird/land.png')
land1.y = -240
land2 = Character('./Flappy Bird/land.png')
land2.y = -240
land2.x = 336
land1.layer = 1
land2.layer = 2
v = 2
# 创建小鸟
bird_img = ['./Flappy Bird/bird0_0.png',
            './Flappy Bird/bird0_1.png',
            './Flappy Bird/bird0_2.png']
bird = Character(bird_img)
bird.play_anim()
bird.x = -30
bird.dt = 0.1
bird.vy = 0
bird.ay = -0.9 
bird.w = 0 # 旋转速度/角速度
bird.dw = -0.3

# 创建水管
d = 80
pipe_up1 = Character('./Flappy Bird/pipe_up.png')
pipe_down1 = Character('./Flappy Bird/pipe_down.png')
y = random.randint(-80, 180)
pipe_up1.y = y - d - 160
pipe_down1.y = y + d + 160

pipe_up2 = Character('./Flappy Bird/pipe_up.png')
pipe_down2 = Character('./Flappy Bird/pipe_down.png')
y = random.randint(-80, 180)
pipe_up2.y = y - d - 160
pipe_down2.y = y + d + 160

pipe_up1.x = 600
pipe_down1.x = 600
pipe_up2.x = 792
pipe_down2.x = 792

running = False # running正在运行中

button = Character('./Flappy Bird/button_resume.png')
button.scale(3)
button.y = -100
bird.ay = 0
bird.dw = 0

# 分数
score = 0 # 分数
scoreText = TextBox(50, font = './Flappy Bird/Sharp Retro.ttf')
scoreText.print(score)
scoreText.goto(0, 180)
scoreText.color = (255,255,255)

# sensor 传感器
sensor1 = Character('./Flappy Bird/pipe_down.png')
sensor1.scale(0.1, 3)
sensor2 = Character('./Flappy Bird/pipe_down.png')
sensor2.scale(0.1, 3)
sensor1.x = pipe_up1.x + 26
sensor2.x = pipe_up2.x + 26
sensor1.layer = -1
sensor2.layer = -1
while True:
    # 地面的运动
    if running:
        land1.backward(v)
        land2.backward(v)
    if land1.x < -336:
        land1.x = land2.x + 336
    if land2.x < -336:
        land2.x = land1.x + 336
    # 小鸟的运动
    bird.vy += bird.ay
    if key_just_pressed(K_SPACE) or get_mouse_just_clicked() and running:
        bird.vy = 10
        bird.rot = 15
        bird.w = 0
    bird.y += bird.vy
    bird.w += bird.dw
    bird.rot += bird.w
    bird.rot = max(-90, bird.rot)

    # 管道运动
    if running:
        pipe_up1.backward(v)
        pipe_down1.backward(v)
        pipe_up2.backward(v)
        pipe_down2.backward(v)
    if pipe_up1.x < -192:
        pipe_up1.x = pipe_up2.x + 192
        pipe_down1.x = pipe_down2.x + 192
        sensor1.show()
    if pipe_up2.x < -192:
        pipe_up2.x = pipe_up1.x + 192
        pipe_down2.x = pipe_down1.x + 192
        sensor2.show()
    sensor1.x = pipe_up1.x + 26
    sensor2.x = pipe_up2.x + 26
    if bird.collide(sensor1):
        sensor1.hide()
        score += 1
        scoreText.print(score)
    if bird.collide(sensor2):
        sensor2.hide()
        score += 1
        scoreText.print(score)
    # 碰撞检测
    # \ 代码换行
    if bird.collide(pipe_up1) \
        or bird.collide(pipe_up2) \
        or bird.collide(pipe_down1) \
        or bird.collide(pipe_down2) \
        or bird.collide(land1) \
        or bird.collide(land2):
        running = False
        bird.w = -20
        bird.dt = 100000
    if bird.collide(land1) or bird.collide(land2):
        bird.y = -180
        bird.vy = 0
        button.show()
    if button.get_mouse_just_released():
        running = True
        bird.y = 0
        bird.vy = 10
        bird.rot = 15
        bird.dt = 0.1
        bird.w = 0 # 旋转速度/角速度
        pipe_up1.x = 600
        pipe_down1.x = 600
        pipe_up2.x = 792
        pipe_down2.x = 792
        sensor1.x = pipe_up1.x + 26
        sensor2.x = pipe_up2.x + 26
        sensor2.show()
        sensor2.show()
        button.hide()
        bird.ay = -0.9 
        bird.dw = -0.3
        score = 0
        scoreText.print(score)
    update()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值