
' Frogger revisited
' http://www.freebasic.net/forum/
' sounds by Wilfried Nass < wilfried.nass@gmail.com >
' http://www.eschecs.fr/

#include "fbgfx.bi"
using fb

#include "audio.bi"
#include "colors.bi"

screenres 372,416,32,,4

type sprite
  x  as integer
  y  as integer
  dx as integer
  dy as integer
  w  as integer
  h  as integer
  speed as integer
end type

function scollision(s1 as sprite, s2 as sprite) as integer
  dim as integer hit
  hit = 0
  if s1.x >= s2.x and s1.x <= s2.x + s2.w then
    if s1.y >= s2.y and s1.y <= s2.y + s2.h then
      hit = 1
    end if
    if s1.y + s1.h >= s2.y and s1.y + s1.h <= s2.y+s2.h then
      hit = 2
    end if
    if s1.y <= s2.y and s1.y + s1.h >= s2.y + s2.h then 'overlap
      hit = 3
    end if
  end if
  if s1.x + s1.w >= s2.x and s1.x + s1.w <= s2.x + s2.w then
    if s1.y >= s2.y and s1.y <= s2.y + s2.h then
      hit = 4
    end if
    if s1.y + s1.h >= s2.y and s1.y + s1.h <= s2.y+s2.h then
      hit = 5
    end if
    if s1.y <= s2.y and s1.y + s1.h >= s2.y + s2.h then
      hit = 6
    end if
  end if
  return hit
end function

dim shared frog as sprite
dim shared car(1 to 4) as sprite
dim shared logs(1 to 4) as sprite

dim as FB.Image ptr iFrog = imagecreate(21,23,0)
line (0,0)-(20,22), BrightGreen, bf
get (0,0)-(20,22), iFrog

dim as FB.Image ptr iCar = imagecreate(40,20,0)
line (0,0)-(39,19), Navy, bf
get (0,0)-(39,19), iCar

dim as FB.Image ptr iLogs = imagecreate(60,20,0)
line (0,0)-(59,19), Cinnamon, bf
get (0,0)-(59,19), iLogs

line (0,000)-(371,031), InternationalOrange, bf
line (0,032)-(371,063), ForestGreen, bf
line (0,064)-(371,191), MediumSeaGreen, bf
line (0,192)-(371,223), ForestGreen, bf
line (0,224)-(371,351), GrayAsparagus, bf
line (0,352)-(371,383), ForestGreen, bf
line (0,384)-(371,415), InternationalOrange, bf

for i as integer = 0 to 5
  circle (i*62+31,47),12, Maroon,,,,f
next i

line (0,256)-(371,256), Goldenrod
line (0,288)-(371,288), Goldenrod
line (0,320)-(371,320), Goldenrod

color Black
draw string (138,12), "FROGGER GAME"

dim as FB.Image ptr backGround = imagecreate(372,416,0)
get (0,0)-(371,415), backGround

dim as string reply

FB_MCI_open "SoundFiles\Croak.wav",  "C"
FB_MCI_open "SoundFiles\Finish.wav", "F"
FB_MCI_open "SoundFiles\Hop.wav",    "H"
FB_MCI_open "SoundFiles\Start.wav",  "S"

dim as integer nC, nF, nH

do
  
  nC = 0 : nF = 0 : nH =0
  
  FB_MCI("play", "S")
  
  reply = ""
  
  frog.x = 170
  frog.y = 357
  frog.w = 21
  frog.h = 23
  frog.speed = 32
  
  car(1).x = 0
  car(1).y = 230
  car(1).dx = 1
  car(1).dy = 0
  car(1).w = 40
  car(1).h = 20
  
  car(2).x = 300
  car(2).y = 262
  car(2).dx = 1
  car(2).dy = 0
  car(2).w = 40
  car(2).h = 20
  
  car(3).x = 150
  car(3).y = 294
  car(3).dx = 1
  car(3).dy = 0
  car(3).w = 40
  car(3).h = 20
  
  car(4).x = 30
  car(4).y = 326
  car(4).dx = 1
  car(4).dy = 0
  car(4).w = 40
  car(4).h = 20
  
  logs(1).x = 0
  logs(1).y = 72
  logs(1).dx = 1
  logs(1).dy = 0
  logs(1).w = 60
  logs(1).h = 20
  
  logs(2).x = 300
  logs(2).y = 104
  logs(2).dx = 1
  logs(2).dy = 0
  logs(2).w = 60
  logs(2).h = 20
  
  logs(3).x = 150
  logs(3).y = 136
  logs(3).dx = 1
  logs(3).dy = 0
  logs(3).w = 60
  logs(3).h = 20
  
  logs(4).x = 30
  logs(4).y = 168
  logs(4).dx = 1
  logs(4).dy = 0
  logs(4).w = 60
  logs(4).h = 20
  
  dim as integer chosenLog
  chosenLog = 0
  
  dim as double now
  now = 0

  do
    
    screenlock()
    cls
    'draw background
    put (0,0), backGround, pset
    'draw logs under frog
    for i as integer = 1 to 4
      put (logs(i).x, logs(i).y), iLogs, trans
    next i
    'draw dead frog if speed = 0 else draw live frog
    if frog.speed = 0 then
      put (frog.x,frog.y),iFrog, trans
    else
      put (frog.x,frog.y),iFrog, trans
    end if
    'draw cars on top of frog
    for i as integer = 1 to 4
      put (car(i).x, car(i).y ), iCar, trans
    next i
    'messages
    if frog.speed = 0 then
      draw string(150,364), "FROG DEAD"
      if nC = 0 then
        FB_MCI("play", "C")
        nC = 1
      end if
    end if
    if frog.y < 64 then
      draw string(150,364), "FROG HOME"
      if nF = 0 then
        FB_MCI("play", "F")
        nF = 1
      end if
    end if
    
    draw string(12,396), "Hit SPACE to start again ESC to end program"
    
    screenunlock()
    
    'update car positions
    for i as integer = 1 to 4
      car(i).x = car(i).x + car(i).dx
    next i
    'update log positions
    for i as integer = 1 to 4
      logs(i).x = logs(i).x + logs(i).dx
      'update frog.x position if on log
      if chosenLog = i then
        frog.x = frog.x + logs(i).dx
      end if
    next i
    'reverse car directions if out of bounds
    for i as integer = 1 to 4
      if car(i).x < 1 or car(i).x > 330 then car(i).dx = -car(i).dx
    next i
    'reverse logs directions if out of bounds
    for i as integer = 1 to 4
      if logs(i).x < 1 or logs(i).x > 310 then logs(i).dx=-logs(i).dx
    next i
    'user input accepted only every 0.25 seconds - feels like the real thing
    if timer > (now + 0.25) then
      
      if multikey(SC_SPACE) then reply = "y"
      if multikey(SC_ESCAPE) then reply = "x"
      if inkey = chr(255)&"k" then reply = "x"
      
      if frog.y > 63 then  'test frog hasn't reached home
        
        if multikey(SC_DOWN) andalso frog.y < 352 then
          FB_MCI("play", "H")
          frog.y = frog.y + frog.speed
          now = timer
          chosenLog = 0
        end if
        if multikey(SC_UP)  then
          FB_MCI("play", "H")
          frog.y = frog.y - frog.speed
          now = timer   'reset timer
          chosenLog = 0 'if log remove log ID
        end if
        if multikey(SC_RIGHT) andalso frog.x < 302 then
          FB_MCI("play", "H")
          frog.x = frog.x + frog.speed
          now = timer
          chosenLog = 0
        end if
        if multikey(SC_LEFT) andalso  frog.x > 10  then
          FB_MCI("play", "H")
          frog.x = frog.x - frog.speed
          now = timer   
          chosenLog = 0 
        end if
      
      end if' frog.y > 63
      
    end if' timer > (now + 0.25)
    
    dim as integer hit
    
    if frog.y >= 224 and frog.y < 352 then
      
      'test for collision with car
      
      hit = 0 
      for i as integer = 1 to 4
        if hit = 0 then
          hit = scollision(frog, car(i))
        end if
      next i
      if hit <> 0 then
        frog.speed = 0  'flag frog dead
      end if
    
    end if
    
    if frog.y >= 64 and frog.y < 192 then
      
      'test collision with a log
      
      hit = 0
      for i as integer = 1 to 4
        hit = scollision(frog, logs(i))
        if (hit <> 0) and (chosenLog = 0) and (frog.speed <> 0) then
          chosenLog = i
        end if
      next i
      if  chosenLog = 0 then frog.speed = 0
    
    end if
    
    sleep 10, 1
    
  loop while reply <> "y" and reply <> "x"
  
loop while reply = "y"

FB_MCI("close", "C")
FB_MCI("close", "F")
FB_MCI("close", "H")
FB_MCI("close", "S")

end
