Hey guys this past weekend I worked on this classic Snake Game I hope you guys like it as much as I do if you want anything added on it let me know and I will add new stuff.
Download Game here + Source code
https://www.4shared.com/rar/p_8COaNV/CppSnakeGameSource_by_voisisal.html?refurl=d1url
video
https://www.youtube.com/watch?v=qjY87q-M3nA
main.h
main.cpp
virus scan
https://virusscan.jotti.org/en/scanresult/bb355290fea296a534aaa45d93417a4a720a7a21
Greets,
voidisalive
Download Game here + Source code
https://www.4shared.com/rar/p_8COaNV/CppSnakeGameSource_by_voisisal.html?refurl=d1url
video
https://www.youtube.com/watch?v=qjY87q-M3nA
main.h
C++:
//Author: voidisalive
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#pragma comment( lib, "Winmm.lib" )
//Defines
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ESC 27
//Game Variables
int snake_body[200][2];
int _ini_pos = 1;
int _snake_size = 3;
int _iniX = 10;
int _iniY = 15;
int _xTar = 30;
int _yTar = 15;
int _snake_direction = 3;
int _snake_speed = 80;
int _heurestica = 1;
int _score = 0;
char movement_key;
//Set stuff at X and Y Pos Routine
void _posXY( int _x, int _y )
{
HANDLE hCon;
COORD dwPos;
dwPos.X = _x;
dwPos.Y = _y;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hCon, dwPos);
}
//Draw Routine
void draw_board( void )
{
//horizontal lines
for( int c = 1; c < 78; c++)
{
_posXY(c, 1);
printf("%c", 220);
_posXY(c, 23);
printf("%c", 220);
}
//vertical lines
for( int c = 3; c < 23; c++)
{
_posXY(1, c);
printf("%c", 219);
_posXY(77, c);
printf("%c", 219);
}
//corner top left
_posXY(1,2);
printf("%c", 219);
//corner bottom left
_posXY(1,23);
printf("%c", 219);
//corner top right
_posXY(77,2);
printf("%c", 219);
//corner bottom right
_posXY(77, 23);
printf("%c", 219);
}
//Store... Save snake Position Routine
inline void store_snake_pos( void )
{
snake_body[_ini_pos][0] = _iniX;
snake_body[_ini_pos][1] = _iniY;
_ini_pos++;
if( _ini_pos == _snake_size )
{
_ini_pos = 1;
}
}
//Draw Snake body Routine
inline void draw_snake_body( void )
{
for( int c = 1; c < _snake_size; c++ )
{
_posXY( snake_body[c][0], snake_body[c][1] );
printf("%c", 2);
}
}
//Remove Snake Body Routine
inline void rem_snake_body( void )
{
_posXY( snake_body[_ini_pos][0], snake_body[_ini_pos][1] );
printf(" ");
}
//Move Snake Routine
inline void mov_snake( void )
{
if( kbhit() )
{
movement_key = getch();
switch( movement_key )
{
case UP:
if( _snake_direction != 2 )
{
_snake_direction = 1;
}
break;
case DOWN:
if( _snake_direction != 1 )
{
_snake_direction = 2;
}
break;
case RIGHT:
if( _snake_direction != 4 )
{
_snake_direction = 3;
}
break;
case LEFT:
if( _snake_direction != 3 )
{
_snake_direction = 4;
}
break;
}//EOF switch
}//EOF if
}
//Add target to snake body Routine
inline void inc_snake_size( void )
{
if(_iniX == _xTar && _iniY == _yTar)
{
PlaySound(TEXT("Coin.wav"), NULL, SND_FILENAME | SND_ASYNC);
_xTar = (rand()%73)+4;
_yTar = (rand()%19)+4;
_snake_size+=1;
_score+=5;
_posXY(_xTar, _yTar);
printf("%c", 3);
}
}
//Game over Routine
inline bool b_game_over()
{
if( _iniY == 1 || _iniY == 23 || _iniX == 1 || _iniX == 77 )
{
PlaySound(TEXT("GameOver.wav"), NULL, SND_FILENAME | SND_ASYNC);
return false;
}
for( int c = _snake_size -1; c > 0; c-- )
{
if( snake_body[c][0] == _iniX && snake_body[c][1] == _iniY)
return false;
}
return true;
}
//Score Routine
inline void score( void )
{
_posXY(35, 0);
printf("Score: %d", _score);
}
//Inc snake speed Routine
inline void inc_speed( void )
{
if(_score == _heurestica*15)
{
_snake_speed -=10;
_heurestica+=1;
}
}
C++:
//Author: voidisalive
#include "main.h"
using namespace std;
int main()
{
SetConsoleTitle("Console Snake");
::MessageBoxA(NULL, "Cpp Snake game by voidisalive" , "voidisalive", MB_OK | MB_ICONEXCLAMATION);
draw_board();
_posXY(_xTar, _yTar);
printf("%c", 3);
while(movement_key != ESC && b_game_over())
{
rem_snake_body();
store_snake_pos();
draw_snake_body();
inc_snake_size();
score();
inc_speed();
mov_snake();
mov_snake();
mov_snake();
if( _snake_direction == 1 )
{
_iniY--;
}
if( _snake_direction == 2 )
{
_iniY+=1;
}
if( _snake_direction == 3 )
{
_iniX+=1;
}
if( _snake_direction == 4 )
{
_iniX-=1;
}
Sleep(_snake_speed);
}
if(b_game_over() == false)
{
_posXY(35,10);
printf("GAME OVER");
}
system("pause>>null");
return 0;
}
https://virusscan.jotti.org/en/scanresult/bb355290fea296a534aaa45d93417a4a720a7a21
Greets,
voidisalive
Last edited: