Advertisement
IlanZiin

Base lua game dev

Jan 28th, 2023
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.78 KB | None | 0 0
  1. love = require 'love'
  2.  
  3. function love.load()
  4.     anim8 = require 'libraries/anim8'
  5.     love.graphics.setDefaultFilter('nearest', 'nearest')
  6.  
  7.     player = {}
  8.     player.x = love.graphics.getWidth()/2 - 80/2
  9.     player.y = love.graphics.getHeight()/2 - 80/2
  10.     player.speed = 5
  11.  
  12.     player.spriteSheet = love.graphics.newImage("sprites/player-sheet.png")
  13.     player.grid = anim8.newGrid(12, 18, player.spriteSheet:getWidth(), player.spriteSheet:getHeight())
  14.  
  15.     player.animations = {}
  16.     player.animations.down = anim8.newAnimation(player.grid('1-4', 1), 0.2)
  17.     player.animations.left = anim8.newAnimation(player.grid('1-4', 2), 0.2)
  18.     player.animations.right = anim8.newAnimation(player.grid('1-4', 3), 0.2)
  19.     player.animations.up = anim8.newAnimation(player.grid('1-4', 4), 0.2)
  20.    
  21.     player.anim = player.animations.left
  22. end
  23.  
  24. function love.update(dt)
  25.     local isMoving = false
  26.  
  27.     if love.keyboard.isDown("a", "left") then
  28.         player.x = player.x - player.speed
  29.         player.anim = player.animations.left
  30.         isMoving = true
  31.     end
  32.  
  33.     if love.keyboard.isDown("d", "right") then
  34.         player.x = player.x + player.speed
  35.         player.anim = player.animations.right
  36.         isMoving = true
  37.     end
  38.  
  39.     if love.keyboard.isDown("s", "down") then
  40.         player.y = player.y + player.speed
  41.         player.anim = player.animations.down
  42.         isMoving = true
  43.     end
  44.  
  45.     if love.keyboard.isDown("w", "up") then
  46.         player.y = player.y - player.speed
  47.         player.anim = player.animations.up
  48.         isMoving = true
  49.     end
  50.  
  51.     if isMoving == false then
  52.         player.anim:gotoFrame(2)
  53.     end
  54.  
  55.     player.anim:update(dt)
  56. end
  57.  
  58. function love.draw()
  59.     player.anim:draw(player.spriteSheet, player.x, player.y, nil, 6, nil, 6, 9)
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement