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

Pygame背景装饰元素每帧闪烁位移问题求助

问题:Pygame背景装饰元素频繁闪烁位移的解决方法

我正在用Pygame开发游戏,已经用纯色填充背景,为了避免单调,制作了装饰素材并通过pygame.image.load()加载,希望将这些装饰随机放置在背景上,编写了如下类:

class Backround_Cosmatics():
    def __init__( self , bg ):
        self.bg = bg
        self.Random_bg = random.randint( 1 , 3 )

    def DrawCosmatics(self , win):

            if self.Random_bg == 1:
                win.blit( flower , ( random.randint( 0 , SCREEN_WIDTH ) , random.randint( 0 , SCREEN_HEIGHT ) ))
                self.bg += 1

            if self.Random_bg == 2:
                win.blit( stone , ( random.randint( 0 , SCREEN_WIDTH ) , random.randint( 0 , SCREEN_HEIGHT ) ))
                self.bg += 1

            if self.Random_bg == 3:

                grass_type = random.randint( 1 , 2 )
                if grass_type == 1:
                    win.blit( grass1 , (( random.randint( 0 , SCREEN_WIDTH ) , random.randint( 0 , SCREEN_HEIGHT ))))
                    self.bg += 1

                if grass_type == 2:
                    win.blit( grass2 , (( random.randint( 0 , SCREEN_WIDTH ) , random.randint( 0 , SCREEN_HEIGHT ))))
                    self.bg += 1

在__init__方法中定义了元素类型(石头、花朵或草),DrawCosmatics方法设置随机放置位置,草分为两种类型。

游戏主循环中的调用代码:

if len(Backround_cosmatics_list) <= Backround_cosmetics_cap:
    cosmatic = Backround_Cosmatics(0)
    Backround_cosmatics_list.append(cosmatic)

for cosmatic in Backround_cosmatics_list:
    cosmatic.DrawCosmatics( Win )

循环外定义的全局变量:

Backround_cosmetics_cap = 15
Backround_cosmatics_list = []

运行后发现背景装饰元素每帧都会改变位置,出现闪烁位移的情况,求解决方法。


解决方法

核心问题分析

闪烁的原因是每次调用DrawCosmatics时都重新生成随机位置,每帧绘制时元素位置都在变,导致视觉上的闪烁。另外主循环的判断条件<=会导致列表长度达到上限后仍持续添加新元素,造成性能浪费。

修改后的代码实现

  1. 重构Backround_Cosmatics类:在初始化阶段就确定元素的图像和固定位置,绘制时直接使用这些预定义的值:
import random

class Backround_Cosmatics():
    def __init__(self):
        # 初始化时就确定元素类型、对应图像和位置
        self.random_type = random.randint(1, 3)
        self.image = None
        self.pos = (random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT))
        
        if self.random_type == 1:
            self.image = flower
        elif self.random_type == 2:
            self.image = stone
        elif self.random_type == 3:
            grass_type = random.randint(1, 2)
            self.image = grass1 if grass_type == 1 else grass2

    def DrawCosmatics(self, win):
        # 仅负责绘制固定的图像和位置
        win.blit(self.image, self.pos)
  1. 修正主循环的判断条件:将<=改为<,避免列表长度达到上限后仍持续添加元素:
if len(Backround_cosmatics_list) < Backround_cosmetics_cap:
    cosmatic = Backround_Cosmatics()
    Backround_cosmatics_list.append(cosmatic)

for cosmatic in Backround_cosmatics_list:
    cosmatic.DrawCosmatics(Win)

说明

  • 每个装饰元素实例在创建时就固定了自身的图像和位置,后续每帧绘制都使用这个固定值,不会再随机变化,解决闪烁问题。
  • 主循环的条件修改后,列表长度达到Backround_cosmetics_cap时就停止添加新元素,避免不必要的性能消耗。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 11:03:20