Nasamos

collision system Löve

Nov 11th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | None | 0 0
  1. function love.draw()
  2.   --draws the black border around the screen.
  3.   love.graphics.setColor(0, 0, 0)
  4.   love.graphics.rectangle("fill", 0, 0, 704, 576)
  5.   --draws the white rectangle in the middle and prints the text on the screen.
  6.   love.graphics.setColor(255, 255, 255)
  7.   love.graphics.rectangle("fill", 64, 64, 576, 448)
  8.   love.graphics.print(printvar)
  9.   --draws the player, a blue rectangle.
  10.   love.graphics.setColor(0, 0, 244)
  11.   love.graphics.rectangle("fill", player.x, player.y, 64, 64)
  12. end
  13.  
  14. --checks which key is pressed when certain keys are pressed it performs certain actions,
  15. --uses WSAD for movement.
  16. function love.keypressed(key)
  17.   if key == "w" and move.up then
  18.     player.y = player.y - 64
  19.   elseif key == "s" and move.down then
  20.     player.y = player.y + 64
  21.   end
  22.   if key == "a" and move.left then
  23.     player.x = player.x - 64
  24.   elseif key == "d" and move.right then
  25.     player.x = player.x + 64
  26.   end
  27. end
  28.  
  29. --resizes the window to x 704 and y 576.
  30. function window()
  31.   love.window.setMode(704, 576)
  32. end
  33.  
  34. --draws our map, changing the numbers here will result in either a collision or not, it only checks for 0 so anything except for 0 is a collision.
  35. function sketch_wall()
  36. sketch = {}
  37. sketch[1] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  38. sketch[2] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
  39. sketch[3] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
  40. sketch[4] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
  41. sketch[5] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
  42. sketch[6] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
  43. sketch[7] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
  44. sketch[8] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
  45. sketch[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  46. end
  47.  
  48. --collision function; player.y / 64 + 1 and player.x / 64 + 1 is the current location on the map, see table above. tldr; +2 is for going right or down 0 is for left and up.
  49. --If you were to change the == 0 with == 1 and switch the true and false you can make more than 1 type of tile.
  50.  function collision(sketch)
  51.   if sketch[player.y / 64 + 1][player.x / 64 + 2] == 0 then
  52.     move.right = true
  53.     move.rightnum = 0
  54.   else
  55.     move.right = false
  56.     move.rightnum = 1
  57.   end  
  58.   if sketch[player.y / 64 + 1][player.x / 64] == 0 then
  59.     move.left = true
  60.     move.leftnum = 0
  61.   else
  62.     move.left = false
  63.     move.leftnum = 1
  64.   end  
  65.   if sketch[player.y / 64][player.x / 64 + 1] == 0 then
  66.     move.up = true
  67.     move.upnum = 0
  68.   else
  69.     move.up = false
  70.     move.upnum = 1
  71.   end  
  72.   if sketch[player.y / 64 + 2][player.x / 64 + 1] == 0 then
  73.     move.down = true
  74.     move.downnum = 0
  75.   else
  76.     move.down = false
  77.     move.downnum = 1
  78.   end
  79. end
  80.  
  81. function love.load()
  82.   --creates a table for the movement.
  83.   move = {}
  84.   move.up = true
  85.   move.down = true
  86.   move.left = true
  87.   move.right = true
  88.   move.upnum = 0
  89.   move.downnum = 0
  90.   move.leftnum = 0
  91.   move.rightnum = 0
  92.   --table for the player
  93.   player = {}
  94.   player.x = 64
  95.   player.y = 64
  96.   --calls the sketch_wall() function.
  97.   sketch_wall()
  98.   --calls the window() function.
  99.   window()
  100. end
  101.  
  102. function love.update()
  103.   --prints the values of the numbers associated with left, right, up and down 0 means you can move to that spot but 1 means there would be a collision.
  104.   printvar = {"                                                        ", "Up: " .. move.upnum, "      ", "Down: " .. move.downnum, "      ", "Left: " .. move.leftnum, "      ",  "Right: " .. move.rightnum}
  105.   collision(sketch)
  106. end
Advertisement
Add Comment
Please, Sign In to add comment