Advertisement
nitrogenfingers

test1

Feb 8th, 2013
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1. --First, we load gameutils. Make sure it's in the
  2. --same directory!
  3. os.loadAPI(shell.resolve(".").."/gameutils")
  4.  
  5. --These keep track of whether collisions have
  6. --happened or not
  7. local rcollision = false
  8. local pcollision = false
  9.  
  10. --We load our two game objects here
  11. local obj1 = gameutils.loadSprite(
  12.     shell.resolve(".").."/obj1.nfp", 1, 1)
  13. local obj2 = gameutils.loadSprite(
  14.     shell.resolve(".").."/obj1.nfp", 6, 1)
  15.  
  16. --This prepares the screen for drawing
  17. gameutils.initializeBuffer()
  18. while true do
  19.   --First we empty the buffer of the old picture
  20.   --For fun, try commenting out this line
  21.   gameutils.clearBuffer(colours.black)
  22.   --We then draw our two sprites to the buffer
  23.   gameutils.writeToBuffer(obj1)
  24.   gameutils.writeToBuffer(obj2)
  25.   --Finally, we output the buffer to the screen
  26.   gameutils.drawBuffer()
  27.  
  28.   --This displays whether or not a collision
  29.   --has occurred
  30.   term.setCursorPos(1,18)
  31.   if not rcollision then
  32.     term.setTextColour(colours.green)
  33.     term.write("No rectangle collision")
  34.   else
  35.     term.setTextColour(colours.orange)
  36.     term.write("Rectangles Intersect!")
  37.   end
  38.   term.setCursorPos(1,19)
  39.   if not pcollision then
  40.     term.setTextColour(colours.green)
  41.     term.write("No pixel collision")
  42.   else
  43.     term.setTextColour(colours.red)
  44.     term.write("Pixels Overlap!")
  45.   end
  46.  
  47.   --This handles movement input
  48.   local _,key = os.pullEvent("key")
  49.   if key == keys.left then
  50.     obj1.x = obj1.x - 1
  51.   elseif key == keys.right then
  52.     obj1.x = obj1.x + 1
  53.   elseif key == keys.up then
  54.     obj1.y = obj1.y - 1
  55.   elseif key == keys.down then
  56.     obj1.y = obj1.y + 1
  57.   elseif key == keys.q then
  58.     break
  59.   end
  60.  
  61.   --Finally, we check our collisions here. This
  62.   --is the core of this part of the program, and
  63.   --it's 2 lines!
  64.   rcollision = obj1:rCollidesWith(obj2)
  65.   pcollision = obj1:pCollidesWith(obj2)
  66. end
  67.  
  68. term.setBackgroundColour(colours.black)
  69. shell.run("clear")
  70. sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement