Snake game help

modification in code

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 3448 Views - Last Post: 28 November 2010 - 12:58 PM Rate Topic: -----

#1 Guest_Asif Ali*


Reputation:

Snake game help

Posted 26 November 2010 - 08:29 AM

Somebody KINDLY fix my code(in C++) i.e the proper "up and down" motion and if the snake is moving rightwards, it shouldn't start moving leftward when the corresponding key is pressed; here is the code. . .

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
void boundry(char A[][79], int x, int y, char direction);
void gotoxy(int x,int y);

void main()
{
	char array[24][79], direction;

	int x,y;
	
	

	srand(time(0));

	x=rand()%22+1;

	y=rand()%75+1;
	
	direction=getch();	
	
	while(true)
		{	
		
		if(kbhit())
		{direction=getch();
		}	 
		boundry(array,x,y,direction);
	Sleep(100);

		if(direction=='d')
		{
				gotoxy(y++,x);
				cout<<endl;
				if (y==78)
				{
					gotoxy(30,10);
					cout<<"Game 0ver!!";	
					break;
				}
		}
			else if(direction =='a')
			{	gotoxy(y--,x);
			cout<<endl;
		if(y==0)
			{
				gotoxy(30,10);
				cout<<"Game 0ver!!";
				break;}
			}
		else if(direction =='w')
			{	
			gotoxy(y,x--);	
			if(x==1)
			{
			gotoxy(30,10);
			cout<<"Game 0ver!!";
			break;}
			}
		else if(direction=='z')
		{
		gotoxy(y,x++);
		
		}
		else if(direction =='s')
			{	
			gotoxy(y,x++);	
		if(x==22)
			{
			gotoxy(30,10);
			cout<<"Game 0ver!!";
			break;}
			}
		}
			
		cout<<endl<<"                         ";
	
}
void boundry(char A[][79], int x, int y,char direction)
{	system("cls");
	for(int n=0;n<24;n++)
{
		for(int u=0;u<79;u++)

			A[n][u]=(char)32;
}
		for(int i =0;i<24;i++)
{
		for(int j=0 ;j<79;j++)
		{ 
			if(i==0 || j==0 ||i==23||j==78)

		A[i][j]=char(219);	
	}
		}
		if(direction=='d')
		{A[x][y]=(char)250;
		A[x][y-1]=(char)254;
		A[x][y-2]=(char)254;}

		else if( direction=='a')
		{A[x][y]=(char)60;
		A[x][y+1]=(char)254;
		A[x][y+2]=(char)254;}
		else if(direction=='w')
	{
		A[x][y+1]=(char)254;
		A[x][y]=(char)254;
		A[x-1][y]=(char)254;
		
	}	
		else if
		(direction=='s')
	{
		A[x][y]=(char)250;
		A[x][y+1]=(char)254;
		A[x+1][y]=(char)254;
	}

	for(int in=0;in<24;in++)
	{
		cout<<endl;
		for(int ou=0;ou<79;ou++)
			cout<<A[in][ou];

	}
	cout<<endl;
}


void gotoxy(int x,int y)
{
	
HANDLE console_handle;
COORD cursor_coord;
cursor_coord.X=x;
cursor_coord.Y=y;
console_handle= GetStdHandle( STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(console_handle,cursor_coord);		
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 26 November 2010 - 08:39 AM


Is This A Good Question/Topic? 0

Replies To: Snake game help

#2 Acifali  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 08:46 AM

Okay, i'll be careful, henceforth. now kindly help me..
Was This Post Helpful? 0
  • +
  • -

#3 Acifali  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 09:04 AM

:helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 09:27 AM

So your snake is moving in opposite direction to the key being pressed?
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Asif Ali*


Reputation:

Re: Snake game help

Posted 26 November 2010 - 09:37 AM

No, kindly run the code once; you'll get what i mean to say..
Was This Post Helpful? 0

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 09:50 AM

Can't be bothered, if you can't be bothered to explain. Sorry. :)
Was This Post Helpful? 0
  • +
  • -

#7 Acifali  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 09:56 AM

So rude!! lemme explain it... unlike a snake game, if i press "d" and it starts moving rightwards, it starts moving leftwards on pressing "a".. and secondly, when i try to move it upwards, it changes it's shape to "L" rather than getting straight..!! :rolleyes2:
Was This Post Helpful? -1
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 10:01 AM

And what happens if you press 's'? Shouldn't you be using 'ints' instead of 'chars' to measure your directions? Using chars you are limiting yourself to the range 0..255 rather than the maximum integer range which will be much greater. I think this may be at least one of the reasons why this may be happening.

And be sure to make them unsigned if appropriate.

This post has been edited by ButchDean: 26 November 2010 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#9 Acifali  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 10:06 AM

actually, i'm using these keys as directions..

the co-ordinates're "x,y". .

kindly try to fix it, g0tta submit it on monday..:(
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 10:29 AM

You shouldn't be doing it like that. I'm happy to help you work it out, but I won't fix it for you as it violates forum rules. Here are some things to look at:

1. Instead of using 'if-else' look into the switch statement so that you have something like

    switch(direction)
    {
        case 'd':
            // Move right.
            break;
        case 'a':
            // Move left.
            break;
        case 'w':
            // Move up.
            break;
        case 's':
            // Move down.
            break;
        default:
            break;
    }



2. Something looks wrong with your loops, which I'm going to have a look at now.
Was This Post Helpful? 1
  • +
  • -

#11 Acifali  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 10:32 AM

thank you, :smartass:
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1370
  • View blog
  • Posts: 4,741
  • Joined: 19-February 09

Re: Snake game help

Posted 26 November 2010 - 10:33 AM

In gotoxy() the x and y are like values on a X-Y graph. x values relate to the X-axis (horizontal) and y values relate to Y-Axis (vertical/row number).

So on the usual console screen x can be 0 - 79, and y 0 - 24.

So we can argue that this is the wrong way round.
 x=rand()%22+1;
 y=rand()%75+1;



You changed the parameters around to compensate for this, in gotoxy()
gotoxy(y++,x);



In boundry() you don't.
boundry(array,x,y,direction);




Also, in your design you seem to trying to use two conflicting design stratagems.

Direct
With gotoxy() you appear to want to draw to the console directly.

Indirect
With your array you seem to wish to create an image in memory then print the whole array to screen.
Was This Post Helpful? 1
  • +
  • -

#13 Acifali  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 10:37 AM

Correct, how'll that get fixed? what if i don't use the "gotoxy()" function? how can i bring the cursor to the desire or a random position?
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Snake game help

Posted 26 November 2010 - 10:52 AM

first of all I dont know how your code compiled without your compiler screaming.

iostream.h is deprecated use iostream

at the top of your code you should be using
using namespace std;



or prefixing all your cout statements with std::
and also dont use void main()
use
int main()
and that should
return 0;

Why does your border flash ?
wouldnt it be better to have the border of the maze not flash like that by drawing it once and the leaving it ??
Was This Post Helpful? 0
  • +
  • -

#15 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake game help

Posted 26 November 2010 - 10:54 AM

View PostAcifali, on 26 November 2010 - 09:37 AM, said:

Correct, how'll that get fixed? what if i don't use the "gotoxy()" function? how can i bring the cursor to the desire or a random position?

Is it working now?

I'm currently playing around with it and it needs a rewrite - it exits abnormally as I expected.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3