screen.blit(self.img, (self.x – self.img.get_width()/2,self.y – self.img.get_height()/2)); # Blit image onto the display surface
pygame.draw.rect(screen,(255,255,255),(self.x – self.img.get_width()/2,self.y – self.img.get_height()/2,self.img.get_width(),self.img.get_height()),1)
the exact point of collision. If greater accuracy is required (such as in a physics simulation) a third test is generally used:
always keep the same orientation (they are not rotated), the bounding
box/circle method can give us a good approximation of the angle of
self.surf1 = pygame.Surface((2*self.radius, 2*self.radius)); #create a new surface the size of the particle
self.surf1.set_colorkey(BACKGROUND_COLOUR); #set the background colour to be transparent
self.surf1.set_alpha(100); #set the transparency to 100/255
pygame.draw.circle(self.surf1, (255, 0, 0),(self.radius, self.radius), self.radius); #draw the circle to the transparent surface
s = pygame.Surface((1000,750), pygame.SRCALPHA) # per-pixel alpha s.fill((255,255,255,128)) # notice the alpha value in the color windowSurface.blit(s, (0,0))
We can then check for a collision in the particles move() function. If a collision occurs, I have chosen to change to colour of the drawn circle:
self.colour = GREEN;
self.colour = RED;
After commenting out the bounding boxes, we get the following:
We will now look at circular collisions between the bubbles and get them to bounce off each other. There will be some small differences in the way we structure this code as we are looking for collisions between instances of the same object.
There is no particular function for collisions between circles. The code is relatively simple; all we need to do is check if the distance between the circle centers is less than the sum of their radii.
<this section follows closely to this>
Apparently, “it’s mathematically impossible
to solve the equations describing three or more interacting objects.” We’ll have to make some “simplifications, which we make
it ‘inaccurate’, however, it should still represent a reasonable
approximation to reality.”
The first thing we will do is add where to check for a collision. We will do this in the current for loop, but will enumerate the loop effectively giving the particles an index number:
for particle2 in particles[i+1:]:
Note that the nested for loop only searches the remaining particles in the list. This is because the first bubble is compared to all the others, when we compare the second, there is no need to compare it to the first again.
We can now write the circular_collision function, I will change the colour of the particles when they collide (you will need to comment out the else the resets the colour in the player-particle collision):
if (math.sqrt((p1.x – p2.x)**2 + (p1.y – p2.y)**2) <= (p1.radius + p2.radius)):
p1.colour = BLUE;
p2.colour = BLUE;
When you run the program now, the red outlines should change to blue when the particles collide, and change to green when they collide with the player.
Transparent surfaces: http://stackoverflow.com/questions/6339057/draw-a-transparent-rectangle-in-pygame