Creating multiple class objects with a for loop in python

KingVarric

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()
Just learned it

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating multiple class objects using a loop

From Dev

Creating multiple objects in loop

From Java

How to create multiple class objects with a loop in python?

From Dev

Creating objects in a class in Python

From Java

Creating multiple objects using for loop

From Dev

for loop not creating multiple ggplot objects

From Dev

Creating a list of objects with a class in Python

From Dev

How can I define multiple class objects with a for loop in python?

From Dev

Creating a for loop to create multiple objects to add to a graph

From Dev

creating multiple columns in a for loop python

From Dev

Vector Isn't Creating Multiple Class Objects

From Dev

creating multiple instances of a class in Python

From Dev

Creating objects in a loop Python -- puzzling behavior

From Dev

Creating Set of objects of user defined class in python

From Dev

Creating a vector of all instances of a class when objects are being instantiated in a loop?

From Dev

Problem in creating multiple json files in python for loop

From Dev

Creating multiple lists in for loop with dynamic names in Python

From Dev

input buffer while creating multiple objects using for loop in java

From Dev

Python creating static objects which are shared amongst all class objects

From Dev

Creating multiple buttons in tkinter in a class - python 3

From Dev

creating multiple python class instance from json

From Java

Using a for loop to run class methods from multiple class objects in Java

From Dev

How to add Objects to Class with a loop in Python?

From Dev

Python - Loop though dataframe and create class objects

From Dev

creating a list of class objects

From Dev

Creating a Class and Objects

From Dev

Creating multiple arrays of objects

From Dev

Creating objects with multiple classes

From Dev

creating multiple objects graphics

Related Related

  1. 1

    Creating multiple class objects using a loop

  2. 2

    Creating multiple objects in loop

  3. 3

    How to create multiple class objects with a loop in python?

  4. 4

    Creating objects in a class in Python

  5. 5

    Creating multiple objects using for loop

  6. 6

    for loop not creating multiple ggplot objects

  7. 7

    Creating a list of objects with a class in Python

  8. 8

    How can I define multiple class objects with a for loop in python?

  9. 9

    Creating a for loop to create multiple objects to add to a graph

  10. 10

    creating multiple columns in a for loop python

  11. 11

    Vector Isn't Creating Multiple Class Objects

  12. 12

    creating multiple instances of a class in Python

  13. 13

    Creating objects in a loop Python -- puzzling behavior

  14. 14

    Creating Set of objects of user defined class in python

  15. 15

    Creating a vector of all instances of a class when objects are being instantiated in a loop?

  16. 16

    Problem in creating multiple json files in python for loop

  17. 17

    Creating multiple lists in for loop with dynamic names in Python

  18. 18

    input buffer while creating multiple objects using for loop in java

  19. 19

    Python creating static objects which are shared amongst all class objects

  20. 20

    Creating multiple buttons in tkinter in a class - python 3

  21. 21

    creating multiple python class instance from json

  22. 22

    Using a for loop to run class methods from multiple class objects in Java

  23. 23

    How to add Objects to Class with a loop in Python?

  24. 24

    Python - Loop though dataframe and create class objects

  25. 25

    creating a list of class objects

  26. 26

    Creating a Class and Objects

  27. 27

    Creating multiple arrays of objects

  28. 28

    Creating objects with multiple classes

  29. 29

    creating multiple objects graphics

HotTag

Archive