Play Tetris in CATIA V5 drawing


CATIA V5 as a gaming platform? Well, nothing is impossible. Thanks to the powerful VBA language, you can create simple games even in CATIA V5. But which game to choose? Good old Tetris is probably the best choice. Simple and addictive game is an excellent candidate to test CATIA as a game engine. And CATIA, as usual, did a great job.


Wanna see it in action?

Then watch this video:

Play Tetris in CATIA V5 drawing (VBA)
Play Tetris in CATIA V5 product (VBA)

Tetris 2D Project

Project consists of several classes:
  • GameCore class
  • CatiaDraftingPictUI class
  • Grid class
  • Piece class
  • RandomPieceMaker class
  • QPCTimer class

GameCore class

It is the heart of the Tetris game. It contains the game logic, timer, it serves keyboard events, virtually all interaction except the user interface. There is only one public method called MainLoop and with the outside world it communicates through the set of events such as LinesClearedEvent, RedrawGridEvent, RedrawPieceEvent, RedrawNextPieceEvent, and GameOverEvent. The advantage of such an approach is that game logic is completely independent of the user interface and you can easily port this game to any application which supports VBA. As a frontend, I have chosen CATIA drawing, but it could be easily ported to any VBA compatible application like MS Excel, MS Access or MS PowerPoint. All you have to do is to implement GameCore class to your application a handle GameCore events.

CatiaDraftingPictUI class

This class takes care of the game user interface. It handles events of GameCore class and updates the screen according to current game state. At the beginning of the game, it creates all game elements (DrawingPicture objects), "hide" them and put them into the point (0,0). Because DrawingPicture CATIA class does not contain a method to hide picture element, "hiding" means setting the Width and Height property to zero. The class also updates the playfield, falling tetromino, score and the number of cleared lines in a response to the events fired from GameCore class.

Grid class

This is our playfield. The size of the playfield is 22 rows x 10 columns, but the first two rows are hidden. It contains methods like ClearLines() to clear full lines or AddPiece() which adds the tetromino to the playground when it lands on the floor or other blocks.

Piece class

This class represents a tetromino. A tetromino is made of four square blocks. The seven one-sided tetrominoes are I, O, T, S, Z, J, and L. Class handles the movement and rotation of the tetrominoes on the playfield with methods like CanGoLeft(), GoLeft(), CanGoRight(), GoRight(), CanGoDown(), etc. Rotation is provided by Rotate() method.

RandomPieceMaker class

Choose the next tetromino randomly. It generates a sequence of all seven one-sided tetrominoes (I, J, L, O, S, T, Z) permuted randomly as if they were drawn from a bag. Then it deals all seven tetrominoes to the piece sequence before generating another bag. This way it is much less likely that the player will get an obscenely long run without a desired tetromino.

QPCTimer class

Very useful class, which provides methods and properties to accurately measure elapsed time. I use it heavily during game development to identify bottlenecks within the application. Results from QPCTimer execution measurements helped me to find the most effective way to implement rendering of the game.

Performance

Before I started with programming I made a couple of tests to find the best way to implement game graphics. Originally I wanted to use CATIA 2D component (DrawingComponent class) as a grid/piece element, but I found out that a picture (DrawingPicture class) performs much better. On my computer rendering of the picture was roughly 5x faster than the rendering of the 2D component.

Another problem was the very slow performance on computers with a 64-bit version of CATIA V5. Obviously, VBA implementation under the 64-bit version is not very efficient and this is the reason why I decided to port some parts of the code to VBScript, which does not have performance issues. Because of this, you find scripts CreatePictures.catvbs and RedrawGrid.catvbs in a project folder. The first of them load all game elements at the start of the game, the second one redraws the playfield when tetromino lands.

To test it, import the VBA project from a zip file and launch procedure StartHere() from the module Startup. In CATIA an active document must be a drawing and an empty view must be activated.

A Tetris2D.zip file with the VBA project can be downloaded here Tetris in CATIA V5 drawing
Watch an application video here Play Tetris in CATIA V5 drawing (VBA)



Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>