title: How to update player sprite upon collision - Pygame
body: <p>Alright so currently when my player collides with an enemy (mob) the health gets reducted by 10 and a sound plays.  Now I want to change the sprite in the same for loop but it doesn't seem to be working the way I want it to.
  Here's the part where I try to change the sprite: (player sprite is already defined btw)</p>
  
  <pre><code>collided_mobs = pygame.sprite.spritecollide(player, mobs, True)
      for mob_sprite in collided_mobs:
  
          playersprite == pygame.image.load("triangleDMG.png")
  
          player_health = player_health - 10
          pygame.mixer.Sound.play(HITSOUND)
  
          m = Mob()
          sprites.add(m)
          mobs.add(m)
  </code></pre>
  
  <p>And here is the full code, it's very messy currently, sorry about that.:</p>
  
  <pre><code>from pygame import *
  import random as rand
  from colors import *
  import os
  import time
  from timeit import default_timer as timer
  # START TIMER
  start = timer()
  
  pygame.mixer.pre_init(44100, 16, 2, 4096)
  pygame.init()
  
  fps = 60
  window_size = window_width, window_height = 800, 600
  
  font = pygame.font.SysFont( "Power Green, Times New Roman, Arial", 25  )
  font2 = pygame.font.SysFont( "Power Green, Times New Roman, Arial", 100  )
  
  #all sprites load
  playersprite = pygame.image.load( "triangle.png" )
  playerspritedmg = pygame.image.load("triangleDMG.png")
  lifeimg = pygame.image.load("health.png")
  #bulletsprite = pygame.image.load(nothingyet)
  '''
  circle_sprites = []
  circle_list = ["orange_circle.png", "purple_circle.png", "red_circle.png"]
  
  for img in circle_list:
      circle_sprites.append(pygame.image.load(img))
      '''
  
  
  
  zap = pygame.mixer.Sound("zap.wav")
  GAMEOVER = pygame.mixer.Sound("gameover.wav")
  secret = pygame.mixer.Sound("secret.wav")
  HITSOUND = pygame.mixer.Sound("jab.wav")
  pew = pygame.mixer.Sound("pew.wav")
  
  overmsg = font2.render( 'GAME OVER', True, white )
  
  score = 0
  
  pygame.mixer.music.load('music.wav')
  pygame.mixer.music.play(-1)
  
  explode = pygame.mixer.Sound("explode.wav")
  
  
  class Player(pygame.sprite.Sprite):
      #sprite for player
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image = (playersprite)
          self.rect = self.image.get_rect()
          self.setprop()
          self.xspeed = 0
          self.delay = 100
          self.last_shot = pygame.time.get_ticks()
  
  
  
  
      def setprop( self ):
  
          self.rect = self.image.get_rect()
  
          self.orgin_x = self.rect.centerx
          self.orgin_y = self.rect.centery
  
      def set_position(self, x, y):
          self.rect.x = x - self.orgin_x
          self.rect.y = y - self.orgin_y
  
      def update(self):
          self.xspeed = 0
          keystate = pygame.key.get_pressed()
          '''
          keystate = pygame.key.get_pressed()
          if keystate[pygame.K_LEFT]:
              #speed left
              self.xspeed = -9
          if keystate[pygame.K_RIGHT]:
              #speed right
              self.xspeed = 9
          self.rect.x += self.xspeed
          '''
          if keystate[pygame.MOUSEBUTTONDOWN]:
              self.shoot()
  
          if self.rect.right &gt; window_width:
              self.rect.right = window_width
  
          if self.rect.left &lt; 0:
              self.rect.left = 0
  
      def shoot(self):
          now = pygame.time.get_ticks()
          if now - self.last_shot &gt; self.delay:
              self.last_shot = now
              bullet = Bullet(self.rect.centerx, self.rect.top)
              sprites.add(bullet)
              bullets.add(bullet)
  
  class Mob(pygame.sprite.Sprite):
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image  = pygame.image.load('circle.png')
          self.rect   = self.image.get_rect()
          self.rect.x = rand.randrange(window_width - self.rect.width)
          self.rect.y = rand.randrange( -100, -40 )
          self.speedy  = rand.randrange(6, 8)
          self.speedx = rand.randrange( -3, 3 )
  
      def play_sound(self):
          self.sound.play()
  
      def update(self):
          self.rect.x += self.speedx
          self.rect.y += self.speedy
          if self.rect.top &gt; window_height + 10 or self.rect.left &lt; -20 or self.rect.right &gt; window_width + 20:
              self.rect.x = rand.randrange(window_width - self.rect.width)
              self.rect.y = rand.randrange( -100, -40 )
              #enemy speed range
              self.speedy  = rand.randrange(6, 10 )
  
  
  
  class Bullet(pygame.sprite.Sprite):
      def __init__(self, x, y):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.Surface((10, 20))
          self.image.fill(red)
          self.rect = self.image.get_rect()
          self.rect.bottom = y
          self.rect.centerx = x
          self.speedy = -10
  
      def update(self):
          self.rect.y += self.speedy
          if self.rect.bottom &gt; 0:
              self.kill
  
  class Pow(pygame.sprite.Sprite):
      def __init__(self, center):
          pygame.sprite.Sprite.__init__(self)
          self.image = lifeimg
          self.rect = self.image.get_rect()
          self.rect.center = center
          self.speedy = 5
  
      def update(self):
          self.rect.y += self.speedy
          if self.rect.top &gt; window_height:
              self.kill
  
  
  def health_bars(player_health):
      if player_health &gt; 75:
          player_health_color = green
      elif player_health &gt; 50: 
          player_health_color = yellow
      else:
          player_health_color = red
  
      pygame.draw.rect(window, player_health_color, (20,20, player_health, 25))
  
  def set_message(text):
      global message, previous_message
      message = font.render( 'HEALTH:' + str(player_health), True, white )
      previous_message = message
  
  def set_score(text):
      global scoremsg
      scoremsg = font.render( 'SCORE:' + str(score), True, white )
  
  
  
  
  
  
  
  
  #SPRITES
  sprites = pygame.sprite.Group()
  mobs = pygame.sprite.Group()
  bullets = pygame.sprite.Group()
  player = Player()
  sprites.add(player)
  powerups = pygame.sprite.Group()
  for i in range(8):
      m = Mob()
      sprites.add(m)
      mobs.add(m)
  player.set_position( window_width/2, window_height/2 +215 )       
  
  
  #main variables so window is a thing
  window = pygame.display.set_mode( window_size, pygame.RESIZABLE )
  pygame.display.set_caption('Dodge the boulders or shoot them (shooting gets you score)')
  clock = pygame.time.Clock()
  
  
  player_health = 100
  running = True
  
  #game loop
  while ( running ):
      for event in pygame.event.get():
          if( event.type == pygame.QUIT ):
              running = False
          if( event.type == pygame.MOUSEMOTION ):
              mpos = pygame.mouse.get_pos()
              player.set_position(mpos[0], mpos[1])
          if( event.type == pygame.MOUSEBUTTONDOWN ):
              player.shoot()
              pygame.mixer.Sound.play(pew)
              print('pew')
  
      clock.tick( fps )
      set_message( player_health)
      set_score(set_score)
  
  
      window.fill(black)
      sprites.update()
      health_bars(player_health)
  
      #collide group with group
      hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
      if hits:
          pygame.mixer.Sound.play(zap)
          score += 1
          if rand.random() &gt; 0.9:
              pow = Pow(hit.rect.center)
              sprites.add(pow)
              powerups.add(pow)
  
      for hit in hits:
          m = Mob()
          sprites.add(m)
          mobs.add(m)
  
      #sprite with powerup
      hits = pygame.sprite.spritecollide(player, powerups, True)
      for hit in hits:
          player_health = player_health + 5
  
  
  
  
      #sprite with group
      collided_mobs = pygame.sprite.spritecollide(player, mobs, True)
      for mob_sprite in collided_mobs:
  
          playersprite == pygame.image.load("triangleDMG.png")
  
          player_health = player_health - 10
          pygame.mixer.Sound.play(HITSOUND)
  
          m = Mob()
          sprites.add(m)
          mobs.add(m)
  
  
  
      sprites.draw(window)
      window.blit( message, (20, 50) )
      window.blit( scoremsg, (window_width/2 + 310, window_height - 35) )
  
      #blits how long you lasted for
      if (player_health == 0):
          pygame.mixer.Sound.play(explode)
          pygame.mixer.music.stop()     
          pygame.mixer.Sound.play(GAMEOVER)
          #need the -1 so timeset wont go up until you get hit again
          player_health = player_health - 1
          elapsed_time = timer() - start # in seconds
          print ("You lasted " + (str(elapsed_time)) + " seconds!")
          timeset = font.render( "You lasted " + (str(elapsed_time)) + " seconds!", True, white )
          window.blit(timeset, (window_width / 2 - 160, window_height / 2 + 100))
  
      if (player_health == (-5)):
          pygame.mixer.Sound.play(explode)
          pygame.mixer.music.stop()     
          pygame.mixer.Sound.play(GAMEOVER)
          elapsed_time = timer() - start # in seconds
          timeset = font.render( "You lasted " + (str(elapsed_time)) + " seconds!", True, white )
  
      if (player_health &lt;= 0):
  
  
  
          window.blit(overmsg, (window_width / 2 - 160, window_height / 2 -75))
          #timer msg
          window.blit(timeset, (window_width / 2 - 170, window_height / 2 + 100))
  
  
          '''SECRET
          if (player_health &lt;= (-1000)):
  
              pygame.mixer.Sound.play(secret)
  
              if (player_health &lt;= (-1150)):
                  running = False
                  '''
  
  
  
      pygame.display.update()
  
  
  pygame.display.quit()
  pygame.quit()
  </code></pre>