Advertisement
Guest User

cockroach

a guest
May 31st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.83 KB | None | 0 0
  1. w, h = term.getSize()
  2. startX = math.floor( w / 2 )
  3. startY = math.floor( h / 2 )
  4. x = math.floor( w / 2 )
  5. y = math.floor( h / 2 )
  6. fruitFunc = true
  7. counter = 0
  8. gameSpeed = 0.2
  9.  
  10.  
  11.  
  12. function score()
  13.   paintutils.drawLine(1, 1, w, 1, colors.black)
  14.   term.setCursorPos(1, 1)
  15.   term.setTextColor(colors.yellow)
  16.   write("Score: " .. counter .. " | Current x/y: " .. x .. " " .. y - 1 .. " | Fruit x/y: " .. fruitCoord["x"] .. " " .. fruitCoord["y"])
  17. end
  18.  
  19. function controls()
  20.   event, key = os.pullEvent("key")
  21.   return key
  22. end
  23.  
  24. function direction()
  25.  local cont = controls()
  26.   if cont == 17 then return {0, -1}
  27.   elseif cont == 31 then return {0, 1}
  28.   elseif cont == 32 then return {1, 0}
  29.   elseif cont == 30 then return {-1, 0}
  30.   end
  31. end
  32.  
  33. term.clear()
  34. paintutils.drawPixel(startX, startY, colors.red)
  35.  
  36. tab = {0, 0}
  37.  
  38. function smb()
  39.   tab = direction()
  40.   sleep(gameSpeed)
  41. end
  42.  
  43. function flow()
  44.   paintutils.drawPixel(-1, -1, colors.black)
  45.   x = x + tab[1]
  46.   y = y + tab[2]
  47.   term.clear()
  48.   if x > w then x = 1
  49.    elseif x < 1 then x = w
  50.    elseif y > h then y = 2
  51.    elseif y < 2 then y = h
  52.   end
  53.   if fruitCoord["isExists"] == true then
  54.   fruitFunc = false
  55.   else fruitFunc = true end
  56.   fruit(fruitFunc)
  57.   paintutils.drawPixel(x, y, colors.red)
  58.   fruitCatch()
  59.   score()
  60.   sleep(gameSpeed)
  61.   return x, y
  62. end
  63.  
  64. fruitCoord = {}
  65.  
  66. function fruit(state)
  67.   if state == true then
  68.    fruitCoord["x"] = math.random(1, w)
  69.    fruitCoord["y"] = math.random(2, h)
  70.    fruitCoord["isExists"] = true
  71.    end
  72.    paintutils.drawPixel(fruitCoord["x"], fruitCoord["y"], colors.green)      
  73. end
  74.  
  75. function fruitCatch()
  76.   if fruitCoord["x"] == x then
  77.     if fruitCoord["y"] == y then
  78.       counter = counter + 1
  79.       fruitCoord["isExists"] = false
  80.      end
  81.    end  
  82. end
  83.  
  84. while true do
  85.   parallel.waitForAny(flow, smb)
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement