# breakout program
# 2010-06-13 j.m.reneau

# sounds click.mp3, toggle.mp3, and beep.mp3
# Licensed under CC-BY 3.0
# Authored by Mike Koenig

print "use mouse to bounce ball on paddle."

color black
rect 0,0,300,300

blocks = 24 # number of blocks
dx = 1	# initial ball speed
dy = 1
speedup = .2 # how much faster after each paddle strike

ball = blocks + 1 # sprite numbers for ball and paddle
paddle = blocks

spritedim blocks + 2

spriteload ball, "ball.png"
spriteplace ball,100,100
spriteshow ball
spriteload paddle, "paddle.png"
spriteplace paddle,150,270
spriteshow paddle

for block = 0 to blocks - 1
   col = block % 6 * 50 + 25
   row = block \ 6 * 10 + 5
   spriteload block, "block.png"
   spriteplace block, col, row
   spriteshow block
next block

blocksleft = blocks

while spritey(ball) < 295 and blocksleft > 0
   spriteplace paddle, mousex, spritey(paddle)
   if spritex(ball) < 5 or spritex(ball) > 295 then
      dx = dx * -1
      wavplay "click.mp3"
   end if
   if spritey(ball) < 5 then
      dy = dy * -1
      wavplay "toggle.mp3"
   end if
   if spritecollide(ball, paddle) then
      # bounce back and speed up the falling a bit
      dy = (dy + speedup) * -1
      dx = dx + ((spritex(ball)-spritex(paddle))/(spritew(paddle/2)))
      wavplay "beep.mp3"
   end if
   spritemove ball, dx, dy
   
   blocksleft = 0
   for block = 0 to blocks - 1
      if spritev(block) then
         blocksleft = blocksleft + 1
         if spritecollide(block, ball) then
            # hide blocks that the ball hit and send the ball bouncing back
            spritehide(block)
            dx = dx * -1
            dy = dy * -1
            wavplay "beep.mp3"
         end if
      end if
   next block
   
end while

print "game over."