Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local keyboard = require("keyboard")
- local gpu = component.gpu
- local screen = component.screen
- gpu.bind(screen.address)
- local foreColor = 0xC3C3C3
- local backColor = 0x5A5A5A
- local direction = {0,0}
- local w,h = gpu.getResolution()
- local Square = {pos = {5,5}, width = 6, height = 3, lastPos = {5,5}, color = 0xFF0000}
- function Square:draw ()
- gpu.fill(self.lastPos[1], self.lastPos[2], self.width, self.height, " ")
- gpu.setBackground(self.color)
- gpu.fill(self.pos[1], self.pos[2], self.width, self.height, " ")
- gpu.setBackground(backColor)
- end
- function Square:update ()
- self.lastPos[1] = self.pos[1]
- self.lastPos[2] = self.pos[2]
- self.pos[1] = math.min(math.max(1, self.pos[1] + direction[1]),w-self.width)
- self.pos[2] = math.min(math.max(1, self.pos[2] + direction[2]),h-self.height)
- end
- function getDirection()
- direction[1] = 0
- direction[2] = 0
- -- 'w'
- if keyboard.isKeyDown(0x11) then direction[2] = -1
- -- 's'
- elseif keyboard.isKeyDown(0x1F) then direction[2] = 1
- end
- -- 'a'
- if keyboard.isKeyDown(0x1E) then direction[1] = -2
- -- 'd'
- elseif keyboard.isKeyDown(0x20) then direction[1] = 2
- end
- end
- gpu.setForeground(foreColor)
- gpu.setBackground(backColor)
- gpu.fill(1,1,w,h," ")
- while(true) do
- getDirection()
- Square:update()
- Square:draw()
- os.sleep(0.032)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement