Saturday 30 November 2013

The Old Snake Game

This is actually an edited post. Been thinking of posting 4 times at least, in a month. I didn't want November to slip by, so I quickly posted a random song on the last night of November, to save the spot.

Anyway, this is a crude recreation of the old snake game. The game was invented by Scott McCarthy and was first published in 1976.

I'm still working on the database for the doctor. Hit some glitches that have been giving me sleepless nights. So I decided to take a break and try to write the snake game we used to play on our B&W Nokias.

The fact that the food may appear within the body of the snake sometimes is NOT A BUG, it's sheer laziness.
Besides that it's sure to have some bugs, I haven't played it much, if you find some let me know in the comments section.

I have set the variable 'speed' to 5000 (If you copy paste this into C:B, it should be line 14). Decreasing this number will speed the game up, increasing this will slow the game down.

If you don't already have the include file myconstdwin.h, click HERE to find out how to create it yourself.

The snake is controlled with the arrow keys, hitting Esc will exit the game or if you say 'No' to "Play Again?".

If you are new to earth, welcome to the planet and here are the game instructions :P
The objective of the game is to "eat" as many of the food as you can without hitting the borders, and without crashing into your own body as you coil around. 'Eating', here, means crashing head-long into the food, the snake will keep moving in the direction you point it in.
When you eat one, another piece of food appears in a random spot. Every piece of food you eat increases the snakes length.

Nothing else I can think of saying, so here's the code:


#include <myconstdwin.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

void time_seed(void); //seed randomizer from the clock
int random(int range); //this generates a random number. The "range" is how big you want the number to be
int get_key(void); //detect keys

void draw_box(void);
void food(int x,int y); //displays food
void snake(int *sxt,int *syt,int *sxh,int *syh); //sxt= snake x coordinate of tail

int pause,direction=3; //direction, counting from left, clockwise: left=1,up=2,right=3,down=4
int len=2,speed=5000,redo=0; //len (length of snake is actually 3 in the beginning, but we are counting from 0.

int main(void)
{
    int i,sx[500],sy[500],key,points=0;
    /**co-ordinates sx[0],sy[0] will always be the tail, sx[len],sy[len] the head**/
    sx[0]=23,sx[1]=24,sx[2]=25,sy[0]=7,sy[1]=7,sy[2]=7;
    int foodx,foody;
    int again=1,game;
    char c;

    while(again)
    {

        system("cls");
        gotoxy(1,17);
        printf("Points: %i",points);
        game=1;
        draw_box();
        /**Snake**/
        for(i=0;i<len;++i)
        {
            gotoxy(sx[i],sy[0]);
            printf("%c",15);
        }

        time_seed();
        foodx=random(43);
        foody=random(14);

        food(foodx+1,foody+1);

        while(game)
        {
            if(sx[len]==foodx+1 && sy[len]==foody+1) //if the head and food are at the same spot
            {
                food(foodx+1,foody+1);
                printf("\b \b%c",15);
                /**create new food**/
                time_seed(); //seed randomizer with clock
                foodx=random(43); //create a random number range upto 43
                foody=random(13); //create a random number range 13

                food(foodx+1,foody+1); //the +1 prevent the food from appearing at the lines
                len++; //increase length
                if(direction==1 || direction==3)
                {
                    if(direction==1)
                        sx[len]=sx[len-1]-1;
                    else if(direction==3)
                        sx[len]=sx[len-1]+1;

                    sy[len]=sy[len-1];
                }
                else
                {
                    if(direction==2)
                        sy[len]=sy[len-1]-1;
                    else if(direction==4)
                        sy[len]=sy[len-1]+1;
                    sx[len]=sx[len-1];
                }
                points+=10;
                gotoxy(1,17);
                printf("Points: %i",points);
            }

            snake(&sx[0],&sy[0],&sx[len],&sy[len]);

            if(redo==1)
            {
                /**if the food appears within the snakes body, the next line prevents the food
                   from disappearing when the snake has finished passing through it**/
                food(foodx+1,foody+1);

                for(i=0;i<len-1;i++)
                {
                    if((sx[len]==sx[i] && sy[len]==sy[i]) || (sx[len]==46) || (sy[len]==15) || (sx[len]==0) || (sy[len]==0))
                    {
                        gotoxy(19,17);
                        printf("OOPS!!! Play Again(Y/N): ");
                        while(1)
                        {
                            c=toupper(getch());
                            if(c=='N')
                            {
                                game=0;
                                again=0;
                                system("cls");
                                break;
                            }
                            else if(c=='Y')
                            {
                                game=0;
                                len=2,redo=0,points=0;
                                sx[0]=23,sx[1]=24,sx[2]=25,sy[0]=7,sy[1]=7,sy[2]=7;
                                direction=3;
                                break;
                            }
                        }
                        break;
                    }
                }

                for(i=0;i<len;i++)
                {
                    sx[i]=sx[1+i];
                    sy[i]=sy[1+i];
                }

                if(direction==1) //going left
                {
                    sy[len]=sy[len-1];
                    sx[len]--;
                }
                else if(direction==2) //going up
                {
                    sy[len]--;
                }
                else if(direction==3) //going right
                {
                    sy[len]=sy[len-1];
                    sx[len]++;
                }
                else if(direction==4) //going down.
                {
                    sy[len]++;
                }
                redo=0;
            }

            if(kbhit())
            {
                key=get_key();

                if(key==584) //up
                {
                    if(direction==1 || direction==3)
                    {
                        direction=2;
                    }
                }
                else if(key==592) //down
                {
                    if(direction==1 || direction==3)
                    {
                        direction=4;
                    }
                }
                else if(key==587) //left
                {
                    if(direction==2 || direction==4)
                    {
                        direction=1;
                    }
                }
                else if(key==589) //right
                {
                    if(direction==2 || direction==4)
                    {
                        direction=3;
                    }
                }
                else if(key==27) //esc
                {
                    exit(0);
                }
            }
        }
    }

    return 0;
}

void food(int x,int y)
{
    gotoxy(x,y);
    printf("%c",219);
}

void snake(int *sxt,int *syt,int *sxh,int *syh)
{
    pause++;

    if(pause==speed) //this controls game speed
    {
        redo=1;
        gotoxy(*sxt,*syt);
        printf(" ");
        gotoxy(*sxh,*syh);
        printf("%c",15);

        pause=0;
    }
}

void draw_box(void)
{
    int i;

    /**Draw Box**/
    gotoxy(0,0);
    printf("%c",218);
    for(i=0;i<45;i++)
    {
        printf("%c",196);
    }
    printf("%c",191);
    for(i=0;i<15;i++)
    {
        gotoxy(0,1+i);
        printf("%c",179);
        gotoxy(46,1+i);
        printf("%c",179);
    }
    gotoxy(0,15);
    printf("%c",192);
    for(i=0;i<45;i++)
    {
        printf("%c",196);
    }
    printf("%c",217);
}

int random(int range)
{
    int y;
    y=rand()%range;
    return(y);
}

void time_seed(void)
{
    srand((unsigned)time(NULL));
}

int get_key()
{
    int c=getch();
    switch(c)
    {
      case 0:   return getch()+256;
      case 224: return getch()+512;
    }
    return c;
}

No comments:

Post a Comment