-- sprite move example 02 function love.load() -- init -- get window width and height scrw = love.graphics.getWidth() scrh = love.graphics.getHeight() -- load image bgimg = love.graphics.newImage("bg_640x480.png") playerimg = love.graphics.newImage("spaceship_001_64x64.png") -- player work player = {} player.x = scrw / 2 player.y = scrh / 2 player.speed = 250 player.imgw = playerimg:getWidth() player.imgh = playerimg:getHeight() end function love.update(dt) -- update -- key check local ang = -1 if love.keyboard.isDown("left") then ang = 180 elseif love.keyboard.isDown("right") then ang = 0 end if love.keyboard.isDown("up") then if ang < 0 then ang = 270 elseif ang == 0 then ang = 270 + 45 else ang = 180 + 45 end elseif love.keyboard.isDown("down") then if ang < 0 then ang = 90 elseif ang == 0 then ang = 45 else ang = 180 - 45 end end if ang >= 0 then local spd = player.speed * dt local ra = math.rad(ang) player.x = player.x + (spd * math.cos(ra)) player.y = player.y + (spd * math.sin(ra)) end -- move area check local wh = player.imgw / 2 local hh = player.imgh / 2 local xmin = wh local ymin = hh local xmax = scrw - wh local ymax = scrh - hh player.x = math.min(math.max(player.x, xmin), xmax) player.y = math.min(math.max(player.y, ymin), ymax) end function love.draw() -- draw BG love.graphics.setColor(255, 255, 255, 255) love.graphics.draw(bgimg, 0, 0) -- love.graphics.rectangle("fill", 0, 465, scrw, 150) love.graphics.setColor(255, 255, 255, 255) -- draw player ofsx = player.imgw / 2 ofsy = player.imgh / 2 love.graphics.draw(playerimg, player.x - ofsx, player.y - ofsy) love.graphics.print("FPS: "..tostring(love.timer.getFPS()), 10, 10) end function love.keypressed(key, isrepeat) -- ESC to exit if key == "escape" then love.event.quit() end end