to fast


SUBMITTED BY: Guest

DATE: Sept. 12, 2013, 12:40 p.m.

FORMAT: Text only

SIZE: 2.4 kB

HITS: 1266

  1. function love.load()
  2. player = {
  3. grid_x = 256,
  4. grid_y = 256,
  5. act_x = 200,
  6. act_y = 200,
  7. speed = 10,
  8. dir = "Your move"
  9. }
  10. map = {
  11. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  12. { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
  13. { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
  14. { 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
  15. { 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
  16. { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
  17. { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  18. { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  19. { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  20. { 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0 },
  21. { 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
  22. { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
  23. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
  24. }
  25. end
  26. function love.update(dt)
  27. player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
  28. player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
  29. if player.dir == "u" then
  30. if testMap(0, -1) then
  31. player.grid_y = player.grid_y - 32
  32. end
  33. end
  34. if player.dir == "d" then
  35. if testMap(0, 1) then
  36. player.grid_y = player.grid_y + 32
  37. end
  38. end
  39. if player.dir == "l" then
  40. if testMap(-1, 0) then
  41. player.grid_x = player.grid_x - 32
  42. end
  43. end
  44. if player.dir == "r" then
  45. if testMap(1, 0) then
  46. player.grid_x = player.grid_x + 32
  47. end
  48. end
  49. function love.draw()
  50. love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
  51. --Render the ones in the map.
  52. for y=1, #map do
  53. for x=1, #map[y] do
  54. if map[y][x] == 1 then
  55. love.graphics.rectangle("line", x *32, y * 32, 32, 32)
  56. end
  57. end
  58. end
  59. end
  60. function love.keypressed(key)
  61. if key == "up" then
  62. if testMap(0, -1) then
  63. player.dir = "u"
  64. end
  65. elseif key== "down" then
  66. if testMap(0, 1) then
  67. player.dir = "d"
  68. end
  69. elseif key == "left" then
  70. if testMap(-1, 0) then
  71. player.dir = "l"
  72. end
  73. elseif key == "right" then
  74. if testMap(1, 0) then
  75. player.dir = "r"
  76. end
  77. end
  78. end
  79. function testMap(x, y)
  80. if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
  81. return false
  82. end
  83. return true
  84. end
  85. end

comments powered by Disqus