Daniel_I_Am

Snake

Dec 25th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.03 KB | None | 0 0
  1. io = require("io")
  2. os = require("os")
  3. component = require("component")
  4. term = require("term")
  5. sides = require("sides")
  6. colors = require("colors")
  7. fs = require("filesystem")
  8. BA = require("ButtonAPI")
  9. event = require("event")
  10.  
  11. gpu = component.gpu
  12.  
  13. width = 50
  14. height = 10
  15.  
  16. gpu.setResolution(width,height)
  17.  
  18. snakePositions = {{20,5}}
  19.  
  20. function drawSnake(snakePositions)
  21.     for i =1, #snakePositions do
  22.         setPixel(snakePositions[i][1], snakePositions[i][2], true)
  23.     end
  24. end
  25.  
  26. function setPixel(x,y,state)
  27.     state = state or false
  28.     if state == true then col = 0xffffff else col = 0x000000 end
  29.     gpu.setBackground(col)
  30.     gpu.fill(x,y,1,1," ")
  31.     gpu.setBackground(0x000000)
  32. end
  33.  
  34. function moveSnake(positionArray, direction)
  35.     for i = 2,#positionArray do
  36.         for j = 1, 2 do
  37.             positionArray[i][j] = positionArray[i-1][j]
  38.         end
  39.     end
  40.     if direction == "right" then
  41.         positionArray[1][1] = positionArray[1][1]+1
  42.     end
  43. end
  44.  
  45. repeat
  46.     snakePositions = moveSnake(snakePositions, "right")
  47.     drawSnake(snakePositions)
  48.     os.sleep(1)
  49. until false
Advertisement
Add Comment
Please, Sign In to add comment