ref: f16a708e5c6b95dd75bdf02d5678b19ec368a020
author: grey-S <63976509+grey-S@users.noreply.github.com>
date: Mon Apr 20 06:57:38 IDT 2020
Add files via upload
--- /dev/null
+++ b/config.h
@@ -1,0 +1,49 @@
+#include <unistd.h>
+#include <ncurses.h>
+#include <time.h>
+#include <stdbool.h>
+#include <stdlib.h>
+typedef struct point { int x , y ;}point;+typedef enum dir{left,right,up,down}dir;+
+#define TIMEOUT 3
+#define MAX_SNAKE 30000
+#define START_DIR right
+#define START_SIZE 5
+
+
+int FG = COLOR_YELLOW;
+int BG = COLOR_BLACK;
+int color_slow = COLOR_CYAN;
+int color_speed = COLOR_RED;
+
+bool game_over = false;
+
+const static bool teleport = true;
+const bool auto_color_change = true;
+
+const char snake_c[]=" ";
+const char food_c[]="G";
+const char head_char[]="N";;
+const char speed_head_char[]="X";
+const char slow_head_char[]="S";
+
+bool speed = false ;
+bool slow = false ;
+
+point snake[MAX_SNAKE];
+point food;
+dir current_dir = START_DIR;
+
+int max_x , max_y , snake_len = START_SIZE , score , highest_score,last_scores[10];
+int next_x , next_y;
+int delay = 40000;
+
+void draw_obj(point , const char[]);
+void create_food();
+void init_screen();
+void init_game();
+void shift_snake();
+void draw_all();
+bool loop();
+void pause_game();
--- /dev/null
+++ b/gsnake.c
@@ -1,0 +1,177 @@
+#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;}}
+
--
⑨