Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

collision

By: a guest on May 6th, 2012  |  syntax: Lua  |  size: 1.34 KB  |  views: 66  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. g = love.graphics
  2. t = love.timer
  3. k = love.keyboard
  4. m = love.mouse
  5.  
  6. function love.load()
  7. width = g.getWidth()
  8. height = g.getHeight()
  9. playerX = 0
  10. playerY = 0
  11. wallX = 200
  12. wallY = 200
  13. speed = 5
  14.        
  15. spriteSheet = g.newImage("spriteSheet.png")
  16. player = g.newQuad(0, 0, 32, 32, 400, 400)
  17. wall = g.newQuad(32, 0, 64, 32, 400, 400)
  18. end
  19.  
  20. function love.update(dt)
  21.  
  22. -- Movement
  23. if (k.isDown("w"))then
  24.     playerY = playerY - speed
  25. end
  26. if (k.isDown("s"))then
  27.     playerY = playerY + speed
  28. end
  29. if (k.isDown("a"))then
  30.     playerX = playerX - speed
  31. end
  32. if (k.isDown("d"))then
  33.     playerX = playerX + speed
  34. end
  35.  
  36. -- Collision with sides
  37. if(playerX >= width-26)then
  38.         playerX = playerX - speed
  39. end
  40. if(playerX <= -5)then
  41.         playerX = playerX + speed
  42. end
  43. if(playerY >= height-26)then
  44.         playerY = playerY - speed
  45. end
  46. if(playerY <= -5)then
  47.         playerY = playerY + speed
  48. end
  49.  
  50. -- Collision with block
  51. -- Left side of block
  52. --(playerX+26 >= wallX)
  53. -- Right side of block
  54. --(playerX <= wallX+26)
  55. -- Top side of block
  56. --(playerY+26 >= wallY)
  57. -- Bottom side of block
  58. --(playerX <= wallY+26)
  59.  
  60. if((playerX+26 >= wallX)and(playerX <= wallX+26)and(playerY+26 >= wallY)and(playerY <= wallY+26))then
  61.         playerX = playerX - speed
  62. end
  63.  
  64. end
  65.  
  66. function love.draw()
  67. g.drawq(spriteSheet, player, playerX, playerY)
  68. g.drawq(spriteSheet, wall, wallX, wallY)
  69. end
clone this paste RAW Paste Data