C-language Snake (Curses library function)

Source: Internet
Author: User

The initial purpose of college learning programming is directed at the game ~ but in just learning C language, completely unable to use the knowledge learned to make a playable game ╮ (╯_╰) ╭, after 1 years of learning is still the simplest greedy snake no idea (of course, not to say no code, but there is no way to make dynamic things, And then went to the algorithm that go ~), until now sophomore, the use of winter vacation time to get started Linux, the discovery of the curses library function! When I see the role of it, outraged, TM this thing is not my dream to achieve the interface of the Dongdong! When I learned a little bit, I realized that using a move () and PRINTW () function would completely write the level of the snake ...


Since you want to write to write you like to play the ~ a snake is too monotonous not to play the value, get two is just a bit challenging!

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, each snake can only eat their own food. Hitting the wall, touching yourself or another snake's body, eating another snake's food will end the game.


Operating environment: Linux operating system, please confirm that the curses library is installed, no, please enter sudo apt-get libncurses5-dev installation in the terminal (bad transplant ~ pity)

Compile directives: For example, Save as SNAKE.C on the terminal type: g++-o snake snake.c-lcurses

Execution instructions: in terminal type:./snake


Ideas:

How do you make a snake? I think it is more convenient to use double-linked lists, and each node records the current coordinate of the snake body and the successor of the precursor. In this way, when the snake moves, add a node before the head node, move () to the snake head coordinates, print the snake head, and then, if you do not have food to eat, delete the tail knot, and move () to the tail of the snake to print a null value. After ~ finished (high school Chinese are passing low points drifting over, plus lazy, can only write so little ... )


Feeling:

Alas ~ Want to think, actually move hands to still have a bit of trouble. The main problem is the keyboard response, because it is just contact curses library, learning is still very shallow. Or that sentence, the language is too poor, even the problem does not know how to describe ... Anyway is pressing the next direction key does not let go, after a period of time release, and then in another direction, it will first move in the direction of the previous, until after moving in other directions. The more slowly you move one step, the more obvious this phenomenon is. It means that I don't know how to ignore the extra input getch () in the time of moving one step. Another egg-ache is that I have found an objective thing in countless attempts: C does not support reference parameters for functions. I'm an egg! I have countless times in the terminal input GCC in the beginning of the lovely sentence to find the problem, the results can be imagined. originally wanted to add points, and later found that two snake words, do not know how to count the fairness, alas ~ also need a little bit of math ... the last thing to say is that many times in programming, we are not lack of ability, but lack of tools. (Like this example: I know a little C language, but I do not understand the curses library, I can not do anything), so not to say proficient, but at least to skillfully use a language, and then to find the right tool, programming is not what difficult situation. Write here, suddenly reminds me, also reminds you, learn programming, basic and practice is very important, anyway must not hurry! Believe others can do, you can!


PS: This is just finished version, bug many ~ I think in the process of continuing to learn curses to add vitality to the program, so it is not so boring. Although the curses library is text-based graphic programming, it can be as good as the game. Lay the groundwork for QT first ...


Finally, the characters in the CSND blog show that there is something wrong ...

Reprint Please specify source:http://blog.csdn.net/u013351484?viewmode=contents, thank you!


#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <curses.h> #include < Time.h>int 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;//random function 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 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);} int Judge_kill (int y, int x) {if (y = = LINES-1 | | x = = COLS-1 | | y = = 0 | | x = 0) return 1;if (1 = = Vis[y][x]) return 1; return 0;} 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 rintw ("%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 rintw ("%c", Snake. H); Refresh ();} 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-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 rintw ("%c", Snake.food);} void Reverse_direction (Snake &snake) {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];//The snake length is not 1 o'clock, the next move is opposite to the snake movement 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;}}} int Move (Snake &snake, int fy, int fx) {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;//output snake head move (head_y, head_x);p rintw ("%c", Snake. H); vis[head_y][head_x] = 1;//If there is a snake body, the output if (head->sub) {Move (head->sub->y, head->sub->x);p rintw ("#");} if (head_y = = Food_y && head_x = = food_x) New_food (snake);//Eat food else if (head_y = = fy && head_x = FX) retur N 1;//eats the food of another snake else{//nothing to eat to move (Tail->y, tail->x);p rintw (""); Vis[tail->y][tail->x] = 0;free (tail); if (Tail->pre) {tail = Tail->pre;tail->sub = NULL;}} return 0;} int main () {INITSCR (); curs_set (0);//Hide physical Pointer noecho ();//Do not echo input box (STDSCR, ' | ', '-');//Frame snake snake1, Snake2;snake1.food = ' $ '; snake2.food = ' * '; snake1. H = ' @ '; snake2. H = ' + ';//Two snakes in the initial position (small snake in the middle of the more comfortable) int init1_y = Lines/2;int init1_x = cols/2-1;int init2_y = lines/2;int init2_x = COL S/2 + 1;vis[init1_y][init1_x] = vis[init2_y][init2_x] = 1;//Initialize two snake position with food location initialization (snake1, init1_y, init1_x); I Nitialization (Snake2, init2_y, init2_x); keypad (STDSCR, true);//open keyboard function key int key = 0;key = Getch (); while (1) {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;//Gets the coordinates of the forward movement and determines whether it is opposite to the movement of the snake (a snake with a length not 1 in motion//is a direction that cannot walk) reverse_direction (SNAKE1); Reverse_direction (snake2);//Determine if the game is over if (Judge_kill (snake1. Head_y, Snake1. head_x) | | Judge_kill (Snake2. Head_y, SNake2. head_x)) {//release memory, very important free (snake1.head); Free (snake2.head); Add the Snake Body Add (snake1) to the respective list; ADD (snake2);//Move the snake and judge whether to eat another snake's food int over1 = 0, Over2 = 0;over1 = Move (Snake1, Snake2. Food_y, Snake2. food_x); over2 = Move (Snake2, Snake1. Food_y, Snake1. food_x); if (Over1 | | over2) Break;refresh (); Usleep (150*1000);//stay 150 milliseconds if (ERR! = Halfdelay (1)) key = Getch ();// Wait for input within 100 milliseconds, such as no input, execute}sleep (3), Endwin (); exit (0);








C-language Snake (Curses library function)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.