Snakes game in C++, using graphics.h BGI library

Snakes – Along the routes of Peru
STORY
Two snakes named Venoro and Vercari, try to escape from the hunger that plagues them, but they will be pursued by CyberSer, who will try everything to starve them to death.
The snakes must travel several kilometers through the deserts of Peru, until they reach the highlands and then discover who are the real snake killers who program CyberSer ….
OBJECTIVE
The objective is the same as Nibbles, with the difference that instead of numbers you will eat mice. You control a snake, which can move around the screen “without brakes”, so you have to avoid hitting obstacles, walls, other snakes and even your own tail. To survive you will have to eat 9 mice, which appear one by one at a random point on the screen. Each time a mouse is eaten, the snake grows, becoming so big that it becomes difficult to control. When you eat the last mouse (number 9) you change level, and logically this one will be more difficult, it will have more obstacles.
CHARACTERISTICS
Type: Ability
Players: 1 or 2 simultaneous players (I recommend playing between 2 people).
Sprites: Japanese anime style
AUTHOR’S COMMENTS
This is my best video game developed in C++, which includes my own graphic library GrafVGA created from scratch, which works as a base with the putimage function of WinBGI, to speed up the graphics painting.
You won’t regret playing it, because despite being a classic game, new rules, video effects, 90’s style techno music and “Japanese anime” style sprites were added, thanks to the expert graphic designer Ricardo Bruno.
This game is based on my first creation Nibbles, but you could say that it is an improved version, in all aspects: programming, graphics, story and sound.
All the levels include interesting touristic places of my beloved land: “El Perú”, which goes through coast, highlands and jungle.
Internally in the programming was used entirely C++ (POO).
VERSION HISTORY
Before playing:
- Animated logo of the author.
- Presentation (1 frame for the moment). During this time you can do tricks…(Top Secret still).
- Main menu and purely graphical options menu.
- Graphical character selection menu (snakes).
- Supports change of movement keys for each player independently.
- Activation of a DEMO of the game when no key is pressed in the main menu.
During the game:
- Lengthened the waiting time before collision against a wall. This gives more maneuverability to the snake.
- Pause can be set.
- The upper main information panel (header) has been changed, now it reports graphically the number of lives, mice eaten and score; for each snake.
- At the end of a phase (Level) statistics and phase results are reported (in purely graphical mode).
- Each character is limited to 5 lives.
- Every time a life is lost, “pain” sprites are shown on the characters.
- When all lives are lost for each character, a “continue” screen is displayed, showing the character’s face a bit haggard and with a countdown display.

Programming Details (Graphics Library):
- 80% of the game is made with OOP (Object Oriented Programming).
- Supports 320×200 VGA Video mode with 256 colors.
- It is allowed to define n virtual screens, but as maximum it works with 3. Theoretically this memory is the only one that should be used (without counting the sprites).
- It supports static sprites (only 1 frame) and animated sprites (more than 1 frame), animations can be made using additional memory, and without using memory, making direct dumps.
- Among the main effects used are: Scroll (Horizontal, Vertical, Diagonal), Pixelated, Fading and AntiFading. Among those that are finished and unused in the game for now are: AquaWave, free 2D and 3D rotation of sprites.
- Intelligent optimization of the VGA color palette. It is possible to delete the palette of a sprite in disuse of the VGA, to introduce a new one when it is needed.
You can download the software executable for FREE from here:
Get the SOURCE CODE of “Snakes – Along the routes of Peru” and increase your programming experience:
Source code available in Visual C++ 2017 project.
- Explore how a video game is made in the purest old-school retro style, but with easy-to-understand C++ code:
- It includes my own GrafVGA graphics library created from scratch, which works as a base with the putimage function of WinBGI, to speed up the painting of graphics on the screen.
- This graphic library includes several video effects such as scrolling effect, fading effect, 3D AquaWave effect, 3D Rotate Image Z Axis effect, among others.
- A retro character database is included, where each character is in a monochrome image of 5 pixels wide by 7 pixels high, and is stored in an array defined in the same code.
- Use of virtual screens (invisible screens in memory) for fast drawing of on-screen graphics.
- Color palette management.
- Loading of fixed and animated sprites from pcx graphics files.
- The CAudio class is created, to play mp3 files simultaneously, which is based on the mciSendString of the Windows API. It is used to play background audio (90’s style techno dance music) and sound effects.
- It allows to read keystrokes for one or two players simultaneously, the keys used for each player can be customized from the configuration.
- The game features an introduction animation, player selection screen, configuration screen and defeated player screen.
- The game plays a pre-recorded demo version of level 1, if no option is selected in the initial options screen.

The Developed Team
C++ Programming :
YACSHA – Software & Desing, since 1999, Lima – Perú
The World of Chaos – EL MUNDO DEL CAOS – Unlimited Programming
Graphics :
Ricardo Bruno
HISTORY
Version 2 – 18-IV-2024
- The GrafVGA library has been converted into the CVGA class with all its static methods, for better maintenance.
- The Fading effect is adapted to the modern GVGA class and minor bugs are corrected. Previously this function considered a maximum R, G, or B color intensity value of 63, but currently the palette works with a maximum of 255 and therefore the function has been adapted to this new maximum
- The Pixelated effect is adapted to the modern GVGA class and minor bugs are corrected.
- – Screen drawing functions of the GVGA class are modernized, previously they used assembler, now the CScreenBitmapVGA class is used, which uses the putimage function of WinBGI, to draw on the screen quickly
- Fixed some bugs that prevented the home screens from flowing correctly
- Some unnecessary files are removed
- Added the missing ‘fases.dat’ file, without which the game could not be started
- The code from the “Teclado.h” library is adapted to WinBGI, instead of using the old DOS BIOS functions. The disadvantage is that it now does not recognize some keys such as CTRL, SHIFT, F10, etc. which it did recognize before, since WinBGI does not recognize these keys.
- The main code of the game is adapted to the new Teclado.h library
- The key to exit the game while playing is now F9 instead of F10
- The config.dat file now saves 4-byte integers (total file size is 56 bytes). Previously it used 2-byte integers and weighed 28 bytes. This is done to use 4-byte integers in the source code and avoid data corruption when casting.
- The Scroll3 function effect code is improved, using the new GVGA functions. Now it is an equivalent code, but more compact and simpler, without taking up as many buffers as the previous function. A 20 second delay is added to slow the speed of the effect.
- The Scroll2 function that was not being used is commented out.
- New methods are introduced in CScreenBitmapVGA and CVGA that will allow direct access to the virtual screen matrices.
- Program crash is corrected due to not correctly deleting the image pointer. delete[] is now used instead of delete, because it is a dynamically initialized pointer as an array.
- The position of the CAnime::create method is ordered
- The CScreenBitmapVGA class is changed to CVirtualScreenVGA.
- The CVirtualScreen class is created that will contain only the methods required by the 3 virtual screens used in the video game taken from CScreenBitmapVGA. Palette handling and BITMAP object were removed from this class
- The CVirtualScreenVGA class is a child of CVirtualScreen and therefore inherits all its methods, and in addition, this class adds the management of the palette and the BITMAP object, which had been removed in CVirtualScreen. In such a way that the CVirtualScreenVGA class behaves like a simple virtual screen and allows its content to be exported to the VGA (window that the user sees)
- The enableDraw parameter is removed from the CVirtualScreenVGA class, it is no longer necessary. Drawing will always be activated in this class.
- The GVGA class is adapted to the new CVirtualScreen and CVirtualScreenVGA classes, taking advantage of polymorphism to draw in the same way in both the VirtualScreen and the VGA
- Scaling is enabled from the virtual screen to the VGA screen. That is, now the VGA (window that the user sees) can be at a different resolution than the game’s native resolution of 320×200. So the VGA game is now viewed at 1024×640, to maintain the aspect ratio with the native resolution
- The colors of the standard palette are updated, previously a 6-bit palette was used per R, G or B channel. Now the classic VGA standard palette of 8 bits per channel is used
- Corrected the degrades below the snake names, in the snake selection stage. Since these were based on a 16-bit per channel palette, the classic 8-bit per channel palette is now used.
- The snake selection message is improved, which now indicates which buttons must be pressed to select the snakes of both the 1st player and 2nd player
- A comment is added, to indicate which part must be commented/uncommented to activate key recording, for the demo stage of level 1, which appears when the main screen has no events after 5 seconds.
- The default PAUSE button on snake 2 is changed to the SPACE key. The config.dat file is edited
- An unknown memory hole has been detected that sometimes causes the game to crash
- The dynamic arrays of the CImagen, CSprite, CPaleta and CAnime classes, which are managed new/delete, are replaced by std::vector containers for better memory management when creating and destroying the containers
- Fix bug on rev1: the initialization of the CAnime CImage object vector is added to the CAnime “crear” method. This initialization was removed by mistake in “Fix memory BUG rev1”
- Fix millenary bug, which possibly corrupted memory, the palette movement was being done by value instead of by reference
- – The Asignar_paleta_a_VGA method is created that processes objects of the CImagen class, since previously an image was processed as a vector of images with a single element, but that worked in dynamic arrays, but in a vector container it was more complicated, so It was decided to process the image object directly.
- Bug is corrected, which appeared since rev2, which caused the program to exit unexpectedly to console mode, indicating a false error in reading sprites.col
- The define _RECORD_DEMO is added, which when uncommented allows recording the keyboard inputs pressed during the deployment of the DEMO level. Use it when you want to record a new version of the DEMO level. The demo inputs are saved in the demo.dat file.
- A new demo.dat file has been recorded, with a new version of how the snake traverses the DEMO level. Because the demo.dat file now saves 4-byte integers, instead of 2-byte integers like in the DOS version.
- Warnings related to casting are corrected. Ex. Converting signed variables to unsigned variables, or converting double to float variables.
- Fix millenary bug: CargarConfiguracion was not returning the value 1 in case everything happens satisfactorily.
- Fix millenary bug: It was done in rev 4, but the comment was not added: MostrarMenu was not returning the value 0, as the default return value in case the program does not flow satisfactorily
- The “Estrella” class is improved, converting its global preprocessor parameters into static type variables, for better maintenance of the class.
- The delay of the Fading effect is set to 8ms, instead of 10ms. Because the effect was a little slow at a resolution of 1024×640.
- Unnecessary files are removed.
- Some files are renamed (uppercase to lowercase)
- Update credits and history version.
- Corrects return values of errors due to lack of files necessary for the correct loading of the video game.
- Translates from Spanish to English, the credits window displayed when closing the software. In addition, color formatting is added and versions and dates are updated.
- Translates from Spanish to English, the end-of-game window, when moving from the last pre-loaded level in the video game.
- It detects if the demo.dat file has not been found, and displays an output message in the console, instead of the program crashing.
Version 1.8 Beta – 16-III-2001
- Second public release, available for download on the website of “The World of Chaos 2001
Version 1.73 – 03-II-2001
- The MultiScroll effect is added, which produces a scroll in the vertical, horizontal or diagonal direction. The advantage is that it uses a new algorithm that is faster than the previous one and speed is optimized.
- We start designing the phase results screen, which will be displayed after the end of each phase, which will show information about how many mice each snake caught per phase, and will increase some BONUS points to each snake, by means of a visual score increase effect.
- Translated with DeepL.com (free version)
Version 1.72 – 30-I-2001
- ContraFading effect is added, after Fading, in the screen change between logo and presentation.
Version 1.71 – 28-I-2001
- Fixed the error that hung the game due to lack of memory. It was only necessary to optimize the code for speed.
- CVirScr ( Virtual Screen ) class is created, to be able to manage virtual screens easily.
Version 1.70 – 23-I-2001
- A flying stars effect (space effect) is added to the author’s logo. logo.
Version 1.63 03-I-2001
- NEW IDEA!!!: The graphical presentation of the game is created, including author’s logo. For this purpose, the animated author’s logo is added and the letters “presenta” in graphic mode.
Version 1.62 – 26-XII-2000
- A sprite is created for when the snake collides, this is a sprite with a haggard face ( it came out a little ugly… but I hope that my graphic designer fixes it ). This sprite is shown for half a second, after a collision.
Version 1.61 – 23-XII-2000
- NEW IDEA!!! (“Level 1 DEMO” beta ): Successfully implemented a DEMO of level 1, which is observed when no key is pressed for 5 seconds in the main menu. This demo is loaded from the demo.dat file.
- The upper main information panel (header) was changed, now the number of lives, mice eaten and score are graphically reported for each snake.
Version 1.60 – 22-XII-2000
- At last the most annoying BUG of SNAKES has been fixed. Which consisted in if you brake and then resume the movement, the tail of the snake is drawn looking down, and also when the snake is completely stopped only his head was seen :). Although if you make 2 quick brakes the tail doesn’t appear anymore 🙁
- The PAUSE key was implemented (freezing the whole screen and at the same time fading the color a little), and it is independent for each snake.
Version 1.51 – 01-XI-2000
- The death of snakes has been made independent.
- The corresponding error messages are displayed, if any resource file (*.dat, *.col) is missing from the program, or if any of them is corrupted.
Version 1.50 – 30-X-2000
- The sprites are loaded from a graphic collection file ( *.col ). The col format allows grouping several pcx graphic files in a single file. The CColGraf class defined in CCOLGRAF.H was created for this purpose. Then a global CColGraf object ( colgraf ) is created, with which all the graphics of the game will be loaded.
Versión 1.40 – 28-IX-2000
- A GLITCH from the NIBBLES version was fixed. Which consisted in that it was possible to cross a wall when pressing backwards, and it was also possible to cross the main wall (the perimeter of the phase), being able to appear in the header of the game (the places reserved for comments and score). and score).
Version 1.31 – (06 to 08)- IX-2000
- The snake graphical selection menu was created.
- Fixed the BUG that if some keys were left pressed in the keyboard buffer at the end of a phase, at the beginning of the next phase these keys were pressed automatically and made the snake start running in a different direction than the default one.
Version 1.30 – ( 02 to 06 )-IX-2000
- NEW IDEA!!! The graphical presentation of the Main Menu (selection of the number of players, the graphical options menu, and the Exit option) was created.
Version 1.26 – 19-VIII-2000
- Added the EFFECTS.H graphical effects library, which provides outstanding effects, such as: Pixelated, Fading, Scroll, etc….
Version 1.25 – 14-VIII-2000
- Improvements were made to the source code, due to the new changes made to the classes in CIMAGEN.H.
- Two CSprite objects (mouse and wall) are created to contain the image and the palette of these sprites.
Version 1.24 – 13-VIII-2000
- Improvements were made to the source code, due to the changes made to the classes in CIMAGEN.H.
- A CAnime object is created as a member of the NIBBLE class, to replace the sprite array and palette created in version 1.22.
- A new sprite is created for the snake2 (I didn’t do this one… a friend did it… and I think it looks like it…). a friend… and I think it came out cool )
Version 1.23 – 10-VIII-2000
- ERROR Patched!!!
- Error: The snakes start in a constant position and direction in any phase, so sometimes the snakes start colliding with a wall.
- Solution: A modification was made to the phase file format ( phases.txt ), now the coding is as follows: For any given phase the first 115 bytes are the phase bitmap (for the old format only this part was considered). The next 3 bytes are information about the initial address of the snake1. And their content is as follows:
byte1 : Initial X position 0<=X<40
byte2 : Initial Y position 0<=Y<23
byte3 : ( initial address ) can take the following values:
Value of byte3 | Initial snake address |
0 | Left |
1 | Right |
2 | Up |
3 | Down |
And finally the next 3 bytes are the address information of snake2. And its content is stored in the same way as for snake1.
So now a phase occupies 115 bytes for the bitmap plus 6 bytes for the DirIni (initial address), which adds up to a total of 121 bytes.
- To achieve this we had to make drastic modifications to the phase editor. Now it is possible to make phases in this format quite quickly.
- The loading of the phases is optimized. Now every time you die the phase is no longer loaded from disk, but now each phase is loaded only once at startup and copied to the PosmapOriginal array, and it is from there that it is now loaded. Also from there you get the new Posmap every time you die.
Version 1.22 – August 9, 2000
- A sprite array is created inside the NIBBLE class and a color palette is created for the graphic of a snake. Now it is possible to differentiate the snake of player 1 with the one of player 2. Because different sprites can be stored in each NIBBLE object.
- A new sprite is created for the snake2 (as I can’t think of any new face for the snake2 I just made it the same as the snake1 only now I put a green color…that highlights it vacan!!!!)
- A new mouse sprite is created (I think this one is better than the previous one…)
- ERROR Patched!!!
- Error: The mouse appears on the tail of the snake or on the wall of the phase.
- Solution: The mouse can only appear on an empty region of the screen, i.e. on a pixel marked with 0 in the Posmap.
Version 1.21 – 08-VIII-2000
- Common errors were detected in the loading of the graphic and phase files, that is to say, when a graphic file or the phase file is not found, the program terminates and an error message is displayed.
- The sprite for snake2 is created, which is similar to the one for snake1 only in green color.
Version 1.2 – 07-VIII-2000
- A library of TECLADO.H functions was created to capture any key pressed in the keyboard (this function is GetTecla() similar to getch()). Now the capture is faster than with getch().
Version 1.11 – 02-VII-2000
- A sprite of a mouse is created (it came out a little ugly…but that it is going to be done), so that instead of catching a number this sprite is caught.
Version 1.1 – 28-VI-2000
- A new phase editor exclusive for SNAKES is designed, whose main characteristics are that it contains all the phases in features are that it contains all the phases in a single file and that it occupies less space occupies less space than the phases created for nibbles ( *.nib ).
- With this editor the first 4 phases are designed identical to Nibbles.
Version 1.0 – 11-I-2000
- Based on the game Nibbles Version 6.1 also created by JLDL-SOFTWARE, this first version of SNAKES was made modifying only the graphical part of the game.
- To work with the graphical part, 3 powerful libraries of graphical functions were previously created: CPCX.H, CIMAGEN.H and GRAFVGA.H. To work with them we proceed as follows:
- The PCX class located in CPCX.H is used to read the bitmap of a *.pcx graphic file.
- The classes CSprite and CPaleta located in CIMAGEN.H are used to load the SPRITE and the color PALETTE respectively of the BITMAP obtained with the PCX class, and then save this information in dynamic arrays defined in these classes.
- And finally the functions defined in GRAFVGA.H are used to work in the VGA graphic mode of 320×200 resolution with 256 colors. Then the SPRITE and the PALETTE of the BITMAP are copied to the VGA memory and automatically you can see on the screen the BITMAP with their respective colors in the desired position. (These functions are mostly made in ENSAMBLER language, which results in a good speed when displaying moving images, i.e. no flickering).
It is necessary to emphasize that the previous classes are interrelated, that is to say, one depends on the other.
- The first animated snake is created (it came out SUPER COOL) that consists of 4 frames (4 frames of animation), when passing consecutively the frames you can see how the snake sticks out its tongue while breathing…!!!!! You have 10 main sprites, which by rotating and/or inverting you can obtain the 22 secondary sprites; being a total of 32 sprites (16 for the head ( every 4 sprites is for the head looking up, down, left and right ), 8 for the body ( 4 when it is in horizontal position and 4 when it is in vertical position ), 4 for the tail and 4 elbows (when the snake bends) ).
- It is not possible to load the phases of the game and there is no score. A background obtained from a pcx file is placed.