title: How to increase points when a rect collides with another rect and the left mouse button is clicked?
body: <p>So I am making a game where the player has to click  on a moving rect (a tennis ball) to get points. So far I have created the tennis ball and some other balls, and I have got them moving randomly round the screen and bouncing off the edges. I have also made a rect called target which moves with the mouse. I would like the game to add 10 points every time the player clicks on the tennis ball (with the target) and deduct a life if they click elsewhere on the screen. Any help would be greatly appreciated!
  Thanks :)</p>
  
  <pre><code>import pygame
  import sys
  
  from random import *
  from pygame.locals import *
  
  
  #game        
  pygame.init()
  running=True
  screen_width=800
  screen_height=600
  screen=pygame.display.set_mode([screen_width,screen_height])
  background_colour = (0,0,150)
  screen.fill(background_colour)
  
  clock=pygame.time.Clock()
  game_sound='pumpup.wav'
  pygame.mixer.init()
  points=0
  lives=3
  
  #balls
  x= randint(20,40)
  y= randint(20,40)
  ball=pygame.image.load ('ball_t.png')
  ball_movement=[x,y]#amount ball moves each time (x,y)
  ball_location=[100,100]#x,y co-ordinates for start position
  ball_rect=ball.get_rect()#gets rectangle for ball
  
  x2= randint(5,15)
  y2= randint(5,15)
  
  ball2_movement=[x2,y2]#amount ball moves each time (x,y)
  ball2_location=[10,10]#x,y co-ordinates for start position
  ball2=pygame.image.load ('ball_tt.png')
  ball2_rect=ball2.get_rect()#gets rectangle for ball
  
  x3= randint(10,40)
  y3= randint(10,30)
  
  ball3_movement=[x3,y3]#amount ball moves each time (x,y)
  ball3_location=[10,100]#x,y co-ordinates for start position
  ball3=pygame.image.load ('ball_b.png')
  ball3_rect=ball3.get_rect()#gets rectangle for ball
  
  x4= randint(10,30)
  y4= randint(10,30)
  
  ball4_movement=[x4,y4]#amount ball moves each time (x,y)
  ball4_location=[200,100]#x,y co-ordinates for start position
  ball4=pygame.image.load ('ball_bs.png')
  ball4_rect=ball4.get_rect()#gets rectangle for ball
  
  #target
  target_location=[200,100]#x,y co-ordinates for start position
  target=pygame.image.load ('Target.png')
  target_rect=target.get_rect()#gets rectangle for ball
  target_rect.center=target_location
  
  #score
  font=pygame.font.Font(None, 50)#default font
  score="Score: {}".format(points)
  score_text=font.render(score,1,(0,200,250))
  text_pos=[10,10] #position on screen
  
  #lives
  font=pygame.font.Font(None, 50)#default font
  livest="Lives: {}".format(lives)
  lives_text=font.render(livest,1,(0,200,250))
  textl_pos=[650,10]#position on screen
  
  
  
  
  #show initial screen
  screen.blit(ball, ball_rect)#draws ball on its rectangle
  screen.blit(ball2, ball2_rect)
  screen.blit(ball3, ball3_rect)
  screen.blit(ball4, ball4_rect)
  screen.blit(target, target_rect)
  screen.blit(score_text, text_pos)
  screen.blit(lives_text, textl_pos)
  
  pygame.display.flip() #displays screen
  
  
  while running: #event loop
      clock.tick(50) #won't run at more than 60 frames per second
      screen.fill([0,0,150])#clears screen
  
      for event in pygame.event.get():
          if event.type==pygame.QUIT:
              running=False
          elif event.type==pygame.MOUSEMOTION:
          target_rect.centerx=event.pos[0] #moves target with mouse
          target_rect.centery=event.pos[1]
          if event.type == pygame.MOUSEBUTTONDOWN and target_rect.colliderect(ball2_rect):
         points=100 
  
  
  
  
      if ball_rect.left&lt;0 or ball_rect.right&gt;screen_width: #check whether ball is off screen (x)
          ball_movement[0]=-ball_movement[0]#change direction on x axis
      if ball_rect.top&lt;0 or ball_rect.bottom&gt;screen_height: #check whether ball is off screen (y)
          ball_movement[1]=-ball_movement[1] #change direction on y axis
  
      if ball2_rect.left&lt;0 or ball2_rect.right&gt;screen_width: #check whether ball is off screen (x)
          ball2_movement[0]=-ball2_movement[0]#change direction on x axis
      if ball2_rect.top&lt;0 or ball2_rect.bottom&gt;screen_height: #check whether ball is off screen (y)
          ball2_movement[1]=-ball2_movement[1] #change direction on y axis
  
      if ball3_rect.left&lt;0 or ball3_rect.right&gt;screen_width: #check whether ball is off screen (x)
          ball3_movement[0]=-ball3_movement[0]
      if ball3_rect.top&lt;0 or ball3_rect.bottom&gt;screen_height: #check whether ball is off screen (y)
          ball3_movement[1]=-ball3_movement[1] #change direction on y axis
  
      if ball4_rect.left&lt;0 or ball4_rect.right&gt;screen_width: #check whether ball is off screen (x)
          ball4_movement[0]=-ball4_movement[0]
      if ball4_rect.top&lt;0 or ball4_rect.bottom&gt;screen_height: #check whether ball is off screen (y)
          ball4_movement[1]=-ball4_movement[1] #change direction on y axis
  
  
  
      if ball_rect.top &gt; screen_height and lives &gt;0:
          lives=lives-1
          livest="Lives: {}".format(lives)
          lives_text=font.render(livest,1,(0,0,0))
          pygame.time.delay(500)#pause
          ball_rect.topleft=[10,0] #reinstate ball
      if lives ==0:
          pygame.display.flip()
          pygame.time.delay(2500)#pause
          running=False
  
  
  
  
  
      ball_rect=ball_rect.move(ball_movement)#move ball
      ball2_rect=ball2_rect.move(ball2_movement)
      ball3_rect=ball3_rect.move(ball3_movement)
      ball4_rect=ball4_rect.move(ball4_movement)
      screen.blit(ball, ball_rect)#redraws ball
      screen.blit(ball2, ball2_rect)
      screen.blit(ball3, ball3_rect)
      screen.blit(ball4, ball4_rect)
      screen.blit(target, target_rect)
      screen.blit(score_text, text_pos)
      screen.blit(lives_text, textl_pos)
  
  
      pygame.display.flip() #displays screen
  
  pygame.quit() #quits game
  </code></pre>