Use the Love2D engine to develop snake games and love2d snake games
Today, I will introduce a game [snake] recently played by bloggers. I believe that everyone will not be unfamiliar with this game. Today, the blogger will use the Love2D game engine to implement a simple Snake game. In this article, we will introduce the basic algorithms of the snake and Lua programming language, I hope that I can provide some reference and consideration for developing similar games. If there are any deficiencies in this article, I hope you can understand it, because the blogger's game development is basically learning slowly, therefore, there will inevitably be deficiencies.
Game Algorithms
Let's first look at how snakes move?
Through the demonstration of the four images, we can find such a rule:
The movement of a snake actually moves the last element of the snake's body to the position of the first element.
There are two steps to complete such a job:
1. Insert a new element at the position of the Snake Head.
2. Remove the last element at the end of the snake
Now that we understand the movements of snakes, let's think about another question. How can we determine whether a snake has eaten food? The idea is similar to the movement of a snake. We mainly consider the relationship between the element inserted in the snake header and the food. If the coordinates of the element are the same as those of the food, then we can think that the snake has eaten food. At this time, the body of the snake should become longer, so we only need to insert an element in the head of the snake. On the contrary, if a snake does not eat food, it should be moved, so it can be handled by moving. How can we determine the element inserted at the position of the snake header? Let's look at the following program:
-- Calculate the next target point function getNextPoint () -- calculate the next target point snake ={} if (dir = 0) then snake. x = snail Kes [1]. x snake. y = snail Kes [1]. y-20 end if (dir = 1) then snake. x = snail Kes [1]. x snake. y = snail Kes [1]. y + 20 end if (dir = 2) then snake. x = snail Kes [1]. x-20 snake. y = snail Kes [1]. y end if (dir = 3) then snake. x = snail Kes [1]. x + 20 snake. y = snail Kes [1]. y end return snail keend
The getNextPoint () method is defined here to calculate the next element added at the position of the Snake Head. Here we notice that the different directions of the snake's moving (dir) are different, 0 indicates the top, 1 indicates the bottom, 2 indicates the left, 3 indicates the right, calculate the location of the next element, because in this game, the grid size is 20, therefore, the position of an element can be calculated based on coordinates. A snake is a table that stores the coordinates of all the elements of the current snake. By maintaining this table, we can use the drawing function to draw a snake's body so that the snake can move. Let's see how snakes move:
-- Core Algorithm -- Snake's mobile function, "Snake update ()" -- obtains the number of elements. local n = table. maxn (snail KES) if (table. maxn (snkes)> 0) then if (getNextPoint (). x = foodX and getNextPoint (). y = foodY) then -- Insert the location of the next target point into the table. insert (snkes, 1, getNextPoint () -- set the food status to BeEated foodState = "BeEated" else -- insert the location of the next target point into the table. insert (snkes, 1, getNextPoint () -- remove the last element table. remove (snkes, n + 1) end endend
Here we define a foodState variable to save the food state. When the food state is BeEated, it indicates that the food is eaten by a snake. At this time, we should generate a new coordinate for the food, in this case, the State of the transaction changes to WaitToEat. The coordinates of food are stored in the foodX and foodY variables. You can view them in the complete code.
We know that when a snake hits a wall, it will die, and the game is over. This is relatively simple. You only need to judge the relationship between the coordinates of the Snake Head and the screen. Because the screen size in this game is 640X640, you can write the code to determine whether the game is over:
-- Determine the game status if (snail Kes [1]. x <= 0 or snail Kes [1]. x & gt; = 640 or snkes [1]. y <= 0 or snkes [1]. y> = 640) then gameState = 0 else gameState = 1 end
Here, the gameState is 0, indicating that the game is over, and the gameState is 1, indicating that the game is normal.
After completing these core algorithms, let's hand over the rest to the Love2D engine for drawing, and finally provide the complete program code:
-- Define window width and height local w = 640 local h = 640 -- Define grid unit size local unitSize = 20; -- the initial position of the Square: local initX = 320 local initY = 320 -- moving direction: local dir = 1 -- greedy snake collection: local snakes ={} -- food status -- WaitToEat: Drawing food -- BeEated: randomly generated food local foodState = "WaitToEat" -- game status -- 0: Game end -- 1: game normal local gameState = 1 -- Food location local foodX = 0 local foodY = 0 -- Love2D loading event function love. load () -- set the window title love. window. setTitle ("Love2D-Snake Game") -- set the window size love. window. setMode (w, h) -- Define the font myFon T = love. graphics. newFont (30) -- set the font to love. graphics. setFont (myFont) -- set the background color love. graphics. setBackgroundColor (255,255,255,255) -- set the line type to smooth love. graphics. setLineStyle ("smooth") -- set the width of love. graphics. setLineWidth (0.1) -- initialize the snake (the length of the snake is 5) for I = do snake ={} snake. x = initX + (I-1) * 20 snake. y = initY snkes [I] = snake end -- Food initialization foodX = love. math. random (32-1) * 20 foodY = love. math. random (32-1) * 20end -- Love2D draw event funct Ion love. draw () -- draw a vertical line love. graphics. setColor (0, 0, 0,255) for I = 0, w, unitSize do love. graphics. line (0, I, h, I) end -- draw a horizontal line for j = 0, h, unitSize do love. graphics. line (j, 0, j, w) end -- draw a snake for I = 1, table. maxn (snail KES) do love. graphics. setColor (255,255,) love. graphics. rectangle ("fill", snkes [I]. x, snail Kes [I]. y, 20,20) end -- draw the food if (foodState = "WaitToEat") then love. graphics. setColor (255, 0, 0,255) love. graphics. r Ectangle ("fill", foodX, foodY, 20, 20) end -- if the game ends, GameOver if (gameState = 0) then love is displayed. graphics. setColor (255, 0, 0,255) love. graphics. print ("Game Over", 250,300) endend -- function love. update (dt) -- determine the game status if (snail Kes [1]. x <= 0 or snail Kes [1]. x & gt; = 640 or snkes [1]. y <= 0 or snkes [1]. y> = 640) then gameState = 0 else gameState = 1 end -- if the game status is normal if (gameState = 1) then snkeupdate () FoodUpdate () endend-core algorithm-snake mobile funct Ion snkeupdate () -- obtain the number of elements. local n = table. maxn (snail KES) if (table. maxn (snkes)> 0) then if (getNextPoint (). x = foodX and getNextPoint (). y = foodY) then -- Insert the location of the next target point into the table. insert (snkes, 1, getNextPoint () -- set the food status to BeEated foodState = "BeEated" else -- insert the location of the next target point into the table. insert (snkes, 1, getNextPoint () -- remove the last element table. remove (snkes, n + 1) end endend -- randomly generate the food function FoodUpdate () -- if the food is eaten by a snake, regenerate the food if (f OodState = "BeEated") then foodX = love. math. random (32-1) * 20 foodY = love. math. random (32-1) * 20 foodState = "WaitToEat" endend -- Define function love in different directions based on the key position pressed by the player. keypressed (key) if (key = "a" and dir ~ = 3) then dir = 2 end if (key = "d" and dir ~ = 2) then dir = 3 end if (key = "w" and dir ~ = 1) then dir = 0 end if (key = "s" and dir ~ = 0) then dir = 1 endfunction getNextPoint () -- calculate the next target point snake ={} if (dir = 0) then snake. x = snail Kes [1]. x snake. y = snail Kes [1]. y-20 end if (dir = 1) then snake. x = snail Kes [1]. x snake. y = snail Kes [1]. y + 20 end if (dir = 2) then snake. x = snail Kes [1]. x-20 snake. y = snail Kes [1]. y end if (dir = 3) then snake. x = snail Kes [1]. x + 20 snake. y = snail Kes [1]. y end return snail keend
After compressing the code into a. love file, you can run it. Let's take a look at the final effect:
Github