After writing the snake based on the curses library in Linux, I found that only a win API (moving the cursor) would be used to rewrite the move (int y, int x) function, and then add the Getch () of the conio.h (the generic compiler will contain the header file), You can run it on your Windows system!
Linux version link: http://blog.csdn.net/u013351484/article/details/43940803
Game rules:
Initially two snakes will be in the middle, a snake head is ' @ ', the food is ' $ ', the other one is ' + ', the food is ' * ', the snake Body is ' # '. WASD control ' + ' snake direction, direction key control ' @ ' snake direction, spacebar to pause the game, then press once to continue the game. Each snake can only eat its own food. Hitting the wall, touching yourself or another snake's body, eating another snake's food will end the game.
Operating Environment: Windows system
Compiler: VC + +, G++,GCC, VS
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include < time.h> #define KEY_UP 72#define key_down 80#define key_left 75#define key_right 77#define LINES #define COL S 70int dir_y[] = {-1, 1, 0, 0};int dir_x[] = {0, 0,-1, 1};int vis[100][100];int scord;typedef struct body{int y, x; struct body *pre, *sub;} Body;typedef struct{body *head, *tail;int direction;int head_x;int head_y;int food_x;int FOOD_Y;char food;char H;} Snake;int get_rand_number (int k) {static int first_time = 0;if (!first_time) {first_time = 1;srand ((unsigned int) (Time ( NULL)));} return rand ()%k + 1;} void move (int y, int x)//move cursor position {COORD C; C.x=x; C.y=y; SetConsoleCursorPosition (GetStdHandle (Std_output_handle), c); }void Add (Snake *snake) {Body *s;s = (Body *) malloc (sizeof (body)); s->y = Snake->head_y;s->x = snake->head_x; S->pre = Null;s->sub = Snake->head;snake->head->pre = S;snake->head = s;} void Free (Body *head) {if (NULL = = head) return; Free (head->sub), free (head);} void initialization (Snake *snake, int y, int x) {snake->direction = 0;int &food_y = Snake->food_y;int &FOOD_X = Snake->food_x;snake->head_y = y;snake->head_x = X;while (1) {food_y = Get_rand_number (LINES-3); food_x = Get_rand_number (COLS-3), if (!vis[food_y][food_x]) {vis[food_y][food_x] = 2;break;}} Move (food_y, food_x);p rintf ("%c", snake->food), Snake->head = (Body *) malloc (sizeof (body));snake->head-> y = y;snake->head->x = X;snake->head->pre = Snake->head->sub = Null;snake->tail = snake->head; Move (y, x);p rintf ("%c", Snake->h);} void New_food (Snake *snake) {scord++;int &food_y = snake->food_y;int &food_x = snake->food_x;while (1) { food_y = Get_rand_number (LINES-2); food_x = Get_rand_number (COLS-2), if (!vis[food_y][food_x]) {vis[food_y][food_x] = 2;break;}} Move (food_y, food_x);p rintf ("%c", Snake->food);} int reverse_direction (Snake *snake, int fy, int FX) {int &head_y = Snake->head_y;int &head_x = snake->head_x; Body *&head = snake->head; Head_y + = dir_y[snake->direction]; head_x + = dir_x[snake->direction];if (head_y = = FY && head_x = = FX) return 1;if (head->sub && head_y = = Head->sub->y && head_x = = head->sub->x) {if (head_y = = head->y) {if (head_x > Head->x) head_x = Head->x-1;else head_x = head->x + 1;} Else{if (head_y > Head->y) head_y = Head->y-1;else head_y = head->y + 1;}} return 0;} void Move (Snake *snake) {int &food_y = Snake->food_y;int &food_x = Snake->food_x;int &head_y = Snake-> ; Head_y;int &head_x = snake->head_x; Body *&head = snake->head; Body *&tail = Snake->tail;move (head_y, head_x);p rintf ("%c", snake->h); vis[head_y][head_x] = 1;if (head-> Sub) {Move (head->sub->y, head->sub->x);p rintf ("#");} if (head_y = = Food_y && head_x = = food_x) {printf ("%c", 7); New_food (snake);} Else{move (Tail->y, Tail->x);p rintf (""); Vis[tail->y][tail->x] = 0;if (tail->pre) {Body *tmp = Tail;tail = tail-> Pre;tail->sub = Null;free (TMP);}}} void Box () {move (0, 0), int x,y;for (y=0; y<lines; y++) {for (x=0; x<cols; x + +) {if (0 = = X | | COLS-1 = = X | | 0 = = Y | | LINES-1 = = y) {printf ("#"); vis[y][x] = 1;} else printf ("");} printf ("\ n");}} int Judge_kill (Snake *snake) {if (1 = = vis[snake->head_y][snake->head_x]) return 1; return 0;} int main () {Box (); Snake snake1, Snake2;snake1.food = ' $ '; snake2.food = ' * '; snake1. H = ' @ '; snake2. H = ' + '; int init1_y = Lines/2;int init1_x = cols/2-1;int init2_y = Lines/2;int init2_x = cols/2 + 1;vis[init1_y] [init1_x] = vis[init2_y][init2_x] = 1;//initializes the position and food location of the two snakes initialization (&snake1, init1_y, init1_x); initialization ( &snake2, init2_y, init2_x); int key = 0;int Pause = 0;key = Getch (); while (1) {if (Kbhit ()! = 0)////checks whether there is currently a keyboard input, and returns a non-0 value if any , otherwise returns 0 {while (kbhit ()! = 0)//There may be multiple keys, to be all taken out, to the last primary key = Getch ();} if (key_up = = KEY)Snake1. DIRECTION = 0;else if (Key_down = = KEY) snake1. DIRECTION = 1;else if (key_left = = KEY) snake1. DIRECTION = 2;else if (key_right = = KEY) snake1. DIRECTION = 3;else if (' w ' = = key) Snake2. DIRECTION = 0;else if (' s ' = = key) Snake2. DIRECTION = 1;else if (' a ' = = key) Snake2. DIRECTION = 2;else if (' d ' = = key) Snake2. DIRECTION = 3;else if (' = = key ') {//Pause while ((key = Getch ()) = "); key = 0;} int over = 0, Over1 = 0, Over2 = 0;//determines where to move next and whether to eat food to another snake Over1 = reverse_direction (&snake1, Snake2. Food_y, Snake2. food_x); over2 = Reverse_direction (&snake2, Snake1. Food_y, Snake1. food_x);//Judgment Game over if (Judge_kill (&snake1) | | Judge_kill (&snake2) | | Over1 | | Over2) {free (snake1.head); Free (snake2.head); Add a node Add (&SNAKE1) for the respective list; ADD (&snake2);//Mobile Snake Move (&snake1); Move (&SNAKE2); Sleep (250);} Move (LINES, 0); Sleep (+); return 0;}
C-Language Snake (conio.h)