Urvorn

Basic Platformer Prototype source code ( by Urvorn )

Apr 14th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. -- Icon and title
  2.  
  3. Icon = love.image.newImageData("images/Monster.png")
  4. love.window.setTitle("Basic Platformer Prototype")
  5. love.window.setIcon(Icon)
  6.  
  7.  
  8.  
  9.  
  10. function love.load()
  11.  
  12.   -- Window
  13.  
  14.   WindowWidth = love.graphics.getWidth()
  15.   WindowHeight = love.graphics.getHeight()
  16.  
  17.    -- Character
  18.  
  19.   Monster = love.graphics.newImage("images/Monster.png")
  20.   MonsterWidth = (Monster):getWidth()
  21.   MonsterHeight = (Monster):getHeight()
  22.   MonsterX = love.graphics.getWidth() / 2
  23.   MonsterY =  love.graphics.getHeight() - 100
  24.   MonsterSpeed = 400
  25.   MonsterYVelocity = 0
  26.   JumpHeight = - 500
  27.   Gravity =  - 700
  28.  
  29.   -- Ground
  30.  
  31.   GroundX = 0
  32.   GroundY = MonsterY
  33.   GroundWidth = WindowWidth
  34.   GroundHeight = WindowHeight - GroundY
  35.  
  36.   -- Platform
  37.  
  38.   Platform = love.graphics.newImage("images/Platform.png")
  39.   PlatformWidth = (Platform):getWidth()
  40.   PlatformHeight = (Platform):getHeight()
  41.   PlatformX = 450
  42.   PlatformY = love.graphics.getHeight() - (100 + PlatformHeight)
  43.  
  44. end
  45.  
  46. function love.update(dt)
  47.  
  48.   if love.keyboard.isDown("right") and MonsterX < (WindowWidth - MonsterWidth) then
  49.     MonsterX = MonsterX + (MonsterSpeed * dt)
  50.   elseif love.keyboard.isDown("left") and MonsterX > 0 then
  51.     MonsterX = MonsterX - (MonsterSpeed * dt)
  52.   end
  53.  
  54.   if love.keyboard.isDown("space") then
  55.    
  56.     if MonsterYVelocity == 0 then
  57.       MonsterYVelocity = JumpHeight
  58.     end
  59.    
  60.   end
  61.    
  62.     if MonsterYVelocity ~= 0 then
  63.       MonsterY = MonsterY + (MonsterYVelocity * dt)
  64.       MonsterYVelocity = MonsterYVelocity - (Gravity * dt)
  65.     end
  66.    
  67.     if MonsterY > GroundY then
  68.       MonsterYVelocity = 0
  69.       if MonsterY == PlatformY and MonsterX >= PlatformX and MonsterX <= (PlatformX + PlatformWidth) then
  70.       MonsterY = PlatformY
  71.       else
  72.       MonsterY = GroundY
  73.      
  74.      end
  75.      
  76.     end
  77.  
  78.    
  79.    
  80.  
  81. end
  82. function love.draw()
  83.  
  84.   -- Character
  85.   love.graphics.setColor(1,1,1)
  86.   love.graphics.draw(Monster, MonsterX, MonsterY, 0, 1, 1, 0, 32)
  87.  
  88.   -- Background
  89.  
  90.   love.graphics.setBackgroundColor(.5,.5,.5)
  91.  
  92.   love.graphics.setColor(0,0,0)
  93.   love.graphics.rectangle("fill", GroundX, GroundY, GroundWidth, GroundHeight)
  94.  
  95.   -- Platform
  96.  
  97.   love.graphics.setColor(1,1,1)
  98.   love.graphics.draw(Platform, PlatformX, PlatformY)
  99.  
  100.  
  101. end
Add Comment
Please, Sign In to add comment