Skip to content

Instantly share code, notes, and snippets.

@amigojapan
Created May 28, 2024 02:55
Show Gist options
  • Save amigojapan/1e8b353bc24bb1b40effca34b84545f6 to your computer and use it in GitHub Desktop.
Save amigojapan/1e8b353bc24bb1b40effca34b84545f6 to your computer and use it in GitHub Desktop.
--rename to main.lua
--run love .
player={}
player.x=100
player.y=100
player.speed=50
object1={}
object1.x=300
object1.y=300
colliding="false"
function love.load()
player.image = love.graphics.newImage('mario.png')
object1.image = love.graphics.newImage('pipe.png')
end
function love.update(dt)
if love.keyboard.isDown("up") then
player.y = player.y - player.speed * dt
end
if love.keyboard.isDown("down") then
player.y = player.y + player.speed * dt
end
if love.keyboard.isDown("left") then
player.x = player.x - player.speed * dt
end
if love.keyboard.isDown("right") then
player.x = player.x + player.speed * dt
end
if detectCollision(player.x, player.y, player.image:getWidth(), player.image:getHeight(), object1.x, object1.y, object1.image:getWidth(), object1.image:getHeight()) then
colliding = "true"
else
colliding = "false"
end
end
function love.draw()
love.graphics.print("colliding:"..colliding, 0, 0)
love.graphics.draw(player.image, player.x, player.y)
love.graphics.draw(object1.image, object1.x, object1.y)
end
function detectCollision(x1, y1, width1, height1, x2, y2, width2, height2)
if x1 + width1 > x2 and x1 < x2 + width2 and y1 + height1 > y2 and y1 < y2 + height2 then
return true
else
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment