Advertisement
Rapptz

player.lua

Oct 22nd, 2012
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.00 KB | None | 0 0
  1. player = {}
  2. -- The sprite for the Player
  3. player.sprite = love.graphics.newImage("images/basicpl.png")
  4. -- The x-axis location of the Player
  5. player.x = 300
  6. -- The x-axis velocity of the Player
  7. player.xvelocity = 0
  8. -- The y-axis location of the Player
  9. player.y = 300
  10. -- The y-axis velocity of the player
  11. player.yvelocity = 0
  12. -- The speed of the player.
  13. player.speed = 40
  14. -- The health of the player.
  15. player.health = 20
  16. -- The attack power of the player.
  17. player.attack = 4
  18. -- The friction of the player
  19. player.friction = 40
  20.  
  21. function player.draw()
  22.     love.graphics.draw(player.sprite,player.x,player.y)
  23. end
  24.  
  25. function player.move(dt)
  26.     -- Sets up player velocity for smooth pixel movement
  27.     player.x = player.x + player.xvelocity
  28.     player.xvelocity = player.xvelocity * (1-math.min(tonumber(dt)*player.friction,1))
  29.     player.y = player.y + player.yvelocity
  30.     player.yvelocity = player.yvelocity * (1-math.min(tonumber(dt)*player.friction,1))
  31.     -- Moves the player
  32.     if love.keyboard.isDown("right") and player.xvelocity < 25 then
  33.         player.xvelocity = player.xvelocity + (player.speed*tonumber(dt))
  34.     elseif love.keyboard.isDown("left") and player.xvelocity > -25 then
  35.         player.xvelocity = player.xvelocity - (player.speed*tonumber(dt))
  36.     elseif love.keyboard.isDown("up") and player.yvelocity > -25 then
  37.         player.yvelocity = player.yvelocity - (player.speed*tonumber(dt))
  38.     elseif love.keyboard.isDown("down") and player.yvelocity < 25 then
  39.         player.yvelocity = player.yvelocity + (player.speed*tonumber(dt))
  40.     end
  41.     -- Checks for screen collision
  42.     if player.x < 0 then
  43.         player.x = 0
  44.     elseif player.y < 0 then
  45.         player.y = 0
  46.     elseif player.y + player.sprite:getHeight() > love.graphics.getHeight() then
  47.         player.y = love.graphics.getHeight() - player.sprite:getHeight()
  48.     elseif player.x + player.sprite:getWidth() > love.graphics.getWidth() then
  49.         player.x = love.graphics.getWidth() - player.sprite:getWidth()
  50.     end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement