📜 ⬆️ ⬇️

How I made a snake on LabVIEW

"From grain you to do / just for lulz" is dedicated to ...



Snake. The game is old (as Wikipedia says: the middle or even the end of the 1970s), but not less interesting, at least as an example of a simple but interesting algorithm to illustrate the possibilities of graphical programming in LabVIEW 2009.
')


Actually the algorithm.



The basis of the game is a two-dimensional array of integers, which will represent the playing field. The snake will be located in this array. When a snake's head moves to a new array field (depending on the direction of the snake's movement), the lifetime of a particular cell will be added.

Each cycle of the game cycle runs through the array reducing the values ​​of nonzero cells by one. After that, non-zero cells on the screen are filled with color, and zero cells remain white. Thus, a head of non-zero cells with a length equal to the lifetime of the first cell will constantly stretch behind the head moving across the field. Thus, the cell's lifetime determines the length of the snake. And the length can be increased or decreased.

Increasing the length of the snake occurs when eating some kind of "food". Food - a point on the playing field, described only by its two coordinates. As soon as it is eaten, that is, the coordinates of the head of the snake coincide with the coordinates of the food, the length of the snake increases by 1, and the food gets a new pair of randomly selected coordinates.

Reducing the length of the snake occurs when biting its own tail. Yes, I understand that this is no longer a snake, but a worm of some kind, and that in a classic snake you should have ended the game with an amnover, but not the point. This is done in the same simple way: if at some point the head of a snake enters a cell already occupied by its body, then its depth decreases by the value recorded at the bite site. The remaining tail will lie quietly on the field, exhausting its time of life.

The task is simple and clear. Now let's turn to LabVIEW.

Game interface



Create an element on the form - an array and the simplest of the boolean indicators “Flat Square Button” from the ClassicBoolean toolbar.



For a boolean indicator, we will hide its title and color it so that if it is false, he and his frame would be white, and if true, dark green and light green, respectively. The indicator sizes are set to 10 x 10 pixels.

Place the indicator in the array. Let's make the array two-dimensional, as well as hide the array index indicators and its header. The array itself is spread so that it displays 32 x 24 copies of the indicator.

Let's paint the shape in nice colors. This item is optional, but is my favorite.

After all the manipulations, the playing field with a random set of true and false values ​​looks like this.



The main cycle of the program.



The program will be based on a While loop, terminated by the false condition, that is, never. Inside the loop we place the terminal of our array, and also connect the following variables to the array, which will store the main parameters of the game. These are the coordinates of the head of the snake, the direction of its movement and its length, the coordinates of the food, combined into corresponding clusters, as well as the integer array mentioned in the first chapter. It all looks like this:



Loop content


Inside the loop, we will add the Event Structure with the help of which we will subsequently capture events with the goal of processing signals from the keyboard about pressing control arrows, and a sequential structure within which we will step by step arrange the entire sequence of processing each step of the snake. And the first step will be to run through the entire array with a decrease in all non-zero elements by 1. This is done using two for each nested in one another.



The next step is to change the coordinates of the head in accordance with the direction of movement and change the length of the snake in case of eating food or biting off its own tail. The change of coordinates occurs with the help of Case - the structure to the terminal of the condition of which the variable direction is connected. Depending on the value of the direction, one of the tabs of the structure is executed, on which the corresponding coordinate increases or decreases by 1. If at the same time the coordinate goes beyond the maximum or zero, then it is zeroed, or set to the maximum to provide the so-called “passage through the edge of the screen”.

If the new coordinates are pairwise equal to the food coordinates (two comparison operators and one And operator), then the length is increased by 1, and for food, new coordinates are generated using a random number generator (from 0 to 1) and coefficients. Otherwise (the false tab is not shown) all three variables get the same values.

If the array element with the coordinates of the new head position is not zero, then the value of this element is subtracted from the length of the snake. If the length of the snake becomes less or equal to zero, then the value 1 is assigned to it, since the snake’s head always remains.

Clusters of values ​​are going back to absorbing new values. Values ​​that have not been changed at this step can be not recalled again - they will remain the same and so on.



In the next step, the snake's length is written to the new field as its lifetime, generating a Boolean array and uploading it to the indicator on the front panel. That's not difficult. Do not forget just before unloading, in addition, add true to the cell with coordinates equal to the food coordinates.



Control



That's the whole, actually snake, it remains only to fasten the control. For him, we have already determined the Event - the structure, or speaking in Russian, the structure of events. This multi-tab structure waits for one of the predefined events in it for a specified time interval, and executes the corresponding tab when it waits (well, or the timeout tab if it does not wait). Events can be diverse, ranging from mouse clicks on the front panel of the application, and ending with system events. We will be interested in the key down event of the keyboard.

Move the event structure to the last frame of the sequential structure and insert into it the conductor containing the Snake cluster (since we will change its value by pressing the keyboard keys).

Add an event, as an event source (Event Source) select:. And as an event: Key -> Key Down.

Of the event parameters returned by the structure, we are only interested in the VKey parameter, which contains the key code that triggered the event. Add a Case - a structure that will give out 0 if the key was pressed to the left, 1 - if down, 2 - to the right, and 3 - up. and if any other key was pressed, the direction value would remain unchanged. an additional delay is added so that the triggering of the event structure does not shorten the interval between ticks.



That's all. You can run and play. Of course, it would be nice to display the number of points scored on the front panel, the level of complexity on which the game speed would depend (the amount of time between measures) and much more, well, you can improve anything indefinitely, and further improvements do not carry illustrative goals.

For those who wish, you can familiarize yourself with the full scheme of the application here .

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


All Articles