Today to introduce Bo Master recently churn a small game [snake], snake this game believe that everyone will not feel strange it. Today Bo Lord through love2d this game engine to achieve a simple snake game for everyone, in this article we will be involved in the basic algorithm of the snake, Lua language programming and other basic content, hope to develop similar games for everyone to provide reference and thinking, the article if there are shortcomings, Also hope that we can understand, because Bo Master game development is so slowly groping to learn, so inevitably there will be insufficient places.
Game algorithms
Let's start by looking at how the snake moves.
With this four-figure demonstration, we can find a pattern:
The movement of a snake actually moves the last element of the snake's body to the position of the first element.
It takes two steps to complete such a job:
1. A new element will be inserted in the position of the snake head
2. Remove the last element of the snake's tail position
Well, after the snake's movement, let's consider a question, how to tell if the snake eats food? The idea is similar to the movement of a snake, mainly considering the relationship between the element and the food in the head of the snake, if the coordinates of this element are the same as the coordinates of the food, then it can be thought that the snake eats the food, and the body of the snake should be longer, so long as it is inserted into the head of the snake. Conversely, if the snake does not eat food, then the snake should be mobile, so you can follow the mobile way to deal with. So how do you determine the element that is inserted at the head of the snake? Let's take a look at the following procedure:
--Calculate the next target point function getnextpoint() --Calculate the next target pointsnake={}if(dir==0) Thensnake.x=snakes[1].x snake.y=snakes[1].y- - End if(dir==1) Thensnake.x=snakes[1].x snake.y=snakes[1].y+ - End if(dir==2) Thensnake.x=snakes[1].x- -snake.y=snakes[1].yEnd if(dir==3) Thensnake.x=snakes[1].x+ -snake.y=snakes[1].yEnd returnSnakeEnd
The method of Getnextpoint () is defined here to calculate the next element added at the snake head position, where we notice that depending on the movement direction (dir) of the snake, where 0 means the upper, 1 means lower, 2 means left, 3 represents right, and the next element is calculated, Because the grid size is 20 in this game, it is possible to calculate the position of an element directly from the coordinates. Snakes is a table that holds the coordinates of all the elements of the current snake. By maintaining this table, we can draw the body of the snake using the drawing function so that the snake can move. Let's take a look at how snakes move:
-- Core algorithm--the movement of snakes function snakeupdate() --Get the number of elements Localn=Table. MAXN (Snakes)if(Table. MAXN (Snakes) >0) Then if(Getnextpoint (). X==foodx andGetnextpoint (). Y==foody) Then --Insert the location of the next target point into the table Table. Insert (Snakes,1, Getnextpoint ())--Set the food status to BeeatedFoodstate="beeated" Else --Insert the location of the next target point into the table Table. Insert (Snakes,1, Getnextpoint ())--Remove the last element Table. Remove (snakes,n+1)End EndEnd
Here we define a foodstate variable to preserve the state of the food, when the state of the food is beeated, the food is eaten by the snake, and the coordinates of the food should be regenerated, when the state of the thing becomes waittoeat. The coordinates of the food are stored in the two variables Foodx and foody, which can be viewed in the complete code.
We know the snake will die when it touches the wall and the game is over. This is relatively simple, just determine the coordinates of the snake head and the relationship between the screen can be. Because the size of the screen in this game is 640x640, the code that determines whether the game is finished can be written like this:
--判断游戏状态 if(snakes[1].x<=0or snakes[1].x>=640or snakes[1].y<=0or snakes[1].y>=640then gameState=0 else gameState=1 end
Here GameState 0 means the game is over, GameState 1 means the game is working.
After the completion of these core algorithms, the rest is left to the Love2d engine to draw, and finally give the complete program code:
--Define window width and heightLocalw=640LocalH=640--Define grid cell sizeLocalUnitsize= -;--The initial position of the blockLocalinitx= theLocalinity= the--Moving directionLocalDir=1--Snake collectionLocalsnakes={}--Food status--waittoeat: Drawing food--beeated: Randomly generated foodLocalFoodstate="Waittoeat"--Game status--0: Game Over--1: Normal gameLocalGamestate=1--The location of the foodLocalfoodx=0Localfoody=0--love2d Load Events function love.load() --Set window titleLove.window.setTitle ("love2d-Snake Game")--Set window sizeLove.window.setMode (W,H)--Define fontsMyfont=love.graphics.newfont ( -)--Set fontLove.graphics.setFont (MyFont)--Set background colorLove.graphics.setBackgroundColor (255,255,255,255)--Set line type to smoothLove.graphics.setLineStyle ("Smooth")--Set the line widthLove.graphics.setLineWidth (0.1)--The initialization of the snake (the length of the snake is 5) forI=1,5 Dosnake={} SNAKE.X=INITX + (I-1) * -Snake.y=inity Snakes[i]=snakeEnd --Food initializationFoodx=love.Math. Random ( +-1)* -Foody=love.Math. Random ( +-1)* -End--love2d Drawing Events function love.draw() --Draw a vertical lineLove.graphics.setColor (0,0,0,255) forI=0, w,unitsize DoLove.graphics.line (0, I,h,i)End --Draw horizontal lines forj=0, h,unitsize DoLove.graphics.line (J,0, j,w)End --Draw snakes forI=1,Table. MAXN (Snakes) DoLove.graphics.setColor (0,0,255,255) Love.graphics.rectangle ("Fill", Snakes[i].x,snakes[i].y, -, -)End --Draw food if(foodstate=="Waittoeat") ThenLove.graphics.setColor (255,0,0,255) Love.graphics.rectangle ("Fill", Foodx,foody, -, -)End --Show Gameover if game is over if(gamestate==0) ThenLove.graphics.setColor (255,0,0,255) Love.graphics.Print("Game over", -, -)EndEnd -- function love.update(DT) --judging the state of the game if(snakes[1].x<=0 orsnakes[1].x>=640 orsnakes[1].y<=0 orsnakes[1].y>=640) ThenGamestate=0 ElseGamestate=1 End --If the game status is normal if(gamestate==1) ThenSnakeupdate () foodupdate ()EndEnd-- Core algorithm--the movement of snakes function snakeupdate() --Get the number of elements Localn=Table. MAXN (Snakes)if(Table. MAXN (Snakes) >0) Then if(Getnextpoint (). X==foodx andGetnextpoint (). Y==foody) Then --Insert the location of the next target point into the table Table. Insert (Snakes,1, Getnextpoint ())--Set the food status to BeeatedFoodstate="beeated" Else --Insert the location of the next target point into the table Table. Insert (Snakes,1, Getnextpoint ())--Remove the last element Table. Remove (snakes,n+1)End EndEnd--Randomly generated food function foodupdate() --Regenerate food if food is eaten by snakes if(foodstate=="beeated") ThenFoodx=love.Math. Random ( +-1)* -Foody=love.Math. Random ( +-1)* -Foodstate="Waittoeat" EndEnd--Define different directions according to the player's pressed keys function love.keypressed(key) if(key=="a" anddir~=3) ThenDir=2 End if(key=="D" anddir~=2) ThenDir=3 End if(key=="W" anddir~=1) ThenDir=0 End if(key=="S" anddir~=0) ThenDir=1 EndEnd function getnextpoint() --Calculate the next target pointsnake={}if(dir==0) Thensnake.x=snakes[1].x snake.y=snakes[1].y- - End if(dir==1) Thensnake.x=snakes[1].x snake.y=snakes[1].y+ - End if(dir==2) Thensnake.x=snakes[1].x- -snake.y=snakes[1].yEnd if(dir==3) Thensnake.x=snakes[1].x+ -snake.y=snakes[1].yEnd returnSnakeEnd
After compressing the code into a. Love file, you can run it, and we'll look at the final effect:
Github
Using the Love2d engine to develop snake games