Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. debug = true
  2.  
  3. background = { x = 1280, y = 720, speed = 0, img = nil}
  4. player = { x = 350, y = 250, width = 52, height = 116, speed = 335, img = nil}
  5. local bump = require 'bump'
  6. local dumpdebug = require 'dumpdebug'
  7. local rectangles = {}
  8. local world = bump.newWorld(16)
  9. local debugDraw
  10. local directionX = 0
  11. local directionY = 0
  12. local sti = require "STI"
  13.  
  14. function love.load(arg)
  15. player.crouching = false
  16. player.standimg = love.graphics.newImage('assets/Albert.png')
  17. background.mapzero = love.graphics.newImage('assets/Map0.png')
  18. background.mapone = love.graphics.newImage('assets/Map1.png')
  19. background.maptwo = love.graphics.newImage('assets/Map2.png')
  20. background.mapthree = love.graphics.newImage('assets/Map3.png')
  21. background.mapfour = love.graphics.newImage('assets/Map4.png')
  22. player.crouchimg = love.graphics.newImage('assets/Bob.png')
  23. player.img = player.standimg
  24. remainingSpeed = 335
  25. -- We now have an asset ready to be used inside love
  26.  
  27. for line in love.filesystem.lines("map0.txt") do
  28. local x1,y1,x2,y2 = line:match('(%d+),(%d+),(%d+),(%d+)')
  29. x1 = tonumber(x1)
  30. x2 = tonumber(x2)
  31. y1 = tonumber(y1)
  32. y2 = tonumber(y2)
  33. local w = x2 - x1
  34. local h = y2 - y1
  35. local rectangle = { x1, y1, w, h }
  36. table.insert(rectangles, rectangle)
  37. world:add(rectangle, x1, y1, w, h)
  38. end
  39. world:add(player, player.x, player.y, player.width, player.height)
  40. end
  41.  
  42. -- Updating
  43. function love.update(dt)
  44. -- I always start with an easy way to exit the game
  45. -- Vertical movement
  46. -- Binds us to the map
  47. if player.crouching == false then
  48. directionX = "0"
  49. directionY = "0"
  50. if love.keyboard.isDown('up', 'w') then
  51. if player.y > 0 then
  52. player.y = player.y - (player.speed*dt)
  53. directionY = 'up'
  54. end
  55. elseif love.keyboard.isDown('down', 's') then
  56. if player.y < (love.graphics.getHeight() - player.img:getHeight()) then
  57. player.y = player.y + (player.speed*dt)
  58. directionY = 'down'
  59. end
  60. end
  61. if love.keyboard.isDown('left', 'a') then
  62. if player.x > 0 then
  63. player.x = player.x - (player.speed*dt)
  64. directionX = 'left'
  65. end
  66. elseif love.keyboard.isDown('right', 'd') then
  67. if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
  68. player.x = player.x + (player.speed*dt)
  69. directionX = 'right'
  70. end
  71. end
  72. else
  73. if directionY == 'up' then
  74. player.y = player.y - remainingSpeed
  75. elseif directionY == 'down' then
  76. player.y = player.y + remainingSpeed
  77. end
  78. if directionX == 'left' then
  79. player.x = player.x - remainingSpeed
  80. elseif directionX == 'right' then
  81. player.x = player.x + remainingSpeed
  82. end
  83. end
  84. remainingSpeed = remainingSpeed + (0 - remainingSpeed)*dt
  85. player.x,player.y = world:move(player, player.x, player.y)
  86. end
  87.  
  88.  
  89. function love.draw(dt)
  90. love.graphics.setColor(255,255,255)
  91. if debugDraw then
  92. dumpdebug.draw(world)
  93. end
  94. love.graphics.draw(background.mapzero)
  95. love.graphics.draw(player.img, player.x, player.y)
  96. love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 50)
  97. local coords = love.mouse.getX().. ", " .. love.mouse.getY()
  98. love.graphics.print(coords, 10, 10)
  99. for i, v in pairs(rectangles) do
  100. love.graphics.rectangle('line', v[1], v[2], v[3], v[4])
  101. end
  102. end
  103.  
  104. function love.keypressed(key)
  105. if key == "escape" then
  106. love.event.quit()
  107. end
  108. if key == "f12" then
  109. debugDraw = not debugDraw
  110. end
  111. if key == "lctrl" then
  112. player.crouching = true
  113. player.img = player.crouchimg
  114. world:update(player, player.x,player.y,52,116)
  115. remainingSpeed = 3
  116. end
  117. end
  118.  
  119. function love.keyreleased(key)
  120. if key == "lctrl" then
  121. player.crouching = false
  122. player.img = player.standimg
  123. world:update(player, player.x,player.y,52,116)
  124. remainingSpeed = 0
  125. end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement