Advertisement
joseleeph

Untitled

Apr 27th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. --create a player class
  2. Player = Object:extend()
  3.  
  4. function Player:new()
  5. self.image = love.graphics.newImage("panda.png")
  6. -- enable player to move with arrow keys
  7. self.x = 300
  8. self.y = 20
  9. self.speed = 500
  10.  
  11. self.width = self.image:getWidth()
  12. end
  13.  
  14. function Player:update(dt)
  15. if love.keyboard.isDown("left") then
  16. self.x = self.x - self.speed*dt
  17. elseif love.keyboard.isDown("right") then
  18. self.x = self.x + self.speed*dt
  19. end
  20. -- the player to can move out of the window. use if statements to fix this
  21. local window_width = love.graphics.getWidth()
  22. --if x is too far to the left
  23. if self.x < 0 then
  24. self.x = 0
  25. -- or too far to the right
  26. elseif self.x + self.width > window_width then
  27. --set the right side to the window's width
  28. self.x = window_width - self.width
  29. -- the player can no longer
  30. end
  31. end
  32.  
  33. function Player:draw()
  34. love.graphics.draw(self.image, self.x, self.y)
  35. end
  36.  
  37. function Player:keyPressed(key)
  38. --if the spacebar is pressed
  39. if key == "space" then
  40. -- put a new instance of a bullet inside list of bullets
  41. --table.insert(listOfBullets, Bullet(self.x, self.y))
  42. table.insert(listOfBullets, Bullet(self.x, self.y))
  43. end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement