  #compact
  % filename "t.exe"
 'uses RTL32
  % Title    "PONG BASE 2"
  % Animated
  % ScaleUp
  % PlaceCentral
 '% AnchorCentral
  uses ConsoleG

  indexbase 0

  'sys sseed=GetTickCount


  sub main()
  ==========
  '
  'static float ay
  'static sys   pki,count
  glClearColor 0,0.20,0,0
  if not pick
    thickness 3
    if opening
      static float byl=10
      static float bxl=15
      static float byd=11
      static float bxd=16
    endif
    '
    flat

    color 0,.40,0
    float z=-0.0001
    glbegin GL_QUADS
    glvertex3f -bxd,-byd,z
    glvertex3f bxd,-byd,z
    glvertex3f bxd,byd,z
    glvertex3f -bxd,byd,z
    glend
    '
    color .99,.22,0
    glbegin GL_LINES
    glvertex3f -bxd,-byd,0
    glvertex3f bxd,-byd,0
    '
    glvertex3f -bxd,byd,0
    glvertex3f bxd,byd,0
    '
    glvertex3f -bxd,-byd,0
    glvertex3f -bxd,byd,0
    '
    glvertex3f bxd,-byd,0
    glvertex3f bxd,byd,0
    '
    glend
    '
    shading

    color .90,.90,0
    static float blx,bly
    static float vlx,vly 'ball velocity
    static int tal1,tal2 'scores
    if opening
      vlx=0.2
      vly=0.1
    endif
    '
    pushstate
    move blx,bly
    go sphere 'radius 0.5
    popstate

    blx+=vlx
    bly+=vly
    '
    'LIMIT AND BOUNCE
    '
    if blx>bxl
       vlx = -vlx
   elseif blx< -bxl
      vlx = -vlx
    endif
    '
    if bly>byl
      vly = -vly
    elseif bly < -byl
      vly = -vly
    endif
    '
    'PONG BAT AND SCORE
    '
    static float btu1, btx1, bty1 'LEFT BAT
    static float btu2, btx2, bty2 'RIGHT BAT
    '
    if opening
      btu1=2.0
      btx1=-bxl+2.0
      bty1=0.0
      btu2=2.0
      btx2=bxl-2.0
      bty2=0.0
    endif
    '
    'PONG LEFT BAT
    '
    if blx<=btx1+1.0
      if vlx>0.0
        'bouncing behind bat1
      elseif bly>bty1+btu1+0.5
        'above bat
      elseif bly<bty1-btu1-0.5
        'below bat
      else
        tal1++
        vlx = -vlx
      endif
    endif
    '
    'PONG RIGHT BAT
    '
    if blx>=btx2-1.0
      if vlx<0.0
        'bouncing behind bat
      elseif bly>bty2+btu2+0.5
        'above bat
      elseif bly<bty2-btu2-0.5
        'below bat
      else
        tal2++
        vlx = -vlx
      endif
    endif
    '
    'SCORE
    flat
    pushstate
    move -bxd,byd+1.0
    scale 2.0
    print str(tal1)
    popstate
    '
    pushstate
    move bxd-2.0,byd+1.0
    scale 2
    print str(tal2)
    popstate
    '
    '
    'pong bats render
    pushstate
    glbegin GL_QUADS
    'left bat
    glvertex3f btx1-0.25,bty1-btu1,0
    glvertex3f btx1+0.25,bty1-btu1,0
    glvertex3f btx1+0.25,bty1+btu1,0
    glvertex3f btx1-0.25,bty1+btu1,0
    'right bat
    glvertex3f btx2-0.25,bty2-btu2,0
    glvertex3f btx2+0.25,bty2-btu2,0
    glvertex3f btx2+0.25,bty2+btu2,0
    glvertex3f btx2-0.25,bty2+btu2,0
    glend
    popstate
    '
  end if
  '
  'KEYBOARD CONTROLS
  '
  'LEFT BAT
  if GetAsyncKeyState 0x57 'Q' up
    if byd>bty1+btu1 'limit
      bty1+=0.1
    endif
  endif
  if GetAsyncKeyState 0x5a 'Z' down
    if  -byd < bty1-btu1
      bty1-=0.1
    endif
  endif
  '
  'RIGHT BAT
  if GetAsyncKeyState VK_UP
    if byd>bty2+btu2 'limit
      bty2+=0.1
    endif
  endif
  if GetAsyncKeyState VK_DOWN
    if  -byd < bty2-btu2
      bty2-=0.1
    endif
  endif
    '
  end sub

  EndScript
