python - 在背景中绘制随机星星,pygame

标签 python pygame

我正在开发一款平台游戏,我想在背景中添加随机星星。我在函数 drawBackDrop() 中编写了一些代码,它将在屏幕上的随机位置生成一些星星。

它工作得很好,但是为了绘制星星,我必须每帧更新它,所以每次都会调用该函数,并且每次都会以不同的方式生成随机星星。

我希望它只出现一次,然后不更新,因此星星保持在恒定的位置。

我的drawBackDrop()函数:

def drawBackdrop():
    if globals.theme == "dark":
        a = random.randint(3, 12)
        x = random.randint(5, globals.screen_width - 12)
        y = random.randint(5, globals.screen_height - 12)
        numberOfStars = random.randint(35, 55)
        starRect = pygame.Rect(x, y, a, a)
        for i in range(numberOfStars):
            pygame.draw.rect(globals.screen, "White", starRect)

    if globals.theme == "light":
        pass #Sun and Clouds

这是我调用该函数的场景代码:

def draw(self, sceneManager, screen):
        if globals.theme == "light":
            screen.fill(globals.light_blue)
        elif globals.theme == "dark":
            screen.fill(globals.black)
        draw.drawBackdrop()
        self.cameraSystem.update(screen)

我得到了什么:

如您所见,每一帧都在形成星星。我希望它只形成一次,然后在屏幕上绘制相同的星星,直到我赢/输/离开。

如何只调用该函数一次?或者还有其他方法来绘制随机星星吗?

最佳答案

有两种方法可以到达这里。

  • 将星星绘制到背景图层上,并在每一帧上进行 blit
  • 生成随机星星列表一次,并存储它们。每帧重新绘制相同的矩形。 (正如 @matszwecja 在评论中建议的那样)

但首先请注意,您的代码中存在一个小错误。它没有正确生成星星 - IFF 我正确理解你的意图。看起来您想生成一组随机的星星,但它一遍又一遍地绘制同一个星星。

def drawBackdrop():
    if globals.theme == "dark":
        numberOfStars = random.randint(35, 55)    # <<-- MOVED outside loop
        for i in range(numberOfStars):            # <<-- MOVED Loop
            a = random.randint(3, 12)
            x = random.randint(5, globals.screen_width - 12)
            y = random.randint(5, globals.screen_height - 12)         
            starRect = pygame.Rect(x, y, a, a) 
            pygame.draw.rect(globals.screen, "White", starRect)

背景表面

def makeBackgroundStars(): 
    """ Generate a transparent background overlay with stars """ 
    background = pygame.Surface( ( globals.screen_width, globals.screen_height ), pygame.SRCALPHA )
    numberOfStars = random.randint(35, 55)
    for i in range(numberOfStars):
        a = random.randint(3, 12)
        x = random.randint(5, globals.screen_width - 12)
        y = random.randint(5, globals.screen_height - 12)
        starRect = pygame.Rect(x, y, a, a)
        pygame.draw.rect( background, "White", starRect )
    return background

您只需要制作初始图像,然后每帧将其传输。也许您也会将所有背景图形包含到该图像中。

star_field = makeBackgroundStars()

# in main loop
# ...
globals.screen.blit( star_field, ( 0, 0 ) )

星形矩形列表

star_positions = []   # global to hold rects

def genererateStars():
    """ generate a random amount of stars for the background """
    global star_positions
    numberOfStars = random.randint(35, 55)    # <<-- MOVED outside loop
    for i in range(numberOfStars):            # <<-- MOVED Loop
        a = random.randint(3, 12)
        x = random.randint(5, globals.screen_width - 12)
        y = random.randint(5, globals.screen_height - 12)
        star_positions.append( pygame.Rect(x, y, a, a) )   # save it for later

def paintStars( screen ):
    """ Draw the stars to the background """
    global star_positions
    for starRect in star_positions:
        pygame.draw.rect( screen, "White", starRect )

关于python - 在背景中绘制随机星星,pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73880748/

相关文章:

python - Pygame如何改变背景颜色而不删除其他任何东西

python - 我怎样才能从中心而不是pygame中的角落旋转图像?

python - pandas 重新采样应用 np.average

python - QTreeView/QStandardItemModel 中选定的行

python - 碰撞矩形

python - 在 PyGame 中混合 per-alpha 和颜色键的解决方法

python - 使用PyGame播放音乐而不显示

python - 将数据帧返回函数应用于基础数据帧的每一行

python - OpenCV Python : Closed Contour Approximation For A Speech Bubble Shape

python3.5 : asyncio, 如何等待 "transport.write(data)"完成或返回错误?