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()
Python程序:用Pygame Zero做FlappyBird小游戏
于 2025-03-17 09:24:25 首次发布