BenniShifer919

The mini Maze-Game.

Nov 5th, 2023 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.74 KB | Source Code | 0 0
  1. local w,h = term.getSize()
  2.  
  3. local map = {} -- 3x3
  4. local inventory = {
  5.     food = 0,
  6.     water = 0,
  7.     poison = 0,
  8. }
  9.  
  10. local keybinds = {
  11.     drink = keys.t,
  12.     eat = keys.y,
  13.     poison = keys.u
  14. }
  15.  
  16. local upgrades = {}
  17. local shop_items = { -- {name = name, price = price}
  18.     {name = "Food", price = 10, id = "food"},
  19.     {name = "Water", price = 25, id = "water"},
  20.     {name = "Alcohol", price = 500, id = "poison"}
  21. }
  22.  
  23. local coins = 25
  24. local food = 100 --100% = 100 units
  25. local water = 10 --100% = 10 units.
  26.  
  27. local wwpt = 0.5 --Water waste per Turn
  28. local fwpt = 5 --Water waste per Turn
  29.  
  30. local rarity = { --1 in rarity^rarity room. (it's random to it's more like a chance)
  31.     food = 3,
  32.     water = 2,
  33.     coins = 5
  34. }
  35.  
  36. local plrS = "P"
  37. local shpS = "S"
  38.  
  39. local gameOver = false
  40. local reason = "Something went wrong"
  41.  
  42. function possibleToMove(direction)
  43.     if direction == "top" then
  44.         return map[2][2].top
  45.     elseif direction == "right" then
  46.         return map[2][3].left
  47.     elseif direction == "down" then
  48.         return map[3][2].top
  49.     elseif direction == "left" then
  50.         return map[2][2].left
  51.     end
  52. end
  53.  
  54. function init()
  55.     for i=1,3,1 do
  56.         table.insert(map, {})
  57.         for y=1,3,1 do
  58.             table.insert(map[i], nil)
  59.         end
  60.     end
  61. end
  62.  
  63. function generateMap()
  64.     for y=1,3,1 do
  65.         for x=1,3,1 do
  66.             if map[y][x] == nil then
  67.                 local variant = math.random(1,3)
  68.                 local type = math.random(1,30)
  69.                 local inventory = {}
  70.                 if type == math.random(1,30) then
  71.                     type = "shop"
  72.                 else
  73.                     type = "room"
  74.                 end
  75.                 local wC1,wC2 = math.random(1,rarity.water),math.random(1,rarity.water)
  76.                 local fC1,fC2 = math.random(1,rarity.food),math.random(1,rarity.food)
  77.                 local cC1,cC2 = math.random(1,rarity.coins),math.random(1,rarity.coins)
  78.                 if wC1 == wC2 then
  79.                     table.insert(inventory,"water")
  80.                 end
  81.                 if fC1 == fC2 then
  82.                     table.insert(inventory,"food")
  83.                 end
  84.                 if cC1 == cC2 then
  85.                     table.insert(inventory,"coin")
  86.                 end
  87.                 local form = {top = false, left = false, inventory = inventory, type = type}
  88.                 if variant == 1 then
  89.                     form.top = true
  90.                     map[y][x] = form
  91.                 elseif variant == 2 then
  92.                     form.left = true
  93.                     map[y][x] = form
  94.                 elseif variant == 3 then
  95.                     form.left = true
  96.                     form.top = true
  97.                     map[y][x] = form
  98.                 end
  99.             end
  100.         end
  101.     end
  102. end
  103.  
  104. function shiftMap(direction)
  105.     if direction == "top" then
  106.         for y=2,3,1 do
  107.             for x=1,3,1 do
  108.                 map[y][x] = map[y-1][x]
  109.             end
  110.         end
  111.         for x=1,3,1 do
  112.             map[1][x] = nil
  113.         end
  114.     elseif direction == "right" then
  115.         for y=1,3,1 do
  116.             for x=2,3,1 do
  117.                 map[y][x] = map[y][x+1]
  118.             end
  119.         end
  120.         for y=1,3,1 do
  121.             map[y][3] = nil
  122.         end
  123.     elseif direction == "down" then
  124.         for y=2,1,-1 do
  125.             for x=1,3,1 do
  126.                 map[y][x] = map[y+1][x]
  127.             end
  128.         end
  129.         for x=1,3,1 do
  130.             map[3][x] = nil
  131.         end
  132.     elseif direction == "left" then
  133.         for y=1,3,1 do
  134.             for x=2,1,-1 do
  135.                 map[y][x] = map[y][x-1]
  136.             end
  137.         end
  138.         for y=1,3,1 do
  139.             map[y][1] = nil
  140.         end
  141.     end
  142. end
  143.  
  144. init()
  145. generateMap()
  146.  
  147. while true do
  148.  
  149.     local function draw()
  150.         term.setBackgroundColor(colors.black)
  151.         term.clear()
  152.         --Map draw logic
  153.         local offset = 3
  154.         for y=1,3,1 do
  155.             for x=1,3,1 do
  156.                 term.setBackgroundColor(colors.black)
  157.                 term.setTextColor(colors.white)
  158.                 term.setCursorPos(x*2-1+offset,y*2-1+offset)
  159.                 if map[y][x].top == true then
  160.                     term.write("# ")
  161.                 else
  162.                     term.write("##")
  163.                 end
  164.                 term.setCursorPos(x*2-1+offset,y*2+offset)
  165.                 local s = ""
  166.                 if y == 2 and x == 2 then
  167.                     s = plrS
  168.                 end
  169.                 if map[y][x].type == "shop" then
  170.                     s = shpS
  171.                 end
  172.                 if map[y][x].left == true then
  173.                     term.write(" "..s)
  174.                 else
  175.                     term.write("#"..s)
  176.                 end
  177.             end
  178.         end
  179.         -- Coins, Food, Water level
  180.         --FOOD
  181.         term.setBackgroundColor(colors.brown)
  182.         term.setCursorPos(1,h)
  183.         term.clearLine()
  184.         term.setTextColor(colors.white)
  185.         term.write("Food level: "..food)
  186.         --Water
  187.         term.setBackgroundColor(colors.cyan)
  188.         term.setCursorPos(1,h-1)
  189.         term.clearLine()
  190.         term.setTextColor(colors.white)
  191.         term.write("Water level: "..water*10)
  192.         --Coins
  193.         term.setBackgroundColor(colors.orange)
  194.         term.setCursorPos(1,h-2)
  195.         term.clearLine()
  196.         term.setTextColor(colors.white)
  197.         term.write("Coins: "..coins)
  198.         --Shop menu display
  199.         term.setBackgroundColor(colors.black)
  200.         term.setTextColor(colors.white)
  201.         if map[2][2].type == "shop" then
  202.             for i=1,#shop_items,1 do
  203.                 term.setCursorPos(w-7,i*2-1)
  204.                 term.write(shop_items[i].name)
  205.                 term.setCursorPos(w-7,i*2)
  206.                 term.write(shop_items[i].price)
  207.             end
  208.         end
  209.         --Inventory menu display
  210.         term.setBackgroundColor(colors.black)
  211.         term.setTextColor(colors.white)
  212.         term.setCursorPos(w-7,8)
  213.         term.write("Food")
  214.         term.setCursorPos(w-7,9)
  215.         term.write(inventory.food.."x")
  216.         term.setCursorPos(w-7,10)
  217.         term.write("Water")
  218.         term.setCursorPos(w-7,11)
  219.         term.write(inventory.water.."x")
  220.         term.setCursorPos(w-7,12)
  221.         term.write("Alcohol")
  222.         term.setCursorPos(w-7,13)
  223.         term.write(inventory.poison.."x")
  224.         --Room inventory display
  225.         term.setBackgroundColor(colors.black)
  226.         term.setTextColor(colors.white)
  227.         if #map[2][2].inventory > 0 then
  228.             for i = 1,#map[2][2].inventory,1 do
  229.                 term.setCursorPos(w-7,14+i)
  230.                 term.write(map[2][2].inventory[i])
  231.             end
  232.         end
  233.     end
  234.     draw()
  235.     local e,a,d,b,c,x = os.pullEvent()
  236.     if e == "key_up" then
  237.         local key = a
  238.         if key == keys.left or key == keys.a then
  239.             if possibleToMove("left") then
  240.                 shiftMap("left")
  241.                 generateMap()
  242.                 water = water - wwpt
  243.                 food = food - fwpt
  244.             end
  245.         end
  246.         if key == keys.up or key == keys.w then
  247.             if possibleToMove("top") then
  248.                 shiftMap("top")
  249.                 generateMap()
  250.                 water = water - wwpt
  251.                 food = food - fwpt
  252.             end
  253.         end
  254.         if key == keys.right or key == keys.d then
  255.             if possibleToMove("right") then
  256.                 shiftMap("right")
  257.                 generateMap()
  258.                 water = water - wwpt
  259.                 food = food - fwpt
  260.             end
  261.         end
  262.         if key == keys.down or key == keys.s then
  263.             if possibleToMove("down") then
  264.                 shiftMap("down")
  265.                 generateMap()
  266.                 water = water - wwpt
  267.                 food = food - fwpt
  268.             end
  269.         end
  270.        
  271.         if key == keybinds.eat then
  272.             if inventory.food > 0 then
  273.                 inventory.food = inventory.food - 1
  274.                 food = food + 15
  275.                 if food > 110 then
  276.                     food = 110
  277.                 end
  278.             end
  279.         end
  280.         if key == keybinds.drink then
  281.             if inventory.water > 0 then
  282.                 inventory.water = inventory.water - 1
  283.                 water = water + 1
  284.                 if water > 15 then
  285.                     water = 15
  286.                 end
  287.             end
  288.         end
  289.         if key == keybinds.poison then
  290.             if inventory.poison > 0 then
  291.                 inventory.poison = inventory.poison - 1
  292.                 reason = "You drinked alcohol!"
  293.                 gameOver = true
  294.             end
  295.         end
  296.     end
  297.     if e == "mouse_click" then
  298.         local button,x,y = a,d,b
  299.         if x >= w-7 then
  300.             --Shop logic
  301.             if map[2][2].type == "shop" then
  302.                 if y >= 1 and y <= 6 then
  303.                     local item = math.ceil(y/2)
  304.                     if coins >= shop_items[item].price then
  305.                         inventory[shop_items[item].id] = inventory[shop_items[item].id] + 1
  306.                         coins = coins - shop_items[item].price
  307.                     end
  308.                 end
  309.             end
  310.             -- Inventory logic
  311.             if y >= 8 and y <= 9 then
  312.                 if inventory.food > 0 then
  313.                     inventory.food = inventory.food - 1
  314.                     food = food + 15
  315.                     if food > 110 then
  316.                         food = 110
  317.                     end
  318.                 end
  319.             end
  320.             if y >= 10 and y <= 11 then
  321.                 if inventory.water > 0 then
  322.                     inventory.water = inventory.water - 1
  323.                     water = water + 1
  324.                     if water > 15 then
  325.                         water = 15
  326.                     end
  327.                 end
  328.             end
  329.             if y >= 12 and y <= 13 then
  330.                 if inventory.poison > 0 then
  331.                     inventory.poison = inventory.poison - 1
  332.                     reason = "You drinked alcohol!"
  333.                     gameOver = true
  334.                 end
  335.             end
  336.             --Room inventory logic
  337.             if y > 14 and y <= 17 then
  338.                 local item = y-14
  339.                 if #map[2][2].inventory >= item then
  340.                     if map[2][2].inventory[item] ~= "coin" then
  341.                         inventory[map[2][2].inventory[item]] = inventory[map[2][2].inventory[item]] + 1
  342.                     else
  343.                         coins = coins + math.random(1,6)*5
  344.                     end
  345.                     table.remove(map[2][2].inventory,item)
  346.  
  347.                 end
  348.             end
  349.         end
  350.     end
  351.     if food < 0 or water < 0 then
  352.         reason = "You ran out of food/water"
  353.         gameOver = true
  354.         break
  355.     end
  356.     if gameOver then
  357.         break
  358.     end
  359.     if coins > 666 then
  360.         reason = "You reached 666 coins!"
  361.         break
  362.     end
  363. end
  364. if gameOver then
  365.     term.setBackgroundColor(colors.red)
  366.     term.clear()
  367.     term.setTextColor(colors.white)
  368.     term.setCursorPos((w-string.len("GAME OVER!"))/2,h/2)
  369.     term.write("GAME OVER!")
  370.     term.setTextColor(colors.lightGray)
  371.     term.setCursorPos((w-string.len(reason))/2,h/2+1)
  372.     term.write(reason)
  373.     sleep(3)
  374.     term.setBackgroundColor(colors.black)
  375.     term.clear()
  376.     term.setCursorPos(1,1)
  377. else
  378.     term.setBackgroundColor(colors.green)
  379.     term.clear()
  380.     term.setTextColor(colors.white)
  381.     term.setCursorPos((w-string.len("YOU WON!"))/2,h/2)
  382.     term.write("YOU WON!")
  383.     term.setTextColor(colors.lightGray)
  384.     term.setCursorPos((w-string.len(reason))/2,h/2+1)
  385.     term.write(reason)
  386.     sleep(3)
  387.     term.setBackgroundColor(colors.black)
  388.     term.clear()
  389.     term.setCursorPos(1,1)
  390. end
Advertisement
Add Comment
Please, Sign In to add comment