pygame代码中的逻辑错误,旨在改变屏幕上blitted的圆列表的颜色

时间:2018-02-15 00:12:16

标签: python pygame

我使用pygame创建了一小段代码,用于在屏幕上生成一定数量的“雪花”,并在每个代码到达结尾之前将其中的每一个从白色变为黄色。屏幕并重置。

该程序生成一个包含40个雪花的列表,并为每个雪花分配一个x,y和颜色变量(包含R,G和B值)。

问题是当i -0.1到每个雪花的特定R,G或B值时,它会影响列表中的每个元素,而不仅仅是特定的雪花i

因此,每个雪花的颜色与其余雪花同步,实际上我希望它对每个雪花元素都是唯一的。

我做错了什么,我该如何解决?

可以看到完整的代码here

    import pygame, sys, random, time #import modules
pygame.init()
screen = pygame.display.set_mode((800, 600))

BLACK = (0,0,0)
pygame.display.set_caption("Snow Animation")
clock = pygame.time.Clock()


class Animation():
    def __init__(self):
        self.snow_list = [] #create snow list (list of circles to blit across screen)
        self.R = 200 #RGB values for each snow 
        self.G = 200
        self.B = 200
        self.colour  = [self.R,self.G,self.B] #combines values together in list
        for i in range(20): #Creates 40 snow items, 20 from x axis, 20 from y axis
            self.x1 = random.randint(-1000, 0)
            self.y1 = random.randint(0, 600)
            self.snow_list.append([self.x1, self.y1, self.colour]) #2D list. Gives each snow element an x,y value and assigns the colour variable
            self.x2 = random.randint(0, 800)
            self.y2 = random.randint(-1000, 0)
            self.snow_list.append([self.x2, self.y2, self.colour])



    def snow(self):
        for i in range(len(self.snow_list)):
            pygame.draw.circle(screen, (self.snow_list[i][2]), (self.snow_list[i][0],self.snow_list[i][1]), 5) #draw circles
            # Move the snow flake down and across one pixel 
            self.snow_list[i][1] += 1 
            self.snow_list[i][0] += 1
            self.snow_list[i][2][2] = self.snow_list[i][2][2] - 0.1 #-0.1 to self.G in self.colour variable for each snow_list element
            # If the snow flake has moved off the bottom of the screen
            if self.snow_list[i][1] > 600:
                # Reset it just above the top
                self.snow_list[i][2][2] = 200 #reset colour variable for each snow element
                self.new_pos1 = random.randint(-50, -10)#gives it new random position at these coordinates before y=0 axis
                self.snow_list[i][1] = self.new_pos1
                self.y_change = random.randint(0, 800) #sets random variable along y axis
                self.snow_list[i][0] = self.y_change
                # Give it a new x position
            if self.snow_list[i][0] > 800: #does same as the 6 lines above but for the x axis snow flakes
                self.snow_list[i][2][2] = 200
                self.new_pos2 = random.randint(-50, -10)
                self.snow_list[i][0] = self.new_pos2
                self.x_change = random.randint(0, 600)
                self.snow_list[i][1] = self.x_change
                print (self.snow_list[i][2][2])




        pygame.display.flip() #update display
        clock.tick(30) #clock tick

Snowing = Animation() #create class instance
while True:
    screen.fill(BLACK) #fill screen black before snowflakes are blit
    Snowing.snow() #run snow module
    for event in pygame.event.get():   # User does something
        if event.type == pygame.QUIT:  #if user quits
            pygame.quit(), sys.exit() #quit

我很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为您的问题出在54行。

如果我理解正确,您希望每个雪花独立更新它的颜色。因此,您应该更新 for循环中的显示

54应缩进一次,以便在for循环的每次迭代中更新颜色。

希望这有帮助!