📜 ⬆️ ⬇️

Love in pixels or what is Love2d

What is Love2d and what is this post about? This is a cross-platform framework for developing 2d games. Why choose love2d? Because it is a free, easy, cross-platform, open source, and most importantly engine made with love.


Training

You can write games on it at least in notepad, but I will use Sublime Text 2 , as it is simple and flexible. You can download love here for all popular desktop platforms. We launch Sublime text and immediately go to Tools-> Build System-> New Build System ... And we write there this:

{ "selector": "source.lua", "cmd": ["c:\\Program Files\\LOVE\\love", "${project_path:${folder}}"] } 

(If you have x64 change the path to love)

This is for easy launch, now all you need is to press Ctrl + B. Create a folder where we will store our game. In it we create main.lua. The base of our game will be stored in this file. And in Sublime text add a folder to the project. All is ready. It should be something like this:
')
.

I want a code!

All the logic will be updated in the love.update (dt) function, and the drawing will be in love.draw (), the initialization will be love.load (). So let's write them right away:

 function love.load() end function love.update(dt) end function love.draw() end 


Now let's add the load image and draw it immediately. Images are loaded using the love.graphics.newImage (filename) function, and are drawn in love.graphics.draw (image, x, y). Add this picture to the folder. And write the code:

 local habrImage, width, height function love.load() habrImage = love.graphics.newImage("habr.png") width = love.graphics.getWidth() height = love.graphics.getHeight() end function love.update(dt) end function love.draw() love.graphics.draw(habrImage, width / 2 - habrImage:getWidth() / 2, height / 2 - habrImage:getHeight() / 2) end 


Ctrl + B and we have something not beautiful, let's correct and make the background white. Add this line at the beginning of the drawing:

 love.graphics.setBackgroundColor(255, 255, 255) 


And so, nice looking picture.

This is all, of course, cool, but let's add life to our game and make actions with a picture, namely:
- On the R key, the picture will be spinning
- On the S key, the picture will increase / decrease
- On the M key, the picture will move.

To do this, we add state variables (will be responsible for what is happening), rotation (angle of the picture), scale (its size), ox, ox (offset of the center of the picture) and delta (this variable will be responsible for the update of variables). Now about input, in love, when the key is lowered, the function love.keypressed (key, unicode) is called, and when you raise love.keyreleased (key). We will track the key down. Now the code itself:

 local habrImage, width, height, state, rotation, scale, ox, oy, delta --  function love.load() habrImage = love.graphics.newImage("habr.png") width = love.graphics.getWidth() height = love.graphics.getHeight() state = "none" resetVariables() end --         function resetVariables() rotation = 0 scale = 1 ox = 0 oy = 0 delta = 1 end --    function love.keypressed(key, unicode) if key == "r" then state = "rotation" resetVariables() elseif key == "s" then state = "scalling" resetVariables() elseif key == "m" then state = "moving" resetVariables() elseif key == "space" then --    (: state = "none" resetVariables() end end --  function love.update(dt) if state == "rotation" then --  rotation = rotation + delta * dt elseif state == "scalling" then -- scale = scale + delta * dt elseif state == "moving" then --  ,    : --     --          --   delta = delta + delta * dt local radius = 50 ox = radius * math.sin(delta) oy = radius * math.cos(delta) end end -- function love.draw() --  love.graphics.setBackgroundColor(255, 255, 255) --  love.graphics.draw(habrImage, width / 2 - habrImage:getWidth() / 2, height / 2 - habrImage:getHeight() / 2, rotation, scale, scale, ox, oy) end 


That's all, gentlemen. Good luck with the creation of games! For help, here's a wiki and off site .

Continued.

ZY This is all written in 0.8.0.
ZZY Do not forget to choose Build System to run.

Source: https://habr.com/ru/post/202540/


All Articles