A CATIA V5 remake of the well-known arcade game. Many of us used to play it on our cellphones. Your goal is to move the snake and eat as many "food" segments as possible while the snake grows in length. The game was developed in a pure VBA with some help from WinAPI.
Wanna see it in action?
Then watch this video:Play Snake in CATIA V5 product (VBA)
Snake 3D project
- GameCore class
- Playground class
- SnakeClass class
- CatiaProductUI class
- QPCTimer class
There are also two standard VBA modules called CatUtils and Startup.
The most important part of the Snake game. It contains the main game loop, timers, handles keyboard interaction and stores all game state variables. It encapsulates a Playground class instance. As a timer in a procedure MainLoop we use a Windows API function GetTickCount and to handle keyboard events a WinAPI function GetAsyncKeyState is used. This class, together with Playground and SnakeClass, form an independent part of the game which covers the whole mechanism of the game, except user interface. You can use them in any application that supports VBA (MS Excel, Access, etc.), just create an instance of a GameCore class and capture RedrawGridEvent, ScoreUpdatedEvent and GameOverEvent event. Through these events, you can build a game graphic in a VBA compatible application of your choice.
This class accommodates a game plan which consists of the one-dimensional long array. Values of this array tell us which element belongs to their place. Number 0 stands for an empty space, number 1,2 stands for snake body & head and number 3 means a position of the food. With this information and RedrawGridEvent you can build/rebuild a playfield at any moment of the game. With properties Rows and Columns you can adjust the size of the playfield, by default it is 15 rows x 20 columns.
The class is responsible for the movement of the Snake. Functions like CanGoLeft, CanGoUp, CanGoRight, CanGoDown return either True if the movement of the Snake in the certain direction is possible, otherwise return False. If True we perform the movement by calling the corresponding methods GoLeft, GoUp, GoRight, GoDown, False means that the movement is not possible either because of self-collision or we reached the borders of the playfield. False corresponds to the end of the game and we raise GameOverEvent in the GameCore instance.
This class renders the game graphics by handling the RedrawGridEvent which provides the Playground object and gtc variable. We iterate over the Grid member of the Playground object (one-dimensional long array) and according to values (0, 1, 2 or 3) we put the appropriate CATIA product (nothing, body, head or food) to the right place on the screen. Because of the performance issues, all body elements are pre-created during the initialization phase and placed into the zero point of the assembly. Later they are moved on and off the playfield as required. It is much faster than the creation of the elements on the fly.
The purpose of the gtc variable is to provide the timer for the food rotation animation. Based on this value we are able to rotate the food element fluently as time goes by.
All text in a product (SnakeAssy.CATProduct) was generated with the help of Txt2Sketch application which is freely available on our website.
It has been already described in a post Play Tetris in CATIA V5 drawing.
To test it, import the VBA project from a zip file, open the SnakeAssy.CATProduct and switch it into the Design Mode (this is really important otherwise the script fails!). Then launch the procedure Main from the module Startup.