Snake

Posts 1–15 of 18 · Page 1 of 2
Snake
A while ago I decided to make snake as a c++ practice.
I’m posting it here to get reviewed by more experienced coders and to let the beginning coders learn.
The graphics are not good but the game works and that was my main goal not making a good graphic game.

So can you guys post constructive criticism (i.e. bad coding habits) or if you got any questions regarding the code (or something else) please do ask them!

Special thanks to Why06 for helping me point out that the character array needs to be unsigned.

Code:
#include <iostream>
#include <windows.h>
#include <winbase.h>

using namespace std;

const int FieldWidth = 50;
const int FieldHeigth = 30;
const int Area= FieldWidth*FieldHeigth;
unsigned char Field [FieldWidth*FieldHeigth];
int Body[100];
int Direction=1;
int Length = 2;
int StartTime;
int Delay=100;
int bbq;
bool alive=true;

#include <stdlib.h>
#include <time.h>

void DrawScreen();
void CheckDirection();

void CreateSnake();
void Food();
void MoveSnake();
void GameOver();
void PutTheSnakeInTheField();

int main()
{
	srand ( time(NULL) );
	CreateSnake();
	Food();
	StartTime=GetTickCount();

	while(alive)
	{
		if((int)GetTickCount()>(StartTime+Delay))
		{
			system("cls");
			StartTime=GetTickCount();
			CheckDirection();
			PutTheSnakeInTheField();
			DrawScreen();
		}
		Sleep(10);
		
		
	}
	

	system("pause");
	return 0;


}

void DrawScreen()
{
	if(!alive)
	{
		for(int i=0; i<Area;i++)
		{
			Field[i]=176;
		}
	}
	for(int i=0; i <(FieldWidth+2);i++)
	{
		cout<<"#";
	}
	cout<<endl;
	
	for(int i=0; i<FieldHeigth; i++)
	{
		cout<<"#";
		for (int j =0;j<FieldWidth; j++)
		{
			cout<< Field [(FieldWidth*i)+j];
		}
		cout<<"#";
		cout<<endl;
	}
	for(int k=0; k <(FieldWidth+2);k++)
	{
		cout<<"#";
	}
	cout<<endl;
	
}

void CreateSnake()
{
	Body[0]=Area/2-FieldWidth/2;
	Body[1]=Area/2-FieldWidth/2-1;
	Body[2]=Area/2-FieldWidth/2-2;
}

void Food()
{
	
	do
	{
		bbq = rand()%Area;
	}while(Field[bbq]);
	Field[bbq]=229;
}

void PutTheSnakeInTheField()
{
	if(Body[0]+Direction==bbq)
	{
		Length++;
		Food();
	}
	else if((Field[Body[0]+Direction])==167)GameOver();
	else
	{
		switch(Direction)
		{
			case 1:
				if(!((Body[0]+1)%FieldWidth))GameOver();
				break;
			case (FieldWidth):
				if((Body[0]/FieldWidth)==FieldHeigth-1)GameOver();
				break;
			case -1:
				if(!(Body[0]%FieldWidth))GameOver();
				break;
			case (-FieldWidth):
				if(!(Body[0]/FieldWidth))GameOver();
				break;
		}
		Field[Body[Length]]='\0';
	}
	MoveSnake();
	Field[Body[0]]=235;
	for(int i=1;i<Length;i++)
	{
		Field[Body[i]]=167;
	}
}


void MoveSnake()
{
	for(int i=Length;i>0;i--)
	{
		Body[i]=Body[i-1];//klopt dit wel? //yup, als een bus
	}
	Body[0]=Body[0]+Direction;
}

void GameOver()
{
	alive=0;
}

void CheckDirection()
{
	if(GetAsyncKeyState(VK_LEFT)&&!(Direction==1))
	{
		Direction=-1;
	}
	else if(GetAsyncKeyState(VK_UP)&&!(Direction==FieldWidth))
	{
		Direction=-FieldWidth;
	}
	else if(GetAsyncKeyState(VK_RIGHT)&&!(Direction==-1))
	{
		Direction=1;
	}
	else if(GetAsyncKeyState(VK_DOWN)&&!(Direction==-FieldWidth))
	{
		Direction=FieldWidth;
	}
}
'//klopt dit wel? //yup, als een bus'

lmao xD
not bad not bad

If you want to see bad coding check out my snake game :3
The music is made by my stephbrother(© Retox Entertainment /ridicule)
D3DXLines for drawing ftw

It was made in a minute time for a school project :3
SnakeMaatschapijleer.rarattachment deleted · 35 downloads before removal
Quote Originally Posted by Hell_Demon View Post
'//klopt dit wel? //yup, als een bus'

lmao xD
not bad not bad

If you want to see bad coding check out my snake game :3
The music is made by my stephbrother(© Retox Entertainment /ridicule)
D3DXLines for drawing ftw

It was made in a minute time for a school project :3
I won the game.
awh constructive criticism...
I actually understand this
I'm not so bad at C++ then.
Quote Originally Posted by Nathan View Post
I actually understand this
I'm not so bad at C++ then.
Err. If you're so happy to understand basic school projects why did you posted this. ?
http://www.mpgh.net/forum/27-coders-...183-title.html
Quote Originally Posted by Nathan View Post
After I fully know C++, what language should I learn?
I was thinking Java -> PHP & SQL -> Assembly -> I'll see then

So what do you think?
(I'm not going for hacking mainly)
Quote Originally Posted by Melodia View Post


Err. If you're so happy to understand basic school projects why did you posted this. ?
I learn fast
So I'll know enough C++ in about 1-2 months.

And I wasn't actually happy.
I was just stating that I know how it works.
Quote Originally Posted by Omar the Sand****** View Post


I learn fast
So I'll know enough C++ in about 1-2 months.
hah.


@Hell_Demon: That was actly one of the most enjoyable snake games I've played, and the music wasnt bad either. which either goes to say how good a job you did or how much I enjoy snake games.
Quote Originally Posted by why06 View Post
hah.
You don't believe me
Quote Originally Posted by Nathan View Post


You don't believe me
No, I don't. Sorry. =/
Quote Originally Posted by why06 View Post


No, I don't. Sorry. =/
Then I'll just have to prove you wrong
Quote Originally Posted by Nathan View Post


Then I'll just have to prove you wrong
You won't be able to
Quote Originally Posted by Hell_Demon View Post
You won't be able to
I'll prove you wrong too then.
But I'll definitly have it done in 4 months, because I'll have 2 months of holiday in 2 months.
You'll never know 'enough' C++
Quote Originally Posted by Hell_Demon View Post
You'll never know 'enough' C++
Enough to be able to program almost anything then .

That will be reachable right?
Posts 1–15 of 18 · Page 1 of 2

Post a Reply

Tags for this Thread

None

Talk with us