Collision Between two sprites - Python 3.5.2
<p>I have an image of a ufo and a missile. I'm trying to get it to where if the missile hits the ufo they both would explode and disappear and then a few moments later another ufo would respawn but the collision code isn't working. can someone explain to me how to make the code work?</p>

<pre><code>pygame.display.init()
pygame.font.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
ufo = pygame.image.load("ufo.png")
rocket = pygame.image.load("rocket.png")
done = False
debug = False
fontObj = pygame.font.SysFont("Courier New", 20)

#Making my Empty Lists
missiles = [] #[x,y]
ufo_list = [] #[x,y,hspeed]
particle_list = []

#UFO Respawn Info
ufoRespawn = True
ufoHits = 0
ufoSpawnTimer = 0.0
ufoSpeed = 500.0

#MISSILE Info
launchX = 400
launchY = 550
missileSpeed = 100.0
missileDirection = 0

#creating the Starfield
myStars = []            # An (initially) empty list
for i in range(1000):
    x = random.randint(0, 800)
    y = random.randint(0, 600)
    newStar = [x, y]    # A 2-element list
    myStars.append(newStar)
starSpeed = 100.0       # Rate of star movement (px / s)
starDirection = 0       # 0 = not moving, -1 = left, +1 = right

#input
while not done: 
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        done = True

    keys = pygame.key.get_pressed()
    if keys[pygame.K_ESCAPE]:
        done = True

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_d:
            debug = not debug


    dt = clock.tick() /1000.0

#the missile range (making it disappear after it hits the top)
    for missile in missiles:
        missile[1] -= missileSpeed * dt 
        if missile[1] &lt; 0:    missiles.remove(missle)


#triangle following mouse position
    mx, my = pygame.mouse.get_pos()
    if mx &gt; launchX:
        launchX += .3
    if mx &lt; launchX:
        launchX -= .3


#bullets firing when pressing with mouse
    mbuttons = pygame.mouse.get_pressed()
    if mbuttons [0]:
        x = launchX
        y = launchY
        newMissiles = [x,y]
        missiles.append(newMissiles)

#Creating the UFOs
    ufoSpawnTimer -= dt
    if ufoSpawnTimer &lt;= 0:
        if random.choice (("head", "tail")) == "head":
            x = 0
            hspeed = random.randint (10,50)
        else:
            x = 800
            hspeed = random.randint (-50, -10)
        y = random.randint (0,300)
        new_ufo = [x,y,hspeed]
        ufo_list.append(new_ufo)
        ufoSpawnTimer = 5.0


#Moving the Starfield
    for i in range(len(myStars)):
        myStars[i][0] += starSpeed * dt * starDirection
        if myStars[i][0] &lt; 0:       myStars[i][0] = 800
        if myStars[i][0] &gt; 800:     myStars[i][0] = 0   



    screen.fill ((0,0,0))
#drawing the triangle  a.k.a missle launcher :D
    pygame.draw.polygon(screen, (255,255,255), [[launchX, launchY], [launchX + 10, launchY + 10], \
                        [launchX - 10, launchY + 10]], 3)

    for missile in missiles:
        x = int(missile[0])
        y = int(missile[1])
        screen.blit(rocket, (x,y))


    #drawing the ufo
    for v in ufo_list:
        v[0] += v[2] * dt
        screen.blit(ufo,(v[0],v[1]))


    #Missle distance from UFO  - NEED HELP ON THIS PORTION
    #Hit Detection
    missileDist = ((x - v[0]) ** 2 + (y - v[1]) ** 2) ** 0.5
    if **????** :
        ufoRespawn = True
        ufoHits += 10

    #drawing th starfield
    for star in myStars:
        x = int(star[0])
        y = int(star[1])
        pygame.draw.circle(screen, (255,255,255), (x,y), 2)



    pygame.display.flip()
pygame.font.quit()
pygame.display.quit()
</code></pre>

<p>Well there are many different ways to detect collision, And it might be worth looking at libraries that would do so, but the simplest method by far is to use <code>pygame.sprite.spritecollide()</code>.</p>

<p>But before I can show how to use the function, you need to know what a <code>pygame.sprite.Group()</code> is and what a sprite class is.</p>

<p>Basicly, what a <code>pygame.sprite.Group()</code> is, is a way to keep track of and hold multiple sprites. In your case, it seems making a missile group for your missiles would be the best choice.</p>

<p>So I would create a group to hold your missiles:
<code>missiles_group = pygame.sprite.Group()</code>. You can add missiles to the group by saying <code>missiles_group.add(&lt;sprite instance name&gt;)</code>.</p>

<p>As for the sprite class, please see this answer I gave to a question. To be terse, a Sprite class is a modular way to create a sprite. Instead of using just  a plain image, a sprite class would hold necessary methods and attributes of a sprite. I will be using a sprite class in my example below, so if more detail is needed, please read the answer I linked to above. </p>

<hr>

<p>With that out of the way, and without going into too much detail, here is how you fill in each function parameter to the above function.</p>

<ul>
<li><code>sprite</code>: This is the sprite that will be tested against a group of sprites</li>
<li><code>group</code>: This is the group that will be used to test with the sprite.</li>
<li><code>dokill</code>: This is a boolean value. If set to true, each time the <code>sprite</code> parameter collides with something in the <code>group</code> parameter, and object from the group parameter will be deleted. And visa versa if the <code>dokill</code> argument is set to false.
The is one more parameter that the function takes, but for what you're trying to do, it is not needed.</li>
</ul>

<p>Incorporating the above information, here is an example. The example creates a sprite and a list of sprites. Each time the sprite collides with a sprite from the group, <code>HIT</code> is printed to the screen:</p>

<pre><code>import pygame #import the pygame module into the namespace &lt;module&gt;

WIDTH = 640 # define a constant width for our window
HEIGHT = 480 # define a constant height for our window

#create a pygame window, and
#initialize it with our WIDTH and HEIGHT constants
display = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock() # create a game clock

class Sprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((20, 20))
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.x = WIDTH / 2
        self.rect.y = HEIGHT / 2
        self.vx = 0
        self.vy = 0

    def update(self):
        self.vx = 0
        self.vy = 0
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.vx = -1
        elif key[pygame.K_RIGHT]:
            self.vx = 1
        if key[pygame.K_UP]:
            self.vy = -1
        elif key[pygame.K_DOWN]:
            self.vy = 1
        self.rect.x += self.vx
        self.rect.y += self.vy

# cretae a player sprite
player = Sprite()

# create a group to hold all of our sprites
sprites = pygame.sprite.Group()

# create a group to hold sprites we want to
# test collions against. These sprites will
# still be added to the sprites list
# but we need a seperate group to test for
# collisions against
collision_sprites = pygame.sprite.Group()

# add a sprite to out collison sprite group
# We also add the sprite to our sprites group
# that holds all sprites
tmp = Sprite()
tmp.update = lambda: None
sprites.add(tmp)
collision_sprites.add(tmp)

# add a player sprites to the player group
player.rect.x = 10
sprites.add(player)


running = True # our variable for controlling our game loop
while running:
    for e in pygame.event.get(): # iterate ofver all the events pygame is tracking
        clock.tick(60) # make our clock keep pour game at 60 FPS
        if e.type == pygame.QUIT: # is the user trying to close the window?
            running = False # if so break the loop
            pygame.quit() # quit the pygame module
            quit() # quit is for IDLE friendliness

    sprites.update()

    # here is where we test for collision
    if pygame.sprite.spritecollide(player, collision_sprites, False):
        print("HIT!")

    display.fill((180, 180, 180)) # fill the pygame screen with white
    sprites.draw(display)
    pygame.display.flip() # update the screen
</code></pre>

<p>My example if fairly big, so take your time and step through it carefully. I tried to add as many good comments as I could. Good luck!</p>

python pygame collision-detection