5. Sounds and Sprites

# set up the block data structure
player = pygame.Rect(300,100,40,40)
playerImage = pygame.image.load('player.png')
plyaerStretchedImage = pygame.transform.scale(playerImage(40,40))
foodImage = pygame.image.load('cherry.png')
foods = []
for i in range(20):
   foods.append(pygame.Rect(random.randint(0,WINDOWWIDTH - 20),random.randint(0,WINDOWHEIGHT - 20),20,20))
# set up music

pickUpSound = pygame.mixer.Sound('pickup.wav')
pygame.mixer.music.load('background.mid')
pygame.mixer.music.play(-1,0.0)
musicPlaying = True
if event.key == ord('m'):
   if musicPlaying:
      pygame.mixer.music.stop()
   else:
      pygame.mixer.music.play(-1,0,0)
   musicPlaying = not musicPlaying

if event.type == MOUSEBUTTONUP:
   foods.append(pygame.Rect(event.pos[0]-10,event.pos[1] - 10,20,20))
foodCounter += 1
if foodCounter >= NEWFOOD:
   #add new food
   foodCounter = 0
   foods.append(pygame.Rect(random.randint(0,WINDOWWIDTH-20),random.randint(0,WINDOWHEIGHT - 20),20,20))
# check if the block has intersected with any food squares.
for food in foods[:]:
   if player.colliderect(food):
      foods.remove(food)
      player = pygame.Rect(player.left,player.top,player.right,player.down)
      playerStretchedImage = pygame.transform.scale(player.left,player.top,player.right,player.down)
      if musicPlaying:
         pickUpSound.play()

# draw the food
for food in foods:
   windowSurface.blit(foodImage,food)
if baddieAddCounter == ADDNEWBADDIERATE:
   baddieAddCounter = 0
   baddieSize = random.randint(BADDIEMINSIZE,BADDIEMAXSIZE)
   newBaddie = {'rect':pygame.Rect(random.randint(0,WINDOWWIDTH-baddieSize),0-baddieSize,baddieSize,baddieSize),'speed':random.randint(BADDIEMINSPEED,BADDIEMAXSPEED),'surface':pygame.transform.scale(baddieImage,(baddieSize,baddieSize))}

   baddies.append(newBaddie)
You have attempted of activities on this page