In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use C language to achieve gluttonous snake game under Linux". In daily operation, I believe that many people have doubts about how to use C language to achieve gluttonous snake game under Linux. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt of "how to use C language to achieve gluttonous snake game under Linux". Next, please follow the editor to study!
Code presentation
The purpose of # include#include#define up 1 / / 1 and-1 is to use the abs () function to prevent the up and down # define down-1#define left 2#define right-2struct snake {/ / create a structure int hang; int lie; struct snake * next;}; struct snake * head = null; / / globally define a head and tail struct snake * tail= null;int key; / / define the int dir;// of a key to define the struct snake food of a direction Void initfood () {/ / define a food # # can randomly generate int x = rand (); int y = rand (); food.hang = x; food.lie = y;} void initncurse () {initscr (); keypad (stdscr,1); noecho ();} int hassnakenode (int I, int j) {/ / display snake body struct snake * p; p = head; while (p! = null) {if (p-> hang = = I & p-> lie = j) {return 1 } p = p-> next;} return 0;} int hasfood (int iGraint j) {/ / there is food if (food.hang = = I & & food.lie = = j) {return 1;} return 0;} void gamepic () {/ / Game graphical display int hang; int lie; move (0Magnum 0); for (hang=0;hanglie = tail- > lie; break; case down: new- > hang= tail- > hang+1; new- > lie = tail- > lie; break; case left: new- > hang= tail- > hang New- > lie = tail- > lie-1; break; case right: new- > hang = tail- > hang; new- > lie = tail- > lie+1; break;} tail- > next = new; tail = new;} void initsnake () {/ / initialize snake struct snake * p; dir = right; while (head! = null) {p = head; head = head- > next; free (p);} initfood (); head = (struct snake *) malloc (sizeof (struct snake)); head- > hang = 1; head- > lie = 1; head- > next = next Tail = head; addnode ();} void delenode () {/ / delete the last node struct snake * p; p = head; head = head- > next; free (p);} int ifsnakedie () {/ / output a 1 to resurrect struct snake * p when you hit the border and are late for yourself; p = head; if (tail- > hang)
< 0 || tail->Lie = = 0 | tail- > hang = = 20 | tail- > lie = = 20) {return 1;} while (p-> next! = null) {if (p-> hang = = tail- > hang & & p-> lie = = tail- > lie) {return 1;} p = p-> next;} return 0;} void movesnake () {/ / Snake movement addnode (); if (hasfood (tail- > hang,tail- > lie) {/ / do not delete the last node initfood () if you eat the food;} else {delenode () } if (ifsnakedie ()) {initsnake ();}} void refreshjiemian () {/ / Refresh interface thread while (1) {movesnake (); gamepic (); refresh (); usleep (150000); / / Refresh Frequency}} void turn (int direction) {/ / prevent if (abs (dir)! = abs (direction)) {dir = direction;} void changedir () {/ / change direction while (1) {key = getch () Switch (key) {case key_down: turn (down); break; case key_up: turn (up); break; case key_left: turn (left); break; case key_right: turn (right); break;}} int main () {pthread_t T1; / / define thread 1 pthread_t T2; initncurse (); / / initialize ncurse initsnake (); / / initialize snake gamepic (); / initialize interface pthread_create (& t1fly nullrefreshjiemian.null) / / start the function pthread_create in the thread (& T2 getch null); while (1); / / Thread 3 getch (); endwin (); return 0;}
Compile the code
Gcc snake.c-lcurses-lpthread
Generate a.out run file
Run the code
. / a.out
Picture display
At this point, the study of "how to use C language to realize the gluttonous Snake game under Linux" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.