Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- debug = true
- background = { x = 1280, y = 720, speed = 0, img = nil}
- player = { x = 350, y = 250, width = 52, height = 116, speed = 335, img = nil}
- local bump = require 'bump'
- local dumpdebug = require 'dumpdebug'
- local rectangles = {}
- local world = bump.newWorld(16)
- local debugDraw
- local directionX = 0
- local directionY = 0
- local sti = require "STI"
- function love.load(arg)
- player.crouching = false
- player.standimg = love.graphics.newImage('assets/Albert.png')
- background.mapzero = love.graphics.newImage('assets/Map0.png')
- background.mapone = love.graphics.newImage('assets/Map1.png')
- background.maptwo = love.graphics.newImage('assets/Map2.png')
- background.mapthree = love.graphics.newImage('assets/Map3.png')
- background.mapfour = love.graphics.newImage('assets/Map4.png')
- player.crouchimg = love.graphics.newImage('assets/Bob.png')
- player.img = player.standimg
- remainingSpeed = 335
- -- We now have an asset ready to be used inside love
- for line in love.filesystem.lines("map0.txt") do
- local x1,y1,x2,y2 = line:match('(%d+),(%d+),(%d+),(%d+)')
- x1 = tonumber(x1)
- x2 = tonumber(x2)
- y1 = tonumber(y1)
- y2 = tonumber(y2)
- local w = x2 - x1
- local h = y2 - y1
- local rectangle = { x1, y1, w, h }
- table.insert(rectangles, rectangle)
- world:add(rectangle, x1, y1, w, h)
- end
- world:add(player, player.x, player.y, player.width, player.height)
- end
- -- Updating
- function love.update(dt)
- -- I always start with an easy way to exit the game
- -- Vertical movement
- -- Binds us to the map
- if player.crouching == false then
- directionX = "0"
- directionY = "0"
- if love.keyboard.isDown('up', 'w') then
- if player.y > 0 then
- player.y = player.y - (player.speed*dt)
- directionY = 'up'
- end
- elseif love.keyboard.isDown('down', 's') then
- if player.y < (love.graphics.getHeight() - player.img:getHeight()) then
- player.y = player.y + (player.speed*dt)
- directionY = 'down'
- end
- end
- if love.keyboard.isDown('left', 'a') then
- if player.x > 0 then
- player.x = player.x - (player.speed*dt)
- directionX = 'left'
- end
- elseif love.keyboard.isDown('right', 'd') then
- if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
- player.x = player.x + (player.speed*dt)
- directionX = 'right'
- end
- end
- else
- if directionY == 'up' then
- player.y = player.y - remainingSpeed
- elseif directionY == 'down' then
- player.y = player.y + remainingSpeed
- end
- if directionX == 'left' then
- player.x = player.x - remainingSpeed
- elseif directionX == 'right' then
- player.x = player.x + remainingSpeed
- end
- end
- remainingSpeed = remainingSpeed + (0 - remainingSpeed)*dt
- player.x,player.y = world:move(player, player.x, player.y)
- end
- function love.draw(dt)
- love.graphics.setColor(255,255,255)
- if debugDraw then
- dumpdebug.draw(world)
- end
- love.graphics.draw(background.mapzero)
- love.graphics.draw(player.img, player.x, player.y)
- love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 50)
- local coords = love.mouse.getX().. ", " .. love.mouse.getY()
- love.graphics.print(coords, 10, 10)
- for i, v in pairs(rectangles) do
- love.graphics.rectangle('line', v[1], v[2], v[3], v[4])
- end
- end
- function love.keypressed(key)
- if key == "escape" then
- love.event.quit()
- end
- if key == "f12" then
- debugDraw = not debugDraw
- end
- if key == "lctrl" then
- player.crouching = true
- player.img = player.crouchimg
- world:update(player, player.x,player.y,52,116)
- remainingSpeed = 3
- end
- end
- function love.keyreleased(key)
- if key == "lctrl" then
- player.crouching = false
- player.img = player.standimg
- world:update(player, player.x,player.y,52,116)
- remainingSpeed = 0
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement