Create Snake Game In LabVIEW part 2

Hi all. This is the second post in my series on how to create a snake game in LabVIEW 2020 Community Edition part 2. In the last post, I create a modern game screen (https://bln364.com/create-snake-game-in-labview-part-1/) and in this post, I want to create an interactive GUI for this modern game. So, let’s start.

Start Screen

First, I will show you the final result, just a small YouTube demo of GUI working.

Plan

My plan for this post is to create State Machine for Snake Game and Implement start and end screen for this game. So, our state machine should look something like this.

State machine

If you want to create FSM diagrams in Latex check out my other post:

States inside LabVIEW

Init State

Init State

Init State (Game Init) is very simple. Here we are just setting init display color and enabling indicator that contains Main, New Game and Quit String indicator. The next State is always Events. In our loop date we have our state info, game active mode and selected UI indicator.

Game Active mode is an enumeration with Init Screen, Game Screen and End Screen Items. This will act as an indicator in our event structure so that we know are our events for UI control or snake movements. Of course, I’m initializing all UI indicators in this state.

Events

Events state is basically an event structure that acts differently for Init, Game or End Screen. So, if Init or End Screen are set Event structure will react only “UP”, “Down” arrow and Enter or Return. With this, we can navigate between New Game and Quit. When event from “Up” or “Down” arrow occurs, next state is set to Update UI, and UI index is set to 0 or 1, which corresponds to “Up” and “Down” arrow.

Snake Game Welcome Screen

Update UI

Update UI State

In Update UI state I’m just updating background for two indicators (New Game and Quit). So, if UI index is set to 0 (“Down” arrow pressed), I will set transparent background for Quit Indicators, and set background for New Game indicators. After this, I’m returning to Events state and waiting for another event.

If Enter is pressed on Init Screen, there are couple of possibilities.

If UI index is set to 0 (selected New Game), next state is Clear UI and if UI index is 1 (selected Quit), then next state is Close.

Clear UP and Wait and Exit

For this GUI demo, I created Wait and Exit state. This state is just a temporary state for testing GUI and it will be replaced when Game engine is implemented. So, after enter is pressed when New Game indicator is selected, we are going in Clear UP state. There we are disabling indicators that are on Init screen and we need to leave only score and level indicators.

From Clear UI state, next state is always Wait and Exit. In this case, I will wait for a couple of seconds, then change the background color for End screen. Also, change mode from Game Screen to End Screen, and now we can return to Events state, but now events that are detected will indicate change on End Screen.

Close

Close stare will just stop while loop in order to close application properly. I also added property node to detect application kind, so that we can compliantly close Snake Game if we run this game standalone.

Final Words

This series is very interesting for me to create because I created Snake Game in LabVIEW a long time ago, but GUI for that game was very, very bad. With this, I wanted to combine a nice working snake game engine and a beautiful GUI, and I think that this GUI is acceptable. Do you like this kind of post? Again, check out the first post in this Snake Game series with LabVIEW 2020 Community edition on the next link:

In next post, I will integrate the Snake game engine into this GUI and create an executable for you to download and play, and find bugs, of course. Thank you, stay safe and see you in the next one.

Leave a Comment