好的,以下是实现上下水管位置随机出现的代码:
```python
import pgzrun
from random import randint
import keyboard
WIDTH = 400
HEIGHT = 708
bird = Actor('bird1', (75, 350))
pipes = []
for i in range(3):
pipe_top = Actor('top', (500 + i * 200, 0))
pipe_bottom = Actor('bottom', (500 + i * 200, 500))
pipes.append((pipe_top, pipe_bottom))
game_over = False
score = 0
def update():
global game_over, score
if not game_over:
bird.y += 2
for pipe_top, pipe_bottom in pipes:
pipe_top.x -= 2
pipe_bottom.x -= 2
if pipe_top.right < 0:
pipe_top.left = WIDTH
pipe_bottom.left = WIDTH
pipe_top.y = randint(-250, -50)
pipe_bottom.y = pipe_top.y + 800
score += 1
if bird.colliderect(pipe_top) or bird.colliderect(pipe_bottom):
game_over = True
if bird.bottom > HEIGHT:
game_over = True
if keyboard.space:
bird.y -= 50
def draw():
screen.blit('background', (0, 0))
for pipe_top, pipe_bottom in pipes:
pipe_top.draw()
pipe_bottom.draw()
bird.draw()
screen.draw.text(str(score), (10, 10), color='white', fontsize=30)
if game_over:
screen.draw.text('Game Over!', center=(WIDTH/2, HEIGHT/2), color='white', fontsize=60)
pgzrun.go()
```
在这个修改后的代码中,我们在每个水管出现时随机生成上下水管的位置。具体来说,我们使用randint函数生成一个随机数作为上水管的y坐标,然后将下水管的y坐标设置为上水管的y坐标加上800(即两个水管之间的距离)。这样就可以实现上下水管位置随机出现了。