Particle Simulator

Well I got it under control. The reason it was all crazy acting, is because of the collision detection I had going, which is shown in the code at the bottom of this post. Check out the short video here

The problem was the I was testing the instances rect against all the rects on the screen, including itself, so it always registered a collision, even if it was alone, which was how I managed to debug it. Pretty strange out bugs turn out to be simple in that they’re one-line changes, but the behaviour can change significantly.

I really like the list comprehensions, now that I have a better grasp on the concept. Here’s the comprehension if you have a hard time finding it:

 #a new list without self in it:
listWithoutSelf = [i for i in listParticlesRect if i != self.blitted]


Pretty much was it does is what the line of code says actually, which is pretty snazzy: i here is the item in the list so it reads as: The new list called ‘listWithoutSelf’ contains every item in the older list ‘listParticlesRect’ so long as the item isn’t ‘self.blitted’ which in my code is the rect of the sprite on the screen that gets returned when you blit one surface onto another.

Anyways, in other news, what I’m trying to work on now is a essentially a clipboard catcher, that stores all the things you copy onto the windows clipboard. I’m aware that other programs to do this, and even would be possible within Tkinter and wxPython, but I’m not sure that it can be done with those two if the windows don’t have focus, since I’m looking to have the python script run in the background. If you have any ideas, please let me know either in the comment section, or the reddit post I’ve created!

#----------------------------------------------------------------------
    def move(self):
        """move via: pos+ dir*spd"""
        
        x, y = self.pos
        dx, dy = self.direction
        spd = self.speed
        
        #write temp vars then test  wall
        wallBounce = False
        #check for walls:
        if 0 > x or x > WIDTH:
            dx = dx * -1
            #print 'x is out', x
            wallBounce = True
            #if dx2 > 0:
                #dx2 += 1
            #elif 0 < dx2:
                #dx2 += -1
                
            #else:
                #print dx2, 'x probably equals 0'
            
        if 0 > y or y > HEIGHT:
            dy = dy * -1
            
            #print 'y is out', y
            wallBounce = True
            #if dx2 > 0:
                #dy2 += 1
            #elif 0 < dy2:
                #dy2 += -1
                
            #else:
                #print dy2, 'y probably equals 0'            

        #check for particle collision        
        if not  wallBounce :
                
            #a new list without self in it:
            listWithoutSelf = [i for i in listParticlesRect if i != self.blitted]
            
            index = self.blitted.collidelist(listWithoutSelf)    
            if index != -1:
                print 'there has been a collision, searching', index
                other = listParticles[index]
                
                #values for the distance between two points
                left = abs(other.rect.left - self.rect.right)
                right = abs(other.rect.right - self.rect.left)
                top = abs(other.rect.top - self.rect.bottom)
                bottom = abs(other.rect.bottom - self.rect.top)
                
                ox, oy = dx, dy
                #dx, dy = self.direction
                #find the lowest value
                if left == min(left, right, top, bottom):
                    dx, dy = -abs(dx), dy
                    print 'some sorta collision left'
                
                elif right == min(left, right, top, bottom):
                    dx, dy = abs(dx), dy
                    print 'some sorta collision right'
                    
                elif top == min(left, right, top, bottom):
                    dx, dy = dx, -abs(dy)
                    print 'some sorta collision top'
                    
                elif bottom == min(left, right, top, bottom):
                    dx, dy = dx, abs(dy)
                    print 'some sorta collision bottom'
                    
                #check if different:
                if ox != dx or oy != dy:
                    print 'there"s a difference'
                    
                
            else:
                #print 'there was no collision found'
                pass
        x += (dx * spd)
        y += (dy * spd)
          
        
        self.direction = dx, dy
        
        self.pos = x, y

Leave a Reply

Your email address will not be published.