wm: gsnake

ref: c4b48d61620f9b3611a9a322e48670eb72ad6ad3
dir: /gsnake.c/

View raw version
#include "config.h"

void change_color(){
	int tmp = FG;
	FG = rand() % 7;
	while(FG == tmp ||  FG == BG){
		FG = rand() % 7;}}


void pause_game(){
	char c;
	while(c!='p'){
		c=getchar();}
}

void draw_obj(point p , const char c[]){
	mvprintw(p.y,p.x,c);}

void create_food(){
	food.x = (rand() % max_x);
	food.y = (rand() % max_y);}

void init_screen(){
	initscr();
	noecho();
	keypad(stdscr,TRUE);
	start_color();
	
	init_pair(1,FG,BG);
	attron(A_BOLD | A_STANDOUT | A_REVERSE | COLOR_PAIR(1));
	timeout(TIMEOUT);
	curs_set(0);
	cbreak();
	getmaxyx(stdscr,max_y,max_x);}

void init_game(){
	srand(time(0));
	int j = 0 ;

	for(int i = snake_len -1 ; i > 0 ; i--){
		snake[j].y = max_y /2;
		snake[j].x = i;
		j++;}

	create_food();
	refresh();}

void shift_snake(){
	point tmp = snake[snake_len -1];
	for(int i = snake_len -1 ; i > 0 ; i--){
			snake[i] = snake[i-1];}
	snake[0]=tmp;}

void draw_all(){
	clear();
	for(int i  = 0 ; i < snake_len ; i++){
		draw_obj(snake[i],snake_c);}
	if(speed){
	draw_obj(snake[snake_len -1 ],speed_head_char);}
	
	else if(slow){
		draw_obj(snake[snake_len -1],slow_head_char);}
	
	else{draw_obj(snake[snake_len -1],head_char);}

	attroff(A_STANDOUT | A_REVERSE);
	draw_obj(food,food_c);

	mvprintw(max_y -2 , (max_x /2)-5 , "score:%d",score);
	attron(A_STANDOUT | A_REVERSE);
	
	if(speed){
		init_pair(1,color_speed,COLOR_BLACK);	
		usleep(delay - 30000);}
	else if(slow){
		init_pair(1,color_slow,COLOR_BLACK);
		usleep(100000);}

	else{
	usleep(delay);
	init_pair(1,FG,BG);}
}
bool loop(){
	int x , y;
	int ch ;
	while(1){
		if(game_over){break;}
		getmaxyx(stdscr,max_y,max_x);
		if(x != max_x || y != max_y){
			create_food();
		}
		x = max_x ; y = max_y;
		ch=getch();
		srand(time(0));


		if((ch == KEY_LEFT || ch == 'a') &&
		(current_dir != left && current_dir != right))
		{current_dir = left;}
		
		else if((ch == KEY_RIGHT || ch == 'd') && 
		(current_dir != left && current_dir != right))
		{current_dir = right;}

		else if((ch == KEY_UP || ch == 'w') && 
		(current_dir != down && current_dir != up)){current_dir = up;}
		
		else if((ch == KEY_DOWN || ch == 's')  &&
		(current_dir != down && current_dir != up))
		{current_dir = down;}
		
		else if(ch == 'x'){
		if(speed){speed = false; slow = true;}
		
		else if(slow){slow = false;}
		else{speed = true;}}
		
		else if(ch =='p'){pause_game();}
		else if(ch == 'c'){change_color();}
		next_x = snake[0].x;
		next_y = snake[0].y;

		switch(current_dir){
			case right:next_x++;break;
			case left:next_x--;break;

			case up:next_y--;break;
			case down:next_y++;break;}
		

		if(next_x == food.x && next_y == food.y){
			snake[snake_len].x = food.x;
			snake[snake_len].y = food.y;
			
			snake_len++;
			if(speed){score += 10;}
			else if(slow){score += 2;}
			else{score += 5;}
			delay-= 400;
			
			if(auto_color_change){
			change_color();}

			create_food();
			}

		else{	
			for(int i = 0 ; i  < snake_len ; i++){
				if(next_x == snake[i].x && next_y == snake[i].y){
				game_over = true;}}
			snake[snake_len -1 ].x = next_x;
			snake[snake_len -1 ].y = next_y;}

		if(!teleport){
			if((next_x >= max_x || next_x <= 0) || (next_y >= max_y || next_y <=0)){
				game_over = true;}}

		else if(teleport){
			if(next_x >= max_x){timeout(0);snake[snake_len -1 ].x = 0;}
			else if(next_x < 0 ){timeout(0);snake[snake_len -1].x = max_x;}
			else if(next_y > max_y){timeout(0);snake[snake_len -1].y = 0;}
			else if(next_y < 0){timeout(0);snake[snake_len -1].y = max_y;}
			timeout(TIMEOUT);
		}
		draw_all();
		shift_snake();}
	return game_over;
}
int main(){
	init_screen();
	init_game();
	bool stat = loop();
	if(stat){
		endwin();
		printf("\e[1;31mGameOver:))\n%s : %d\n\e[0;m",getenv("USER"),score);
		return 0;}}