Advertisement
Guest User

test.lua

a guest
Apr 4th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1.  
  2. local component = require("component")
  3. local keyboard = require("keyboard")
  4. local gpu = component.gpu
  5. local screen = component.screen
  6. gpu.bind(screen.address)
  7.  
  8. local foreColor = 0xC3C3C3
  9. local backColor = 0x5A5A5A
  10.  
  11. local direction = {0,0}
  12.  
  13. local w,h = gpu.getResolution()
  14. local Square = {pos = {5,5}, width = 6, height = 3, lastPos = {5,5}, color = 0xFF0000}
  15.  
  16. function Square:draw ()
  17.   gpu.fill(self.lastPos[1], self.lastPos[2], self.width, self.height, " ")
  18.   gpu.setBackground(self.color)
  19.   gpu.fill(self.pos[1], self.pos[2], self.width, self.height, " ")
  20.   gpu.setBackground(backColor)
  21. end
  22.  
  23. function Square:update ()
  24.   self.lastPos[1] = self.pos[1]
  25.   self.lastPos[2] = self.pos[2]
  26.   self.pos[1] = math.min(math.max(1, self.pos[1] + direction[1]),w-self.width)
  27.   self.pos[2] = math.min(math.max(1, self.pos[2] + direction[2]),h-self.height)
  28. end
  29.  
  30. function getDirection()
  31.   direction[1] = 0
  32.   direction[2] = 0
  33.   -- 'w'
  34.   if keyboard.isKeyDown(0x11) then direction[2] = -1
  35.   -- 's'
  36.   elseif keyboard.isKeyDown(0x1F) then direction[2] = 1
  37.   end
  38.   -- 'a'
  39.   if keyboard.isKeyDown(0x1E) then direction[1] = -2
  40.   -- 'd'
  41.   elseif keyboard.isKeyDown(0x20) then direction[1] = 2
  42.   end
  43. end
  44.  
  45.  
  46. gpu.setForeground(foreColor)
  47. gpu.setBackground(backColor)
  48. gpu.fill(1,1,w,h," ")
  49.  
  50.  
  51. while(true) do
  52.   getDirection()
  53.   Square:update()
  54.   Square:draw()
  55.   os.sleep(0.032)
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement