Monday, May 20, 2013

Tips to study for exams

Study without ambition is a bird without wings. So its better to develop wings so that we do not have trouble in flying in future. Here are some of the few tips that will help you to develop the wings till you are able to fly... :)

  • If you ask help from others, don't joke around. Concentrate on what you are doing.
  • Always try to be happy, don't give stress to the personal matter while studying
  • Drink lots of water. It helps to stay focused and gives your brain extra power.
  • Always be hopeful, happy and don't doubt yourself on the eve of the examinations
  • Keep your phone switched off while studying.
  • Remember this analogy: 'Study without ambition is a bird without wings'.
  • Do not study a subject only once. If you have more time you can revise for your studies twice or thrice, but only if you have a lot of time and have revised for all your other subjects at least once or you may forget what you revised.
  • Sleep well before exams, so your mind will be alert before, after, and during the exam.
  • Eat a healthy breakfast-- it will help you concentrate.
  • Make sure you actaully pay attention in class. Everything that is said in class will probably come up in an exam.
  • Revise the work that you did in class on the same day.
  • Study less, with more concentration, if possible. If you want to study for a long time then you can take breaks in between to regain your concentration after you return from the break.
  • Its better to study slowly rather than cramming at the last minute.
  • Asking teachers for help conveys your commitment to the material, and can be helpful in the future as well as with your exams. Always remember to ask your teachers if you do not know what she is talking about or if you need more information. The teacher will gladly help.
  • Meditate before going to study or refresh yourself.
  • Sometimes people find it easier to study with a friend, while others find that distraction. While studying with a friend can be lots of fun, don't forget that you're supposed to actually study -- not chat!
  • Don't study laying on top of your bed: you easily get sleepy.

Wednesday, May 15, 2013

Exam Preparation: Ten Study Tips

1. Give yourself enough time to study

Don't leave it until the last minute. Despite what people say, cramming all the information into your brain the night before isn't the best way to approach an exam.
Set out a timetable for your study. Write down how many exams you have and the days on which you have to sit them. Then organize your study accordingly. You may want to give some exams more study time than others, so find a balance that you feel comfortable with.

2. Organize your study space

Make sure you have enough space to spread your textbooks and notes out. Have you got enough light? Is your chair comfortable? Are your computer games out of sight?
Try and get rid of all distractions and then arrange your books into piles. Put your Math books in one pile, and computer books in another, so when it’s time to study for that exam, you've got everything you need in front of you.

3Make a flow chart

This is a bit like brainstorming, but instead of coming up with new ideas, you're writing down everything you already know about a topic.
Put it in an easy to follow diagram with key points that you can easily replicate in an exam. That way, when the exam starts, you can spend five minutes preparing for your answer and then expand on your ideas.

4. Practice on old exams

It's always good to find out what kind of questions are going to be in the exam and the best way is too look through old exams.
That will give you an idea of the layout of an exam, the number of short answers and long answers there will be and the amount of time you should be giving each section.

5. Explain your answers to others

Explain or discuss an answer to a question to your brother, sister or close friends. That will help you to get it clear in your head.
If you find it difficult to explain, perhaps you need to do a bit more study. But at least you won't turn up to the exam and realize you don't know the answer to a question!

6. Organize study groups with friends

You may have questions that they have the answers to and vice versa. You're social life doesn't have to be non-existent during study times. Grab your textbooks and get studying with your mates.

7. Take a break

Don't force yourself to sit studying for 24 hours a day. If you were training for a marathon, you wouldn't try and run 24 hours a day.
Develop a study routine that works for you. If you study better in the morning, then start early before taking a break at lunchtime. Do something mindless in the afternoon and then start studying again in the evening if you know you're more productive at night.
Don't feel guilty that you're out enjoying the sunshine instead of hunched over your textbooks. Vitamin D is important for the brain!

8. Snack on 'brain food'

Keep away from junk food and opt for a bowl of nuts instead. They're much better for the waistline and for the brain. You need to fuel your body while you study so make sure you eat nutritious food that has been proven to help your brain focus such as fish, nuts, yogurt and blueberries.

9. Pack your pencil case

Make sure your pens work and your pencils are sharpened. Nothing is worse for your nerves then having your pen run out of ink at the start of an exam.
If you're sitting a mathematics or computer exam, make sure you have all the necessary equipment you'll need as well like rulers, erasers and calculators.

10. Stock up with water

Being well hydrated is essential for your brain to work at its best, so remember to keep drinking water regularly.

Best of luck for your next exam!

Sunday, May 12, 2013

Snake Game in C


Code:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>


void main(void)
{
   int gdriver = DETECT, gmode, errorcode;
   void
*body,*food,*tail1,*tail2,*tail3,*tail4,*head1,*head2,*head3,*head4;
   int x, y,X[5000],Y[5000],i=3,
maxx,maxy,speed=100,bo=10,t[10],score=0,hscore=20;
   unsigned int size;
   char a='D',b,scor[4],hs[4];
   int k=2,l,r1,r2,f=0,z=100,first=0,second=1;;
   r1=300;
   r2=350;

   ifstream infile("c:\\turboc3\\bin\\rattle.txt");
   infile.getline(hs,4);
   infile.close();
   hscore = atoi(hs);



   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }
   maxx = getmaxx();
   maxy = getmaxy();
   y = 160;
   x = 80;
   for(int j=0; j<=3; j++)
   {
X[i-j]=x-bo*(j+1);
Y[i-j]=y;

   }

   //body

   setfillstyle(1,2);
   bar(10,10,20,20);
   setcolor(0);
   setlinestyle(0,1,3);
   line(9,9,21,21);
   line(9,21,21,9);
   size = imagesize(10,10,20,20);
   body = malloc(size);
   getimage(10,10,20,20,body);
   cleardevice();

   setcolor(0);
   //tail1-right
   t[0]=20; t[1]=10;
   t[2]=10; t[3]=10;
   t[4]=20; t[5]=15;
   t[6]=10; t[7]=20;
   t[8]=20; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail1 = malloc(size);
   getimage(10,10,20,21,tail1);
   cleardevice();

   //tail2-left
   t[0]=10; t[1]=10;
   t[2]=20; t[3]=10;
   t[4]=10; t[5]=15;
   t[6]=20; t[7]=20;
   t[8]=10; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail2 = malloc(size);
   getimage(10,10,20,21,tail2);
   cleardevice();

   //tail3-up
   t[0]=10; t[1]=20;
   t[2]=10; t[3]=10;
   t[4]=15; t[5]=20;
   t[6]=20; t[7]=10;
   t[8]=20; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail3 = malloc(size);
   getimage(10,10,20,21,tail3);
   cleardevice();

   //tail4-down
   t[0]=10; t[1]=10;
   t[2]=10; t[3]=21;
   t[4]=15; t[5]=10;
   t[6]=20; t[7]=21;
   t[8]=20; t[9]=10;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail4 = malloc(size);
   getimage(10,10,20,21,tail4);
   cleardevice();

   //head1-right
   setlinestyle(1,1,1);
   setcolor(2);
   fillellipse(10,10,10,5);
   setcolor(4);
   //eyes
   fillellipse(15,7,1,1);
   fillellipse(15,13,1,1);
   size = imagesize(10,5,20,15);
   head1 = malloc(size);
   getimage(10,5,20,15,head1);

   //head2-left
   //eyes
   fillellipse(5,7,1,1);
   fillellipse(5,13,1,1);
   size = imagesize(0,5,10,15);
   head2 = malloc(size);
   getimage(0,5,10,15,head2);
   cleardevice();

   //head3-up
   //eyes
   setcolor(2);
   fillellipse(12,12,5,10);
   setcolor(4);
   fillellipse(9,6,1,1);
   fillellipse(15,6,1,1);
   size = imagesize(7,2,18,12);
   head3 = malloc(size);
   getimage(7,2,18,12,head3);


   //head4-down
   //eyes
   fillellipse(9,17,1,1);
   fillellipse(15,17,1,1);
   size = imagesize(7,12,18,22);
   head4 = malloc(size);
   getimage(7,12,18,22,head4);
   cleardevice();


   //food
   setcolor(15);
   setfillstyle(1,15);
   fillellipse(10,10,3,5);
   size = imagesize(5,3,15,17);
   food = malloc(size);
   getimage(5,3,15,17,food);
   cleardevice();

    /* repeat until a key is pressed */

   maxx=getmaxx();
   maxy=getmaxy();
   setlinestyle(0,1,3);
   setcolor(9);
   rectangle(0,0,maxx,maxy);
   setlinestyle(0,1,2);
   line(maxx-150,0,maxx-150,maxy);
   setcolor(8);
   setfillstyle(1,8);
   bar(maxx-147,3,maxx-3,63);
   setcolor(12);
   int maze=1;
   if(maze==1)
   {
   setfillstyle(1,6);
   bar(4,4,maxx-154,14);
   bar(4,4,14,maxy-4);
   bar(4,maxy-4,maxx-154,maxy-14);
   bar(maxx-154-10,4,maxx-154,maxy-4);
   }


   settextstyle(0,0,1);
   delay(300);
   outtextxy(maxx-125,30,"RATTLE SNAKE");
   delay(300);
   setcolor(11);
   setlinestyle(0,1,2);
   rectangle(maxx-145,65,maxx-5,205);
   outtextxy(maxx-128,70,"INSTRUCTIONS");
   delay(200);
   setcolor(6);
   outtextxy(maxx-140,90,"Commands     Key");
   delay(300);
   setcolor(8);
   outtextxy(maxx-140,110," Move Up      W");
   delay(50);
   outtextxy(maxx-140,130," Move Down    S");
    delay(50);
   outtextxy(maxx-140,150," Move Right   D");
    delay(50);
   outtextxy(maxx-140,170," Move Left    A");
    delay(50);
   outtextxy(maxx-140,190," Exit         0");
   delay(200);
   setcolor(12);
   setlinestyle(0,1,2);
   rectangle(maxx-145,210,maxx-5,350);
   setcolor(1);
   outtextxy(maxx-130,220,"Player's Name");
   setcolor(15);
   outtextxy(maxx-100,230,"RAVAN");
   setcolor(4);
   delay(200);
   itoa(score,scor,10);
   outtextxy(maxx-140,250,"Score");
   outtextxy(maxx-50,250,scor);
   delay(200);
   setcolor(10);
   outtextxy(maxx-140,280,"Level");
   delay(200);
   setcolor(13);
   outtextxy(maxx-140,310,"Maze        1");
   delay(200);
   setcolor(14);
   outtextxy(maxx-140,340,"High Score  ");
   outtextxy(maxx-50,340,hs);

   for(j=0; j<=1000; j++)
   {
putpixel(random(maxx-150),random(maxy),6);
delay(1);
   }

   while (a!='0')
   {

       // plot new image
      if(a=='D')
   putimage(x, y, head1, XOR_PUT);
      if(a=='A')
   putimage(x, y, head2, XOR_PUT);
      if(a=='W')
   putimage(x, y, head3, XOR_PUT);
      if(a=='S')
   putimage(x, y, head4, XOR_PUT);

      for(j=0; j<k; j++)
      {
  putimage(X[i-j], Y[i-j], body, XOR_PUT);
      }

      if((X[i-j-1]-X[i-j])<0  && (Y[i-j-1]-Y[i-j])==0)
   putimage(X[i-j], Y[i-j], tail1, XOR_PUT);

      if((X[i-j-1]-X[i-j])>0  && (Y[i-j-1]-Y[i-j])==0)
   putimage(X[i-j], Y[i-j], tail2, XOR_PUT);

      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])<0)
   putimage(X[i-j], Y[i-j], tail3, XOR_PUT);

      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])>0)
   putimage(X[i-j], Y[i-j], tail4, XOR_PUT);

      delay(speed);

      if(second==1)
      {
setcolor(15);
outtextxy(maxx-140,360,"Press Any Key...");
getch();
setcolor(0);
outtextxy(maxx-140,360,"Press Any Key...");
a='D';

second=2;
      }


      if(first==1)
      {
setcolor(15);
outtextxy(maxx-140,360,"Congratulations  ");
sound(100);
delay(300);
nosound();
getch();
setcolor(0);
outtextxy(maxx-140,360,"Congratulations  ");
second++;
first=2;

      }

// erase old image
      if(a=='D')
   putimage(x, y, head1, XOR_PUT);
      if(a=='A')
   putimage(x, y, head2, XOR_PUT);
      if(a=='W')
   putimage(x, y, head3, XOR_PUT);
      if(a=='S')
   putimage(x, y, head4, XOR_PUT);


      for(j=0; j<k; j++)
      {
  putimage(X[i-j], Y[i-j], body, XOR_PUT);

      }

      if((X[i-j-1]-X[i-j])<0  && (Y[i-j-1]-Y[i-j])==0)
   putimage(X[i-j], Y[i-j], tail1, XOR_PUT);

      if((X[i-j-1]-X[i-j])>0  && (Y[i-j-1]-Y[i-j])==0)
   putimage(X[i-j], Y[i-j], tail2, XOR_PUT);

      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])<0)
   putimage(X[i-j], Y[i-j], tail3, XOR_PUT);

      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])>0)
   putimage(X[i-j], Y[i-j], tail4, XOR_PUT);


      if(f==0)
      {
putimage(r1,r2,food,XOR_PUT);
f=1;
      }

      z--;
      if((x>=r1 && y>=r2 && x<=r1+10 && y<=r2+10) || (x<=r1 && y<=r2 &&
x>=r1-10 && y>=r2-10) || z==0)
      {
if(z!=0)
{
sound(800);
delay(20);
setcolor(0);
score += 10;
outtextxy(maxx-50,250,scor);
itoa(score,scor,10);
if(score>hscore)
{
strcpy(hs,scor);
setfillstyle(1,1);
bar(maxx-140,337,maxx-20,348);
setcolor(14);
outtextxy(maxx-50,340,hs);
outtextxy(maxx-140,340,"High Score  ");
if(first==0)
{
first=1;
}
}
setcolor(4);
outtextxy(maxx-50,250,scor);
k++;
nosound();
}
z=100;
putimage(r1,r2,food,XOR_PUT);
repr1:
r1=random(450);
   if(r1<50)
      goto repr1;
repr2:
r2=random(400);
   if(r2<50)
goto repr2;

f=0;
      }


      i++;
      X[i]=x;
      Y[i]=y;

      b=a;
      if(kbhit())
      {
rep:
a=getche();

if((b=='D' && a=='A') || (b=='A' && a=='D'))
a=b;

if((b=='W' && a=='S') || (b=='S' && a=='W'))
a=b;

if(a!='D' && a!='A' && a!='W' && a!='0' && a!='S')
a=b;
      }

 /*     if(x>maxx-170)
x=0;

      if(y>maxy)
y=0;*/

      if(a=='W')
y -= bo;

      if(a=='S')
y += bo;

      if(a=='A')
x -= bo;

      if(a=='D')
x += bo;

      for(j=i+1; j<i-k; j--)
      {
if((Y[i]>=Y[j] && Y[i]<=Y[j]+10) || (Y[i]<=Y[j] && Y[i]>=X[j]-10))
{
if((X[i]>=X[j] && X[i]<=X[j]+10) || (X[i]<=X[j] && X[i]>=X[j]-10))
{
a='0';
}

outtextxy(maxx-140,360,"C ");


}

if((X[i]>=X[j] && X[i]<=X[j]+10) || (X[i]<=X[j] && X[i]>=X[j]-10))
{
if((Y[i]>=Y[j] && Y[i]<=Y[j]+10) || (Y[i]<=Y[j] && Y[i]>=X[j]-10))
{
a='0';
}

outtextxy(maxx-140,360,"C  ");
}

       }

/*      for(j=i; j<i-k; j--)
      {
if((y>=Y[j] && y<=Y[j]+10) || (y<=Y[j] && y>=Y[j]-10))
{
if((x>=X[j] && x<=X[j]+10) || (x<=X[j] && x>=X[j]-10))
{
a='5';
}
}

if((x>=X[j] && x<=X[j]+10) || (x<=X[j] && x>=X[j]-10))
{
if((y>=Y[j] && y<=Y[j]+10) || (y<=Y[j] && y>=Y[j]-10))
{
a='5';
}
}
if(j==0)
break;
      }      */


  /* if(x<=0)
x=maxx-170;

if(y<=0)
y=maxy;*/

if(maze==1)
{
if(x<=14 || x>=maxx-184 || y<=14 || y>=maxy-14)
{
  a='0';
}
}

   }

   if(score>hscore)
   {
ofstream onfile("c:\\turboc3\\bin\\rattle.txt");
onfile<<scor;
onfile.close();
   }

   setcolor(15);
   outtextxy(maxx-140,390,"    Gameover");
   sound(100);
   delay(400);
   nosound();
   getch();
   /* clean up */
   free(body);
   closegraph();

}

Snake Game Program in C using Linked List in detail with explanation [mini project]

Basic Concept: Here we join different lines to look like continuous snake.Each line is stored in different node of linked list.When the last line (Head of snake) has co-ordinates similar to pixel (food) size of the snake increases,score increases and co-ordinates of food changes according to rand(). When the snake touches any boundary i.e (end of screen) or touches himself . The game gets over.


Code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <graphics.h>
#include <process.h>
#include <dos.h>
void create(char);
void check();
void out();
char prevKey = 'd', pre = 'd', getKey = 'a';
struct node {
    char ch;
    int x1, y1, len, flag;
    struct node *next;
};
struct node *start, *t, *temp, *temp2;
int flagOut, foodx, foody, max = 100, score = 0;
void main() {
    int gd = DETECT, gm = 0, i,j;
    flagOut = 0;
    foodx = rand() % 620;
    foody = rand() % 460;
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");    //according to your compiler change the url.
    start = (struct node *) malloc(sizeof (struct node));
    start->next = NULL;
    start->x1 = 150;
    start->y1 = 200;
    start->ch = 'd';
    start->len = max;
    start->flag = 1;
    while (1) {
    clrscr();
    if (kbhit()) {
        getKey = getch();
        if (prevKey != getKey && getKey == 'w' || getKey == 'a' || getKey == 's' || getKey == 'd') {
        if (prevKey == 'w' && getKey != 's' || prevKey == 's' && getKey != 'w' || prevKey == 'a' && getKey != 'd' || prevKey == 'd' && getKey != 'a') {
            create(getKey);
            prevKey = getKey;
        }
        }
    }
    for(i=-1;i<=1;i++)
    {
        for(j=-1;j<=1;j++)
        {
         putpixel(foodx+i, foody+j, 4);
        }
    }
    check();
    out();
    t = start;
    do {
        if (t->ch == 'd') {
        setcolor(4);
        line(t->x1, t->y1, t->x1 - t->len, t->y1);
        if (t->flag)
            t->x1++;
        if (t->x1 >= 640)
            flagOut = 1;
        }
        if (t->ch == 'w') {
        setcolor(4);
        line(t->x1, t->y1, t->x1, t->y1 + t->len);
        if (t->flag)
            t->y1--;
        if (t->y1 <= 0)
            flagOut = 1;
        }
        if (t->ch == 'a') {
        setcolor(4);
        line(t->x1, t->y1, t->x1 + t->len, t->y1);
        if (t->flag)
            t->x1--;
        if (t->x1 <= 0)
            flagOut = 1;
        }
        if (t->ch == 's') {
        setcolor(4);
        line(t->x1, t->y1, t->x1, t->y1 - t->len);
        if (t->flag)
            t->y1++;
        if (t->y1 >= 480)
            flagOut = 1;
        }
        if (flagOut == 1) {
        printf("Game Over\nScore=%d", score);
        delay(2000);
        exit(1);
        }
        t = t->next;
    } while (t);

    delay(20);
    }//while close
}

void create(char v) {
    t = temp = start;
    while (t) {
    t->flag = 0;
        temp = t;
        t = t->next;
    }
    temp2 = (struct node *) malloc(sizeof (struct node));
    temp->next = temp2;
    temp2->next = NULL;
    temp2->flag = 1;
    temp2->x1 = temp->x1;
    temp2->y1 = temp->y1;
    temp2->ch = v;
    temp2->len = -1;
}

void check() {
    t = start;
    if (t->next != NULL)
        t->len--;
    if (t->len <= 0) {
        start = t->next;
        free(t);
    }
    t = start;
    do {
        temp = t;
        t = t->next;
    } while (t);
    if (temp->len < max)
        temp->len++;
    if (temp->x1 > foodx - 2 && temp->x1 < foodx + 2 && temp->y1 > foody - 2 && temp->y1 < foody + 2) {
        max = max + max / 10;
        foodx = rand() % 620;
        foody = rand() % 460;
        score = score + score / 10 + 10;
    }
}

void out() {
    t = temp2 = start;
    while (t) {
        temp = temp2;
        temp2 = t;
        t = t->next;
    }
    t = start;
    while (t->next) {
        if (temp2->ch == 'w' && t->ch != 's' && t->ch != 'w' || temp2->ch == 'a' && t->ch != 'd' && t->ch != 'a' || temp2->ch == 's' && t->ch != 'w' && t->ch != 's' || temp2->ch == 'd' && t->ch != 'a' && t->ch != 'd') {
            if (temp2->x1 == t->x1) {
                if (t->ch == 'w') {
                    if (temp2->y1 >= t->y1 && temp2->y1 <= (t->y1 + t->len))
                        flagOut = 1;
                }
                if (t->ch == 's') {
                    if (temp2->y1 <= t->y1 && temp2->y1 >= (t->y1 - t->len))
                        flagOut = 1;
                }

            }

            if (temp2->y1 == t->y1) {
                if (t->ch == 'a') {
                    if (temp2->x1 >= t->x1 && temp2->x1 <= (t->x1 + t->len))
                        flagOut = 1;
                }
                if (t->ch == 'd') {
                    if (temp2->x1 <= t->x1 && temp2->x1 >= (t->x1 - t->len))
                        flagOut = 1;
                }
           }
        }
        if (temp2->y1 == t->y1 && temp2->x1 == t->x1)
            flagOut = 0;
        t = t->next;
    }
}


Explanation:  Yup program is quite difficult to understand ,lets start with it.
 Why we require linked list?
values of each line of snake is stored respective node.When we change direction of snake a new is created.
Starting with linked list structure,which contains char ch  int x1, y1, len, flag struct node *next.Lets see the use of each element.
ch will contain the values 'a' or 's' or 'd' or 'w' i.e direction of that single line.
x1 and y1 will save the starting x and y co-ordinate respectively from which line starts.
len will save the current length of that particular line.
flag will indicate the line is increasing or constant.we will see in detail.
*next will save the address of next node in linked list.

Other variables: flagOut which is initialized to 0 indicates snake is alive and 1 indicates game over.
 foodx,foody where to print food.

 start = (struct node *) malloc(sizeof (struct node));//allocating memory
    start->next = NULL;                                        //as only 1 node is there
    start->x1 = 150;                                              //initialized starting point
    start->y1 = 200;                                              
    start->ch = 'd';                                                //initialized the direction
    start->len = max;                                             //initialized to max
    start->flag = 1;    //indicates the line should move in direction initialized above
 while (1)  //so that loop goes in infinite loop

kbhit() this is called whenever any key is pressed
getKey = getch(); //using getch() we take the input i.e which key has been pressed

if (prevKey != getKey && getKey == 'w' || getKey == 'a' || getKey == 's' || getKey == 'd')

prevKey stores the previous direction and getKey stores current so ,if prevKey==getKey then the direction is same so no new linked list should be created and further if both are different then i have checked that the key pressed is legal key i.e a,s,d or w.

if (prevKey == 'w' && getKey != 's' || prevKey == 's' && getKey != 'w' || prevKey == 'a' && getKey != 'd' || prevKey == 'd' && getKey != 'a')

If snake is going up so if some one presses down it should not go as only way is it can go right or left.Similarly other cases.

prevKey = getKey; //this will become our prevous keystroke.

for(i=-1;i<=1;i++)
    {
        for(j=-1;j<=1;j++)
        {
         putpixel(foodx+i, foody+j, 4);     //prints the pixel at +- 1 postion.
        }
    }


 if (t->ch == 'd') {
        line(t->x1, t->y1, t->x1 - t->len, t->y1); //lines of co-ordinate and from where to where it should be printed
        if (t->flag)   //if direction has been changed then the current line should diminished at a point
            t->x1++;
        if (t->x1 >= 640) // check if snake crosses a boundary
            flagOut = 1;  //indicates game over
        }

similar other 3 groups codes will work

create() will be called when valid key is pressed to change the direction of snake.
So we create new linked list initialized it with the point where direction has to be changed. Length of this line will start from -1 and will increase up-to max.

check() performs two task.
Score in snake game1)If the line which was decreasing has become 0 or not if it has become 0 then free the list.
2)Checks if the snake has eat the food(snake has passed from plotted pixel).If so then generate new pixel position and increment the score.

  t = temp2 = start;
snake touching the walls and getting out    while (t) {
        temp = temp2;
        temp2 = t;
        t = t->next;
snake touching himself and getting out    }
Through this code we can traverse the linked list and reached the head of snake.So temp2 becomes the head of snake.The whole code in out() is to check if any side of snake is touching each other.