aUCBLogo Demos and Tests / am3
			
				
			
			setCaseIgnored false
to am3
  cS
;  windowset "Commander 2
;  windowset "Editor 2
;  setfocus[MSWLogo Screen]
   initO
;   noRefresh
  setScreenColor 0
  checkScreen
  initChars
  initMain
  gameLoop
;  keyboardoff
;  mouseoff
;  bye
end
to addBonus :type_
  if type_ == 1 [
    if or quit == true reset_ == true [stop]
    timeLeft=timeLeft-1
    score=score+5
    textCount=1  printText
    textCount=2  printText
    waitStop 1
    if timeLeft > 0 [addBonus type_]
  ]
  if type_ == 2 [
    if or quit == true reset_ == true [stop]
    lives=lives-1
    score=score+400
    textCount=2  printText
    textCount=3  printText
    waitStop 40
    if lives > 1 [addBonus type_]
  ]
end
to buttonDraw num action
  local [bdTurtle bdPos bdHeading bdPen]
  bdTurtle=Turtle
  setTurtle turtle0
  bdPos=Pos
  bdHeading=Heading
  bdPen=pen
  setPC RGB 1 1 0
  PU setPos Item 3 (Item num buttonList)
  drawText Item 1 (Item num buttonList) "getLength
  setH 270
  PU fd textLength/2 tfd 9 rt 90
  sbitblock textLength+13*textScale+2 38*textScale+1
  if action == "draw
  [   PD
    repeat 2
    [   tfd 8 fd 22*textScale tfd 8 rt 90
      tfd 8 fd textLength tfd 5 rt 90
    ]
    PU tfd 9 rt 90 tfd 9
    tempPos=Pos
    drawText Item 1 (Item num buttonList) "draw
    setPos tempPos
    x=round(first tempPos)
    y=round(last tempPos)
    setPos List x y
    setH 180 tfd 4 lt 90
    PD
    ifElse((Item 1 (Item num buttonList)) == [Instructions]) [tfd 7] [tfd 12]
  ]
  setTurtle turtle0
  PU setPos bdPos
  setH bdHeading
  setpen bdPen
  setTurtle bdTurtle
end
to buttonHit
  local [bhTurtle bhPos bhHeading bhPen bhDone bhNum]
  bhTurtle=Turtle
  setTurtle turtle0
  bhPos=Pos
  bhHeading=Heading
  bhPen=pen
  bhNum=0
  bhDone=false
  do_while
  [ bhNum=bhNum+1
    if(Item 2 (Item bhNum buttonList)) == true
    [ drawText Item 1 (Item bhNum buttonList) "getLength
      x1=(first Item 3 (Item bhNum buttonList))-textLength/2-10*textScale
      x2=(first Item 3 (Item bhNum buttonList))+textLength/2+6*textScale
      y1=last Item 3 (Item bhNum buttonList)
      y2=(last Item 3 (Item bhNum buttonList))+42*textScale
      if(and ((first    MousePos) > x1) ((first    MousePos) < x2)
             ((first bF MousePos) > y1) ((first bF MousePos) < y2))
      [
;(pr "HIT bhNum)
        ;Menu
        if bhNum == 1 [
          menuOn true
          bhDone=true
        ]
        ;Menu Off
        if bhNum == 2 [
          menuOn false
        ]
     ;Play
        if bhNum == 3 [commandPlay]
     ;Instructions
        if bhNum == 4 [commandInstructions]
     ;Quit
        if bhNum == 5 [
          quit=true
          open_quit=true
        ]
     ;High Scores
        if bhNum == 6 [commandHighScores]
      ;High Scores Done
        if bhNum == 7 [commandDone]
      ;Instructions Done
        if bhNum == 8 [commandDone]
      ;Continue
        if bhNum == 9 [
          paused=false
        ]
      ;reset_
        if bhNum == 10 [
          paused=false
          reset_=true
        ]
      ]
    ]
  ][and bhNum < numButtons bhDone == false]
  setTurtle turtle0
  PU setPos bhPos
  setH bhHeading
  setpen bhPen
  setTurtle bhTurtle
end
to buttonOn num action
  setItem num buttonList (List (Item 1 (Item num buttonList)) action (Item 3 (Item num buttonList)))
  if action == true [buttonDraw num "draw]
  if action == false [buttonDraw num "erase]
end
to clearBox
   cS
end
to clearBox_
  setTurtle turtle1
  setPos(List (displayWidth/2-displayWidth)+1 (displayHeight/2-displayHeight)+1)
  setFC 0
  fillRect Pos (List displayWidth+1 displayHeight+1)
end
to commandPlay
;  mouseoff
  playing=true
  menuOn false
  open_quit=true
end
to commandInstructions
;  keyboardoff
  menuState=menuOn_
  doneButtonOn=true
  displayInstructions_=true
  open_quit=true
  menuOn false
  buttonOn 1 false
  bhDone=true
end
to commandHighScores
;  keyboardoff
  menuState=menuOn_
  doneButtonOn=true
  displayHighScores_=true
  open_quit=true
  menuOn false
  buttonOn 1 false
  bhDone=true
end
to commandDone
  displayHighScores_=false
  open_quit=false
  displayInstructions_=false
  buttonOn 8 false
  buttonOn 7 false
  menuOn_=menuState
  doneButtonOn=false
end
to displayHighScores
  loadScores
  setTurtle turtle0
  drawText [High Scores] "getLength
  setPos List 0-(textLength/2) (displayHeight/2-textBarHeight*2)
  drawText [High Scores] "draw
  repeat 10
  [
    setTurtle turtle0
    setPos List (-1*(12*scale*17/2)) ((22*scale)*6-(22*scale)*repCount)
    name_=Item repCount hiNames
    drawText name_ "draw
    scoreS=Item repCount AM_Hiscores
    if scoreS == 0 [scoreS="]
    drawText scoreS "getLength
    setPos List ((12*scale*17/2)-textLength) ((22*scale)*6-(22*scale)*repCount)
    drawText scoreS "draw
  ]
end
to displayInstructions
  clearBox
  setTurtle turtle0
  drawText [Instructions] "getLength
  setPos List 0-(textLength/2) (displayHeight/2-30*textScale)
  drawText [Instructions] "draw
  ty=displayHeight/2-40*textScale
  tx=0-displayWidth/2+102*textScale
  th=0-displayWidth/2+66*textScale  ;heading
  ti=0-displayWidth/2+150*textScale  ;indent
  liney=32*textScale
  setPos(List th ty-liney*1) drawText [Playing the game] "draw
  setPos(List ti ty-liney*2) drawText [Navigate your mining ship through the asteroid field and] "draw
  setPos(List tx ty-liney*3) drawText [pick uP the crystals. \ When you have collected all the crystals] "draw
  setPos(List tx ty-liney*4) drawText [you can see, you advance to the next level to mine a new section] "draw
  setPos(List tx ty-liney*5) drawText [of the asteroid field. \ if you can finish all 13 levels, you win!] "draw
  setPos(List ti ty-liney*6) drawText [Don't get going too fast. \ In space, the only way to slow] "draw
  setPos(List tx ty-liney*7) drawText [down is to turn around and fire your engine in the other] "draw
  setPos(List tx ty-liney*8) drawText [direction. \ By the time you finish doing that, you may be dust!] "draw
  setPos(List th ty-liney*9.5) drawText [Ship controls] "draw
  setPos(List tx ty-liney*10.5) drawText [J....Turn the ship to the left] "draw
  setPos(List tx ty-liney*11.5) drawText [K....Fire the engines] "draw
  setPos(List tx ty-liney*12.5) drawText [L....Turn the ship to the right] "draw
  setPos(List th ty-liney*14) drawText [Game controls] "draw
  setPos(List tx ty-liney*15) drawText [Q....Quit] "draw
  setPos(List tx ty-liney*16) drawText [P....pause or reset] "draw
end
to displayText text_ pause_
  clearBox
  drawText text_ "getLength
  setPos(List 0-textLength/2  0)
  drawText text_ "draw
  waitStop pause_
end
be drawAsteroid r
   be get i j
      i=Int trunc Modulo i s*2
      j=Int trunc Modulo j s
      if i == s*2 [i=0]
      if j == s   [j=0]
      output (dist.i).j
   end
   be put i j d
      i=Int trunc Modulo i s*2
      j=Int trunc Modulo j s
      if i == s*2 [i=0]
      if j == s   [j=0]
      dist.i.j=d
   end
   be getw theta phi
      output get s*2*theta/180 s*phi/360
   end
   be putw theta phi d
      put s*2*theta/180 s*phi/360 d
   end
   be displayIt
;      refresh
;      clearScreen
      setPC RGB .5 .5 .5
      d=getw 0 0
      stp=4
      Surface
      [   for [phi 0 360 stp]
         [   for [theta stp 180 stp]
            [   d=getw theta phi
      ;         setPC hsb i*j 1 1
               PD fd d PU
               back d
               down stp
            ]
            up 180
            SurfaceColumn
            rightRoll stp
         ]
      ]
;      updateGraph
;      rotatescene2
;      noRefresh
   end
   be loadIt
      filename="drawasteroids.dat
      openReadBin filename
      setReader filename
      s=readIntBin
      k=readIntBin
      dist=(array s*2 0)
      repeat s*2
      [   i=repcount-1
         dist.i=(readFloatArrayBin s 0)
      ]
      setReader []
      close filename
   end
   s=0
   k=0
   dist={}
   loadIt
   perspective
   orthographic
   ::asteroidGraphic=Graphic [displayIt]
end
to drawChar c action
   validChar=true
   setTurtle turtle0
  setPenSize List round(0.4*scale) round(0.4*scale)
  setH 0
  cPos=Pos
  setPC RGB 1 1 0
   PD
   if not action == "getLength
  [   charvar=Word "char_ c
   ;   if name? charvar [run thing charvar]
      if procedure? charvar [run charvar]
  ]
  charWidth=0
  if and c >= "A  c <= "Z [charWidth=12]
  if and c >= "a  c <= "z [charWidth=10]
  if and c >= "0  c <= "9 [charWidth=12]
  if c == "I  [charWidth=7]
  if c == "f  [charWidth=9]
  if c == "j  [charWidth=5]
  if c == "i  [charWidth=1]
  if c == "l  [charWidth=1]
  if c == "m  [charWidth=11]
  if c == "r  [charWidth=9]
  if c == "t  [charWidth=9]
  if c == "1  [charWidth=11]
  if c == "  [charWidth=1]
  if c == "-  [charWidth=10]
  if c == "?  [charWidth=11]
  if c == "!  [charWidth=1]
  if c == ".  [charWidth=3]
  if c == ",  [charWidth=5]
  if c == "'  [charWidth=5]
  if c == "" [charWidth=6]
  if c == "_  [charWidth=11]
  if c == 32  [charWidth=7]
  PU
  setPos cPos
  setH 0
  ifElse charWidth == 0
  [   validChar=false
  ][
    if not action == "getLength [
      ;the 3 in "tfd 3" is assumed to be 3 by inputText.keyHit and most button procedures
      rt 90 tfd charWidth tfd 3 lt 90
    ]
    textLength=textLength+charWidth+3
  ]
end
to drawCrystal pos1 action
  local "points
  if not((last pos1) == offScreenY) [
    setTurtle turtle0
    points=Array 6
    setPenSize(List round(0.4*scale) round(0.4*scale))
    setH 0
    color=abs(Remainder first(pos1) 7)
    if action == "draw [
      if color == 0 [setPC RGB 0 1 1]
      if color == 1 [setPC RGB 1 .63 .74]
      if color == 2 [setPC RGB .95 .66 1]
      if color == 3 [setPC RGB 0 .7 1]
      if color == 4 [setPC RGB 1 1 1]
      if color == 5 [setPC RGB .66 1 .66]
      if color == 6 [setPC RGB 1 1 .66]
    ]
    if ee1 == true [
    color=abs(Remainder first(pos1) 2)
      if action == "draw [
        if color == 0 [setPC RGB 1 .4 .4]
        if color == 1 [setPC RGB 0 1 0]
      ]
    ]
    if action == "erase [setPC 0]
    repeat 6 [
      PU setPos pos1 PD
      ifElse(or repCount == 1 repCount == 4) [
        fd crystalRad
        setItem repCount points Pos
      ][
        fd (crystalRad/4*3)
        setItem repCount points Pos
      ]
      ifElse(or repCount == 2 repCount == 5) [rt 90] [rt 45]
    ]
    setPos Item 6 points
    repeat 6 [setPos Item repCount points]
    PU
  ]
end
to drawPlayground
   drawStars
   repeat numRocks [drawRock repcount]
   repeat maxCrystals [drawCrystal (Item repCount crystals) "draw]
end
to drawRock nr
   setTurtle turtle0
   pos1=rocks.nr
   setPos pos1
   setOrientation rockori.nr
   rightRoll 1
   rockori.nr=Orientation
   (drawGraphic asteroidGraphic 0.28*rockRad/25*rocksRad.nr)
   setOrientation [0 0 0]
end
to drawShip x y d action
   setMaterialSpecular HSB 70 0.5 0.5
   setMaterialShininess 10
   setLightSpotExponent 2
  setTurtle turtle0
  setPenSize(List
     round(1*scale*shipRad/normalShipRad)
     round(1*scale*shipRad/normalShipRad))
  r=((255-138)/shipFadeStart*shipGlow+138)/255
  g=((255-60)/shipFadeStart*shipGlow+60)/255
  b=((255-255)/shipFadeStart*shipGlow+255)/255
  if shipGlow > shipFadeStart [r=1 g=1 b=1]
  if action == "draw
  [blink=blink+1
    if blink == 41 [blink=0]  ;blink cyles through two seconds, then resets (there are 20 fps)
  ]
  if (or action == "draw action == "redraw) [setPC RGB r g b]
  if action == "erase [setPC 0]
  ifElse exploding == false
  [;draw right side of nose
    setPos List x y
    setH d
    fd shipRad
    pos1=Pos
    bk shipRad*1.8
      (Cylinder shipRad*1.8 shipRad*0.3 0)
   fd shipRad*0.8   
    rt 67.5
    fd shipRad3
    pos2=Pos
    PD
   Tesselation
   [ 
      fd shipRad3*2
      bk 1*scale
      light1Pos=Pos
      fd 1*scale
      rt 112.5
      fd shipSide
      ;    if (or leaveStep < numLeaveSteps/3*2  leaving == false)
      ;   [drawShip_light light1Pos]
      ;draw back of ship, trace back over it partway, draw engine if thrusting, then trace forward again--
      ;this keeps the engine over top of all the other lines
      rt 45
      fd shipSide-1.414*scale
      rt 45
      if(or thrust == true  action == "erase)
      [setPenSize(List
         round(2*scale*shipRad/normalShipRad)
         round(2*scale*shipRad/normalShipRad))
      ]
      if(and (leaveStep < numLeaveSteps/5*4) (thrust == true)
               (or action == "draw action == "redraw))
      [setPC RGB 1 .9 .5
      ]
      fd shipSide+2*scale
      rt 45
      fd shipSide-1.414*scale
      if (or action == "draw action == "redraw) [setPC RGB r g b]
      setPenSize(List
      round(1*scale*shipRad/normalShipRad)
      round(1*scale*shipRad/normalShipRad))
      rt 45
      fd shipSide
      ;draw left side of nose
      rt 112.5
      fd scale
      light2Pos=Pos
      bk scale
      fd shipRad3*2
      if (or leaveStep < numLeaveSteps/3*2  leaving == false)
      [drawShip_light light2Pos]
      lt 93.06
      fd scale*shipSide*1.2
      rt 141.11
      fd scale*shipSide*1.2
   ] 
   PU
   setPos List x y
   setH d
   ;draw cockpit begin
   rt 180
   setPC RGB 0 0.5 1
   (Cylinder shipRad*0.4 shipRad*0.18 shipRad*0.25)
   lt 90  
   up 90 fd 1 down 90
   PD
   setPC RGB 0.7 0.5 1
   Tesselation
   [   fd shipRad6/2
      rt 45  fd shipRad6Diag
      rt 45  fd shipRad3
      rt 90 rt 45 fd shipRad6Diag
      lt 45 fd shipRad6
      lt 45 fd shipRad6Diag rt 45
      rt 90  fd shipRad3
      rt 45  fd shipRad6Diag
      rt 45  fd shipRad6/2
      lt 90 PU
   ]
    ;draw cockpit end
  ]
  [ ;explosion--random numbers for the fragment attribute lists are picked in playLoop
    if action == "draw [expLen=expLen-shipRad/numExpSteps]
    repeat numFrags
    [ setPos List x y  setH d
      rt 360/numFrags*repCount
      fal=Item repCount fragments  ;fal stands for fragment attribute list
      fd shipRad*(Item 1 fal)+expStep*(Item 2 fal)
      rt (Item 3 fal)+expStep*(Item 4 fal)
      PD
      fd expLen*(Item 5 fal)*scale
      rt 180
      fd expLen*(Item 6 fal)*scale
      if (Item 7 fal) == 1
      [ rt (Item 8 fal)  fd expLen*(Item 9 fal)*scale
      ]
      PU
    ]
  ]
  if and(shipGlow > 0) (action == "draw)
  [ shipGlow=shipGlow-1
  ]
  erase_=false
end
to drawShip_light lightPos
  PU
  h_=Heading
  pos3=Pos
  setPos lightPos
  setH 225 fd 1.414*scale
  ;draw light if it is on, erase it if it is off
  if and (blink > 19) (or action == "draw action == "redraw) [
    setFC RGB .8 .2 .2
;    sbitblock round(2.4*scale/(leaveStep+1)) round(2.4*scale/(leaveStep+1))
    setFC 0
  ]
  if (action == "erase) [
;    sbitblock round(2.4*scale) round(2.4*scale)
  ]
  setPos pos3
  setH h_
  PD
end
to drawStars
   setPixelXYZ starsX starsY starsZ starsC
end
to drawText_ txt action
   setTurtle turtle0
   setH 90
   setLabelAlign 1 0
   ifElse action=="getLength
   [ textLength=first LabelSize txt
   ][Label txt
   ]
end
to drawText txt action
  setTurtle turtle0
  tPos=Pos
  textLength=0
  setH 0
  ifElse List? txt
  [ numWords=count txt
    repeat numWords
    [ chars=Item repCount txt
      numChars=count chars
      repeat numChars
      [   drawChar Item repCount chars  action
      ]
      if not repCount == numWords
      [ rt 90 tfd 6 lt 90
         textLength=textLength+6
      ]
    ]
  ][
    numChars=count txt
    repeat numChars
    [ drawChar (Item repCount txt) action
    ]
  ]
  textLength=round (textLength*textScale )
  if action == "getLength [PU setPos tPos setH 0]
end
to findXy object
   findNew=false
   if object == "rock
   [   neighbor=0
      x=round ((random round(gameWidth-rockRad*4)) -
            (gameWidth-rockRad*4)/2)
      y=round((random round(gameHeight-rockRad*4)) -
            (gameHeight-rockRad*4)/2-textBarHeight/2)
      setPos(List x y)
   
      ;make sure rock is not on top of ship
      if (Distance[0 0]) < (rockRad+shipRad+6) [findNew=true]
   
      ;make sure rock is not touching another--this must be the last thing checked for a rock
      ;if it is too close to another for a ship to pass between them, that's OK only if the other rock does not already have a neigbor
      repeat (loop1-1)
      [   if (Distance rocks.repCount) < (rockRad*2+shipRad*2+6)
         [   ifElse (or  (Item repCount rockNeighbors) > 0
                     (neighbor>0)
                     (Distance rocks.repCount) < rockRad*2+3
                  )
            [   findNew=true
               neighbor=0
            ][   neighboringRock=repCount
               neighbor=1
            ]
         ]
      ]
      if and (findNew == false) (neighbor > 0) 
      [   setItem loop1 rockNeighbors neighbor
         setItem neighboringRock rockNeighbors loop1
      ]
   ]
   if object == "crystal 
   [   x=round ((random round(gameWidth-shipRad*3.2)) -
             (gameWidth-shipRad*3.2)/2)
      y=round((random round(gameHeight-shipRad*3.2)) -
            (gameHeight-shipRad*3.2)/2-textBarHeight/2)
      setPos(List x y)
   
      ;make sure crystals don't overlap rocks
      repeat numRocks 
      [   if (Distance(Item repCount rocks)) < (rockRad+crystalRad+2) 
         [   findNew=true
         ]
      ]
      ;make sure crystal is not on top of ship
      if (Distance[0 0]) < (crystalRad+shipRad+2) [findNew=true]
   
      ;make sure crystal is not too close to another
      if findNew == false 
      [   repeat (loop1-1) 
         [   if (Distance(Item repCount crystals)) < (crystalRad*2+2) 
            [   findNew=true
            ]
         ]
      ]
      ;make sure crystal is not so close to two rocks that the ship can't get to it
      if findNew == false 
      [   repeat numRocks 
         [   if (Distance(Item repCount rocks)) < (shipRad*2+rockRad+2) 
            [   y=y+0.001   ;marks crystal as too close to a rock to glint
               firstRock=repCount
               repeat numRocks 
               [   if not(repCount == firstRock) 
                  [   if (Distance(Item repCount rocks)) < (shipRad*2+rockRad+2) 
                     [   findNew=true
                     ]
                  ]
               ]
            ]
         ]
      ]
   ]
   
   if (and object == "rock  findNew == true) 
   [   rockCounter=rockCounter+1
      if rockCounter < 250 [findXy "rock]
   ]
   if (and object == "crystal  findNew == true) [findXy "crystal]
end
to gameLoop
  quit=false
  reset_=false
;  (keyboardon [processKey keyboardvalue]
;              [processKey keyboardvalue+200]
; )
;  mouseon [buttonHit] [] [] [] []
  menuOn false
  open_loop
  if not(quit == true) [
    initGame
    levelLoop
  ]
  if not(quit == true) [gameLoop]
  setTurtle turtle0 hT PU
  setTurtle turtle1   hT PU
  setTurtle turtle2 hT PU
  setTurtle turtle3 hT PU
end
to glint action
  if glintStep == 1 [
    setH 315
    glintSize=0
    speedUp=random 3
    ifElse speedUp == 0 [rotateSpeed=0] [rotateSpeed=(random 20)+5]
  ]
  if glintStep == 20 [drawCrystal (Item glintCrystal crystals) "draw]
  setTurtle turtle3
  setPenSize(List round(1*scale) round(1*scale))
  PU
  setPos Item glintCrystal crystals
  setH 0
  if speedUp == 0 [rt 45 fd crystalRad/4*3 lt 45]
  if speedUp == 1 [lt 45 fd crystalRad/4*3 rt 45]
  if speedUp == 2 [rt 135 fd crystalRad/4*3 lt 135]
  PD
  if glintStep > 0 [
    setPC 0
    setH 0
    rt rotateSpeed*(glintStep-1)
    repeat 4[fd glintSize bk glintSize rt 90]
    drawCrystal (Item glintCrystal crystals) "draw
  ]
  if and glintStep < 20 action == "animate [
    setTurtle turtle3
    if glintStep < 11 [glintSize=glintStep*scale]
    if glintStep > 10 [glintSize=21*scale-glintStep*scale]
    setPC RGB 1 1 1
    setH 0
    if speedUp == 0 [rotateSpeed=rotateSpeed+2]
    rt rotateSpeed*glintStep
    repeat 4[fd glintSize bk glintSize rt 90]
  ]
  PU
end
to checkScreen
  x=800   ;item 3 machine
  y=600   ;item 4 machine
;  if (y < 310) [
;    messagebox [Screen Problem] [The screen height needs to be at least 310. Set the Asteroid Miner shortcut accordingly. (Try "C\MSWLogo\logo.exe -h 500 -w 700 -f -l AM.lgo" as the shortcut target.)]
;    bye
;  ]
;  if (or (x/y < 1.39) (x/y > 1.405)) [
;    messagebox [Screen Problem] [The screen width needs to be 1.4 times the height. Set the Asteroid Miner shortcut accordingly. If the Logo screen fills the desktop, the height or width may be too large, in which case Logo is ignoring it.]
;    bye
;  ]
end
to hitCrystalCheck
  ;checks all the crystals, whether they are on the screen or not, to keep the speed more constant on slow computers
  repeat maxCrystals [
    x=first(Item repCount crystals)
    y=last(Item repCount crystals)
    dist2=(shipX-x)*(shipX-x)+(shipY-y)*(shipY-y)
    if and (dist2 < shipCrystal2) (exploding == false) [
      if and glintStep < 20 glintCrystal == repCount [
        glintStep=glintStep+1
        glint "erase
        glintStep=20
      ]
      drawCrystal (Item repCount crystals) "erase
      setItem repCount crystals(List offScreenX offScreenY)
      numCrystals=numCrystals-1
      if numCrystals == 0 [
        bonus=timeLeft*5
        leaving=true
        thrust=true
        timerOff
      ]
      score=score+crystalWorth
      textCount=1  printText
      textCount=2  printText
      crystalWorth=crystalWorth+5
      shipGlow=shipFadeStart+4
    ]
    if and dist2 < (shipCrystal2*4) (exploding == true) [
       drawCrystal(Item repCount crystals) "draw
    ]
  ]
end
to hitRockCheck
  ;checks all the rocks, whether they are on the screen or not, to keep the speed more constant on slow computers
  repeat numRocks [
    x=first(Item repCount rocks)
    y=last(Item repCount rocks)
    dist2=(shipX-x)*(shipX-x)+(shipY-y)*(shipY-y)
    shipRock2=sqr shipRad+rockRad*rocksRad.repCount
    if (and dist2 < shipRock2  leaving == false  exploding == false) [
      timerOff
;      drawShip shipXInt shipYInt dr "erase
      ;save the point where the ship hit the rock, for the explosion bitmap
      ;turtle was set by drawShip
      setH towards (Item repCount rocks) fd shipRad-shipRad3
      expCtr=(List ((first Pos)-shipRad) ((last Pos)-shipRad))
      exploding=true
      expLen=shipRad
    ]
    if and dist2 < (shipRock2*2) (or exploding == true leaving == true) 
    [   drawRock repCount
    ]
    if (and (dist2 < shipRock2) (leaving == true)) [
       behindRock=true
    ]
  ]
end
to initChars
   charlist=
   [   [A tfd 11 make "pos1 pos pu tfd 10 rt 90 tfd 5.5 make "pos2 pos tfd 5.5 rt 90 tfd 10 pd rt 90 tfd 11 tbk 11 lt 90 tfd 11 tbk 11 setpos :pos2 setpos :pos1]
      [B tfd 21 rt 90 tfd 7 rtCnr tfd 2 rtCnr tfd 7 lt 90 tfd 11 lt 90 tfd 8 ltCnr tfd 2 ltCnr]
      [C pu rt 90 tfd 11 rt 180 pd tfd 1 rtCnr2 tfd 0 rtCnr2 tfd 1]
      [D tfd 21 rt 90 tfd 1 rtCnr2 tfd 0 rtCnr2 tfd 1]
      [E rt 90 tfd 11 tbk 11 lt 90 tfd 11 rt 90 tfd 10 tbk 10 lt 90 tfd 10 rt 90 tfd 12]
      [F tfd 11 rt 90 tfd 10 tbk 10 lt 90 tfd 10 rt 90 tfd 12]
      [G pu rt 90 tfd 11 lt 90 pd tfd 11 lt 90 tfd 5 tbk 5 rt 90 tbk 11 lt 90 tfd 3 rtCnr2 tfd 0 rtCnr2 tfd 3]
      [H tfd 11 rt 90 tfd 11 tbk 11 lt 90 tfd 11 pu rt 90 tfd 11 rt 90 tfd 1 pd tfd 21]
      [I pu rt 90 pd tfd 6 tbk 3 lt 90 tfd 21 lt 90 tfd 3 tbk 7]
      [J pu tfd 6 rt 180 pd tfd 2 ltCnr tfd 1 ltCnr tfd 17 lt 90 tfd 3 tbk 6]
      [K tfd 21 tbk 10 rt 45 tfd 14 tbk 14 rt 90 tfd 15]
      [L rt 90 tfd 11 tbk 11 lt 90 tfd 21]
      [M pu rt 90 tfd 11 lt 90 pd tfd 21 make "pos1 pos tbk 21 lt 90 pu tfd 6 rt 90 tfd 11 make "pos2 pos tbk 11 lt 90 tfd 5 rt 90 pd tfd 21 setpos :pos2 setpos :pos1]
      [N pu rt 90 tfd 11 pd make "pos1 pos lt 90 tfd 21 tbk 21 pu lt 90 tfd 11 rt 90 pd tfd 21 setpos :pos1]
      [O pu tfd 4 pd repeat 2 [tfd 13 rtCnr tfd 3 rtCnr]]
      [P tfd 21 rt 90 tfd 6 rtCnr tfd 2 rtCnr tfd 6]
      [Q pu tfd 4 pd repeat 2 [tfd 13 rtCnr tfd 3 rtCnr] pu rt 90 tfd 6 rt 45 pd tfd 8]
      [R tfd 21 rt 90 tfd 6 rtCnr tfd 2 rtCnr tfd 6 rt 45 tbk 15]
      [S pu rt 90 fd 3 lt 180 rtCnr rt 180 ltCnr tfd 3 ltCnr tfd 2 ltCnr tfd 1 rtCnr tfd 2 rtCnr tfd 1 rtCnr]
      [T pu rt 90 tfd 6 pd lt 90 tfd 21 lt 90 tfd 6 tbk 12]
      [U pu tfd 4 pd tfd 17 rt 180 tfd 17 ltCnr tfd 2 ltCnr tfd 17]
      [V pu rt 90 tfd 6 lt 75 pd tfd 22 tbk 22 lt 30 tfd 22]
      [W pu rt 90 tfd 11 lt 90 tfd 21 rt 180 rt 90 tfd 11 lt 90 pd tfd 21 make "pos1 pos tbk 21 lt 90 pu tfd 6 rt 90 tfd 11 make "pos2 pos tbk 11 lt 90 tfd 5 rt 90 pd tfd 21 setpos :pos2 setpos :pos1]
      [X pu rt 90 tfd 11 lt 90 tfd 21 rt 180 pu make "pos1 pos tfd 21 lt 45 tfd 1.4 make "pos2 pos tbk 1.4 rt 45 rt 90 tfd 11 lt 45 tfd 1.4 make "pos3 pos tbk 1.4 rt 45 rt 90 tfd 21 pd setpos :pos2 pu setpos :pos1 pd setpos :pos3]
      [Y pu rt 90 tfd 6 lt 90 pd tfd 11 rt 30 tfd 12 tbk 12 lt 60 tfd 12]
      [Z make "pos1 pos rt 90 tfd 12 pu tbk 1 lt 90 tfd 21 pd lt 90 tfd 11 tbk 11 setpos :pos1]
      
      [a pu rt 90 tfd 4 pd tfd 5 tbk 1 lt 90 tfd 12  tbk 1 lt 90 tfd 4 ltCnr tfd 2.5 ltCnr]
      [b tfd 21 tbk 10 rt 90 tfd 3 rtCnr tfd 2 rtCnr tfd 3]
      [c pu rt 90 tfd 3 tfd 4 rt 180 pd tfd 4 rtCnr tfd 2.5 rtCnr tfd 4]
      [d pu rt 90 tfd 2 pd tfd 5 tbk 1 lt 90 tfd 21  tbk 10 lt 90 tfd 4 ltCnr tfd 2.5 ltCnr]
      [e pu rt 90 tfd 3 tfd 4 rt 180 pd tfd 4 rtCnr tfd 2.5 rtCnr rtCnr tfd 1 rt 90 tfd 8]
      [f pu rt 90 tfd 2 pd lt 90 tfd 11 lt 90 tfd 3 tbk 6 tfd 3 rt 90 tfd 8 rtCnr tfd 1]
      [g pu rt 90 tfd 3 pd tfd 4 rt 90 tfd 2 rtCnr rtCnr rt 180 ltCnr ltCnr tfd 14  tbk 1 lt 90 tfd 4 ltCnr tfd 2.5 ltCnr]
      [h tfd 21 tbk 10 rt 90 tfd 4 rtCnr tfd 7]
      [i rt 90 tfd 3 tbk 1.5 lt 90 tfd 12 pu tfd 3 pd tfd 2]
      [j pu rt 90 tfd 4 lt 90 tfd 17 rt 180 pd tfd 2 pu tfd 3 pd tfd 13 rtCnr]
      [k tfd 7 make "pos1 pos tfd 14 tbk 21 rt 90 pu tfd 10 rt 90 tfd 1 make "pos2 pos tbk 1 lt 90 tbk 1 lt 90 tfd 11 pd setpos :pos1 setpos :pos2]
      [l tfd 22]
      [m tfd 11 tbk 4 rtCnr rtCnr tfd 7 rt 180 tfd 7 rtCnr rtCnr tfd 7]
      [n tfd 11 tbk 4 rtCnr tfd 2 rtCnr tfd 7]
      [o pu tfd 4 pd repeat 2 [tfd 3 rtCnr tfd 1 rtCnr]]
      [p tbk 6 tfd 17 tbk 4 rtCnr tfd 1 rtCnr tfd 3 rtCnr tfd 1 rtCnr]
      [q pu rt 90 tfd 5 ltCnr rt 180 pd rtCnr tfd 1 rtCnr tfd 3 rtCnr tfd 1 rtCnr tfd 13 tbk 17]
      [r tfd 11 tbk 4 rtCnr tfd 1 rtCnr]
      [s rt 180 ltCnr ltCnr ltCnr rtCnr rtCnr rtCnr]
      [t pu rt 90 tfd 4 pd lt 90 tfd 11 lt 90 tfd 4 tbk 8 tfd 4 rt 90 tfd 8]
      [u pu tfd 11 rt 180 pd tfd 6 ltCnr tfd 1 ltCnr tfd 6]
      [v pu tfd 12 make "pos1 pos tbk 12 rt 90 tfd 5 make "pos2 pos tfd 4 lt 90 tfd 11 pd setpos :pos2 setpos :pos1]
      [w pu tfd 6 pd tfd 5 tbk 5 pu make "pos1 pos tbk 6 rt 90 tfd 2 make "pos2 pos tfd 2 lt 90 tfd 5 make "pos3 pos tbk 5 rt 90 tfd 2 make "pos4 pos tfd 2 lt 90 tfd 6 pd tfd 5 tbk 5 setpos :pos4 setpos :pos3 setpos :pos2 setpos :pos1]
      [x pu rt 90 tfd 9 lt 90 tfd 11 rt 180 pu make "pos1 pos tfd 11 lt 45 tfd 1.4 make "pos2 pos tbk 1.4 rt 45 rt 90 tfd 9 lt 45 tfd 1.4 make "pos3 pos tbk 1.4 rt 45 rt 90 tfd 11 pd setpos :pos2 pu setpos :pos1 pd setpos :pos3]
      [y pu tfd 11 make "pos1 pos tbk 19 make "pos2 pos tfd 8 rt 90 tfd 4 make "pos3 pos tfd 5 lt 90 tfd 11 pd setpos :pos3 setpos :pos1 setpos :pos3 setpos :pos2]
      [z make "pos1 pos rt 90 tfd 10 pu tbk 1 lt 90 tfd 11 pd lt 90 tfd 9 tbk 9 setpos :pos1]
      
      [0 pu tfd 4 pd repeat 2 [tfd 13 rtCnr tfd 2 rtCnr] pu tfd 6 rt 90 tfd 5.3 lt 70 pd tfd 4 tbk 8]
      [1 pu rt 90 tfd 3 pd tfd 8 tbk 4 lt 90 tfd 21 lt 135 tfd 8]
      [2 pu rt 90 tfd 11 lt 180 pd tfd 11 rt 90 tfd 6 rtCnr tfd 2 ltCnr tfd 3 ltCnr tfd 1 ltCnr]
      [3 pu rt 90 tfd 5 lt 180 rtCnr lt 180 pd ltCnr tfd 2 ltCnr tfd 3 ltCnr tfd 2 rt 180 tfd 2 ltCnr tfd 2 ltCnr tfd 1 ltCnr]
      [4 pu rt 90 tfd 8 lt 90 pd tfd 21 lt 135 tfd 13 lt 135 tfd 11]
      [5 pu rt 90 tfd 3 lt 180 rtCnr rt 180 ltCnr tfd 2 ltCnr tfd 3 ltCnr tfd 5 rt 90 tfd 10 rt 90 tfd 10]
      [6 pu tfd 4 pd repeat 2 [tfd 3 rtCnr tfd 2 rtCnr] tfd 13 rtCnr tfd 2 rtCnr]
      [7 pu tfd 21 rt 90 pd tfd 11 rt 115 tfd 23]
      [8 pu tfd 4 pd repeat 2 [tfd 3 rtCnr tfd 2 rtCnr] tfd 3 rtCnr tfd 1.5 rt 180 repeat 2 [tfd 1 rtCnr tfd 2 rtCnr]]
      [9 pu rt 90 tfd 3 lt 180 rtCnr rt 180 ltCnr tfd 1 ltCnr tfd 14 ltCnr tfd 1 ltCnr tfd 2 ltCnr  tfd 1 ltCnr]
      
      [: tfd 2 pu tfd 8.5 pd tfd 2]
      [- pu tfd 11 rt 90 pd tfd 10]
      [? pu rt 90 tfd 2 lt 90 pd tfd 2 pu tfd 3 pd tfd 3 rtCnr tfd 0 ltCnr tfd 2 ltCnr tfd 3 ltCnr tfd 1]
      [! tfd 2 pu tfd 3 pd tfd 17]
      [. tfd 2]
      [, pu tbk 4 rt 30 pd tfd 3 lt 30 tfd 2 lt 90 tfd 3]
      [' pu tfd 17 rt 30 pd tfd 3 lt 30 tfd 2 lt 90 tfd 3]
      [" pu tfd 17 pd tfd 4 pu rt 90 tfd 4 rt 90 pd tfd 4]
      [_ pu tbk 2 rt 90 pd tfd 11]
   ]
   foreach charlist
   [   ;make (word "char_ first ?) bf ?
      define (Word "char_ first ?) (List [] bF ?)
   ]
end
to tfd :dist
   fd :dist*::textScale
end
to tbk :dist
   bk :dist*::textScale
end
to ltCnr
   arc2 -90 -::textScale*3*sqrt 2
;   lt 45 tfd 1.4 lt 45
end
to rtCnr
   arc2 90 ::textScale*3*sqrt 2
;   rt 45 tfd 1.4 rt 45
end
to ltCnr2
   arc2 -90 -::textScale*10.5
;   lt 45 tfd 1.4 lt 45
end
to rtCnr2
   arc2 90 ::textScale*10.5
;   rt 45 tfd 1.4 rt 45
end
to initGame
  score=0
  level=6
  lives=4
  gameOver=false
  wrap
  setTurtle turtle0 hT PU
  setTurtle turtle1   hT PU
  setTurtle turtle2 hT PU
  setTurtle turtle3 hT PU
  setPenSize(List round(1*scale) round(1*scale))
end
to initLevel
   quit=false
   reset_=false
   timeLeft=40+level*40
   numCrystals=level*2+2
   numRocks=level*2
   levelOver=false
   behindRock=false
   crystalWorth=40
   setShipSize normalShipRad
end
to initLife
   clearBox
   drawPlayground
   
   Key=0
   shipX=0
   shipY=0
   shipXInt=0
   shipYInt=0
   dr=0
   dra=0
   oldShipX=shipX
   oldShipY=shipY
   oldDr=dr
   shipDeltaX=0
   shipDeltaY=0
   turnRt=false
   turnLt=false
   thrust=false
   exploding=false
   leaving=false
   expStep=0
   leaveStep=0
   textCount=1
   erase_=false
   lifeOver=false
   shipGlow=0
   blink=0 ;controls the ships blinking lights
   glintCystal=1
   glintStep=100
   rockDrawCount=1
   crystalDrawCount=1
end
to initMain
  scale=1   ;(item 4 machine)/500  ;sets the game scale based on the height set on the command line
  turtle0=Turtle
  turtle1=newTurtle
  turtle2=newTurtle
  turtle3=newTurtle
  setTurtle turtle0 hT PU
  setTurtle turtle1 hT PU
  setTurtle turtle2 hT PU
  setTurtle turtle3
  hT PU
  displayWidth=800   ;item 3 machine
  displayHeight=600   ;item 4 machine
  textLength=0
  numRocks=0
  numCrystals=0
  score=0
  displayHighScores_=false
  displayInstructions_=false
  doneButtonOn=false
  pauseNow=false
  paused=false
  playing=false
  glintCrystal=0
  timer=false
  sbitblock 1 1
  ifElse and (Item 5 time) == 12
  (and (Item 6 time) > 22 (Item 6 time) < 27)
     [ee1=true]
     [ee1=false]
  ifElse and (Item 5 time) == 3 (Item 6 time) == 11
     [ee2=true]
     [ee2=false]
;CONSTANTS
  open_scale=1.25*scale
  textScale=(5/6)*scale
  if scale == 1 [textScale=0.8]
  numFrags=7   ;number of framents in an explosion
  crystalRad=8*scale   ;crystals should be wider than maxSpeed
  textBarHeight=24*scale
  gameWidth=displayWidth
  gameHeight=displayHeight-textBarHeight
  maxLevel=13
  rockRad=40*scale
  normalShipRad=14*scale
  setShipSize normalShipRad
  ;it's faster if the math is done here, once, and saved in a variable
  shipCrystal2=(shipRad+crystalRad)*(shipRad+crystalRad) ;distance squared
  numExpSteps=30
  numLeaveSteps=35
  acceleration=0.4*scale
  maxSpeed2=(12*scale)*(12*scale)   ;maximum speed squared
  shipFadeStart=20   ;steps until the glowing ship starts to fade again
  maxCrystals=maxLevel*2+2
  maxRocks=maxLevel*2
  rocks=(Array maxRocks 1)
  rockori=(Array maxRocks 1)
  rocksRad=FloatArray maxRocks
  rockNeighbors=(Array maxRocks 1)
  crystals=(Array maxCrystals 1)
  offScreenX=0
  offScreenY=displayHeight/2+rockRad*4
   repeat maxCrystals 
   [   setItem repCount crystals (List offScreenX offScreenY)
   ]
   repeat maxRocks [setItem repCount rocks (List offScreenX offScreenY)]
   repeat maxRocks [setItem repCount rockNeighbors 0]
   
   Apos=(List -175*open_scale 105*open_scale)
   Mpos=(List -115*open_scale 5*open_scale)
;BITMAPS
  setTurtle turtle0
;  setbitindex 1
  pos1=(List 0 0-displayHeight/2+10*scale)
  setPos pos1
;  bitload "AM_bitmaps.bmp wait 1
;  bitcut 42 42
  setTurtle turtle2
;  setbitindex 2
  setPos(List 0 0-displayHeight/2+10*scale+42)
;  bitcut 80 110 wait 1
;  bitfit round(53*open_scale) round(73*open_scale)
;  bitmapturtle
  ;this is down here to allow a "wait" to happen before bitfitting--otherwise data seems to be lost
;  setbitindex 1
;  bitfit round(shipRad*2.5*scale) round(shipRad*2.5*scale)
  ;cut numbers  indexes 10-19
  bitNumbers=(Array 10 0)
  repeat 10
  [ setPos pos1
    fillRect [-5 -5] List 10*scale+1 18*scale+1
    drawChar Char (ASCII "0)+repCount-1 "draw
    setPos pos1-2   setH 0
      setItem repCount-1 bitNumbers
         BitCopy round(10*scale)+1 round(20*scale)+1
  ]
  setPos pos1
  fillRect [-5 -5] (List textLength 18*scale+1)
  drawText [Score] "draw
  scoreLength=textLength
  setPos pos1-2   setH 0
   bitScore=BitCopy textLength round(25*scale)+1
  setPos pos1
  fillRect [-5 -5] (List textLength 18*scale+1)
  drawText [Bonus] "draw
  bonusLength=textLength
  setPos pos1-3   setH 0
   bitBonus=BitCopy textLength round(25*scale)+1
   setShipSize 9*scale
   numStars=500
   starsX=FloatArray (random iSeqIA 800 800 numStars)-400
   starsY=FloatArray (random iSeqIA 600 600 numStars)-300
   starsZ=FloatArray iSeqIA -1000 -1000 numStars
   starsC=IntArray numStars
   repeat numStars
   [   starsC.repcount=HSB rnd*360 0.3 rnd
   ]
   initLife
   drawShip 0 0 0 "draw
   PU setH 270 fd shipRad lt 90 fd shipRad   setH 0
   bitShip=BitCopy round(18*scale)+1 round(18*scale)+1
   setShipSize normalShipRad
   drawAsteroid 100
;BUTTONS
   numButtons=10
   buttonList=Array numButtons
   setItem 1 buttonList (List [Menu] true (List 0 0-displayHeight/2+4))
   setItem 2 buttonList (List [Menu Off] false (List 0 0-displayHeight/2+4))
   setItem 3 buttonList (List [Play] false (List -90*scale 0-displayHeight/2+4))
   setItem 4 buttonList (List [Instructions] false (List -197*scale 0-displayHeight/2+4))
   setItem 5 buttonList (List [Quit] false (List 90*scale 0-displayHeight/2+4))
   setItem 6 buttonList (List [High Scores] false (List 192*scale 0-displayHeight/2+4))
   setItem 7 buttonList (List [Done] false (List 0 0-displayHeight/2+4))
   setItem 8 buttonList (List [Done] false (List 0 0-displayHeight/2+4))
   setItem 9 buttonList (List [continue] false (List 0 0+8*textScale))
   setItem 10 buttonList (List [reset] false (List 0 0-42*textScale))
;HIGH SCORE FILE CHECK
   fileExist=filep "AM_Hiscores
;  make "filelist shell [dir]
;  make "fileExist false
;  repeat count filelist
;  [ if (item repcount filelist) == "AM_Hiscores
;     [ make "fileExist true
;     ]
;  ]
   if (fileExist == false)
   [ openWrite "AM_Hiscores
      setWriter "AM_Hiscores
      print "AsteroidMinerHS
      close "AM_Hiscores
      setWriter []
   ]
end
to initO
   local [p]
   setUpdateGraph false
   setSC RGB 0 0 0
   Home
   setPenSize [3 3]
   setPC HSB 60 .6 .8
   PU rt 90 fd 45 lt 90 fd 75 lt 180
   p=Pos
   fd 60 PD
   Ellipse 10 12
   PU lt 60 fd 5 PD Ellipse 4 2
   PU bk 5 rt 120 fd 5 PD Ellipse 4 2
      PU setPos p setH 0 PD
   initO_feet
   rt 180
   initO_feet
   PU fd 50 PD EllipseArc 180 4 8 90
   PU setPos p setH 0 PD
   Ellipse 16 50
   Ellipse 40 30
   Ellipse 40 10
   setPC HSB 60 .3 .9
   Ellipse 40 50
   PU Home
   bitTurtleSizeX=90
   bitTurtleSizeY=135
   bitTurtle=BitCopy bitTurtleSizeX bitTurtleSizeY
   BitMakeTransparent bitTurtle RGB 0 0 0
   setSC 0
;   setUpdateGraph true
end
to initO_feet
   PU lt 40 fd 45 PD EllipseArc 185 6 20 85
   PU bk 45 rt 80 fd 45 PD EllipseArc 185 6 20 90
   PU bk 45 lt 40 PD
end
to initObjects
   do_while 
   [   repeat maxRocks 
      [   rocks.repcount=(List offScreenX offScreenY)
         rocksRad.repcount=0.5+rnd*0.5
         Home
         rightRoll random 360
         down random 180
         rockori.repCount=Orientation
      ]  ;clear the rock list
      repeat maxRocks 
      [   setItem repCount rockNeighbors 0
      ]  ;clear the neighbor list
   
      rockCounter=0
      loop1=1  ;findXy needs this value--it's the number of rocks so far
      repeat numRocks 
      [   if not (rockCounter == 250) 
         [   findXy "rock
            setItem loop1 rocks (List x y)
         ]
         loop1=loop1+1
      ]
   ][rockCounter == 250]  
   ;if rockCounter == 250, then it tried too long to find spots for new rocks,
   ;so it will start placing them all over again
      
   repeat maxCrystals 
   [   setItem repCount crystals (List offScreenX offScreenY)
   ]  ;clear the crystal list
      
   loop1=1  ;findXy needs this value--now it's the number of crystals so far
   repeat numCrystals 
   [   findXy "crystal
      setItem loop1 crystals (List x y)
      loop1=loop1+1
   ]
end
to inputText iPos txt max_
;  setfocus [MSWLogo Screen]
  setPos iPos
  drawText txt "draw
  drawChar 32 "draw
  iPos=Pos
  PU
  drawText [_] "draw
  Key=0
  string="
;  keyboardon [inputText_keyHit]
  do_while
  [   if Key? [inputText_keyHit]
     wait 1
  ][Key != Char 13]
;  keyboardoff
  output string
end
to inputText_eraseChar char_
  drawText char_ "getLength
  PU setH 0 tbk 9 lt 90 fd textLength
  setPC 0
  PD rt 90 tfd 33 PU tbk 33
  sbitblock round(textLength+1) round(33*textScale)
  tfd 9
end
to inputText_keyHit
  Key=readChar
  ifElse Key == Char 8 [
    if (count string) > 0 [
      if not(count string) == max_ [inputText_eraseChar "_]
      ifElse not(count string) == max_ [
        inputText_eraseChar (Item (count string) string)
      ][
        ;erase either the last character or the underscore--whichever is longer
        drawText [_] "getLength
        underscoreLength=textLength
        drawText (Item (count string) string) "getLength
        ifElse underscoreLength > textLength [
          inputText_eraseChar "_
        ][
          inputText_eraseChar (Item (count string) string)
        ]
      ]
      s=string
      string="
      repeat (count s)-1 [string=Word string (Item repCount s)]
      drawText [_] "draw
    ]
  ][
    if not(count string) == max_ [inputText_eraseChar "_]
    drawText Key "getLength
    if validChar == true [
      if not(count string) == max_ [
        string=Word string Key
        drawText Key "draw
        if (count string) == max_ [
          PU lt 90 fd textLength
          drawText [_] "draw
        ]
      ]
    ]
    if not(count string) == max_ [drawText [_] "draw]
  ]
end
to levelLoop
   do_while
   [   initLevel
      ;this block pauses while the objects' positions are selected, then continues pausing until one second has gone by if it hasn't already
      start=TimeMilli
      ifElse level < maxLevel
      [   displayText (List "Level  level) 0
      ][   displayText [last Level] 0
      ]
      initObjects
      now=TimeMilli
      while [now-start < 1000] [now=TimeMilli]
      
      lifeLoop
      if levelOver == true
      [   displayText (List "Level level "Complete!) 60
         ifElse timeLeft > 0
         [ displayText [Adding Bonus...] 0
         ][displayText [No Bonus] 0
         ]
         
         textCount=1  printText
         textCount=2  printText
         waitStop 30
         if(timeLeft > 0) [addBonus 1]
         waitStop 60
         level=level+1
      ]
      if level > maxLevel
      [   gameOver=true
         displayText [Game Complete!] 120
         ifElse lives > 1 
         [   displayText [Adding Extra Ship Bonus...] 0
         ][   displayText [No Extra Ship Bonus] 30
         ]
         textCount=2  printText
         textCount=3  printText
         waitStop 30
         if lives > 1 [addBonus 2]
         waitStop 30
      ]
   ][(and (quit == false) (reset_ == false) (gameOver == false))]
   if(and quit == false  reset_ == false)
   [ scoreCheck
   ]
end
to lifeLoop
  if lives > 1 [
    displayText [Get Ready!] 60
  ]
  if lives == 1 [
    displayText [last Ship...Get Ready!] 60
  ]
  initLife
  timerOn
  playLoop
  timerOff
  if(and quit == false  reset_ == false) [waitStop 60]
  if lives == 0 [
    gameOver=true
    displayText [Game Over!] 0
    textCount=2 printText
    waitStop 50
  ]
  if(and (    quit == false) (    reset_ == false)
            (gameOver == false) (levelOver == false))
  [
    lifeLoop
  ]
end
to loadScores
  hiNames=Array 10
  AM_Hiscores=Array 10
  openRead "AM_Hiscores
  setReader "AM_Hiscores
  tempWord=readWord
  repeat 10
  [ tempWord1=readWord
    tempWord2="
    add=repCount
    repeat (count tempWord1)
    [   tempWord2=Word tempWord2
          Char mod ((ASCII (Item repCount tempWord1))-(117+add)) 256
    ]
    setItem repCount hiNames tempWord2
    tempWord1=readWord
    tempWord2="
    repeat (count tempWord1)
    [   tempWord2=Word tempWord2
          Char mod ((ASCII (Item repCount tempWord1))-(117+add)) 256
    ]
    ifElse tempWord2 == "
    [   setItem repCount AM_Hiscores 0
    ][
      setItem repCount AM_Hiscores tempWord2
    ]
  ]
  close "AM_Hiscores
  setReader []
end
to menuOn action
  if action == true [buttonOn 1 false]
  buttonOn 2 action
  buttonOn 3 action
  buttonOn 4 action
  buttonOn 5 action
  buttonOn 6 action
  menuOn_=action
  if and action == false playing == false [buttonOn 1 true]
end
to obk d
  bk d*open_scale
end
to ofd d
  fd d*open_scale
end
to ofdd d
  fd d*open_scale
  if not(open_quit == true) [waitStop 4]
end
to open
  clearBox
  WindowMode
  playing=false
  open_quit=false
  if score > 0 [textCount=2 printText]
  setPenSize (List round(3*open_scale) round(3*open_scale))
  setTurtle turtle0
  drawStars
  menuOn menuOn_
  if open_quit == false [waitStop 8]
  if ee2 == true [open_author]
  open_ASTEROID
  open_MINER
  open_turtleO
  if open_quit == false [waitStop 60]
  open_author
  repeat 7 [if open_quit == false [waitStop 30]]
  clearBox
  setTurtle turtle2 hT
  if(and displayHighScores_ == false displayInstructions_ == false open_quit == false)
  [   menuOn menuOn_
  ]
  if open_quit == false [displayHighScores]
  ifElse displayHighScores_ == true
  [ displayHighScores
    buttonOn 7 true
;    (keyboardon [processKey keyboardvalue]
;                [processKey keyboardvalue+200])
    do_while [waitStop 10] [displayHighScores_ == true]
  ][
    repeat 8 [if not(open_quit == true) [waitStop 30]]
  ]
  if displayHighScores_ == true
  [ displayHighScores
    buttonOn 7 true
;    (keyboardon[processKey keyboardvalue]
;               [processKey keyboardvalue+200])
    do_while [waitStop 10] [displayHighScores_ == true]
  ]
  if displayInstructions_ == true
  [ displayInstructions
    buttonOn 8 true
;    (keyboardon[processKey keyboardvalue]
;               [processKey keyboardvalue+200])
    do_while [waitStop 10] [displayInstructions_ == true]
  ]
end
to open_ASTEROID
  setPC RGB .78 .47 1
  ;A
  PU  setPos Apos
  setH 180  obk 7  PD
  setH 198
  ofd 55  oPos=Pos  ofdd 105  obk 160
  lt 30  ofdd 80
  obk 20
  setH 270  setPos oPos  if not(open_quit == true) [wait 4]
  ;S
  setPC RGB .71 .49 1
  PU  setPos Apos  setH 90  ofd 70  PD
  setH 280  ofdd 30
  lt 80  ofdd 30
  lt 115  ofdd 40
  rt 90  ofdd 40
  rt 80  ofdd 48
  ;T
  setPC RGB .65 .51 1
  PU  setPos Apos  setH 90  ofd 90  PD
  ofdd 40  obk 20
  rt 95  ofdd 70
  ;E
  setPC RGB .58 .53 1
  PU  setPos Apos  setH 90  ofd 140  PD
  setH 100  ofdd 40  obk 40
  rt 85  ofdd 60  obk 30
  lt 90  ofdd 25  obk 25  rt 90
  ofd 30
  lt 95  ofdd 35
  ;R
  setPC RGB .52 .55 1
  PU  setPos Apos  setH 90  ofd 190  PD
  setH 180
  ofdd 60  obk 60
  lt 60  ofdd 30
  rt 120  ofdd 30
  lt 100  ofdd 50
  ;O skipped until later but pause like it is being done
  waitStop 8
  ;I
  setPC RGB .45 .57 1
  PU  setPos Apos  setH 90  ofd 290  PD
  setH 85
  ofdd 14  obk 7
  rt 90  ofdd 70
  lt 80  obk 7  ofdd 14
  ;D
  setPC RGB .39 .59 1
  PU  setPos Apos  setH 90  ofd 320  PD
  setH 175
  ofdd 75  oPos=Pos  obk 75
  lt 70  ofdd 40
  rt 60  ofdd 30
  setPos oPos  if not(open_quit == true) [waitStop 4]
end
to open_author
  local "fontAttribList
  setTurtle turtle0
  PU
  setPos Apos
  setH 180
  ofd 207
  rt 90
  ofd 50
  rt 180
  setPC RGB .7 .42 .9
  ifelse (item 3 LogoVersion)=="Windows
  [  setLabelFont [Times]
  ][ setLabelFont [FreeSerif]
  ]
  ifElse ee2 == false
     [Label [by Dan Gerhards]]
     [Label [by Dan Gerhards whose birthday is today!]]
end
to open_loop
  open
  if not(open_quit == true) [open_loop]
end
to open_MINER
  ;M
  setPC RGB .78 .49 1
  PU  setPos Mpos  PD
  setH 195  ofdd 70  obk 70
  lt 65  ofdd 30
  lt 80  ofdd 30
  rt 120  ofdd 70
  ;I
  setPC RGB .65 .51 1
  PU  setPos Mpos  setH 90  ofd 75  PD
  setH 95
  ofdd 14  obk 7
  rt 86  ofdd 65
  lt 85  obk 7  ofdd 14
  ;N
  setPC RGB .58 .53 1
  PU  setPos Mpos  setH 90  ofd 110  PD
  setH 185  ofdd 60  obk 60
  lt 32  ofdd 72
  lt 151  ofdd 60
  ;E
  setPC RGB .52 .55 1
  PU  setPos Mpos  setH 0  obk 5  setH 90  ofd 165  PD
  setH 87  ofdd 40  obk 40
  rt 95  ofdd 55  obk 30
  lt 90  ofdd 25  obk 25  rt 90
  ofd 30
  lt 85  ofdd 35
  ;R
  setPC RGB .45 .57 1
  PU  setPos Mpos  setH 90  ofd 220  PD
  setH 180
  ofdd 70  obk 40  oPos=Pos  obk 30
  lt 80  ofdd 30
  rt 45  ofdd 15
  setPos oPos  if not(open_quit == true) [wait 4]
  lt 8  ofdd 115
end
to open_stars
  PU
  white=RGB 1 1 1
  repeat 1000
  [ x=random(round displayWidth)
    y=random(round displayHeight)
    setPixel List x-displayWidth/2 y-displayHeight/2 white
  ]
end
to open_turtleO
  setTurtle turtle2
  PU  setXY (first Apos)+220*open_scale displayHeight/2
  speed=1
  oy=last Pos
  ox=first Pos
  bounce=0
  setUpdateGraph false
  if open_quit != true [open_turtleO_animate]
  setUpdateGraph true
  setY 40*open_scale
end
to open_turtleO_animate
   wait 1
   speed=speed+1*open_scale
   oy=oy-speed
   if oy < 10*open_scale
   [   oy=10*open_scale
      speed=-speed*0.5
      bounce=bounce+1
      setY oy
      wait 1
   ]
   setY oy*open_scale
   otaBuffer=BitCopy bitTurtleSizeX bitTurtleSizeY
   BitPaste bitTurtle
   updateGraph
   if and  bounce < 3  open_quit == false
   [   BitPaste otaBuffer
      open_turtleO_animate
   ]
end
to pauseGame
   local "keepOff
   keepOff=false
   pauseNow=false
   setTurtle turtle1
   setPos List 0-57*scale 0-43*scale
   ;  setbitindex 20
   sbitcut round(110*scale) round(90*scale)
   
   buttonOn 9 true
   buttonOn 10 true
   
   if timer == false [keepOff=true]
   timerOff
   ;  mouseon [buttonHit] [] [] [] []
   paused=true
   do_while 
   [   if key? 
      [   ignore readChar  
         paused=false
      ]
   ][paused == true]
   ;  mouseoff
   if keepOff == false [timerOn]
   
   buttonOn 9 false
   buttonOn 10 false
   
   setTurtle turtle1
   setPos List 0-57*scale 0-43*scale
   ;  setbitindex 20
   ;  BitPaste
   wait 10
end
to playLoop
  noRefresh
   setUpdateGraph false
  do_while
  [ start=TimeMilli
  
      clearScreen
      drawPlayground
    if(or not(oldShipX == shipXInt) not(oldShipY == shipYInt)
          not(oldDr == dr) exploding == true leaving == true
          erase_ == true blink == 0)
    [   if behindRock == false 
      [
      ;   drawShip oldShipX oldShipY oldDr "erase
      ]
    ]
    if exploding == true
    [ expStep=expStep+1
      if expStep == numExpSteps [lives=lives-1]
    ]
    if leaving == true
    [ leaveStep=leaveStep+1  shrinkShip
      if leaveStep == numLeaveSteps [levelOver=true]
    ]
    if (and levelOver == false behindRock == false)
    [ drawShip shipXInt shipYInt dr "draw
    ]
    oldBehindRock=behindRock
    behindRock=false
    setPos (List shipX shipY)
    if (leaving == false)
    [ hitCrystalCheck
    ]
    hitRockCheck
    ;if the ship just emerged from behind a rock, draw it now...
    if (and levelOver == false behindRock == false oldBehindRock == true) [
      drawShip shipXInt shipYInt dr "redraw
    ]
    ;...and if it just went behind one, erase it now
     if (and behindRock == true oldBehindRock == false) [
;      drawShip shipXInt shipYInt dr "erase
    ]
   printText
;    if expStep < numExpSteps [printText]  
    ;the if keeps it from showing the taking of a life just yet
    if and(exploding == true)(expStep == 0) [
      setPos expCtr
;      setbitindex 1
;      BitPaste
      fragments=(Array numFrags 1)
      repeat numFrags [
        setItem repCount fragments
           (List ((random 3)/2)  ((random 3+2)/2)  (random 360)
                ((random 61)+10)  ((random 4)*0.25+0.5)  ((random 6)*0.25)
                 (random 2)  ((random 341)+10) ((random 20)/10)
          )
       ]
    ]
    if expStep == 2 [
      setPos expCtr
;      sbitblock (round shipRad*2.5*scale) (round shipRad*2.5*scale)
    ]
    ifElse glintStep < 20 [
      glintStep=glintStep+1
      glint "animate
    ][
      glint?=random 100
      if glint? == 0 [
        glintCrystal=(random (level*2+2))+1   ;level*2+2 is the number of crystals the level started with
        y=(last (Item glintCrystal crystals))
        if and ((round y) == y) not(y == offScreenY) [glintStep=0]  ;if round y ! ==  y, then crystal is close to a rock
      ]
    ]
    oldShipX=shipXInt
    oldShipY=shipYInt
    oldDr=dr
    if leaving==true
    [   thrust=true
    ]
    processCommand
    shipX=shipX+shipDeltaX
    shipY=shipY+shipDeltaY
    wrapCheck
    shipXInt=round shipX
    shipYInt=round shipY
      updateGraph
      
    now=TimeMilli
    while [now-start < 10] [now=TimeMilli]
    waitStop 1
  ][(and (quit == false) (reset_ == false)
     (expStep < numExpSteps) (levelOver == false))]
   ;the "10" is the animation delay
   refresh
   setUpdateGraph true
end
to printText
  setTurtle turtle1
  ;To speed things up, only one of the three objects at the top of the screen is printed at a time.
  ;They need to be continuously printed though, because the ship can fly through them.
;  if textCount == 1 
;  [ ;paste "Bonus"
    setPos (List (displayWidth/2-displayWidth+4*scale)
            (displayHeight/2-textBarHeight+((textBarHeight-18*scale)/2))
         )
;    setbitindex 4
;    BitPaste
      BitPaste bitBonus
    ;paste Time left
    setPos(List (displayWidth/2-displayWidth+bonusLength+7*scale)
            (displayHeight/2-textBarHeight+((textBarHeight-18*scale)/2))
         )
    setH 90
    ;noyield keeps Time from changing while it is being printed
;    noyield
    repeat count (timeLeft*5) [
;      setbitindex (item repcount (timeLeft*5))+10
;      BitPaste
         BitPaste bitNumbers.int Item repCount (timeLeft*5)
      fd round(13*scale)
    ]
;    yield
    sbitblock round(10*scale)+1 round(18*scale)+1
;  ]
;  if textCount == 2 [
    ;paste "Score"
    setPos(List ((0-((count score)*13+scoreLength*scale))/2)
            (displayHeight/2-textBarHeight+((textBarHeight-18*scale)/2))
         )
    setH 90
    sbitblock round(scoreLength+scale)+1+5*textScale round(18*scale+scale)+1
;    setbitindex 3
;    BitPaste
      BitPaste bitScore
    fd round(scoreLength+2*scale)
    ;paste score
    repeat count score [
      sbitblock round(13*scale+scale)+1 round(18*scale+scale)+1
;      setbitindex (item repcount score)+10
;      BitPaste
         BitPaste bitNumbers.int Item repCount score
      fd round(13*scale)
    ]
;  ]
;  if textCount == 3  [ ;print extra lives left
    extraLives=lives-1
    if extraLives < 0 [extraLives=0]
    setPos(List (displayWidth/2-4)
            (displayHeight/2-textBarHeight+((textBarHeight-17*scale)/2))
         )
    setH 270
;    setbitindex 6
    repeat extraLives
    [ fd 20*scale
      BitPaste bitShip
    ]
    repeat 3-extraLives
    [ fd 20*scale
      sbitblock round(18*scale) round(18*scale)
    ]
;  ]
  textCount=textCount+1
  if textCount == 4 [textCount=1]
end
to processCommand
   if exploding == false 
   [   if turnLt == true [dra=dra-2]
      if turnRt == true [dra=dra+2]
      dr=dr+dra
      if dr > 360 [dr=dr-360]
      if dr < 0 [dr=dr+360]
      if thrust == true 
      [   ;sin and cos are backwards because Logo calls 0 degrees straight up
         xAccel=(Sin (dr))*acceleration
         yAccel=(Cos (dr))*acceleration
   
         shipDeltaX=shipDeltaX+xAccel
         shipDeltaY=shipDeltaY+yAccel
   
         if leaving == false 
         [   ;If the ship is at maximum speed, get rid of the extra speed,
            ; but leave the direction alone.
            speed2=((shipDeltaX)*(shipDeltaX)+(shipDeltaY)*(shipDeltaY))
            if(maxSpeed2-speed2 < 0)
            [   shipDeltaX=(maxSpeed2*shipDeltaX/speed2)
               shipDeltaY=(maxSpeed2*shipDeltaY/speed2)
            ]
         ]
      ]
   ]
   if pauseNow == true [pauseGame]
end
to processKey Key
;the first key of each pair is querty, the second is Dvorak
  if playing == true
  [ if (or Key == "l Key == "n)
    [ turnRt=true]
    if (or Key == "j Key == "h)
    [ turnLt=true]
    ifElse (or Key == "k Key == "t)
    [ thrust=true]
    [ if leaving == false [thrust=false  erase_=true]
    ]
    if (and Key == "p paused == false)
    [pauseNow=true]
    if (or Key == "x  Key == "q  Key == Char 27)
    [   quit=true  
       open_quit=true
    ]
    if paused == true
    [ if Key == "c [paused=false]
      if Key == "r [paused=false  reset_=true]
    ]
  ]
  if playing == false
  [ ifElse doneButtonOn == false
    [ if Key == "p [commandPlay]
      if Key == "i [commandInstructions]
      if Key == "h [commandHighScores]
      if (or Key == "x  Key == "q  Key == Char 27)
      [   quit=true  open_quit=true
      ]
      if Key == "m [ifElse menuOn_ == true [menuOn false] [menuOn true]]
    ][
      if Key == "d [commandDone]
    ]
  ]
end
to saveScores
  openWrite "AM_Hiscores
  setWriter "AM_Hiscores
  print "AsteroidMinerHS
  repeat 10 [
    tempWord1=Item repCount hiNames
    tempWord2="
    add=repCount
    repeat (count tempWord1)
    [   tempWord2=Word tempWord2
          Char mod ((ASCII (Item repCount tempWord1))+ 117+add) 256
    ]
    print tempWord2
    tempWord1=Item repCount AM_Hiscores
    tempWord2="
    repeat (count tempWord1)
    [   tempWord2=Word tempWord2
          Char mod ((ASCII (Item repCount tempWord1))+ 117+add) 256
    ]
    ifElse tempWord2 == "0
    [   print "
    ][
      print tempWord2
    ]
  ]
  print "AsteroidMinerHS
  close "AM_Hiscores
  setWriter []
end
to sbit_position
  sPos=Pos
  sTurtle=Turtle
  sHeading=Heading
  setTurtle turtle1
  PU
  setPos sPos
  setH 0
;  bk round(scale)-1
;  rt 90 bk round(scale)-1
end
to sbitblock swidth sheight
  sbit_position
  fillRect [-2 -2](List swidth sheight)+2
  setTurtle sTurtle
  setPos sPos
  setH sHeading
end
to sbitcut swidth sheight
  sbit_position
;  bitcut swidth+(round(scale)-1)*2 sheight+(round(scale)-1)*2
  setTurtle sTurtle
  setPos sPos
  setH sHeading
end
to scoreCheck
  loadScores
  ifElse score > Item 10 AM_Hiscores [
    displayText [New High Score!] 0
    ;put the score on top of the screen
    textCount=2
    printText
    drawText [Enter your name Dan_] "getLength  ;get the length with a typical(?) input
    name_=inputText (List (0-textLength/2) (0-18*scale*3)) [Enter your name] 11
    index=11
    done=false
    do_while [
      index=index-1
      ifElse score > Item index-1 AM_Hiscores [
        setItem (index) (AM_Hiscores) (Item index-1 AM_Hiscores)
        setItem (index) (hiNames) (Item index-1 hiNames)
      ][setItem (index) (AM_Hiscores) (score)
         setItem (index) (hiNames) (name_)
         done=true
      ]
    ][and (done == false) (index > 2)]
    if done == false [
      setItem 1 (AM_Hiscores) (score)
      setItem 1 (hiNames) (name_)
      done=true
    ]
    saveScores
    clearBox
    setTurtle turtle2 hT
    open_quit=false
    displayHighScores
    repeat 3 [if not(open_quit == true) [waitStop 30]]
  ][
    setWriter[]
    displayText [Final Score] 0
    ;put the score on top of the screen
    textCount=2
    printText
    waitStop 180
  ]
end
to setShipSize rad
  shipRad=rad
  shipSide=((Sin 22.5)*shipRad*2)
  shipRad3=shipRad/3
  shipRad6=shipRad/6
  shipRad6Diag=shipRad/6*1.414
end
to shrinkShip
  shipRad=shipRad-normalShipRad/numLeaveSteps
  if shipRad < 0 [shipRad=0]
  setShipSize shipRad
end
to timerOff
;  if timer == true [cleartimer 1]
  timer=false
end
to timerOn
  ;make Windows call this code (timeLeft stuff) every 200 milliseconds
;  settimer 1 200 [if timeLeft > 0 [make "timeLeft timeLeft-1]]
  timer=true
end
to waitStop delay
  turnRt=false
  turnLt=false
  if leaving==false [thrust=false]
  repeat delay [
    updateGraph 
    wait 1
    if or reset_ == true quit == true [stop]
    if pauseNow == true [pauseGame]
    dispatchMessages
    if Key? [processKey readChar]
    if MouseButtons==1 [while [MouseButtons==1][] buttonHit]
  ]
end
to wrapCheck
;if the center of the object is out of the box, wrap it around to the other side
  if shipX > (gameWidth/2) [
    shipX=(shipX-gameWidth)
  ]
  if shipX < (gameWidth/2-gameWidth)[
    shipX=(shipX+gameWidth)
  ]
  if shipY < (displayHeight/2-displayHeight)[
    shipY=(shipY+displayHeight)
  ]
  if shipY > (displayHeight/2) [
    shipY=(shipY-displayHeight)
  ]
end