RemiCraft

Racer

Jun 15th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.79 KB | None | 0 0
  1. --loadscreen:
  2. function loadscreen()
  3.   term.setBackgroundColor(colors.brown)
  4.   term.setCursorPos(1,1)
  5.   term.clear()
  6.   term.setTextColor(colors.white)
  7.   term.setCursorPos(1,1)
  8.   image = paintutils.loadImage(AppDataPath.."loadimage")
  9.   paintutils.drawImage(image,1,2)
  10.   term.setCursorPos(1,19)
  11.   term.write("by Konlab")
  12.   sleep(5)  
  13.   term.setTextColor(colors.white)
  14. end
  15. --config functions:
  16. AppDataPath = "/.Konlab/Racer/"--if pockyOS then change this to /pockyOS/AppData/Racer.Konlab/.Konlab/Racer
  17. AD1 = "/.Konlab"--if pockyOS then change this to /pockyOS/AppData/Racer.Konlab/.Konlab
  18. AD2 = "/.Konlab/Racer"--if pockyOS then change this to AppDataPath
  19. HS = 0
  20. function check()
  21. --  error(":P you tried raecr not racer!")
  22.  
  23.  
  24.   if not fs.exists(AD1) then
  25.     fs.makeDir(AD1)
  26.   end
  27.   if not fs.exists(AD2) then
  28.     fs.makeDir(AD2)
  29.   end
  30.   if not fs.exists(AppDataPath) then
  31.     error("Can't create AppData files, create theese files manually!Try creating /.Konlab/Racer [if pockyOS:pockyOS/AppData/Racer.Konlab]! If still errors report bug at forums!")
  32.   end
  33.   if not fs.exists(AppDataPath.."hs") then
  34.     Hfile = fs.open(AppDataPath.."hs","w")
  35.     Hfile.write("0")
  36.     Hfile.close()
  37.    
  38.   end
  39.   if not fs.exists(AppDataPath.."bs") then
  40.     saveBeginSpeed(1)
  41.   end
  42.   if not fs.exists(AppDataPath.."sp") then
  43.     setSprite("custom","o-o","|+|","o-o")
  44.   end
  45. if not fs.exists(AppDataPath.."loadimage") then
  46.     shell.run("pastebin","get","eBXhKFSm",AppDataPath.."loadimage")
  47.   end
  48. end
  49. function getHighScore()
  50.   Hfile = fs.open(AppDataPath.."hs","r")
  51.   HSs = Hfile.readLine()
  52.   HS = tonumber(HSs)
  53.   Hfile.close()
  54.    
  55. end
  56. function setHighScore(newhs)
  57.   Hfile = fs.open(AppDataPath.."hs","w")
  58.   Hfile.write(newhs)
  59.   HS = newhs
  60.   Hfile.close()
  61. end
  62. beginSpeed = 1
  63. function saveBeginSpeed(bspeed)
  64.   Sfile = fs.open(AppDataPath.."bs","w")
  65.   Sfile.write(bspeed)
  66.   beginSpeed = bspeed
  67.   Sfile.close()                                          
  68. end
  69. function getBeginSpeed()
  70.   Sfile = fs.open(AppDataPath.."bs","r")
  71.   beSt = Sfile.readLine()
  72.   beginSpeed = beSt
  73.   Sfile.close()  
  74. end
  75. sapi = false
  76. function LoadSpriteAPI()
  77.  
  78.     os.loadAPI(AppDataPath.."SpriteAPI")
  79.  
  80. end
  81. --sprite variables, don't make it local:
  82. spriteType = "default"
  83. spriteline1 = "o-o"
  84. spriteline2 = "|+|"
  85. spriteline3 = "o-o"
  86. function getSprite()
  87.   spfile = fs.open(AppDataPath.."sp","r")
  88.   spriteType = spfile.readLine()
  89.   spriteline1 = spfile.readLine()
  90.   if spriteline1 == nil then
  91.     spriteline1 = "err"
  92.   end
  93.   spriteline2 = spfile.readLine()
  94.   if spriteline2 == nil then
  95.     spriteline2 = "or:"
  96.   end
  97.   spriteline3 = spfile.readLine()
  98.   if spriteline3 == nil then
  99.     spriteline3 = "fmt"
  100.   end
  101.   spfile.close()
  102. end
  103. function setSprite(type,line1,line2,line3)
  104.   spfile = fs.open(AppDataPath.."sp","w")
  105.   spfile.write(tostring(type).."\n"..tostring(line1).."\n"..tostring(line2).."\n"..tostring(line3).."\n")  
  106.   spfile.close()
  107. end
  108. --menu and game functions:
  109. function spriteSet()
  110.   while true do
  111.   term.setTextColor(colors.black)
  112.   term.setBackgroundColor(colors.red)
  113.   term.clear()
  114.   term.setCursorPos(1,1)
  115.   print("Racer - Sprite Settings")
  116.   print()
  117.   print("Sprite style:")
  118.   getSprite()
  119.   print(spriteType)
  120.   print()
  121.   term.setBackgroundColor(colors.white)
  122.   term.setCursorPos(1,6)
  123.   getSprite()
  124.   print(spriteline1)
  125.   print(spriteline2)
  126.   print(spriteline3)
  127.   print()
  128.   print("Set to default              ")
  129.   print()
  130.   print("<-- Go Back                 ")
  131.   term.setTextColor(colors.red)
  132.   event99,button99,x18,y18 = os.pullEvent("mouse_click")
  133.   if y18 == 12 then
  134.     return
  135.   end
  136.   if y18 == 10 then
  137.     setSprite("default","o-o","|+|","o-o")
  138.   end
  139.   if y18 == 6 then
  140.     term.setCursorPos(1,6)
  141.     term.write("   ")
  142.     term.setCursorPos(1,6)
  143.     input = read()
  144.     if #input > 3 or #input < 1 then
  145.       term.setCursorPos(1,6)
  146.       term.write("o-o")
  147.       spriteline1 = "o-o"
  148.       setSprite("custom",spriteline1,spriteline2,spriteline3)
  149.     else
  150.       spriteline1 = input
  151.       setSprite("custom",spriteline1,spriteline2,spriteline3)
  152.     end
  153.   elseif y18 == 7 then
  154.     term.setCursorPos(1,7)
  155.     term.write("   ")
  156.     term.setCursorPos(1,7)
  157.     input = read()
  158.     if #input > 3 or #input < 1 then
  159.       term.setCursorPos(1,7)
  160.       term.write("|+|")
  161.       spriteline2 = "|+|"
  162.       setSprite("custom",spriteline1,spriteline2,spriteline3)
  163.     else
  164.       spriteline2 = input
  165.       setSprite("custom",spriteline1,spriteline2,spriteline3)
  166.     end
  167.   elseif y18 == 8 then
  168.     term.setCursorPos(1,8)
  169.     term.write("   ")
  170.     term.setCursorPos(1,8)
  171.     input = read()
  172.     if #input > 3 or #input < 1 then
  173.       term.setCursorPos(1,8)
  174.       term.write("o-o")
  175.       spriteline3 = "o-o"
  176.       setSprite("custom",spriteline1,spriteline2,spriteline3)
  177.     else
  178.       spriteline3 = input
  179.       setSprite("custom",spriteline1,spriteline2,spriteline3)
  180.     end
  181.   end  
  182.  
  183.   end  
  184. end
  185. yourSide = "l"
  186. ver = "3.0"
  187. function printcar(yc,side,color)
  188.   getSprite()
  189.   if spriteType == "default" then
  190.   if color == colors.white then
  191.     color = colors.red
  192.   end
  193.   if side == "l" then
  194.     xc = 10
  195.   else
  196.     xc = 14
  197.   end
  198.   term.setCursorPos(xc,yc)
  199.   term.setBackgroundColor(color)
  200.   term.write("   ")
  201.   term.setCursorPos(xc+1,yc-1)
  202.   term.write(" ")
  203.   term.setCursorPos(xc,yc-2)
  204.   term.write("   ")
  205.  
  206.   end
  207.   if spriteType == "custom" then
  208.     if color == colors.white then
  209.       color = colors.red
  210.     end
  211.     if side == "l" then
  212.       xc = 10
  213.     else
  214.       xc = 14
  215.     end
  216.     term.setCursorPos(xc,yc)
  217.     term.setBackgroundColor(colors.black)
  218.     term.setTextColor(color)
  219.     if color == colors.black then
  220.     term.setTextColor(colors.black)
  221.     else
  222.     term.setTextColor(color)
  223.     end
  224.    
  225.     if color ~= colors.black then
  226.    
  227.       term.write(spriteline1)
  228.       term.setCursorPos(xc,yc-1)
  229.       term.write(spriteline2)
  230.       term.setCursorPos(xc,yc-2)
  231.       term.write(spriteline3)
  232.     else
  233.       term.write("   ")    
  234.       term.setCursorPos(xc,yc-1)
  235.       term.write("   ")
  236.       term.setCursorPos(xc,yc-2)
  237.       term.write("   ")
  238.      
  239.     end
  240.    term.setTextColor(colors.white)      
  241.   end    
  242. end
  243. bgsSt = 0
  244. function bgsStToNumber()
  245.   toret = tonumber(bgsSt)
  246. --  return toret
  247.   bgsSt = toret
  248. end
  249. function options()
  250.   term.setTextColor(colors.black)
  251.   term.setBackgroundColor(colors.red)
  252.   term.clear()
  253.   term.setCursorPos(1,1)
  254.   print("Racer - Options")
  255.   print("")
  256.   print("Begin speed:")
  257.   term.setBackgroundColor(colors.white)
  258.   print("                              ")
  259.   print("")
  260.   print("Sprite settings...            ")
  261.   print("")
  262.   print("AppData options...            ")
  263.   print("")
  264.   print("<-- Go back                   ")
  265.   term.setBackgroundColor(colors.red)
  266.   getBeginSpeed()
  267. --  beSt = beSt^-1
  268.   term.setCursorPos(2,4)
  269.   beSt = tostring(beginSpeed^-1)
  270.   term.setBackgroundColor(colors.white)
  271.   term.write(beSt)
  272.   term.setBackgroundColor(colors.red)
  273.  
  274.  
  275.   while true do
  276.     ev4,but4,x8,y8 = os.pullEvent("mouse_click")
  277.     if y8 == 4 then
  278.       term.setCursorPos(2,4)
  279.       term.setBackgroundColor(colors.white)    
  280.       term.write("                     ")
  281.       term.setCursorPos(2,4)
  282.       bgsSt = read()
  283.       ok2,err2 = pcall(bgsStToNumber)    
  284.       if not ok2 then
  285.         bgsSt = 1
  286.       end        
  287.       if bgsSt < 1 then
  288.         bgsSt = 1
  289.       end  
  290.       if bgsSt > 20 then
  291.         bgsSt = 20
  292.       end
  293.       term.setCursorPos(2,4)
  294.       term.write("                         ")
  295.       term.setCursorPos(2,4)
  296.       term.write(tostring(bgsSt))
  297.       editedBS = bgsSt^-1
  298.       saveBeginSpeed(editedBS)      
  299.     elseif y8 == 6 then
  300.       spriteSet()
  301.       return
  302.     elseif y8 == 8 then
  303.       term.setBackgroundColor(colors.red)
  304.       term.clear()
  305.       term.setCursorPos(1,1)
  306.       print("AppData Options...")
  307.       print()
  308.       print("Config & files:")
  309.       list = fs.list(AppDataPath)
  310.       foundsa = false
  311.       iserr = false
  312.       for k,v in pairs(list) do
  313.         if v == "sp" then
  314.           print(v.."   - sprite data")
  315.         elseif v == "hs" then
  316.           print(v.."   - high score data")
  317.         elseif v == "bs" then
  318.           print(v.."   - config file")
  319.         elseif v == "SpriteAPI" then
  320.           print(v.."   - sprite settings' API")
  321.           foundsa = true
  322.           elseif v == "loadimage" then
  323.             print(v.."   - loadscreen image")
  324.          
  325.         else
  326.           print(v.." error:can't identify")
  327.           iserr = true
  328.          
  329.         end  
  330.          
  331.       end
  332.       if foundsa == false then
  333.         print()
  334.         print("Please enable HTTP API and try restarting the game")
  335.        
  336.         print("Click to continue")
  337.       elseif iserr then
  338.         print()
  339.         print("Please report at forums with the uniden. file's name")
  340.         print("Click to continue")
  341.       else
  342.         print()
  343.         print("No problems found.")
  344.         print("Click to continue")
  345.       end
  346.       os.pullEvent("mouse_click")
  347.       return
  348.     elseif y8 == 10 then
  349.       return
  350.      
  351.     end
  352.   end
  353. end
  354. function credits()
  355.   term.clear()
  356.   term.setCursorPos(1,1)
  357.   print("Racer - Controls")
  358.   print("A - go to left side")
  359.   print("D - go to right side")
  360.   print("W - speed up game")
  361.   print()
  362.   print("Current version: "..ver)
  363.   print("Click to continue")
  364.   os.pullEvent("mouse_click")
  365. end
  366. char = "s"
  367. function pull()
  368. while true do
  369. eventppp,char = os.pullEvent("char")
  370. if char == "w" or char == "a" or char == "d" then
  371.   return
  372. end
  373. end
  374. end
  375. spUp = 0
  376. speed = tonumber(beginSpeed)
  377. function slep()
  378. sleep(speed)
  379. spUp = spUp + 1
  380. term.setCursorPos(18,18)
  381. --term.write("spUp:"..spUp)
  382. if speed > 0.25 then
  383. if spUp == 15 then
  384.   speed = speed - speed / 3
  385.   spUp = 0
  386.    
  387. end
  388. elseif speed > 0.1 then
  389.   if spUp == 25 then
  390.   speed = speed - speed / 4
  391.   spUp = 0
  392.   end
  393. else
  394. if spUp == 50 then
  395. speed = speed - speed / 10
  396. spUp = 0
  397. end
  398. end
  399. end
  400. function press()
  401.  -- parallel.waitForAny(
  402. parallel.waitForAny(pull,slep)
  403.  
  404.   os.queueEvent("key",10)  
  405.   if char == "a" then
  406.     yourSide = "l"
  407.   elseif char == "d" then
  408.     yourSide =  "r"
  409.   --elseif char == "s" then
  410. --    speed = speed*0.9
  411.   end
  412.  
  413. end
  414. function drawMenu()
  415. term.setBackgroundColor(colors.lightBlue)
  416. term.setTextColor(colors.white)
  417. term.clear()
  418. term.setCursorPos(1,1)
  419. term.setBackgroundColor(colors.blue)
  420. term.write("Racer - by Konlab               ")
  421. print("                                   ")
  422. print("                                  ")
  423. print("")
  424. term.setBackgroundColor(colors.red)
  425. print("Begin race                ")
  426. print("")
  427. print("Controls                  ")
  428. print("")
  429. print("Options                   ")
  430. print("")
  431. print("Exit game                 ")
  432.  
  433. end
  434. function game()
  435.   term.setBackgroundColor(colors.brown)
  436.  
  437.   term.clear()
  438.   term.setCursorPos(1,1)
  439. --  term.setBackgroundColor(colors.black)
  440.   for i=1,20 do
  441.     term.setBackgroundColor(colors.black)
  442.     term.setCursorPos(10,i)
  443.     term.write("       ")  
  444.     term.setBackgroundColor(colors.white)
  445.     term.setCursorPos(13,i)
  446.     term.write(" ")
  447. --    term.setBackgroundColo
  448.   end
  449.  
  450.   --begin of simulation
  451. --    printcar(5,"l",colors.red)
  452. --    printcar(7,"r",colors.blue)
  453. --    printcar(15,"l",colors.green)    
  454. --simulation variables:
  455.   score = 0
  456.   yourSide = "l"
  457.   enemyPos = 3
  458.   enemySide = "r"
  459.   enemy2Pos = -8
  460.   enemy2Side = "l"
  461.   getBeginSpeed()
  462.  
  463.   speed = tonumber(beginSpeed) -- the fastest is 0.01 the slowest 1
  464. --main loop of game
  465.  
  466.   while true do
  467.      term.setCursorPos(18,15)
  468.      term.setBackgroundColor(colors.brown)
  469.      term.write("Speed:")
  470.      term.setCursorPos(18,16)
  471.      spe = speed^-1
  472.      term.write(spe)
  473.      term.setBackgroundColor(colors.black)
  474.      printcar(20,yourSide,colors.white)
  475.          
  476.      press()
  477.      
  478.      if yourSide == "l" then
  479.        printcar(20,"r",colors.black)
  480.      else
  481.        printcar(20,"l",colors.black)
  482.      end
  483.      
  484.      --random generating enemies
  485.      if enemyPos == 3 then
  486.        dd = math.random(1,2)
  487.        if dd == 1 then
  488.        enemySide = "l"
  489.        else
  490.        enemySide = "r"
  491.        end
  492.        score = score+1
  493.      end
  494.      if enemy2Pos == 3 then
  495.        score = score+1
  496.      end
  497. --     printcar(enemyPos,enemySide,colors.blue)
  498.      if enemySide == "l" then
  499.      printcar(enemyPos-1,"l",colors.black)
  500.      else
  501.      printcar(enemyPos-1,"r",colors.black)
  502.      end
  503.      printcar(enemyPos,enemySide,colors.blue)
  504.      enemyPos = enemyPos+1
  505. --     if enemyPos > 17 and enemySide == yourSide then
  506.        function lose()
  507.        term.setTextColor(colors.blue)
  508.        term.setBackgroundColor(colors.yellow)
  509.        term.clear()
  510.        term.setCursorPos(1,10)
  511.        print("Your car has crashed")
  512.        sleep(0.5)
  513.        term.setCursorPos(1,12)
  514.        print("Your score is:")
  515.        sleep(0.5)
  516.        
  517.        print(score)
  518.        sleep(0.5)
  519.        term.setCursorPos(1,15)
  520.        print("Your Highscore is:")
  521.        sleep(0.5)
  522.        
  523.        getHighScore()
  524.        if HS < score then
  525.          setHighScore(score)
  526.          print("New highscore!")
  527.        end
  528.        print(HS)
  529.        term.setCursorPos(1,19)
  530.        sleep(0.75)
  531.        print("Click to continue")
  532.        
  533.        os.pullEvent("mouse_click")
  534.        return
  535.        end
  536.      if enemyPos > 17 and enemySide == yourSide then
  537.        lose()
  538.        return
  539.      end
  540.      if enemyPos == 21 then
  541.        enemyPos = 3
  542.      end    
  543.      if enemy2Pos == 21 then
  544.        enemy2Pos = 3
  545.        printcar(20,enemy2Side,colors.black)
  546.        ran = math.random(1,2)
  547.        if ran == 1 then
  548.          enemy2Side = "l"  
  549.        else
  550.          enemy2Side = "r"
  551.        end
  552.      end
  553.        printcar(enemy2Pos-1,enemy2Side,colors.black)
  554.        printcar(enemy2Pos,enemy2Side,colors.green)
  555.        enemy2Pos = enemy2Pos + 1
  556.        
  557.        if enemy2Pos > 17 and enemy2Side == yourSide then
  558.          term.clear()
  559.          term.setCursorPos(1,1)
  560.        --  print("You lose!")
  561. --         sleep(1)
  562. --         print("Click to continue")
  563. --         os.pullEvent("mouse_click")
  564.           lose()
  565.          return
  566.        end
  567.   end  
  568.  
  569.   --end of simulation
  570. --  os.pullEvent("key")
  571. end
  572. function main()
  573.   term.setBackgroundColor(colors.black)
  574.   term.setTextColor(colors.black)
  575.   check()
  576.   loadscreen()
  577. --  LoadSpriteAPI()
  578.   while true do
  579.     drawMenu()  
  580.    
  581.     ev,bu,x,y = os.pullEvent("mouse_click")
  582.     if y == 10 then
  583.       term.setBackgroundColor(colors.black)
  584.       term.setTextColor(colors.white)
  585.       term.setCursorPos(1,1)      
  586.       term.clear()
  587.       break
  588.     end
  589.     if y == 8 then
  590.       options()
  591.     end      
  592.     if y == 6 then
  593.       credits()
  594.     end
  595.     if y == 4 then
  596.       game()
  597.     end
  598.   end
  599. end
  600. kkk = false
  601. --ok,err = pcall(main())
  602. --main()
  603. ok = false
  604. err = "error"
  605. ok,err = pcall(main)
  606. if not kkk then
  607. if err == "Terminated" then
  608. print("Terminated")
  609. else
  610. if ok then
  611. print("Thanks for using Racer game")
  612. else
  613.   term.setBackgroundColor(colors.red)
  614.   term.setTextColor(colors.white)
  615.   term.clear()
  616.   term.setCursorPos(1,1)
  617.   print("Crash report:")
  618.   print()
  619.   term.setBackgroundColor(colors.gray)
  620.   print("                              ")
  621.   print("Oh no Racer has crashed       ")
  622.   print("                              ")
  623.  
  624.   print()
  625.   term.setBackgroundColor(colors.gray)
  626.   term.setCursorPos(1,7)
  627.   term.write("                                 ")
  628.   term.setCursorPos(1,8)
  629.   term.write("                                 ")
  630.   term.setTextColor(colors.red)
  631.   term.setCursorPos(1,7)
  632.   print(err)
  633.   term.setTextColor(colors.white)
  634.   term.setBackgroundColor(colors.red)
  635.   print()
  636.   print("Please report at cc forums!")
  637.  
  638.   print("Press any key to continue!")
  639.   os.pullEvent("key")
  640.   term.setBackgroundColor(colors.black)
  641.   term.clear()
  642.   term.setCursorPos(1,1)
  643. --  print("--Program:end--")
  644.   return
  645. end
  646. end
  647. end
Add Comment
Please, Sign In to add comment