JustDoesGames

Craftman

Nov 8th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.79 KB | None | 0 0
  1.  
  2. -- Beta Game by Just Does Games --
  3.  
  4. sx,sy = term.getSize()
  5. term.clear()
  6.  
  7. settings_menu_text_cursor = "> "
  8.  
  9. settings_menu_text_cursor_color = "white"
  10. settings_menu_background_cursor_color = "black"
  11.  
  12. settings_menu_text_selected_color = "yellow"
  13. settings_menu_background_selected_color = "black"
  14.  
  15. settings_menu_text_deselected_color = "gray"
  16. settings_menu_background_deselected_color = "black"
  17.  
  18. settings_gamemenu_background_color = "gray"
  19.  
  20.  
  21.  
  22. settings_title_background_color = "gray"
  23. settings_title_text_color = "lime"
  24. settings_title = "Project CraftMan"
  25.  
  26. settings_game_speed = .03
  27. settings_game_player_head_color = "blue"
  28. settings_game_player_worm_color = "lime"
  29. settings_game_bot_head_color = "gray"
  30. settings_game_bot_worm_color = "orange"
  31. settings_game_buffer_text = "Loading game..."
  32.  
  33. player_direction = "right"
  34. bot_direction = "left"
  35. bot_turn_speed = 1
  36. bot_turn_direction_speed_low = 0.2
  37. bot_turn_direction_speed_high = 1
  38.  
  39. start = true
  40. prev_menu = {}
  41. singleplayer = true
  42. sel = 1
  43.  
  44. game_screen = {}
  45. game_player_1_controls = 0
  46. game_player_2_controls = 0
  47.  
  48. sm_bot = {
  49. "Lobby",
  50. "----------",
  51. "Game Speed  =>  "..settings_game_speed,
  52. "",
  53. "Player",
  54. "----------",
  55. "Worm Color  =>  "..settings_game_player_worm_color,
  56. "Head Color  =>  "..settings_game_player_head_color,
  57. "",
  58. "Bot",
  59. "----------",
  60. "Worm Color  =>  "..settings_game_bot_worm_color,
  61. "Head Color  =>  "..settings_game_bot_head_color
  62. }
  63.  
  64. sm_player = {
  65. "Lobby",
  66. "----------",
  67. "Game Speed  =>  "..settings_game_speed,
  68. "",
  69. "Player 1",
  70. "----------",
  71. "Worm Color  =>  "..settings_game_player_worm_color,
  72. "Head Color  =>  "..settings_game_player_head_color,
  73. "",
  74. "Player 2",
  75. "----------",
  76. "Worm Color  =>  "..settings_game_bot_worm_color,
  77. "Head Color  =>  "..settings_game_bot_head_color
  78. }
  79.  
  80. function clr() term.clear() end
  81. function cp(x,y) term.setCursorPos(x,y) end
  82. function setText(col) if type(col) ~= "string" then return error("Invalid Text Color, got "..type(col)) end term.setTextColor(colors[col]) end
  83. function setBack(col) if type(col) ~= "string" then return error("Invalid Background Color, got "..type(col)) end term.setBackgroundColor(colors[col]) end
  84.  
  85. function checkColors(text, back)
  86.     if type(text) ~= "string" then return error("Text Color Invalid, got "..type(text)) end
  87.     if type(back) ~= "string" then return error("Background Color Invalid, got "..type(back)) end
  88.  
  89.     if term.getTextColor() ~= text then
  90.         setText(text)
  91.     end
  92.     if term.getBackgroundColor() ~= back then
  93.         setBack(back)
  94.     end
  95. end
  96.  
  97. function display_title()
  98.     local interval = 3
  99.     local cnt = 0
  100.     checkColors(settings_title_text_color, settings_title_background_color)
  101.     cp(1,3)
  102.     for i=1, sx do
  103.         write(" ") if cnt == interval and start == true then cnt = 0 sleep(.001) else cnt = cnt + 1 end
  104.     end
  105.     cp(1,4)
  106.     for i=1, sx do
  107.         write(" ") if cnt == interval and start == true then cnt = 0 sleep(.001) else cnt = cnt + 1 end
  108.     end
  109.     cp(1,5)
  110.     for i=1, sx do
  111.         write(" ") if cnt == interval and start == true then cnt = 0 sleep(.001) else cnt = cnt + 1 end
  112.     end
  113.     if start then sleep(1) end
  114.     cp(math.ceil(sx/2-string.len(settings_title)/2),4)
  115.     if start then textutils.slowPrint(settings_title) sleep(.5) start = false else print(settings_title) end
  116. end
  117.  
  118. function menu(m)
  119.     local sel = 1
  120.     if m ~= prev_menu then clr() display_title() end prev_menu = m sleep(.15)
  121.     while true do
  122.         for i=1, #m do
  123.             cp(math.floor(sx/2-string.len(m[i])/2)-1,math.floor(sy/2)+i-3)
  124.             if sel == i then
  125.                 checkColors(settings_menu_text_cursor_color, settings_menu_background_cursor_color)
  126.                 write(settings_menu_text_cursor)
  127.                 checkColors(settings_menu_text_selected_color, settings_menu_background_selected_color)
  128.             else
  129.                 write("  ")
  130.                 checkColors(settings_menu_text_deselected_color, settings_menu_background_deselected_color)
  131.             end
  132.             print(m[i])
  133.         end sleep(.001)
  134.         a,i = os.pullEvent("key")
  135.         if i == keys.w or i == keys.up then
  136.             if sel == 1 then sel = #m else sel = sel - 1 end
  137.         elseif i == keys.s or i == keys.down then
  138.             if sel == #m then sel = 1 else sel = sel + 1 end
  139.         elseif i == keys.e or i == keys.enter then
  140.             return sel
  141.         end
  142.     end
  143. end
  144.  
  145. function draw_gsm()
  146.     checkColors("white", "black")
  147.     clr() cp(2,1)
  148.     setBack(settings_gamemenu_background_color)
  149.  
  150.     for i=1, sx/2-2 do -- top
  151.         write(" ")
  152.     end
  153.     cp(1,1)
  154.     for i=1, sy/2+5 do -- left
  155.         print(" ")
  156.     end
  157.     for i=1, sx/2-2 do -- bottom
  158.         write(" ")
  159.     end
  160.     for i=1, sy do
  161.         cp(sx/2-1,i) write(" ")
  162.     end
  163.  
  164.     sleep(.3)
  165.  
  166.     display_settings()
  167.  
  168. end
  169.  
  170. function display_settings()
  171.  
  172.     checkColors("black", "black")
  173.     if singleplayer then
  174.         for i=1, #sm_bot do
  175.             cp(sx/2+1,i)
  176.             write(sm_bot[i])
  177.         end
  178.     else
  179.         for i=1, #sm_player do
  180.             cp(sx/2+1,i)
  181.             write(sm_player[i])
  182.         end
  183.     end
  184.  
  185.     sm_bot = {
  186.     "Lobby",
  187.     "----------",
  188.     "Game Speed  =>  "..settings_game_speed,
  189.     "",
  190.     "Player",
  191.     "----------",
  192.     "Worm Color  =>  "..settings_game_player_worm_color,
  193.     "Head Color  =>  "..settings_game_player_head_color,
  194.     "",
  195.     "Bot",
  196.     "----------",
  197.     "Worm Color  =>  "..settings_game_bot_worm_color,
  198.     "Head Color  =>  "..settings_game_bot_head_color
  199.     }
  200.  
  201.     sm_player = {
  202.     "Lobby",
  203.     "----------",
  204.     "Game Speed  =>  "..settings_game_speed,
  205.     "",
  206.     "Player 1",
  207.     "----------",
  208.     "Worm Color  =>  "..settings_game_player_worm_color,
  209.     "Head Color  =>  "..settings_game_player_head_color,
  210.     "",
  211.     "Player 2",
  212.     "----------",
  213.     "Worm Color  =>  "..settings_game_bot_worm_color,
  214.     "Head Color  =>  "..settings_game_bot_head_color
  215.     }
  216.  
  217.     sleep(.2)
  218.  
  219.     setText("lightGray")
  220.     if singleplayer then
  221.         for i=1, #sm_bot do
  222.             cp(sx/2+1,i)
  223.             write(sm_bot[i])
  224.         end
  225.     else
  226.         for i=1, #sm_player do
  227.             cp(sx/2+1,i)
  228.             write(sm_player[i])
  229.         end
  230.     end
  231.  
  232. end
  233.  
  234. function gsm_menu(m)
  235.     while true do
  236.         for i=1, #m do
  237.             cp(2,2+i)
  238.             if sel == i then
  239.                 checkColors(settings_menu_text_cursor_color, settings_menu_background_cursor_color)
  240.                 write(settings_menu_text_cursor)
  241.                 checkColors(settings_menu_text_selected_color, settings_menu_background_selected_color)
  242.             else
  243.                 write("  ")
  244.                 checkColors(settings_menu_text_deselected_color, settings_menu_background_deselected_color)
  245.             end
  246.             print(m[i])
  247.         end sleep(.001)
  248.         a,i = os.pullEvent("key")
  249.         if i == keys.w or i == keys.up then
  250.             if sel == 1 then sel = #m else sel = sel - 1 end
  251.         elseif i == keys.s or i == keys.down then
  252.             if sel == #m then sel = 1 else sel = sel + 1 end
  253.         elseif i == keys.e or i == keys.enter then
  254.             setText("black")
  255.             for i=1, #m do
  256.                 cp(2,2+i)
  257.                 if sel == i then
  258.                     write(settings_menu_text_cursor)
  259.                 else
  260.                     write("  ")
  261.                 end
  262.                 print(m[i])
  263.             end
  264.  
  265.             return sel
  266.         end
  267.     end
  268. end
  269.  
  270. function game_settings_menu()
  271.     local m = {}
  272.     local m_l = {}
  273.     local m_p = {}
  274.     local m_b = {}
  275.  
  276.     if singleplayer then
  277.         m = {"Lobby Settings", "Player Settings", "Bot Settings", "Start Game", "Main Menu"}
  278.     else
  279.         m = {"Lobby Settings", "Player 1 Settings", "Player 2 Settings", "Start Game", "Main Menu"}
  280.     end
  281.  
  282.     local m_l = {"Game Speed", "Back"}
  283.  
  284.     if singleplayer then
  285.         m_p = {"Player Worm Color", "Player Head Color", "Back"}
  286.         m_b = {"Bot Worm Color", "Bot Head Color", "Back"}
  287.     else
  288.         m_p = {"Player 1 Worm Color", "Player 1 Head Color", "Back"}
  289.         m_b = {"Player 2 Worm Color", "Player 2 Head Color", "Back"}
  290.     end
  291.  
  292.     local m_sel = m
  293.  
  294.     local p_l_game_speed = {0.03,0.01,0.5,.3,0.1,0.08,0.06}
  295.     local p_p_worm_color = {"lime","red","orange","yellow","green","blue","purple","lightGray","gray"}
  296.     local p_p_head_color = {"blue","red","orange","yellow","green","purple","gray","lightGray"}
  297.     local p_b_worm_color = {"orange","red","yellow","green","blue","purple","lightGray","gray"}
  298.     local p_b_head_color = {"gray","red","orange","yellow","green","blue","purple","lightGray"}
  299.  
  300.     local p = {2,2,2,2,2}
  301.  
  302.     draw_gsm()
  303.     while true do
  304.  
  305.         local val = gsm_menu(m_sel)
  306.  
  307.         if m_sel == m then
  308.             if val == 1 then
  309.                 m_sel = m_l sel = 1
  310.             elseif val == 2 then
  311.                 m_sel = m_p sel = 1
  312.             elseif val == 3 then
  313.                 m_sel = m_b sel = 1
  314.             elseif val == 4 then
  315.                 return true
  316.             elseif val == #m_sel then
  317.                 clr() display_title() sel = 1
  318.                 return false
  319.             end
  320.         elseif m_sel == m_l then
  321.             if val == 1 then
  322.                 settings_game_speed = p_l_game_speed[p[1]] p[1] = p[1] + 1
  323.                 if p[1] == #p_b_head_color then
  324.                     p[1] = 1
  325.                 end
  326.             elseif val == #m_sel then
  327.                 m_sel = m sel = 1 sel = 1
  328.             end
  329.             display_settings()
  330.         elseif m_sel == m_p then
  331.             if val == 1 then
  332.                 settings_game_player_worm_color = p_p_worm_color[p[2]] p[2] = p[2] + 1
  333.                 if p[2] == #p_b_head_color then
  334.                     p[2] = 1
  335.                 end
  336.             elseif val == 2 then
  337.                 settings_game_player_head_color = p_p_head_color[p[3]] p[3] = p[3] + 1
  338.                 if p[3] == #p_b_head_color then
  339.                     p[3] = 1
  340.                 end
  341.             elseif val == #m_sel then
  342.                 m_sel = m sel = 1
  343.             end
  344.             display_settings()
  345.         elseif m_sel == m_b then
  346.             if val == 1 then
  347.                 settings_game_bot_worm_color = p_b_worm_color[p[4]] p[4] = p[4] + 1
  348.                 if p[4] == #p_b_head_color then
  349.                     p[4] = 1
  350.                 end
  351.             elseif val == 2 then
  352.                 settings_game_bot_head_color = p_b_head_color[p[5]] p[5] = p[5] + 1
  353.                 if p[5] == #p_b_head_color then
  354.                     p[5] = 1
  355.                 end
  356.             elseif val == #m_sel then
  357.                 m_sel = m sel = 1
  358.             end
  359.             display_settings()
  360.         end
  361.     end
  362. end
  363.  
  364. -- Game --
  365.  
  366. function loadingBuffer()
  367.     clr() cp(math.floor(sx/2-string.len(settings_game_buffer_text)/2),2) checkColors("white", "black") print(settings_game_buffer_text) sleep(math.random(1,1.8))
  368. end
  369.  
  370. function player_input_engine()
  371.     local running = true
  372.     if singleplayer then
  373.         while running do
  374.             local a,i = os.pullEvent("key")
  375.             if i == keys.w or i == keys.up then
  376.                 player_direction = "up"
  377.             elseif i == keys.a or i == keys.left then
  378.                 player_direction = "left"
  379.             elseif i == keys.s or i == keys.down then
  380.                 player_direction = "down"
  381.             elseif i == keys.d or i == keys.right then
  382.                 player_direction = "right"
  383.             elseif i == keys.q then
  384.                 running = false
  385.             end
  386.         end
  387.     else
  388.         while running do
  389.             local a,i = os.pullEvent("key")
  390.             if i == keys.w then
  391.                 player_direction = "up"
  392.             elseif i == keys.a then
  393.                 player_direction = "left"
  394.             elseif i == keys.s then
  395.                 player_direction = "down"
  396.             elseif i == keys.d then
  397.                 player_direction = "right"
  398.             elseif i == keys.q then
  399.                 running = false
  400.             end
  401.         end
  402.     end
  403. end
  404.  
  405. function player_movement_engine()
  406.  
  407.     player_direction = "right"
  408.     local player_x, player_y = 1,1
  409.     while true do
  410.         checkColors("black", settings_game_player_worm_color) cp(player_x,player_y) write(" ")
  411.  
  412.         if game_screen[player_x*player_y] == 0 then
  413.             game_player_1_controls = game_player_1_controls + 1
  414.             game_screen[player_x*player_y] = 1
  415.         elseif game_screen[player_x*player_y] == 2 or game_screen[player_x*player_y] == 4 then
  416.             game_player_2_controls = game_player_2_controls - 1
  417.             game_player_1_controls = game_player_1_controls + 1
  418.             game_screen[player_x*player_y] = 1
  419.         end
  420.  
  421.  
  422.         if player_direction == "right" then
  423.             if player_x == sx then player_x = 1 else player_x = player_x + 1 end
  424.         elseif player_direction == "left" then
  425.             if player_x == 1 then player_x = sx else player_x = player_x - 1 end
  426.         elseif player_direction == "up" then
  427.             if player_y == 1 then player_y = sy-1 else player_y = player_y - 1 end
  428.         elseif player_direction == "down" then
  429.             if player_y == sy-1 then player_y = 1 else player_y = player_y + 1 end
  430.         end
  431.         cp(player_x,player_y) checkColors("black", settings_game_player_head_color) write(" ") sleep(settings_game_speed)
  432.  
  433.         --game_screen[player_x*player_y] = 3
  434.  
  435.     end
  436.  
  437. end
  438.  
  439.  
  440. function bot_input_engine()
  441.     local running = true
  442.     local cnt = 0
  443.     local cnt_pos = 1
  444.  
  445.     local seed = {}
  446.     for i=1, 20 do
  447.         seed[#seed+1] = math.random(bot_turn_direction_speed_low, bot_turn_direction_speed_high)
  448.     end
  449.  
  450.     while true do
  451.         sleep(bot_turn_speed)
  452.         if cnt == seed[cnt_pos] then
  453.             local trans = 1
  454.             local directions = {"right", "left", "up", "down"}
  455.             cnt = 0
  456.             cnt_pos = cnt_pos+1
  457.             if cnt_pos == #seed+1 then cnt_pos = 1 end
  458.             repeat
  459.                 trans = math.random(1,4)
  460.             until directions[trans] ~= bot_direction
  461.             bot_direction = directions[trans]
  462.         else
  463.             cnt = cnt + 1
  464.         end
  465.         if game_player_2_controls < game_player_1_controls+60 then
  466.             bot_turn_speed = 0.2
  467.         elseif game_player_2_controls < game_player_1_controls+45 then
  468.             bot_turn_speed = 0.4
  469.         elseif game_player_2_controls < game_player_1_controls+30 then
  470.             bot_turn_speed = 0.6
  471.         elseif game_player_2_controls < game_player_1_controls+20 then
  472.             bot_turn_speed = 0.8
  473.         else
  474.             bot_turn_speed = 1
  475.         end
  476.     end
  477. end
  478.  
  479. function bot_movement_engine()
  480.     while true do
  481.         bot_direction = "left"
  482.         local bot_x, bot_y = sx,sy-1
  483.         while true do
  484.             checkColors("black", settings_game_bot_worm_color) cp(bot_x,bot_y) write(" ")
  485.  
  486.             if game_screen[bot_x*bot_y] == 0 then
  487.                 game_player_2_controls = game_player_2_controls + 1
  488.                 game_screen[bot_x*bot_y] = 2
  489.             elseif game_screen[bot_x*bot_y] == 1 or game_screen[bot_x*bot_y] == 3 then
  490.                 game_player_1_controls = game_player_1_controls - 1
  491.                 game_player_2_controls = game_player_2_controls + 1
  492.                 game_screen[bot_x*bot_y] = 2
  493.             end
  494.  
  495.             if bot_direction == "right" then
  496.                 if bot_x == sx then bot_x = 1 else bot_x = bot_x + 1 end
  497.             elseif bot_direction == "left" then
  498.                 if bot_x == 1 then bot_x = sx else bot_x = bot_x - 1 end
  499.             elseif bot_direction == "up" then
  500.                 if bot_y == 1 then bot_y = sy-1 else bot_y = bot_y - 1 end
  501.             elseif bot_direction == "down" then
  502.                 if bot_y == sy-1 then bot_y = 1 else bot_y = bot_y + 1 end
  503.             end
  504.             cp(bot_x,bot_y) checkColors("black", settings_game_bot_head_color) write(" ") sleep(settings_game_speed)
  505.  
  506.             --game_screen[bot_x*bot_y] = 4
  507.         end
  508.     end
  509. end
  510.  
  511. function player_2_input_engine()
  512.     local running = true
  513.     while running do
  514.         local a,i = os.pullEvent("key")
  515.         if i == keys.up then
  516.             bot_direction = "up"
  517.         elseif i == keys.left then
  518.             bot_direction = "left"
  519.         elseif i == keys.down then
  520.             bot_direction = "down"
  521.         elseif i == keys.right then
  522.             bot_direction = "right"
  523.         elseif i == keys.q then
  524.             running = false
  525.         end
  526.     end
  527. end
  528.  
  529. -- Game Screen Values --
  530. -- 0 = Empty
  531. -- 1 = Player 1 Space
  532. -- 2 = Player/Bot 2 Space
  533. -- 3 = Player 1 Head
  534. -- 4 = Player/Bot 2 Head
  535.  
  536. function game_text_display()
  537.     -- display stuff like game_screen, time, and who is winning.
  538.     while true do
  539.         cp(1,sy)
  540.         checkColors("white", "black")
  541.         term.clearLine()
  542.         checkColors("white", settings_game_player_worm_color)
  543.         write(" ")
  544.         checkColors("white", "black")
  545.         write(" x "..game_player_1_controls)
  546.         cp(sx,sy)
  547.         checkColors("white", settings_game_bot_worm_color)
  548.         cp(sx,sy)
  549.         write(" ")
  550.         checkColors("white", "black")
  551.         cp(sx-string.len(game_player_2_controls.." x "),sy)
  552.         write(game_player_2_controls.." x ")
  553.         if game_player_1_controls > game_player_2_controls then
  554.             checkColors(settings_game_player_worm_color, "black")
  555.             cp(sx/2-string.len("Winning")/2, sy)
  556.             write("Winning")
  557.         elseif game_player_1_controls < game_player_2_controls then
  558.             checkColors(settings_game_bot_worm_color, "black")
  559.             cp(sx/2-string.len("Winning")/2, sy)
  560.             write("Winning")
  561.         elseif game_player_1_controls == game_player_2_controls then
  562.             checkColors("white", "black")
  563.             cp(sx/2-string.len("Tied")/2, sy)
  564.             write("Tied")
  565.         end
  566.         sleep(settings_game_speed*9)
  567.     end
  568. end
  569.  
  570. function gameSingleplayer()
  571.  
  572.     game_screen = {} -- updated while game runs. impliment in the player_movement_engine & bot_movement_engine for a more secure and faster connection
  573.     for i=1, sy do
  574.         for i=1, sx do
  575.             game_screen[#game_screen+1] = 0
  576.         end
  577.     end
  578.  
  579.     loadingBuffer() checkColors("black", "black") clr()
  580.     parallel.waitForAny(player_movement_engine, player_input_engine, bot_movement_engine, bot_input_engine, game_text_display)
  581.     checkColors("black", "black") clr() display_title()
  582. end
  583.  
  584. function gameMultiplayer()
  585.     --
  586. end
  587.  
  588. function gameSameComputer()
  589.  
  590.     game_screen = {} -- updated while game runs. impliment in the player_movement_engine & bot_movement_engine for a more secure and faster connection
  591.     for i=1, sy do
  592.         for i=1, sx do
  593.             game_screen[#game_screen+1] = 0
  594.         end
  595.     end
  596.  
  597.     loadingBuffer() checkColors("black", "black") clr()
  598.     parallel.waitForAny(player_movement_engine, player_input_engine, bot_movement_engine, player_2_input_engine, game_text_display)
  599.     checkColors("black", "black") clr() display_title()
  600. end
  601.  
  602. -- Runtime --
  603.  
  604. menu_main = {"Play", "Store", "Settings", "Quit Game"}
  605.  
  606. menu_play = {"Singleplayer", "Multiplayer", "Same Computer", "Go Back"}
  607.  
  608. menu_store = {"Nothing Here Yet", "Go Back"}
  609.  
  610. menu_settings = {"Nothing here Yet", "Go Back"}
  611.  
  612. menu_selected = menu_main
  613.  
  614. local running = true
  615. while running do
  616.     local r = menu(menu_selected)
  617.     if menu_selected == menu_main then
  618.         if r == 1 then
  619.             menu_selected = menu_play
  620.         elseif r == 2 then
  621.             menu_selected = menu_store
  622.         elseif r == 3 then
  623.             menu_selected = menu_settings
  624.         elseif r == #menu_main then
  625.             running = false
  626.         end
  627.     elseif menu_selected == menu_play then
  628.         if r == 1 then
  629.             multiplayer = false
  630.             singleplayer = true
  631.             if game_settings_menu() == true then
  632.                 gameSingleplayer()
  633.             end
  634.         elseif r == 2 then
  635.             --
  636.         elseif r == 3 then
  637.             multiplayer = true
  638.             singleplayer = false
  639.             if game_settings_menu() == true then
  640.                 gameSameComputer()
  641.             end
  642.         elseif r == #menu_play then
  643.             menu_selected = menu_main
  644.         end
  645.     elseif menu_selected == menu_store then
  646.         if r == 1 then
  647.             --
  648.         elseif r == #menu_store then
  649.             menu_selected = menu_main
  650.         end
  651.     elseif menu_selected == menu_settings then
  652.         if r == 1 then
  653.             --
  654.         elseif r == #menu_settings then
  655.             menu_selected = menu_main
  656.         end
  657.     end
  658. end
Add Comment
Please, Sign In to add comment