Setup and Optimize the National Instruments PXI-5441 Waveform Generator
The National Instruments PXI-5441 is a powerful PXI waveform generator designed to support high-speed waveform downloads up...
Snake is an incredibly simple and classic game that has entertained players for decades. While many people remember it as a feature on Nokia cell phones, the concept first appeared in 1976 as the arcade game Blockade. Over time, it has become a fun pastime to try and code the game on a range of tech platforms- including LabVIEW! In this post, we’ll go behind the scenes to explore how this game works in LabVIEW, focusing on the key LabVIEW block diagrams that make it all happen.
In the game, players must steer a constantly moving snake left, right, up, or down to avoid colliding with the walls or the rest of the body. The gameplay also features “apples” that the snake eats, growing longer with each bite. This game is an excellent example of how even the smallest aspect of a game uses complex logic and rules to make everything work together smoothly. The logic necessary for this classic game includes the snake movement, the apple placement, and the collision detection. Let’s dive in!
When using LabVIEW, the core of the snake game is a continuous “While Loop”. This loop runs the game logic frame by frame, and is responsible for handling player input, updating the snake’s position, checking for collisions, and refreshing the game field. The loop only stops when the game ends or the player quits.
The game responds to player input by using an Event Structure that listens for keyboard actions. The “Vkey” or “Char” property inside of this block structure determines which key was pressed. When the player presses an arrow key, the game captures that input and updates the direction variables—moving the snake either up, down, left, or right. Since one of the objects of the game is to avoid hitting the body of the snake itself, it is important to note that this logic prevents the snake from reversing directly into itself.
The head of the snake is updated every frame, based on the direction it is going. The block diagram pictured above calculates the new position of the snake’s head by adding the direction vector to its current position. This block diagram compares the new direction to the old one in order to ensure that the snake cannot move backward. If the input direction is invalid, the game retains the previous direction. For example, If Old Direction Y is 1 (down), New Direction Y must not be -1 (up).
If the snake crosses the edge of the grid, it doesn’t crash. Instead, the game implements a wrapping logic that teleports the snake to the opposite side of the grid. For example, if the snake moves past the top boundary, it will reappear at the bottom (and vice versa). This is managed by checking the position against the grid’s dimensions and then wrapping it around when necessary. The top box in the diagram handles the Snake’s position on the Y-axis, while the bottom section handles the snake’s position on the X-axis. The “Next Y” or “Next X” refers to the calculated next position of the snake in the Y- or X-axis direction. “Rows” or “Columns” refer to the total number of rows and columns in the grid.
The logic used is as follows: The first comparison (>=) checks if Next Y is greater than or equal to the total number of rows. If true, it means the snake has gone below the bottom boundary. The second comparison (<) checks if Next Y is less than 0. If true, it means the snake has gone above the top boundary. The results of these comparisons feed into a Select function: If Next Y exceeds the bottom, the output is 0 (wrap to the top of the grid). If Next Y goes above the top, the output is Rows – 1 (wrap to the bottom of the grid). Otherwise, the value of Next Y remains unchanged. The corrected Y position is passed to the output Next Y. The logic is the same for Next X, but with the X-axis instead of Y.
Now that we’ve established the logic behind the movement of the snake, how does the apple appear on the grid? The game generates a random X and Y position for the apple within the grid boundaries. However, it ensures that the apple doesn’t spawn on top of the snake’s body by checking the grid for collisions, using the “Snake Field In” field which represents the current state of the grid and the snake’s position. If the position is occupied, it generates a new random location until a valid spot is found. This ensures that the apple will always appear in a playable position, continually adding a new challenge for the snake to chase.
Each time the snake eats an apple, the game updates the score. This is managed by both increasing the length of the snake and keeping track of the number of apples consumed. The “Score” variable keeps track of each time the snake eats a “food” item.
If the snake’s head intersects with its body or a wall, the game ends- so every frame is checked for collisions using the “Wall Check” and “Hit Yourself” logic fields. This is a crucial part of the game loop to determine if the player loses.
These LabVIEW block diagrams and logic all come together to create a fun activity for everyone! Whether you’re an experienced engineer or a beginner looking to sharpen your skills- this timeless game is a fun way to showcase some of the capabilities of LabVIEW.
A monthly email packed with valuable content—industry news, tutorials, obsolescence updates, and more. No sales pitches, just insights we think you'll find helpful!
The National Instruments PXI-5441 is a powerful PXI waveform generator designed to support high-speed waveform downloads up...
Companies in almost every industry are being transformed by artificial intelligence, and autonomous machines are...
The PXIe Platform from National Instruments offers an incredibly valuable toolset for test and measurement innovation. Its...
It is no secret that simulation and testing is an important part of designing circuits....