Guest User

Untitled

a guest
Jun 17th, 2018
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. directions = {up = {x = 0, y = -1}, down = {x = 0, y = 1}, left = {x = -1, y = 0}, right = {x = 1, y = 0}}
  2.  
  3. function love.load()
  4. game = {}
  5. game.width = love.graphics.getWidth()
  6. game.height = love.graphics.getHeight()
  7. game.running = true
  8.  
  9. player = {}
  10. player.x = game.width / 2
  11. player.y = game.height / 2
  12. player.dir = "left"
  13. player.speed = 150
  14. player.color = {255, 255, 255}
  15. player.points = {}
  16. player.max_length = 100
  17.  
  18. love.graphics.setBackgroundColor(0, 0, 0)
  19. end
  20.  
  21. function love.update(dt)
  22. if game.running then
  23.  
  24. -- Figure out the next position for the snake's head
  25. player.x = player.x + directions[player.dir].x * player.speed * dt
  26. player.y = player.y + directions[player.dir].y * player.speed * dt
  27. table.insert(player.points, 1, {x = player.x, y = player.y})
  28.  
  29. -- Is the player running into a wall?
  30. if player.x <= 0 or player.x >= game.width or player.y <= 0 or player.y >= game.height then
  31. game.running = false
  32. end
  33.  
  34. -- Keep the snake no larger than the max_length
  35. if #player.points >= player.max_length then
  36. table.remove(player.points)
  37. end
  38. end
  39. end
  40.  
  41. function love.draw()
  42. if game.running then
  43.  
  44. -- Draw the snake's segments
  45. love.graphics.setColor(unpack(player.color))
  46. for _, pos in pairs(player.points) do
  47. love.graphics.point(pos.x, pos.y)
  48. end
  49.  
  50. else
  51. -- Game Over Screen
  52. love.graphics.printf("Game Over", game.width / 2, game.height / 2, 0, "center")
  53. end
  54. end
  55.  
  56. function love.keypressed(key)
  57. if directions[key] then
  58. player.dir = key
  59. end
  60.  
  61. if key == "escape" then
  62. love.event.push("q")
  63. end
  64. end
Add Comment
Please, Sign In to add comment