Hello Everybody!!! Welcome To JOTHIGALLARY I H've Presented u some informations additionaly regarding general things such as., Programming Languages C,C++,Java,CG Gaming, Mathematical Puzzles,etc Viewers can contact me for any queries at., jothi388@gmail.com
Saturday, December 15, 2012
Jothi's SNAKE game using C++
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
char a[25][25];
int q1=24,q2=4,t1,t2,c1=0;
int li=3,sc=0;
char c;
typedef struct snake *position;
struct snake
{
int r,c;
int *add;
position next;
};
position first,last,temp,t;
void move();
void eat();
void definition();
void movement(char c);
void memory();
void memory()
{
first=position(malloc(sizeof(struct snake)));
first->r=q1;
first->c=q2;
first->next=NULL;
}
void definition()
{
memory();
temp=first;
last=first;
for(int i=0;i<25;i++)
for(int j=0;j<25;j++)
{if((i<23&&i>1)&&(j<23&&j>1))//&&(i%2!=0)&&(j%2!=0))
{
a[i][j]='.';
c1++;
}
else
a[i][j]=32;
}
a[q1][q2]='#';
for(i=5;i<20;i++)
for(j=5;j<20;j++)
if((i%2==0)&&(j%2==0))
a[i][j]='*';
}
void move()
{
memory();
temp->next=first;
temp=first;
t=last->next;
a[last->r][last->c]=32;
free(last);
last=t;
}
void eat()
{
memory();
temp->next=first;
temp=first;
}
void main()
{
clrscr();
definition();
while(1)
{
clrscr();
cout<<"\n\t\tSnake Game\n\tLife="<<li<<"\tScore"<<sc<<"\n\n\t";
for(int m=0;m<30;m++)
cout<<"=";
cout<<"\n";
for(int i=0;i<25;i++)
{cout<<"\t||";
for(int j=0;j<25;j++)
cout<<a[i][j];
cout<<"||\n";
}
cout<<"\t";
for(m=0;m<30;m++)
cout<<"=";
if(sc>=c1||li<=0)
{
if(sc>=c1)
cout<<"\n\n\nCongradge, U won....\nReally it's a amazing performance";
if(li<=0)
cout<<"\n\n\nSorry, U lost...\nBetter luck next time";
getch();
exit(0);
}
c=getch();
movement(c);
}
}
void movement(char c)
{
if((c=='4')||(c=='8')||(c=='6')||(c=='2'))
{
if(c=='4')
{
if(((a[q1][q2-1]==32)||(a[q1][q2-1]=='.'))&&(q2-1>=0))
{
if(a[q1][q2-1]=='.')
{ sc++;
eat();
}
else move();
a[q1][--q2]='#';
}
else
li--;
}
if(c=='6')
{
if(((a[q1][q2+1]==32)||(a[q1][q2+1]=='.'))&&(q2+1<=24))
{
if(a[q1][q2+1]=='.')
{
sc++;
eat();
}
else move();
a[q1][++q2]='#';
}
else
li--;
}
if(c=='8')
{
if(((a[q1-1][q2]==32)||(a[q1-1][q2]=='.'))&&(q1-1>=0))
{
if(a[q1-1][q2]=='.')
{
sc++;
eat();
}
else move();
a[--q1][q2]='#';
}
else
li--;
}
if(c=='2')
{
if(((a[q1+1][q2]==32)||(a[q1+1][q2]=='.'))&&(q1+1<=24))
{
if(a[q1+1][q2]=='.')
{
sc++;
eat();
}
else move();
a[++q1][q2]='#';
}
else
li--;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment