Skip to content

Instantly share code, notes, and snippets.

@videlais
Created April 10, 2018 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save videlais/f5dc8a452ebc8deaab1a7ba9baeedaad to your computer and use it in GitHub Desktop.
Save videlais/f5dc8a452ebc8deaab1a7ba9baeedaad to your computer and use it in GitHub Desktop.
Image main.lua update
function love.load()
-- A meter will be 64px
love.physics.setMeter(64)
-- Set the X gravity, Y gravity, and if objects can sleep
world = love.physics.newWorld( 0, 0, true )
-- Create a player table
player = {}
-- Add an image from the filename "player.png"
player.image = love.graphics.newImage( "player.png" )
-- Create a new physics body in the world and make it "dynamic"
-- The new coordinates will be where the 'body' is going forward.
-- Any time (like for drawing) the x,y is neededm use getX() and getY()
player.body = love.physics.newBody( world, 100, 100, "dynamic" )
-- Make a rectangle shape with:
-- Starting at 100, 100 (player.body:getX() and player.body:getY() )
-- a width of player.image:getWidth()
-- a height of player.image.getHeight()
player.shape = love.physics.newRectangleShape(
player.body:getX(),
player.body:getY(),
player.image:getWidth( ),
player.image:getHeight( )
)
-- Attach the shape to the body
player.fixture = love.physics.newFixture(player.body, player.shape, 1);
end
function love.update(dt)
-- Feed the world the current delta time (dt)
world:update(dt)
if love.keyboard.isDown("up") then
player.body:applyForce(0, -40)
end
if love.keyboard.isDown("down") then
player.body:applyForce(0, 40)
end
if love.keyboard.isDown("left") then
player.body:applyForce(-40, 0)
end
if love.keyboard.isDown("right") then
player.body:applyForce(40, 0)
end
end
function love.draw()
-- Draw the player.image texture at the coordinates of getX() and getY()
love.graphics.draw(player.image, player.body:getX(), player.body:getY() )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment