📜 ⬆️ ⬇️

We continue to study Love2d


In the previous post, I explained how to draw pictures and, like, explained what was happening in love2d. Today I decided to write a snake, to all who are interested, please under the cat. We launch Sublime Text 2, how to configure it for love I said last time, but today we will use debug, but for this you need to write three extra lines, namely: Next to main.lua, in the root of the project directory, create a file and call his conf.lua. This file is for game settings. We write the following code in it:
function love.conf(t)--   love    io.stdout:setvbuf("no")---  Sublime Text,       , --       print(str)     . end 

You can read more about this feature here and here .

Well, now the code

Prepare the variables:
 local apple, snake, direction, width, height, delay function love.load() width = love.graphics.getWidth() / 10 --       print("Width " .. width) --  height = love.graphics.getHeight() /10 --     print("Height " .. height) apple = { x = math.random() % width; --     X- y = math.random() % height; } -- Y.. snake = { } --     for i = 1, 10 do -- 10    -  point = { x = 10; --  X.. y = 10 + i; } -- Y.. table.insert(snake, point) --   end direction = "up" --  -  delay = 0 --     end 


It seems to have explained everything, for those who once wrote a snake everything should be clear.
')
Now we need to draw a snake (So far without an apple);
 function love.draw() love.graphics.setBackgroundColor(0, 0, 0) --  for k, v in pairs(snake) do --   love.graphics.rectangle("fill", vx * 10, vy * 10, 10, 10) end end 


And now there should be questions, namely:
1) What color is the snake
2) (Less interesting) What is the love.graphics.rectangle function?

Answers:

1) It will be drawn in white, this color will be placed inside the code before calling love.draw (). If you can put this color like this:
 love.graphics.setColor(0, 0, 255) --   

This function has four arguments, but the fourth is not mandatory - this is alpha color. A detailed description is here .
2) As the name suggests, this function draws a quadrilateral of color given by this function. The first argument is the drawing mode, it can be “fine” or “line”. The second and third arguments of the coordinates, Fourth and fifth - dimensions, in detail here .

We can continue

Hooray! We have a fixed snake, let's poke it, fix it, write the Update function:
 function love.update(dt) if delay % 2 == 0 then --  table.remove(snake, table.getn(snake)) --        if direction == "up" then --    ,   table.insert(snake, 1, { x = snake[1].x; y = (snake[1].y - 1) % height; }) elseif direction == "down" then table.insert(snake, 1, { x = snake[1].x; y = (snake[1].y + 1) % height; }) elseif direction == "left" then table.insert(snake, 1, { x = (snake[1].x - 1) % width; y = snake[1].y; }) else table.insert(snake, 1, { x = (snake[1].x + 1) % width; y = snake[1].y; }) end end delay = delay + 1 end 

Now we have an uncontrollable snake, this is also not what we want, so add power:
 function love.keypressed(key) --        -- ,   if key == "up" then direction = "up" elseif key == "down" then direction = "down" elseif key == "left" then direction = "left" elseif key == "right" then direction = "right" elseif key == "escape" then love.event.quit() end end 

Now we manage what is happening, but where is the goal? Add the same apples:
First, we have an apple, but we don’t draw it, we’ll add a drawing to love.draw ():
 love.graphics.setColor(255, 255, 0) --   love.graphics.rectangle("fill", apple.x * 10, apple.y * 10, 10, 10) 

And now we need to grow:
 if delay % 2 == 0 then --  if apple.x == snake[1].x and apple.y == snake[1].y then apple = { x = math.random(width); --  ,  X- y = math.random(height); } -- Y.. else --  ,   table.remove(snake, table.getn(snake)) --        end if direction == "up" then --    ,   table.insert(snake, 1, { x = snake[1].x; y = (snake[1].y - 1) % height; }) elseif direction == "down" then table.insert(snake, 1, { x = snake[1].x; y = (snake[1].y + 1) % height; }) elseif direction == "left" then table.insert(snake, 1, { x = (snake[1].x - 1) % width; y = snake[1].y; }) else table.insert(snake, 1, { x = (snake[1].x + 1) % width; y = snake[1].y; }) end end delay = delay + 1 


We crawl, control, eat, everything is ready!

Thank you for reading.
Sources, with a small addon.

I would be grateful for any feedback (:

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


All Articles