Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:56:29+00:00 2026-06-12T06:56:29+00:00

I’m writing a simple toy in Pygame. When you press a key on the

  • 0

I’m writing a simple toy in Pygame. When you press a key on the home row, it does a little burst of particles.

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

The line you’re looking for is in Burst.draw. For some reason, only the uncommented one works correctly. The other two lines, which should be the same as far as I can tell, only draw the first burst’s particles correctly. Any subsequent bursts change all particles onscreen to match their colour.

I can provide more code, but there’s not much more to it. Basically keypresses add Bursts to an array, and every tick I step through that array calling update() and draw().

Does anyone know what I did wrong, and then accidentally fixed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:56:30+00:00Added an answer on June 12, 2026 at 6:56 am

    Because all particles in the screen belong to the same collection Burst.particles.
    And every time you process a Burst you are processing all the particles, and all gets painted with the last colour.

    Just move the initialization particles = [] to the init method.

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

    Update

    You are using a Java/C# style of coding classes. You shouldn’t put any of the initializations at the class level, unless they are constants or class attributes.

    IE:

    class Burst():
    
        class_attribute = 0       # declaration of class (static) attribute
    
        def __init__(self, ...):
            self.attribute = 0    # declaration of object (regular) attribute
    

    You shouldn’t make class declarations of attribute you will use a object attributes.
    Just remove all the declarations previous to the init method in both classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I am writing an app with both english and french support. The app requests
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.