daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local platform = {}
  2. local player = {}
  3.  
  4.  
  5.  
  6. function love.load()
  7.     platform.width = love.graphics.getWidth()
  8.     platform.height = love.graphics.getHeight()
  9.     platform.x = 0
  10.     platform.y = platform.height*2/3
  11.    
  12.     player.x = love.graphics.getWidth() / 2
  13.     player.y = platform.y
  14.    
  15.     --horizontal movement sprites
  16.     player.movespriteright = love.graphics.newImage("icons/rightsidemario.png")
  17.     player.movespriteleft = love.graphics.newImage("icons/leftsidemario.png")
  18.     player.movesprite = love.graphics.newImage("icons/rightsidemario.png")
  19.    
  20.     leftimgWidth, leftimgHeight = player.movespriteleft:getDimensions()
  21.     rightimgWidth, rightimgHeight = player.movespriteright:getDimensions()
  22.     frames = 3
  23.     mariospriteWidth = 16
  24.     mariospriteHeight = 32
  25.    
  26.    
  27.     lastquads = love.graphics.newQuad(0, 0, mariospriteWidth, mariospriteHeight, rightimgWidth, rightimgHeight)
  28.     jumpleftquads = love.graphics.newQuad(255, 0, mariospriteWidth, mariospriteHeight, leftimgWidth, leftimgHeight)
  29.     jumprightquads = love.graphics.newQuad(85, 0, mariospriteWidth, mariospriteHeight, rightimgWidth, rightimgHeight)
  30.     jumplastquads = jumprightquads
  31.     leftquads = {}
  32.     rightquads = {}
  33.     leftfirstimgx = 323
  34.     rightfirstimgx = 17
  35.    
  36.     for i = 0 , frames  do
  37.         table.insert(leftquads, love.graphics.newQuad(leftfirstimgx - i - i*mariospriteWidth, 0, mariospriteWidth, mariospriteHeight, leftimgWidth, leftimgHeight))
  38.     end
  39.    
  40.     for i = 0, frames  do
  41.         table.insert (rightquads, love.graphics.newQuad(rightfirstimgx + i + i*mariospriteWidth, 0, mariospriteWidth, mariospriteHeight, rightimgWidth, rightimgHeight))
  42.     end
  43.    
  44.     timer_horizontal = 0
  45.     framespeed_horizontal = 15
  46.     horizontal_direction = 0
  47.     vertical_jump = 0
  48.    
  49.     player.speed = 200
  50.     player.speed = 200
  51.     player.ground = player.y
  52.     player.y_velocity = 0
  53.     player.jump_height = -300
  54.     player.gravity = 500
  55. end
  56.  
  57. function love.update(dt)
  58.     --spawning player in opposite sides
  59.     if player.x > love.graphics.getWidth() + mariospriteWidth then
  60.         player.x = - mariospriteWidth
  61.     elseif player.x < - mariospriteWidth  then
  62.         player.x = love.graphics.getWidth() + mariospriteWidth
  63.     end
  64.    
  65.     --moving left and right, and jumping
  66.     if love.keyboard.isDown("right") then
  67.         horizontal_direction = 1
  68.         player.x = player.x + (player.speed * dt)
  69.         timer_horizontal = timer_horizontal + dt * framespeed_horizontal
  70.     end
  71.    
  72.     if love.keyboard.isDown("left") then
  73.         horizontal_direction = -1
  74.         player.x = player.x - (player.speed * dt)
  75.         timer_horizontal = timer_horizontal + dt * framespeed_horizontal
  76.     end
  77.    
  78.     if love.keyboard.isDown("space") then
  79.         if player.y_velocity == 0 then
  80.             player.y_velocity = player.jump_height
  81.         end
  82.     end
  83.    
  84.     if player.y_velocity ~= 0 then
  85.         vertical_jump = 1
  86.         player.y = player.y + player.y_velocity * dt
  87.         player.y_velocity = player.y_velocity + (player.gravity * dt)
  88.     end
  89.      if player.y >= player.ground then    
  90.         player.y_velocity = 0
  91.         player.y = player.ground
  92.         vertical_jump = 0
  93.     end
  94.    
  95.     function love.keyreleased(key)
  96.         if key == "right" or key == "left" or key == "space" then
  97.             horizontal_direction = 0
  98.         end
  99.     end
  100.  
  101. end
  102.  
  103. function love.draw()
  104.     love.graphics.setColor(255, 255, 255)
  105.     love.graphics.rectangle("fill", platform.x, platform.y, platform.width, platform.height)
  106.    
  107.     if horizontal_direction == -1 and vertical_jump == 1 then
  108.         love.graphics.draw(player.movespriteleft, jumpleftquads, player.x, player.y, 0, 2, 2, mariospriteWidth/2, mariospriteHeight)
  109.         jumplastquads = jumpleftquads
  110.         player.movesprite = player.movespriteleft
  111.     elseif horizontal_direction == -1 then
  112.         love.graphics.draw(player.movespriteleft, leftquads[math.floor(timer_horizontal) % frames + 1], player.x, player.y, 0, 2, 2, mariospriteWidth/2, mariospriteHeight)
  113.         player.movesprite = player.movespriteleft
  114.         lastquads = love.graphics.newQuad(340, 0, mariospriteWidth, mariospriteHeight, leftimgWidth, leftimgHeight)
  115.     end
  116.    
  117.     if horizontal_direction == 1 and vertical_jump == 1 then
  118.         love.graphics.draw(player.movespriteright, jumprightquads, player.x, player.y, 0, 2, 2, mariospriteWidth/2, mariospriteHeight)
  119.         jumplastquads = jumprightquads
  120.         player.movesprite = player.movespriteright
  121.     elseif horizontal_direction == 1 then
  122.         love.graphics.draw(player.movespriteright, rightquads[math.floor(timer_horizontal) % frames + 1], player.x, player.y, 0, 2, 2, mariospriteWidth/2, mariospriteHeight)
  123.         player.movesprite = player.movespriteright
  124.         lastquads = love.graphics.newQuad(0, 0, mariospriteWidth, mariospriteHeight, rightimgWidth, rightimgHeight)
  125.     end
  126.    
  127.     if horizontal_direction == 0 and vertical_jump == 1 then
  128.         love.graphics.draw(player.movesprite, jumplastquads, player.x, player.y, 0, 2, 2, mariospriteWidth/2, mariospriteHeight)
  129.     elseif horizontal_direction == 0 then
  130.         love.graphics.draw(player.movesprite, lastquads, player.x, player.y, 0, 2, 2, mariospriteWidth/2, mariospriteHeight)
  131.     end
  132. end
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top