Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尽管有明确的颜色参数,但游戏循环的一次迭代中的所有矩形绘制相同的颜色_Python_Sdl_Pygame - Fatal编程技术网

Python 尽管有明确的颜色参数,但游戏循环的一次迭代中的所有矩形绘制相同的颜色

Python 尽管有明确的颜色参数,但游戏循环的一次迭代中的所有矩形绘制相同的颜色,python,sdl,pygame,Python,Sdl,Pygame,我正在用Pygame编写一个简单的玩具。当您按下主行上的一个键时,它会产生一些粒子爆发 class Particle(): x = 0 y = 0 size = 0 colour = (255, 255, 255) rect = None def __init__(self, x, y, size, colour): self.x = x self.y = y self.size = size

我正在用Pygame编写一个简单的玩具。当您按下主行上的一个键时,它会产生一些粒子爆发

class Particle():
    x = 0
    y = 0
    size = 0
    colour = (255, 255, 255)
    rect = None
    def __init__(self, x, y, size, colour):
        self.x = x
        self.y = y
        self.size = size
        self.colour = colour # Particle has its own colour 
        self.rect = pygame.Rect(self.x, self.y, self.size, self.size)

class Burst():
    x = 0
    y = 0
    colour = (255, 255, 255)
    count = 0
    sound = None
    particles = []

    def __init__(self, x, y, colour, count, sound):
        self.x = x
        self.y = y
        self.colour = colour # Burst has its own colour, too - all its particles should have the same colour as it
        self.count = count
        self.sound = sound
        self.particles.append(Particle(self.x, self.y, 5, self.colour))

    def update(self):
        self.particles.append(Particle(random.randint(1, 30) + self.x, random.randint(1, 30) + self.y, 5, self.colour))
    def draw(self):
        global screen
        for p in self.particles:
            pygame.draw.rect(screen, p.colour, p.rect) # This draws the particles with the correct colours
            #pygame.draw.rect(screen, self.colour, (60, 60, 120, 120), 4) # This draws the particles all the same colour
            #screen.fill(p.colour, p.rect) # This draws the particles all the same colour
你要找的那条线突然断了。出于某种原因,只有未注释的一个才能正常工作。另外两条线,据我所知应该是一样的,只正确地画出了第一次爆发的粒子。任何随后的爆发都会改变屏幕上的所有粒子以匹配它们的颜色

我可以提供更多的代码,但没有更多。基本上,按键会向数组中添加脉冲,我在数组中的每一步都会调用update()和draw()


有人知道我做错了什么,然后意外修复了吗?

因为屏幕中的所有粒子都属于同一个集合Burst.particles。 每次处理一个突发事件时,你都在处理所有粒子,所有粒子都被涂上最后一种颜色

只需将初始化
particles=[]
移动到
init
方法

def __init__(self, x, y, colour, count, sound):
    ...
    self.particles = []
    self.particles.append(Particle(self.x, self.y, 5, self.colour))
更新

您使用的是Java/C风格的编码类。您不应该将任何初始化放在类级别,除非它们是常量或类属性

即:

您不应该对属性进行类声明,因为您将使用对象属性。
只需删除两个类中init方法之前的所有声明。

因为屏幕中的所有粒子都属于同一个集合Burst.particles。 每次处理一个突发事件时,你都在处理所有粒子,所有粒子都被涂上最后一种颜色

只需将初始化
particles=[]
移动到
init
方法

def __init__(self, x, y, colour, count, sound):
    ...
    self.particles = []
    self.particles.append(Particle(self.x, self.y, 5, self.colour))
更新

您使用的是Java/C风格的编码类。您不应该将任何初始化放在类级别,除非它们是常量或类属性

即:

您不应该对属性进行类声明,因为您将使用对象属性。
只需删除两个类中init方法之前的所有声明。

行#pygame.draw.rect(screen,self.color,(60,60,120,120),4)不是将它们绘制为Burst.color,而不是Particle.color吗?是的,但它们应该具有相同的值。Particle.color初始化为与它所属的突发的Burst.color相同。线条#pygame.draw.rect(屏幕,self.color,(60,60,120,120),4)是否将它们绘制为Burst.color,而不是Particle.color?是的,但它们应该具有相同的值。Particle.color被初始化为与它所属的突发的颜色相同。啊!这非常有用。非常感谢你。你的回答以后可能会给我省去很多麻烦。我根本不知道类属性的工作方式。啊!这非常有用。非常感谢你。你的回答以后可能会给我省去很多麻烦。我根本不知道类属性的工作方式。