Advertisement
Guest User

Game collision

a guest
May 12th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.76 KB | None | 0 0
  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 of game
  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.  
  52. -- Left side of block
  53. --(playerX+26 >= wallX)
  54. -- Right side of block
  55. --(playerX <= wallX+26)
  56. -- Top side of block
  57. --(playerY+26 >= wallY)
  58. -- Bottom side of block
  59. --(playerX <= wallY+26)
  60.  
  61. if((playerX+26 >= wallX)and(playerX <= wallX+26)and(playerY+26 >= wallY)and(playerY <= wallY+26))then
  62.     playerX = playerX - speed
  63. end
  64. if((playerX+26 >= wallX)and(playerX <= wallX+26)and(playerY+26 >= wallY)and(playerY <= wallY+26))then
  65.     playerX = playerX + speed
  66. end
  67. if((playerX+26 >= wallX)and(playerX <= wallX+26)and(playerY+26 >= wallY)and(playerY <= wallY+26))then
  68.     playerY = playerY - speed
  69. end
  70. if((playerX+26 >= wallX)and(playerX <= wallX+26)and(playerY+26 >= wallY)and(playerY <= wallY+26))then
  71.     playerY = playerY + speed
  72. end
  73.  
  74. end
  75.  
  76. function love.draw()
  77. g.drawq(spriteSheet, player, playerX, playerY)
  78. g.drawq(spriteSheet, wall, wallX, wallY)
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement