Advertisement
CaptainSpaceCat

Explode!!!

Jun 13th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1. local Star = {
  2. xPos = 0,
  3. yPos = 0,
  4. xDir = 0,
  5. yDir = 0
  6. }
  7. local stars = {}
  8. --term.redirect(peripheral.wrap("right"))
  9. local w, h = term.getSize()
  10.  
  11. function Star:new(xPos, yPos, xDir, yDir)
  12.   local class = {xPos = xPos, yPos = yPos, xDir = xDir, yDir = yDir}
  13.   setmetatable(class, self)
  14.   self.__index = self
  15.   return class
  16. end
  17.  
  18. function Star:move()
  19.   self.xPos = self.xPos + self.xDir
  20.   self.yPos = self.yPos + self.yDir
  21.   if self.xPos == w then
  22.     self.xDir = -1
  23.   elseif self.xPos == 1 then
  24.     self.xDir = 1
  25.   end
  26.   if self.yPos == h then
  27.     self.yDir = -1
  28.   elseif self.yPos == 1 then
  29.     self.yDir = 1
  30.   end
  31. end
  32.  
  33. function Star:display()
  34.   draw("*", self.xPos, self.yPos)
  35.   draw(" ", self.xPos - self.xDir, self.yPos - self.yDir)
  36. end
  37.  
  38. function draw(words,x,y)
  39.   term.setCursorPos(x,y)
  40.   write(words)
  41. end
  42.  
  43. function eventHandler(events)
  44.   if events[1] == "mouse_click" and events[2] == 1 then
  45.     stars[#stars + 1] = Star:new(events[3], events[4], -1, 0)
  46.     stars[#stars + 1] = Star:new(events[3], events[4], -1, -1)
  47.     stars[#stars + 1] = Star:new(events[3], events[4], -1, 1)
  48.     stars[#stars + 1] = Star:new(events[3], events[4], 0, 1)
  49.     stars[#stars + 1] = Star:new(events[3], events[4], 0, -1)
  50.     stars[#stars + 1] = Star:new(events[3], events[4], 1, 1)
  51.     stars[#stars + 1] = Star:new(events[3], events[4], 1, 0)
  52.     stars[#stars + 1] = Star:new(events[3], events[4], 1, -1)
  53.   end
  54. end
  55.  
  56. local gameTimer = os.startTimer(.05)
  57. while true do
  58.   local events = {os.pullEvent()}
  59.   term.clear()
  60.   if events[1] == "timer" and events[2] == gameTimer then
  61.     for i, v in pairs(stars) do
  62.       v:move()
  63.       v:display()
  64.     end
  65.     gameTimer = os.startTimer(.05)
  66.   else
  67.     eventHandler(events)
  68.   end
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement