Jump to content
  • Advertisement
Sign in to follow this  
ksintath

Snake game in C

This topic is 4456 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[j]!=EMPTY );
	board[j] = FEED;
}

void printboard(List *u){
	int a= (u->head->data)/LENGTH;
	int b= (u->head->data)%LENGTH;
	board[a] = SNAKE;
	for(int i=0; i<WIDTH; i++){
		for( int j=0; j<LENGTH; j++){
			if(board[j] == WALL)
				printf("%c",219);
			if(board[j] == EMPTY)
				putchar(' ');
			if(board[j] == FEED)
				printf("O");
			if(board[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[j] = WALL;
			else
				board[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] = WALL;

	for(i=5; i<17; i++)
		board[9] = 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[j+1] == WALL)
			return 0;
		if( board[j+1] == EMPTY){
			addHead(u, u->head->data +1);
			deltail(u);
			board[k][m] = EMPTY;
			direction = RIGHT;
			return 1;
		}
		if( board[j+1] == SNAKE )
			return 0;
		if( board[j+1] == FEED){
			addHead(u, u->head->data +1);
			direction = RIGHT;
			createFeed();
			score++;
			return 1;
		}
	}

	if( n== LEFT){
		if( board[j-1] == WALL)
			return 0;
		if( board[j-1] == EMPTY){
			addHead(u, u->head->data - 1);
			deltail(u);
			board[k][m] = EMPTY;
			direction = LEFT;
			return 1;
		}
		if( board[j-1] == SNAKE )
			return 0;
		if( board[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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!