Advertisement
Tatantyler

Target Shooting Game

Oct 22nd, 2012
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.57 KB | None | 0 0
  1. local targetPos = {}
  2. local score = 0
  3. local hits = 0
  4. local misses = 0
  5. local maxX, maxY = term.getSize()
  6. local startTime = os.clock()
  7. local difficulty = 1
  8. local chain = 0
  9.  
  10. local function getDifficulty()
  11.  
  12.     local function printButton(x,y,text)
  13.         term.setBackgroundColor(colors.blue)
  14.         term.setCursorPos(x,y)
  15.         for i=1, string.len(text)+2 do
  16.             write(" ")
  17.         end
  18.         term.setCursorPos(x,y+1)
  19.         term.write(" ")
  20.         term.write(text)
  21.         term.write(" ")
  22.         term.setCursorPos(x,y+2)
  23.         for i=1, string.len(text)+2 do
  24.             write(" ")
  25.         end
  26.         term.setBackgroundColor(colors.black)
  27.         return
  28.     end
  29.    
  30.     local function Button_pointCollideCheck(x,y,button)
  31.         for i,v in ipairs(button) do
  32.             local currentX, currentY = unpack(v)
  33.             if x == currentX and y == currentY then
  34.                 return i
  35.             end
  36.         end
  37.         return false
  38.     end
  39.     term.setBackgroundColor(colors.black)
  40.     term.clear()
  41.     local buttons = {}
  42.     term.setCursorPos(1,1)
  43.     print("T A R G E T  S H O O T I N G")
  44.     print("")
  45.     print("DIFFICULTY: ")
  46.     print("")
  47.     printButton(2, 5, "HARD")
  48.     printButton(2, 9, "MEDIUM")
  49.     printButton(2, 13, "EASY")
  50.     while true do
  51.         local event, button, x, y = os.pullEvent()
  52.         if event == "mouse_click" then
  53.             if (x >= 2 and x <=6) and (y >= 5 and y <= 9) then -- the lower, the harder
  54.                 difficulty = 0.75
  55.                 return
  56.             elseif (x >= 2 and x <= 8) and (y >= 9 and y <= 13) then
  57.                 difficulty = 1
  58.                 return
  59.             else
  60.                 difficulty = 1.25
  61.                 return
  62.             end
  63.         end
  64.     end
  65. end
  66.  
  67. getDifficulty()
  68.  
  69. local function pointCollideCheck(x,y)
  70.     for i,v in ipairs(targetPos) do
  71.         local currentX, currentY = unpack(v)
  72.         if x == currentX and y == currentY then
  73.             return i
  74.         end
  75.     end
  76.     return false
  77. end
  78.  
  79. local function setTarget(x,y)
  80.     targetPos = {
  81.         {x,y}, {x+1,y}, {x+2,y},
  82.         {x,y+1}, {x+1,y+1}, {x+2,y+1},
  83.         {x,y+2}, {x+1,y+2}, {x+2,y+2},
  84.     }
  85.     for i,v in ipairs(targetPos) do
  86.         local x,y = unpack(v)
  87.         if x > maxX then
  88.             return false
  89.         elseif y > maxY-1 then
  90.             return false
  91.         end
  92.     end
  93.     return true
  94. end
  95.  
  96.  
  97. local function drawScreen()
  98.     term.setBackgroundColor(colors.black)
  99.     term.clear()
  100.     term.setCursorPos(1,1)
  101.     for i,v in ipairs(targetPos) do
  102.         term.setCursorPos(unpack(v))
  103.         if i == 5 then
  104.             term.setBackgroundColor(colors.red)
  105.         else
  106.             term.setBackgroundColor(colors.white)
  107.         end
  108.         term.write(" ")
  109.     end
  110.     term.setBackgroundColor(colors.black)
  111.     term.setCursorPos(1, maxY)
  112.     term.write("Score: "..score.." Hits: "..hits.." Misses: "..misses.." Accuracy: "..math.floor((hits/(hits+misses))*100).."%")
  113.     term.setCursorPos(1,1)
  114.     local multi = (chain/10)*1.5
  115.     term.write("Chain: "..chain.." Multiplier: "..multi.." Playtime: "..(math.floor((os.clock()-startTime)*10)/10).."s")
  116. end
  117.  
  118. local function game_logicThread()
  119.     math.randomseed(os.clock())
  120.     local firstX = math.random(1, maxX-3)
  121.     local firstY = math.random(2, maxY-3)
  122.     setTarget(firstX, firstY)
  123.     local timer = os.startTimer(difficulty)
  124.     while true do
  125.         local event, param1, param2, param3 = os.pullEvent()
  126.         if event == "mouse_click" then
  127.             term.setCursorPos(param2, param3)
  128.             local x = param2
  129.             local y = param3
  130.             local collide = pointCollideCheck(x,y)
  131.             if collide then
  132.                 if collide == 5 then
  133.                     term.setBackgroundColor(colors.red)
  134.                 else
  135.                     term.setBackgroundColor(colors.white)
  136.                 end
  137.                 term.setTextColor(colors.orange)
  138.                 term.write("*")
  139.                 term.setTextColor(colors.white)
  140.                 term.setBackgroundColor(colors.black)
  141.                 chain = chain+1
  142.                 hits = hits+1
  143.                 local multi = (chain/10)*1.5
  144.                 if collide == 5 then
  145.                     if multi >= 1 then
  146.                         score = score+(multi*2)
  147.                     else
  148.                         score = score+2
  149.                     end
  150.                 else
  151.                     if multi >= 1 then
  152.                         score = score+multi
  153.                     else
  154.                         score = score+1
  155.                     end
  156.                 end
  157.                 targetPosTable = {}
  158.                 math.randomseed(os.clock())
  159.                 local newX = math.random(1, maxX-3)
  160.                 local newY = math.random(2, maxY-3)
  161.                 setTarget(newX, newY)
  162.                 timer = os.startTimer(difficulty)
  163.             else
  164.                 term.setBackgroundColor(colors.black)
  165.                 term.setTextColor(colors.orange)
  166.                 term.write("*")
  167.                 term.setTextColor(colors.white)
  168.                 score = score-1
  169.                 misses = misses+1
  170.                 chain = 0
  171.             end
  172.         elseif event == "timer" and param1 == timer then
  173.             score = score-1
  174.             chain = 0
  175.             targetPosTable = {}
  176.             math.randomseed(os.clock())
  177.             local newX = math.random(1, maxX-3)
  178.             local newY = math.random(2, maxY-3)
  179.             setTarget(newX, newY)
  180.             timer = os.startTimer(difficulty)
  181.         end
  182.     end
  183. end
  184.  
  185. local function game_drawThread()
  186.     while true do
  187.         drawScreen()
  188.         os.sleep(0)
  189.     end
  190. end
  191.  
  192. term.clear()
  193. term.setCursorPos(1,1)
  194. parallel.waitForAll(game_logicThread, game_drawThread)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement