Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- player = {}
- -- The sprite for the Player
- player.sprite = love.graphics.newImage("images/basicpl.png")
- -- The x-axis location of the Player
- player.x = 300
- -- The x-axis velocity of the Player
- player.xvelocity = 0
- -- The y-axis location of the Player
- player.y = 300
- -- The y-axis velocity of the player
- player.yvelocity = 0
- -- The speed of the player.
- player.speed = 40
- -- The health of the player.
- player.health = 20
- -- The attack power of the player.
- player.attack = 4
- -- The friction of the player
- player.friction = 40
- function player.draw()
- love.graphics.draw(player.sprite,player.x,player.y)
- end
- function player.move(dt)
- -- Sets up player velocity for smooth pixel movement
- player.x = player.x + player.xvelocity
- player.xvelocity = player.xvelocity * (1-math.min(tonumber(dt)*player.friction,1))
- player.y = player.y + player.yvelocity
- player.yvelocity = player.yvelocity * (1-math.min(tonumber(dt)*player.friction,1))
- -- Moves the player
- if love.keyboard.isDown("right") and player.xvelocity < 25 then
- player.xvelocity = player.xvelocity + (player.speed*tonumber(dt))
- elseif love.keyboard.isDown("left") and player.xvelocity > -25 then
- player.xvelocity = player.xvelocity - (player.speed*tonumber(dt))
- elseif love.keyboard.isDown("up") and player.yvelocity > -25 then
- player.yvelocity = player.yvelocity - (player.speed*tonumber(dt))
- elseif love.keyboard.isDown("down") and player.yvelocity < 25 then
- player.yvelocity = player.yvelocity + (player.speed*tonumber(dt))
- end
- -- Checks for screen collision
- if player.x < 0 then
- player.x = 0
- elseif player.y < 0 then
- player.y = 0
- elseif player.y + player.sprite:getHeight() > love.graphics.getHeight() then
- player.y = love.graphics.getHeight() - player.sprite:getHeight()
- elseif player.x + player.sprite:getWidth() > love.graphics.getWidth() then
- player.x = love.graphics.getWidth() - player.sprite:getWidth()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement