Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --First, we load gameutils. Make sure it's in the
- --same directory!
- os.loadAPI(shell.resolve(".").."/gameutils")
- --These keep track of whether collisions have
- --happened or not
- local rcollision = false
- local pcollision = false
- --We load our two game objects here
- local obj1 = gameutils.loadSprite(
- shell.resolve(".").."/obj1.nfp", 1, 1)
- local obj2 = gameutils.loadSprite(
- shell.resolve(".").."/obj1.nfp", 6, 1)
- --This prepares the screen for drawing
- gameutils.initializeBuffer()
- while true do
- --First we empty the buffer of the old picture
- --For fun, try commenting out this line
- gameutils.clearBuffer(colours.black)
- --We then draw our two sprites to the buffer
- gameutils.writeToBuffer(obj1)
- gameutils.writeToBuffer(obj2)
- --Finally, we output the buffer to the screen
- gameutils.drawBuffer()
- --This displays whether or not a collision
- --has occurred
- term.setCursorPos(1,18)
- if not rcollision then
- term.setTextColour(colours.green)
- term.write("No rectangle collision")
- else
- term.setTextColour(colours.orange)
- term.write("Rectangles Intersect!")
- end
- term.setCursorPos(1,19)
- if not pcollision then
- term.setTextColour(colours.green)
- term.write("No pixel collision")
- else
- term.setTextColour(colours.red)
- term.write("Pixels Overlap!")
- end
- --This handles movement input
- local _,key = os.pullEvent("key")
- if key == keys.left then
- obj1.x = obj1.x - 1
- elseif key == keys.right then
- obj1.x = obj1.x + 1
- elseif key == keys.up then
- obj1.y = obj1.y - 1
- elseif key == keys.down then
- obj1.y = obj1.y + 1
- elseif key == keys.q then
- break
- end
- --Finally, we check our collisions here. This
- --is the core of this part of the program, and
- --it's 2 lines!
- rcollision = obj1:rCollidesWith(obj2)
- pcollision = obj1:pCollidesWith(obj2)
- end
- term.setBackgroundColour(colours.black)
- shell.run("clear")
- sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement