Python程序:用Pygame Zero做FlappyBird小游戏

import pgzrun
from pgzhelper import *
import random

WIDTH=1000
HEIGHT=1000

Bird=Actor('bird',(WIDTH/2, HEIGHT/2))
Pipes=[]
game_over = False

def draw():
    global Pipes, game_over
    screen.clear()
    Bird.draw()
    for Pipe in Pipes:
        Pipe.draw()

    if game_over:
        screen.draw.filled_rect(
            Rect(0, 0, WIDTH, HEIGHT),(0, 0, 0, 200)
        )
        screen.draw.text(
            "GAME OVER",
            center=(WIDTH//2, HEIGHT//2),
            fontsize=60,
            color="red",
            gcolor="yellow"
        )

def update():
    global Pipes, game_over

    Bird.y += 1
    if keyboard.space:
        Bird.y -= 5

    if random.randint(1, 120) == 1:
        if len(Pipes) == 0 or Pipes[-1].right < WIDTH - Pipes[-1].width:
            appear_pipe()

    for Pipe in Pipes[:]:
        Pipe.x -= 1
        if Pipe.right < 0:
            Pipes.remove(Pipe)
    
    if Bird.collidelist(Pipes)!=-1 or Bird.top < 0 or Bird.bottom > HEIGHT:
        Bird.image='bird_hurt'
        game_over=True

def appear_pipe():
    global Pipes

    TopPipe=Actor('pipe');BottomPipe=Actor('pipe')
    TopPipe.angle=180
    TopPipe.left=WIDTH
    BottomPipe.left=WIDTH
    TopPipe.bottom=random.uniform(0, HEIGHT/3)
    BottomPipe.top=random.uniform(HEIGHT*2/3, HEIGHT)

    Pipes.append(TopPipe)
    Pipes.append(BottomPipe)

pgzrun.go()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值