0

Hi everybody!!!

I am a new student of programming.
I received a project from my IT Prof. making a snake game.

So far i did some of my simpliest snake program.
I did it for almost 2 months.

Now my problem is to make a level everytime the snake eats the food.
Also, I dont know what to do with its directions.
I wanted to make the snake go in only forward direction and it will not be allow to make a backward.
And when the snakes collide on its own body then it will show gameover.

Can anyone help me with my probelm???

Here is my code that i have started>>>>

#include <stdlib.h>
#include <iostream>
#include <conio.h> // for getch()
#include <time.h>
#include <string>
#include "ic.hpp" // for textcolor() and gotoxy()

using namespace std;
using namespace ic;
using namespace ic::shorties;

int main()
{
	int snake[50][2];
	int xSnake = 20;
	int ySnake = 10;
	int xFood = 20;
	int yFood = 10;
	int xFoodSave = 0;
	int yFoodSave = 0;
	int x = 0;
	int y = 0;
	int x1 = 0;
	int y1 = 0;
	int i = 0;
	int xSnakeTail= 0;
	int ySnakeTail= 0;
	int snakeLength = 1;
	int speed = 400;
	int points = 0;
	void score(int points);
	void grenze(); //boundary
	char keyboard = 0;
	clrscr(BG_YELLOW);
	snake[0][0] = 10;
	snake[0][1] = 5;
	
	while(1)
	{
		bgcolor(BG_YELLOW);
		grenze();
		gotoxy(xFood,yFood);
		textcolor(FG_RED);
		cout << "*";
		textcolor(FG_YELLOW);
		gotoxy(xSnakeTail, ySnakeTail);
		cout << "@";
		
		for(int a = 0; a < snakeLength; a++)
		{
			textcolor(FG_BLUE);
			gotoxy(snake[a][0], snake[a][1]);
			cout << "@";
		}
		
		xSnakeTail = snake[snakeLength-1][0];
		ySnakeTail = snake[snakeLength-1][1];
		
		for(i = snakeLength - 1; i > 0; i--)
		{
			snake[i][0] = snake[i-1][0];
			snake[i][1] = snake[i-1][1];
		}
		
		x = snake[0][0];
		y = snake[0][1];
		
		if((x <= 0) || (x >= 75) || (y <= 1) || (y >= 24))
		{
			clrscr(BG_BLACK);
			gotoxy(35,12);
			textcolor(FG_RED);
			cout << "Game Over";
			Sleep(3500);
			break;
		}
		
		else
		{
			if((snake[0][0] == xFood) && (snake[0][1] == yFood))
			{
				snake[snakeLength][0] = xFood;
				snake[snakeLength][1] = yFood;
				xFoodSave = xFood;
				yFoodSave = yFood;
				
				do
				{
					xFood = (rand()%72)+1;
					yFood = (rand()%22)+2;
				}
				
				while((xFood == xFoodSave) && (yFood == yFoodSave));
				
				snakeLength++;
				points += 10;
				speed -= 10;
			}
		}
		
		score(points);
		
		if(points >= 50)
		{
			speed = 150;
		}
		
		if(points >= 100)
		{
			speed = 100;
		}
		
		if(points >= 150)
		{
			speed = 50;
		}
		
		if(points >= 200)
		{
			speed = 40;
		}
		
		if(points >= 250)
		{
			speed = 30;
		}
		
		if(kbhit())
		{
			keyboard = getch();
		}
		
		switch(keyboard)
		{
		case 'a': snake[0][0]--; break;
		case 'd': snake[0][0]++; break;
		case 'w': snake[0][1]--; break;
		case 's': snake[0][1]++; break;
		}
		Sleep(speed);
	}
}

void score(int points)
{
	gotoxy(65,0);
	textcolor(FG_BLACK);
	cout << "Scores: " << points;
}

void grenze()
{
	//line boundary right side
	for(int i = 1; i <= 24; i++)
	{
		gotoxy(75,i);
		textcolor(FG_BLACK);
		cout << "#";
	}
	
	//line boundary over
	for(int a = 1; a <= 74; a++)
	{
		gotoxy(a, 1);
		textcolor(FG_BLACK);
		cout << "#";
	}
	
	//line boundary under
	for(int b = 1; b <= 74; b++)
	{
		gotoxy(b, 24);
		textcolor(FG_BLACK);
		cout << "#";
	}
	
	//line boundary left side
	for(int c = 1; c <= 24; c++)
	{
		gotoxy(0,c);
		textcolor(FG_BLACK);
		cout << "#";
	}

}

An advance thank yoou for those who are willing to help me. :)

This zip here is the improved console for the snake. I dont know why but my Prof. gave this to us and said that we really need it to run the program.

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by ayiealigatour
0

hye to all..

cn any one help me..

i need a java games a simple one...
ASAP!!!
pls help me..
im really new with this

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.