Advertisement
Guest User

luagamelove2d

a guest
Feb 12th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.88 KB | None | 0 0
  1. p={}
  2. platforms={}
  3.  
  4.  
  5. function love.load()
  6.  
  7.   p.x=310
  8.   p.y=love.graphics.getHeight()-400
  9.   p.speed=5
  10.   p.face="right"
  11.   p.cooldown=20
  12.   p.onPlat=false
  13.   p.dVel=0
  14.   p.jump=true
  15.   p.bullets={}
  16.   p.shoot = function(face)
  17.     if p.cooldown <= 0 then
  18.             p.cooldown=10
  19.             bullet = {}
  20.             bullet.x=p.x + 10
  21.             bullet.y=p.y + 20
  22.             bullet.face=face
  23.             table.insert(p.bullets, bullet)
  24.         end
  25.    
  26.    
  27.   end
  28.  
  29. end
  30.  
  31.  
  32.  
  33.  
  34. function spawnPlat(x, y, h, w)
  35.   plat={}
  36.   plat.x=x
  37.   plat.y=y
  38.   plat.height=h
  39.   plat.width=w
  40.   table.insert(platforms, plat)
  41.  
  42.  
  43. end
  44.  
  45.  
  46. spawnPlat(0,love.graphics.getHeight()-20, 20, love.graphics.getWidth())
  47. spawnPlat(300, 450, 20, 100)
  48. spawnPlat(600, 550, 20, 100)
  49. spawnPlat(10, 350, 20, 100)
  50. spawnPlat(600, 350, 20, 100)
  51. spawnPlat(300, 250, 20, 100)
  52.  
  53. function checkPlatCollisions(platforms)
  54.  
  55.   for _,v in pairs(platforms) do
  56.    
  57.    
  58.     if p.y+50>=v.y and p.x+30>v.x and p.x<v.x+v.width then
  59.      
  60.       if v.y<p.y+45 then
  61.        
  62.       elseif p.dVel>0 then
  63.      
  64.     p.onPlat=true
  65.   return
  66.   end
  67.   else
  68.     p.onPlat=false
  69.    
  70.    
  71.   end
  72.   end
  73.  
  74. end
  75.  
  76. function calcFall()
  77.  
  78.   if p.onPlat==false then
  79.    
  80.     p.y=p.y+p.dVel
  81.     p.dVel=p.dVel+0.1
  82.    
  83.    
  84.   else
  85.     p.jump=true
  86.     if p.dVel>=0 then
  87.     p.dVel=0
  88.   else
  89.     p.y=p.y+p.dVel
  90.     p.dVel=p.dVel+6
  91.   end
  92.    
  93.   end
  94.  
  95. end
  96.  
  97. function love.update ()
  98.   p.cooldown=p.cooldown-1
  99.  
  100.   checkPlatCollisions(platforms)
  101.   calcFall()
  102.   --Player Control
  103.   if love.keyboard.isDown("left") then
  104.     if p.x>0 then
  105.     p.x=p.x-p.speed
  106.     end
  107.     p.face="left"
  108.   elseif love.keyboard.isDown("right") then
  109.     if p.x<love.graphics.getWidth()-30 then
  110.     p.x=p.x+p.speed
  111.     end
  112.     p.face="right"
  113.     end
  114.   if love.keyboard.isDown("space") then
  115.     p.shoot(p.face)
  116.   end
  117.   if love.keyboard.isDown("up") then
  118.     if p.jump then
  119.     p.dVel=p.dVel-6
  120.     p.jump=false
  121.     end
  122.   elseif love.keyboard.isDown("down") then
  123.     if p.onPlat then
  124.     p.onPlat=false
  125.     p.y=p.y+5
  126.     end
  127.   end
  128.  
  129.  
  130.  
  131.   --Bullet Cleanup/Control
  132.    for i,b in ipairs(p.bullets) do
  133.        
  134.         if b.x < -10 or b.x > 1000 then
  135.             table.remove(p.bullets, i)
  136.         end
  137.       if b.face=="left" then
  138.         b.x=b.x-10
  139.       elseif b.face=="right" then
  140.         b.x=b.x+10
  141.       end
  142.        
  143.     end
  144.  
  145.  
  146.  
  147. end
  148.  
  149. function love.draw()
  150.  
  151.   --Draw Character
  152.   love.graphics.setColor(255,0,0)
  153.   love.graphics.rectangle('fill', p.x,p.y,30,50)
  154.   --Draw Bullet
  155.    love.graphics.setColor(255,255,255)
  156.     for _,b in pairs(p.bullets) do
  157.         love.graphics.rectangle('fill', b.x, b.y, 10, 10)
  158.     end
  159.     --Draw Platforms
  160.     love.graphics.setColor(0,255,255)
  161.     for i,p in ipairs(platforms) do
  162.       love.graphics.rectangle('fill', p.x, p.y, p.width, p.height)
  163.     end
  164.    
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement