Sign in to follow this  

Snake game in C

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I try to code a simple snake game and here is the result. I programmed in C. here is the link: http://www.yousendit.com/transfer.php?action=download&ufid=02B533287B2F7064
[SOURCE]#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include"linkedlist.cpp"
#include <time.h>

#define WIDTH 16
#define LENGTH 23

int board[WIDTH][LENGTH];
int direction;
int score;

void wait ( float seconds );
enum direction{UP, LEFT, DOWN, RIGHT};//movement keys
enum keys{ WALL=5, SNAKE, EMPTY, FEED, HEAD};//helpful keys for printing game
void createFeed();
void printboard(List *u);
void constructMaze(List *u);
int move(List *u, int n);

int main(){
	List *u;
	int dir= RIGHT;
	char c=RIGHT;
	float timer = (float)0.05;

	u = (List* )malloc(sizeof(List));		
	srand(time(NULL));
	construct(u);
	constructMaze(u);
	createFeed();
		
	while( move(u,dir) ){
		if( kbhit() )
			c = getch();
		switch(c){
			case 'W':
			case 'w':
				dir = UP;
				break;
			case 'D':
			case 'd':
				dir = RIGHT;
				break;
			case 'S':
			case 's':
				dir = DOWN;
				break;
			case 'A':
			case 'a':
				dir = LEFT;
				break;
			default:
				break;
		}
		printboard(u);
		wait(timer);
		system("CLS");
	}

	printf("GAME OVER\n");
	printf("Your score is %d", score);

	getchar();
	return 0;
}

void createFeed(){
	int i;
	int j;
	do{
	i = rand()%WIDTH;
	j = rand()%LENGTH;
	}while( board[i][j]!=EMPTY );
	board[i][j] = FEED;
}

void printboard(List *u){
	int a= (u->head->data)/LENGTH;
	int b= (u->head->data)%LENGTH;
	board[a][b] = SNAKE;
	for(int i=0; i<WIDTH; i++){
		for( int j=0; j<LENGTH; j++){
			if(board[i][j] == WALL)
				printf("%c",219);
			if(board[i][j] == EMPTY)
				putchar(' ');
			if(board[i][j] == FEED)
				printf("O");
			if(board[i][j] == SNAKE)
				if( a==i && b==j){
					if(direction == RIGHT)
						printf(">");
					if(direction == LEFT)
						printf("<");
					if(direction == UP)
						printf("^");
					if(direction == DOWN)
						printf("V");

				}
				else
					printf("X");
			
		}printf("\n");
	}printf("\n");
	printf("Your score is %d", score);
}

void constructMaze(List *u){
	for(int i=0; i<WIDTH; i++)
		for( int j=0; j<LENGTH; j++)
			if( i==0 || i==WIDTH-1 || j==0 || j==LENGTH-1 )
				board[i][j] = WALL;
			else
				board[i][j] = EMPTY;
	
	board[3][5] = board[3][6] = board[3][7] = SNAKE;
	addHead(u, 3*LENGTH + 5);
	addHead(u, 3*LENGTH + 6);
	addHead(u, 3*LENGTH + 7);

	for(i=5; i<17; i++)
		board[5][i] = WALL;

	for(i=5; i<17; i++)
		board[9][i] = WALL;
	direction = RIGHT;	
	score = 0;
}

int move(List *u, int n){
	
	int i = (u->head->data)/LENGTH;
	int j = (u->head->data)%LENGTH;
	int k = (u->tail->data)/LENGTH;
	int m = (u->tail->data)%LENGTH;


	if(direction == DOWN && n==UP){
			move(u,DOWN);
			return 1;
	}

	if(direction == UP && n==DOWN){
		move(u,UP);
		return 1;
	}

	if(direction == LEFT && n==RIGHT){
		move(u,LEFT);
		return 1;
	}

	if(direction == RIGHT && n==LEFT){
		move(u,RIGHT);
		return 1;
	}

	if( n== UP){
		if( board[i-1][j] == WALL)
			return 0;
		if( board[i-1][j] == EMPTY){
			addHead(u, u->head->data - LENGTH);
 			deltail(u);
			board[k][m] = EMPTY;
			direction = UP;
			return 1;
		}
		if( board[i-1][j] == SNAKE )
			return 0;
		if( board[i-1][j] == FEED){
			addHead(u, u->head->data - LENGTH);
			direction = UP;
			createFeed();
			score++;
			return 1;
		}
	}

	if( n== DOWN){
		if( board[i+1][j] == WALL)
			return 0;
		if( board[i+1][j] == EMPTY){
			addHead(u, u->head->data + LENGTH);
			deltail(u);
			board[k][m] = EMPTY;
			direction = DOWN;
			return 1;
		}
		if( board[i+1][j] == SNAKE )
			return 0;
		if( board[i+1][j] == FEED){
			addHead(u, u->head->data + LENGTH);
			direction = DOWN;
			createFeed();
			score++;
			return 1;
		}		}

	if( n== RIGHT ){
		if( board[i][j+1] == WALL)
			return 0;
		if( board[i][j+1] == EMPTY){
			addHead(u, u->head->data +1);
			deltail(u);
			board[k][m] = EMPTY;
			direction = RIGHT;
			return 1;
		}
		if( board[i][j+1] == SNAKE )
			return 0;
		if( board[i][j+1] == FEED){
			addHead(u, u->head->data +1);
			direction = RIGHT;
			createFeed();
			score++;
			return 1;
		}
	}

	if( n== LEFT){
		if( board[i][j-1] == WALL)
			return 0;
		if( board[i][j-1] == EMPTY){
			addHead(u, u->head->data - 1);
			deltail(u);
			board[k][m] = EMPTY;
			direction = LEFT;
			return 1;
		}
		if( board[i][j-1] == SNAKE )
			return 0;
		if( board[i][j-1] == FEED){
			addHead(u, u->head->data - 1);
			direction = LEFT;
			createFeed();
			score++;
			return 1;
		}
	}
		
	return 1;

}

void wait ( float seconds ){
  float endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}
[/SOURCE]
[SOURCE]#include<stdio.h>
#include<stdlib.h>

struct node{
	   struct node *prev;
	   struct node *next;
	   int data;	   	   
};

typedef struct node Node;

struct list{
	   Node *head;
	   Node *tail;
	   int elementCount;	   	   
};

typedef struct list List;

void construct( List *u){
	 u->elementCount = 0;
	 u->head = NULL;
	 u->tail = NULL;	 
}

void printlist( List *u);
void addtail(List *u, int n);
void addHead(List *u, int n);
void deltail(List *u);


/*int main(){
    List *u;
	u = (List* )malloc(sizeof(List));
	construct (u); 
	addtail(u,1);
	addtail(u,11);
	addHead(u,12);
	addtail(u,13);
	
	printlist(u);
	
	getchar();
	return 0;
}*/

void printlist( List *u){
	 Node *temp;
	 temp = ( Node *)malloc(sizeof(Node));
	 if(u->elementCount){
            temp = u->head;
            while(temp){
                printf("%d\n", temp->data);
                temp = temp->next;
            }
     }else{
        printf("Thre is nothing to print");
     }
     free(temp);	 
} 

void addtail(List *u, int n){
    Node *temp;
    temp = (Node*)malloc(sizeof(Node));    
    temp->next = NULL;
    temp->prev = NULL;
    temp->data = n;
    if(!u->elementCount){
        u->head = temp;
        u->tail = temp;     
    }else{
        temp->prev = u->tail;
        u->tail->next = temp;
        u->tail = temp;
    }
    u->elementCount++;
}

void addHead(List *u, int n){
    Node *temp;
    temp = (Node*)malloc(sizeof(Node));    
    temp->next = NULL;
    temp->prev = NULL;
    temp->data = n;
    if(!u->elementCount){
        u->head = temp;
        u->tail = temp;     
    }else{
        temp-> next = u->head;
        u->head->prev = temp;
        u->head = temp;
    }
    u->elementCount++;
}

void deltail(List *u){
	Node *temp;
	temp =(Node*)malloc(sizeof(Node));
	if(u->elementCount>1){
		temp = u->tail;
		u->tail= u->tail->prev;
		u->tail->next = NULL;
		temp->prev = NULL;
	}
	if(u->elementCount == 1){
		temp =u->tail;
		u->tail = u->head = NULL;		
	}
	u->elementCount--;
	free(temp);
}









[/SOURCE]

Share this post


Link to post
Share on other sites

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this