-
-
Save videlais/53731e91886acfa7634fdca118ae41bb to your computer and use it in GitHub Desktop.
player.lua updated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object = require("classic") | |
Player = Object:extend() | |
function Player:new(world) | |
-- Add an image from the filename "player.png" | |
self.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() | |
self.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() | |
self.shape = love.physics.newRectangleShape( | |
self.body:getX(), | |
self.body:getY(), | |
self.image:getWidth( ), | |
self.image:getHeight( ) | |
) | |
-- Attach the shape to the body | |
self.fixture = love.physics.newFixture(self.body, self.shape, 1); | |
end | |
function Player:update() | |
if love.keyboard.isDown("up") then | |
self.body:applyForce(0, -40) | |
end | |
if love.keyboard.isDown("down") then | |
self.body:applyForce(0, 40) | |
end | |
if love.keyboard.isDown("left") then | |
self.body:applyForce(-40, 0) | |
end | |
if love.keyboard.isDown("right") then | |
self.body:applyForce(40, 0) | |
end | |
end | |
function Player:draw() | |
-- Draw the player.image texture at the coordinates of getX() and getY() | |
love.graphics.draw(self.image, self.body:getX(), self.body:getY() ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment