Source Code C++ Snake Game Weekend Project

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

voidisalive

Jr.Coder
Full Member
Nobleman
May 27, 2012
67
458
3
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
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;
	}
}
main.cpp
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;
}
virus scan
https://virusscan.jotti.org/en/scanresult/bb355290fea296a534aaa45d93417a4a720a7a21

Greets,

voidisalive
 
Last edited:

c5

Kim Kong Trasher
Dank Tier VIP
Dank Tier Donator
Jul 19, 2012
1,187
12,638
76
It's really great that you share source codes with us, that way everyone can benefit from the game in one way or another :D Thanks!
 

Fleep

Founder
Meme Tier VIP
May 20, 2012
572
11,023
6
Absolutely, Great use of the Mario sounds as well!

Fleep
 

voidisalive

Jr.Coder
Full Member
Nobleman
May 27, 2012
67
458
3
Thanks c5 yeah I share the source because that way members can see how I work and do things altough i did this fast coding :) sunday night and some code is not so great but it pleases me and I have fun playing this game :)

Edit:
Thanks Fleep I was thinking of putting a sound when snake moves but I tough it was going to be to many sounds :) so I leave that to the users if they want to add a sound fxd for when the snake moves or background music, which ever one they prefer.

Greets,

voidisalive
 
Last edited:

voidisalive

Jr.Coder
Full Member
Nobleman
May 27, 2012
67
458
3
Thanks crazy I'll probably be releasing another game :) I just have to think which game to do :) that would be for next weekend :)
 

konsowa

Coder
Full Member
Nobleman
May 24, 2012
178
753
2
Well done void, go for a mario game next will ya?
Would luv to c u do that.. :)
 

c5

Kim Kong Trasher
Dank Tier VIP
Dank Tier Donator
Jul 19, 2012
1,187
12,638
76
I was doing a stickrun with windows forms, but creating new threads & initializing background workers and passing information to main form UI pissed me off so I cancelled it, but it was promising though :D
 

voidisalive

Jr.Coder
Full Member
Nobleman
May 27, 2012
67
458
3
All right I can do a mini Mario with the console just when I get time to make games :) cuz now I'm busy with work. btw I would love to see some of you guys skills with console you know creativity skills.

c5 you posted an empty message

Edit: sorry c5 i was on my iPhone and I could'nt see you message :)

Edit 2: here is a small little code to pop up a msgbox with the score and also save the current score to a file.

Screenshot
https://i.imgur.com/9Tnrm.png

code include the fstream lib
C++:
 #include <fstream>
C++:
ofstream o;
o.open ("currentscore.txt");
o<<"Your score now is: " << _score;
			
char buffer[128];
sprintf(buffer,"%d",_score);
MessageBoxA(NULL, (buffer), "Score", MB_OK | MB_ICONINFORMATION);
This code goes on your main function right where we printf our game is over

Greets,

voidisalive
 
Last edited:

c5

Kim Kong Trasher
Dank Tier VIP
Dank Tier Donator
Jul 19, 2012
1,187
12,638
76
Hehe, the forum has been screwing on me too... sometimes things I post just disappear :(
 

voidisalive

Jr.Coder
Full Member
Nobleman
May 27, 2012
67
458
3
does it happen to you on a phone device or a pc because it never happens to me on my computer or laptop its always on my phone its weird

voidisalive
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods