{"id":2859,"date":"2024-12-12T17:40:40","date_gmt":"2024-12-12T17:40:40","guid":{"rendered":"http:\/\/35.94.173.37\/blog\/?p=2859"},"modified":"2025-09-10T19:25:15","modified_gmt":"2025-09-10T19:25:15","slug":"playing-the-snake-game-using-labview-block-diagrams","status":"publish","type":"post","link":"https:\/\/www.apexwaves.com\/blog\/playing-the-snake-game-using-labview-block-diagrams\/","title":{"rendered":"Playing the Snake Game Using LabVIEW Block Diagrams"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-audio\"><audio controls src=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/Snake-Game-in-LabVIEW-1.mp3\"><\/audio><figcaption class=\"wp-element-caption\"><strong><em>Listen to this blog post here.<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Snake\u00a0<\/strong>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\u00a0<strong>LabVIEW<\/strong>! In this post, we\u2019ll go behind the scenes to explore how this game works in LabVIEW, focusing on the key LabVIEW block diagrams that make it all happen.<\/p>\n\n\n\n<!--more-->\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"How To Read LabVIEW Block Diagrams for the Snake Game\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/a1XDXJ3U374?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<p>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 \u201capples\u201d 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\u2019s dive in!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"577\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/unnamed-1024x577.png\" alt=\"\" class=\"wp-image-2861\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/unnamed-1024x577.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/unnamed-300x169.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/unnamed.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When using LabVIEW, the core of the snake game is a continuous \u201c<strong>While Loop<\/strong>\u201d. This loop runs the game logic frame by frame, and is responsible for handling player input, updating the snake\u2019s position, checking for collisions, and refreshing the game field. The loop only stops when the game ends or the player quits.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/image-1024x572.png\" alt=\"\" class=\"wp-image-2862\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-1024x572.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-300x167.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>LabVIEW Block Diagrams for Snake Movement<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/image-1-1024x572.png\" alt=\"\" class=\"wp-image-2863\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-1-1024x572.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-1-300x167.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-1.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The game responds to player input by using an Event Structure that listens for keyboard actions. The \u201c<strong>Vkey<\/strong>\u201d or \u201c<strong>Char<\/strong>\u201d 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\u2014moving 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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"574\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/image-2-1024x574.png\" alt=\"\" class=\"wp-image-2864\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-2-1024x574.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-2-300x168.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-2.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left\">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\u2019s head by adding the direction vector to its current position. This <a href=\"https:\/\/www.ni.com\/en\/support\/documentation\/supplemental\/08\/labview-block-diagram-explained.html?srsltid=AfmBOooUAbflI20lUv0DVjGSGy6_Cv2LEbtTs-a5GidZg1p-f41tU_Hh\">block diagram<\/a> 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 <strong>1 <\/strong>(down), New Direction Y must not be <strong>-1<\/strong> (up).<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left has-black-color has-text-color\" style=\"font-size:25px\"><strong>LabVIEW Block Diagram for Row and Column Wrapping<\/strong><\/h2>\n\n\n\n<p class=\"has-text-align-left\">If the snake crosses the edge of the grid, it doesn\u2019t crash. Instead, the game implements a <strong>wrapping logic<\/strong> 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\u2019s dimensions and then wrapping it around when necessary. The top box in the diagram handles the Snake\u2019s position on the Y-axis, while the bottom section handles the snake\u2019s position on the X-axis. The \u201c<strong>Next Y<\/strong>\u201d or \u201c<strong>Next X<\/strong>\u201d refers to the calculated next position of the snake in the Y- or X-axis direction. \u201cRows\u201d or \u201cColumns\u201d refer to the total number of rows and columns in the grid.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"573\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/image-3-1024x573.png\" alt=\"\" class=\"wp-image-2865\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-3-1024x573.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-3-300x168.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-3.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left\">The logic used is as follows: The first comparison (<strong>&gt;=<\/strong>) checks if Next Y is <strong>greater than or equal to<\/strong> the total number of rows. If true, it means the snake has gone below the bottom boundary. The second comparison (<strong>&lt;<\/strong>) checks if Next Y is <strong>less than<\/strong> 0. If <strong>true<\/strong>, it means the snake has gone above the top boundary. The results of these comparisons feed into a <strong>Select <\/strong>function: If Next Y <strong>exceeds <\/strong>the bottom, the output is 0 (wrap to the top of the grid). If Next Y goes <strong>above the top<\/strong>, the output is Rows &#8211; 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 <strong>X-axis<\/strong> instead of Y.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left has-black-color has-text-color\" style=\"font-size:25px\"><strong>Apple Generation and Collisions with LabVIEW Block Diagrams<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"575\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/image-4-1024x575.png\" alt=\"\" class=\"wp-image-2866\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-4-1024x575.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-4-300x169.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-4.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left\">Now that we\u2019ve 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\u2019t spawn on top of the snake\u2019s body by checking the grid for collisions, using the \u201c<strong>Snake Field In<\/strong>\u201d field which represents the current state of the grid and the snake\u2019s 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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/image-5-1024x576.png\" alt=\"\" class=\"wp-image-2867\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-5-1024x576.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-5-300x169.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-5.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left\">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 \u201c<strong>Score<\/strong>\u201d variable keeps track of each time the snake eats a \u201cfood\u201d item.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"577\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/image-6-1024x577.png\" alt=\"\" class=\"wp-image-2868\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-6-1024x577.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-6-300x169.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-6.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left\">If the snake\u2019s head intersects with its body or a wall, the game ends- so every frame is checked for collisions using the \u201c<strong>Wall Check<\/strong>\u201d and \u201c<strong>Hit Yourself<\/strong>\u201d logic fields. This is a crucial part of the game loop to determine if the player loses.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"http:\/\/35.94.173.37\/blog\/wp-content\/uploads\/2024\/12\/image-7-1024x576.png\" alt=\"\" class=\"wp-image-2869\" srcset=\"https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-7-1024x576.png 1024w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-7-300x169.png 300w, https:\/\/www.apexwaves.com\/blog\/wp-content\/uploads\/2024\/12\/image-7.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left\">These <strong>LabVIEW block diagrams<\/strong> and logic all come together to create a fun activity for everyone! Whether you\u2019re 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.<\/p>\n\n\n\n<p class=\"has-text-align-left\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Snake\u00a0is 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2860,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[997,8],"tags":[998,124,318,393,437],"class_list":["post-2859","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-labview","category-national-instruments","tag-block-diagrams","tag-circuits","tag-labview","tag-national-instruments","tag-ni"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/posts\/2859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/comments?post=2859"}],"version-history":[{"count":2,"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/posts\/2859\/revisions"}],"predecessor-version":[{"id":3316,"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/posts\/2859\/revisions\/3316"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/media\/2860"}],"wp:attachment":[{"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/media?parent=2859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/categories?post=2859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.apexwaves.com\/blog\/wp-json\/wp\/v2\/tags?post=2859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}