You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pygame中实现图片重复排列(如一行10次)以生成Flappy Bird管道?

Flappy Bird 管道批量生成实现方案

核心思路

不需要为每个管道单独声明变量,用数组/列表容器统一存储所有管道实例,配合「基础预制体克隆」的方式批量生成,后续的移动、碰撞检测、销毁操作都可以直接遍历数组完成,不需要单独绑定变量。
如果不需要一次性预生成20个管道,更推荐用动态生成+对象池的方案,随用随生成,管道移出屏幕后回收复用,性能更好。

不同开发环境的实现示例

Unity/C# 实现

提前制作包含上下管道、碰撞体、移动逻辑的管道预制体,挂载到全局管理脚本的对应字段即可:

using System.Collections.Generic;
using UnityEngine;

public class PipeSpawner : MonoBehaviour
{
    // 提前在Inspector面板拖拽赋值管道预制体
    public GameObject pipePrefab;
    // 存储所有管道的列表
    private List<GameObject> _pipeList = new List<GameObject>();
    public int totalPipeCount = 20;
    public float pipeHorizontalGap = 3f;

    void Start()
    {
        BatchSpawnPipes();
    }

    void BatchSpawnPipes()
    {
        for (int i = 0; i < totalPipeCount; i++)
        {
            // 随机生成管道Y轴偏移,控制上下管道间隙位置
            float randomYOffset = Random.Range(-1.2f, 1.8f);
            Vector3 spawnPos = new Vector3(10 + i * pipeHorizontalGap, randomYOffset, 0);
            // 克隆预制体生成新管道
            GameObject newPipe = Instantiate(pipePrefab, spawnPos, Quaternion.identity);
            // 存入列表统一管理
            _pipeList.Add(newPipe);
        }
    }
}

Pygame/Python 实现

import pygame
import random

pygame.init()
screen = pygame.display.set_mode((400, 700))
# 加载管道基础素材
pipe_surface = pygame.image.load("assets/pipe.png").convert_alpha()
# 存储所有管道的列表
pipe_list = []
TOTAL_PIPE_NUM = 20
PIPE_GAP = 300

# 批量生成管道
for i in range(TOTAL_PIPE_NUM):
    # 随机生成管道高度
    bottom_pipe_y = random.randint(350, 550)
    bottom_pipe_rect = pipe_surface.get_rect(midtop = (400 + i * PIPE_GAP, bottom_pipe_y))
    top_pipe_rect = pipe_surface.get_rect(midbottom = (400 + i * PIPE_GAP, bottom_pipe_y - 180))
    pipe_list.extend([bottom_pipe_rect, top_pipe_rect])

# 帧更新逻辑中直接遍历列表处理
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    # 移动所有管道
    for pipe in pipe_list:
        pipe.centerx -= 2
    # 渲染管道
    for pipe in pipe_list:
        screen.blit(pipe_surface, pipe)
    pygame.display.update()

原生JS/Canvas 实现

const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
canvas.width = 400
canvas.height = 700

// 加载管道基础图片
const pipeImg = new Image()
pipeImg.src = './assets/pipe.png'
// 存储所有管道的数组
const pipes = []
const TOTAL_PIPE_COUNT = 20
const PIPE_HORIZONTAL_GAP = 280

// 批量生成管道
for (let i = 0; i < TOTAL_PIPE_COUNT; i++) {
  const randomYOffset = Math.random() * 180 - 90
  pipes.push({
    x: 400 + i * PIPE_HORIZONTAL_GAP,
    topY: -320 + randomYOffset,
    bottomY: 300 + randomYOffset,
    width: 52,
    height: 320
  })
}

// 帧渲染函数
function render() {
  ctx.clearRect(0, 0, canvas.width, canvas.height)
  // 遍历绘制所有管道
  pipes.forEach(pipe => {
    // 绘制上管道(翻转图片)
    ctx.save()
    ctx.scale(1, -1)
    ctx.drawImage(pipeImg, pipe.x, -pipe.topY, pipe.width, pipe.height)
    ctx.restore()
    // 绘制下管道
    ctx.drawImage(pipeImg, pipe.x, pipe.bottomY, pipe.width, pipe.height)
    // 移动管道
    pipe.x -= 2
  })
  requestAnimationFrame(render)
}

pipeImg.onload = render

优化提示:不用一次性预生成20个管道,可以仅生成4组管道,当管道移动到左侧屏幕外时,直接重置它的X坐标到最右侧、重新随机高度即可循环复用,内存占用更低。

内容的提问来源于stack exchange,提问作者Lukspie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 10:36:03