Blitz Basic Tutes - Answer Ex.3

Ex.3

Graphics 800,600
SetBuffer BackBuffer()
SeedRnd MilliSecs()
pointer_img=LoadImage("pointer.bmp")
Dim cards(52)
Dim face_up(52)
Dim card_imgs(52)
cards_init()
cards_shuffle()
Repeat
  Cls
  cards_display()
  DrawImage pointer_img,MouseX(),MouseY()
  Flip
  If MouseHit(1)
    n=cards_which_one()
    If n>0 Then face_up(n)=Not face_up(n)
  End If
Until KeyHit(1)
End

Function cards_init()
  card_imgs(0)=LoadImage("back.png")
  For i=1 To 52
    cards(i)=i
    face_up(i)=False
    card_imgs(i)=LoadImage(i+".png")
  Next
End Function

Function cards_shuffle()
  For i=1 To 500
    r1=Rand(52)
    r2=Rand(52)
    t=cards(r1)
    cards(r1)=cards(r2)
    cards(r2)=t
  Next
End Function

Function cards_display()
  n=1
  y=8
  For row=1 To 6
    x=68
    For col=1 To 9
      card=0
      If face_up(n) Then card=cards(n)
      DrawBlock card_imgs(card),x,y
      x=x+74
      n=n+1
      If n=53 Then Exit
    Next
    y=y+98
  Next
End Function

Function cards_which_one()
  card=0
  n=1
  y=8
  For row=1 To 6
    x=68
    For col=1 To 9
      If mouse_in(x,y,x+74,y+98) Then card=n
      x=x+74
      n=n+1
      If n=53 Then Exit
    Next
    y=y+98
  Next
  Return card
End Function

Function mouse_in(x1,y1,x2,y2)
  x=MouseX(): y=MouseY()
  m=True
  If x<x1 Then m=False
  If x>x2 Then m=False
  If y<y1 Then m=False
  If y>y2 Then m=False
  Return m
End Function

Valid HTML 4.0 Strict Valid CSS