1

I'm having trouble with making multiple lines draw to the screen when using a class. I don't want to have to make several class instances of the same thing, just want to be able to append it to a list and have it fall. I've only managed to make one appear on the screen and that is all. Any help would be appreciated, thx.

import pygame
import random

#Classes
class Drop:
    def __init__(self, window, color, x, y,speed):
        self.x = x
        self.y = y
        self.speed = speed
        self.window = window
        self.color = color
        
    def fall(self):
        self.y = self.y + self.speed
        pygame.draw.line(self.window , self.color ,[ self.x , self.y],[self.x, self.y + 30], 2)
        
        


#functions

        
#Program Loop
def main():
    
    gameExit = True
    
#Making the raindrops
    for i in range(500):
        x = random.randint(10,500)
        y = 1
        uno = Drop(window , purple,x,y,speed)  
        rain.append(uno)
        

        
    while gameExit != False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = False

        window.fill(white)
        
        uno.fall()
            
        pygame.display.update()
     

#Variables
DISPLAY_HEIGHT = 500
DISPLAY_WIDTH = 800




rain = []

speed = 0.5

purple = (128,0,128)
white = (255,255,255)



#Game init

pygame.init()
window = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption("Purple Rain")


main()

pygame.quit()

1 Answer 1

1

This line

uno.fall()

should be in the for-loop. Outside it will refer to the last drop -> You see only one

Right code:

window.fill(white) # screen-cleaning should be done first
for i in range(500):
    x = random.randint(10,500)
    y = 1
    uno = Drop(window , purple,x,y,speed)  
    rain.append(uno) 
    uno.fall() # changed
Sign up to request clarification or add additional context in comments.

2 Comments

I changed it over and even the last drop never appeared.
Thanks they all appear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.