pilotofsomething

[Computer Craft]RPG

Feb 1st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. if not fs.exists("/res/bg") then
  2.     shell.run("pastebin get zRp6Q7de res/bg")
  3. end
  4.  
  5. bg = paintutils.loadImage("/res/bg")
  6.  
  7. inBattle = false
  8. inItems = false
  9.  
  10. enemy = {
  11.     lvl = 1,
  12.     hp = 0,
  13.     atk = 6
  14. }
  15.  
  16. player = {
  17.     x = 5,
  18.     y = 5,
  19.     xp = 0,
  20.     next = 50,
  21.     level = 1,
  22.     hp = 100,
  23.     maxhp=100,
  24.     atk = 6,
  25.     inventory = {
  26.         [1]="----------",
  27.         [2]="----------",
  28.         [3]="----------",
  29.         [4]="----------",
  30.         [5]="----------",
  31.         [6]="----------",
  32.         [7]="----------",
  33.         [8]="----------",
  34.         [9]="----------",
  35.         [10]="----------"
  36.     }
  37. }
  38.  
  39. function drawHud()
  40.     term.setBackgroundColor(32768)
  41.     term.setTextColor(1)
  42.     term.setCursorPos(1,1)
  43.     term.write("Lvl: "..player.level)
  44.     term.write(" XP: "..player.xp.."/"..player.next)
  45.     term.write(" Atk: "..player.atk)
  46.     term.setCursorPos(1,2)
  47.     term.write("HP: "..player.hp.."/"..player.maxhp)
  48. end
  49.  
  50. function levelUp()
  51.     if player.xp >= player.next then
  52.         player.xp = player.xp - player.next
  53.         player.level = player.level + 1
  54.         player.next = math.ceil(player.next * 1.3)
  55.         player.maxhp = player.maxhp + math.random(25,30)
  56.         player.atk = player.atk + math.random(1,3)
  57.         player.hp = player.maxhp
  58.     end
  59. end
  60.  
  61. while player.hp > 0 do
  62.     sleep(0)
  63.     term.clear()
  64.     paintutils.drawImage(bg,1,1)
  65.     drawHud()
  66.     if inBattle == false then
  67.         paintutils.drawPixel(player.x,player.y,128)
  68.  
  69.         local e, key = os.pullEvent("key")
  70.  
  71.         if key == keys.w then
  72.             if player.y > 3 then
  73.                 player.y = player.y - 1
  74.             end
  75.         elseif key == keys.s then
  76.             if player.y < 19 then
  77.                 player.y = player.y + 1
  78.             end
  79.         elseif key == keys.a then
  80.             if player.x > 1 then
  81.                 player.x = player.x - 1
  82.             end
  83.         elseif key == keys.d then
  84.             if player.x < 51 then
  85.                 player.x = player.x + 1
  86.             end
  87.         end
  88.         if math.random(1,25) == 1 then
  89.             inBattle = true
  90.         end
  91.     else
  92.         if player.level > 1 then
  93.             enemy.lvl = player.level -1
  94.         else enemy.lvl = 1 end
  95.         enemy.atk = enemy.lvl * 1.5
  96.         enemy.hp = enemy.lvl * math.random(10,20)
  97.         while enemy.hp > 0 and player.hp > 0 do
  98.             term.clear()
  99.             paintutils.drawImage(bg,1,1)
  100.             drawHud()
  101.             term.write("  Enemy HP: "..enemy.hp.."  Enemy Lvl: "..enemy.lvl)
  102.             term.setCursorPos(1,3)
  103.             term.setTextColor(32768)
  104.             term.setBackgroundColor(256)
  105.             if not inItems then
  106.                 print("[Attack]")
  107.                 print("[Item]  ")
  108.                 print("[Flee]  ")
  109.             else
  110.                 term.setCursorPos(1,3)
  111.                 print("[Back]      ")
  112.                 for i=1,10 do
  113.                     if player.inventory[i] ~= " " then
  114.                         term.setCursorPos(1,i+3)
  115.                         print(player.inventory[i])
  116.                     end
  117.                 end
  118.             end
  119.             local e, b, x, y = os.pullEvent("mouse_click")
  120.             if not inItems then
  121.                 if y == 3 and x < 9 then
  122.                     enemy.hp = enemy.hp - math.random(0,player.atk)
  123.                     player.hp = player.hp - math.random(0,enemy.atk)
  124.                 elseif y == 4 and x < 9 then
  125.                     inItems = true
  126.                 elseif y == 5 and x < 9 then
  127.                     if math.random(1,2) == 1 then
  128.                         inBattle = false
  129.                         break
  130.                     else
  131.                         player.hp = player.hp - math.random(0,enemy.atk)
  132.                     end
  133.                 end
  134.             else
  135.                 if y == 3 and x < 13 then
  136.                     inItems = false
  137.                 else
  138.                     for i=1,10 do
  139.                         if y == i+3 and x < 13 then
  140.                             elseif player.inventory[i] == "Potion I" then
  141.                                 player.inventory[i] = "----------"
  142.                                 player.hp = player.hp + math.floor((player.maxhp/4))
  143.                                 if player.hp > player.maxhp then
  144.                                     player.hp = player.maxhp
  145.                                 end
  146.                             elseif player.inventory[i] == "Potion II" then
  147.                                 player.inventory[i] = "----------"
  148.                                 player.hp = player.hp + math.floor((player.maxhp/2))
  149.                                 if player.hp > player.maxhp then
  150.                                     player.hp = player.maxhp
  151.                                 end
  152.                             elseif player.inventory[i] == "Potion III" then
  153.                                 player.inventory[i] = "----------"
  154.                                 player.hp = player.hp + math.floor((player.maxhp/4) * 3)
  155.                                 if player.hp > player.maxhp then
  156.                                     player.hp = player.maxhp
  157.                                 end
  158.                             elseif player.inventory[i] == "Potion IV" then
  159.                                 player.inventory[i] = "----------"
  160.                                 player.hp = player.maxhp
  161.                                 end
  162.                             end
  163.                         end
  164.                     end
  165.                 end
  166.             end
  167.         end
  168.         inBattle = false
  169.         if player.hp < 1 then
  170.             term.setBackgroundColor(32768)
  171.             term.setTextColor(1)
  172.             term.clear()
  173.             term.setCursorPos(1,1)
  174.             print("GAME OVER")
  175.             sleep(1.5)
  176.             os.reboot()
  177.         else
  178.             if math.random(1,10) == 1 then
  179.                 for i=1,10 do
  180.                     if player.inventory[i] == "----------" then
  181.                         item = math.random(1,4)
  182.                         if item == 1 then
  183.                             player.inventory[i] = "Potion I"
  184.                         elseif item == 2 then
  185.                             player.inventory[i] = "Potion II"
  186.                         elseif item == 3 then
  187.                             player.inventory[i] = "Potion III"
  188.                         else
  189.                             player.inventory[i] = "Potion IV"
  190.                         end
  191.                         break
  192.                     end
  193.                 end
  194.             end
  195.             player.xp = player.xp + math.random(player.level * 2,player.level * 5)
  196.             levelUp()
  197.         end
  198.     end
  199. end
Add Comment
Please, Sign In to add comment