Overview
This example demonstrates implementing a simple snake game using object oriented programming. The snake is represented as a collection of objects, and the program revolves around modifying and updating the snake by calling its methods. It also demonstrates concepts such as inheritance and access scopes.
This particular example is built upon a version that I uploaded previously and has had a number of changes attributed with it including updating the methods of the classes as well as cleaning up the architecture of the program to better reflect best practices.
Description
This example revolves around the concept of object oriented programming, and demonstrates some of the main principles and concepts inherent to that, such as encapsulation, inheritance, polymorphism and instancing. The snake consists of a "Master" object, which is the head of the snake in this case, and this encapsulates functionality such as controlling the direction of the snake, whether the snake grows or not after eating a food square, as well returning the size and other parameters, and handles all input. In its private variables, it has an array which is full of "Slave" objects, which represent the segments following the snake.
Both the SnakeSegment and SnakeMaster classes inherit from the Segment class, because the two classes both share common functionality in their co-ordinates and location upon the map. The getter and setter methods for these co-ordinates are called from the child classes and co-ordinates are accessed this way. This allows us to change functionality in the Segment parent class, and have it affect both the SnakeSegment and SnakeMaster classes, which is a cornerstone of the OOP paradigm in programming.
All variables in classes are accessed by getter and setter methods, showcasing the encapsulation concept, which is important as it hides the implementation of the code, and allows the code calling that method to not require changing.
Steps to Implement Code
To run this code you will need to unzip the Snake-OOP Revised.zip file attached and run Main.vi.
Requirements
Hardware
No hardware required.
Software
LabVIEW 8.6 or later.
VI Snippet

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.