C program to move a character like snake in snake game

















C program to move a character like snake in snake game

Press the right arrow key to move character right
Press the left arrow key to move character left
Press the up arrow key to move character up
Press the down arrow key to move character down


Note :

75--key code for Left arrow key
77--key code for Right arrow key
72--key code for Up arrow key
80--key code for Down arrow key

Program :



#include <stdio.h>
#include <conio.h>
#include <dos.h>

#define LEFT_ARROW 75
#define RIGHT_ARROW 77
#define UP_ARROW 72
#define DOWN_ARROW 80

void main()
{
int c=33;
int d=14;
int e=33;
int f=14;
int g=33;

clrscr();

char choice;

gotoxy(33,14);
textcolor(2);
cprintf("k");

do
{
if(kbhit())

choice=getch();

if(choice==0)
{
choice=getch();
}

if(choice==75)
{
while(!kbhit())
{
delay(100);

gotoxy(c,d);

textcolor(2);

cprintf("k");

c--;

e=c;

g=c;

f=d;

}
}

if(kbhit())

choice=getch();

if(choice==0)
{
choice=getch();
}

if(choice==72)
{
while(!kbhit())
{
delay(100);

gotoxy(e,d);

textcolor(2);

cprintf("k");

d--;

f=d;

c=e;

}
}
if(kbhit())
choice=getch();

if(choice==0)
{
choice=getch();
}

if(choice==77)
{
while(!kbhit())
{
delay(100);

textcolor(2);

gotoxy(e,f);

cprintf("k");

e++;

g=e;

}
}

if(kbhit())
choice=getch();

if(choice==0)
{
choice=getch();
}

if(choice==80)
{
while(!kbhit())
{
delay(100);

gotoxy(g,f);

textcolor(2);

cprintf("k");

f++;

d=f;

c=g;

}
}

if(kbhit())
choice=getch();
}
while(choice !=27);
}


Output :


1 comments:

Unknown said...

bro thx a lot :)
u made me out of trouble......

Post a Comment