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 > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how Linux uses curses graphics library to achieve a gluttonous snake game. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Install the curses graphics library command under ubuntu
Sudo apt-get install libncurses5-dev
Dual buff is a very excellent mechanism. When writing about Snake, the screen beats violently if you don't use dual buff, and the experience is very good after using dual buff.
The same is true for using the curses graphics library, where the screen will not be updated until the refresh () function is called.
For example, the following code
# include # include # include int main () {initscr (); / * We move the cursor to the point (5Power15) on the logical screen, print "Hello World" and refresh the actual screen. Lastly, we use the call sleep (2) to suspend the program for two seconds, so we can see the output before the program ends. * / move (5,15); addstr ("Hello World"); refresh (); sleep (2); endwin (); exit (EXIT_SUCCESS);}
Compile and run using the following command
Gcc-o t screen1.c-lncurses & &. / t
First initialize a screen, then move to the screen's 5pm 15 position, in the output string Hello World. After dormant for 2 seconds, the program exits.
Using curses to write Snake Code
/ / sudo apt-get install libncurses5-dev / / gcc-o t tanchishe.c-lncurses & &. / t # include / / Linux graphic library # include / / usleep () # include / / rand () # include / / time () # define W 40 # define H 24 int m [W * H], Q [W * H], p = H / 2 * W + (W / 2), a, h = 0, t = 0, d = 1, I Int main (void) {initscr (); noecho (); keypad (stdscr, 1); nodelay (stdscr, 1); curs_set (0); srand (time (NULL)); for (I = 0; I
< W * H; i++) m[i] = !(i / W % (H - 1) && i % W % (W - 1)); m[q[t = (t + 1) % (W * H)] = p] = 1; do { a = rand() % (W * H); } while (m[a]); while ((i = getch()) != 27) { if (i == KEY_UP && d != W) d = -W; else if (i == KEY_DOWN && d != -W) d = W; else if (i == KEY_LEFT && d != 1) d = -1; else if (i == KEY_RIGHT && d != -1) d = 1; if (m[p += d]) break; m[q[t = (t + 1) % (W * H)] = p] = 1; if (p == a) do { a = rand() % (W * H); } while (m[a]); else m[q[h = (h + 1) % (W * H)]] = 0; for (i = 0; i < W * H; i++) mvaddstr(i / W, (i % W) * 2, m[i] ? "[]" : " "); mvaddstr(a / W, (a % W) * 2, "()"); refresh(); usleep(100000); } while (getch() == ERR); endwin(); } 程序运行 简单解释下 for (i = 0; i < W * H; i++) mvaddstr(i / W, (i % W) * 2, m[i] ? "[]" : " "); 构建边框和蛇身的代码,边框是用 [] 构建的,用这个字符从视觉上看会比较舒服。 mvaddstr(a / W, (a % W) * 2, "()"); 随机生成的食物,之前已经用时间srand(time(NULL));作为种子设置了随机数。 if (m[p += d]) break; 碰撞检测 if (p == a) do { a = rand() % (W * H); } while (m[a]); else m[q[h = (h + 1) % (W * H)]] = 0; 如果碰撞到了食物,就增加蛇长度,m[]里面同时保存蛇的数据和边框的数据,并且蛇移动的时候,需要把后面的数值设置为0。 p = H / 2 * W + (W / 2) 蛇的初始位置The code modified by yourself can achieve the effect of passing through the wall.
/ / sudo apt-get install libncurses5-dev / / gcc-o t tanchishe.c-lncurses & &. / t # include / / Linux graphic library # include / / usleep () # include / / rand () # include / / time () # define W 40 # define H 24 int m [W * H], Q [W * H], p = H / 2 * W + (W / 2), a, h = 0, t = 0, d = 1, iBook jigsaw 3 Int main (void) {initscr (); noecho (); keypad (stdscr, 1); nodelay (stdscr, 1); curs_set (0); srand (time (NULL)); for (I = 0; I < W * H; iHI +) m [I] =! (I / W% (H-1) & & I% W% (W-1)); m [Q [t = (t + 1)% (W * H)] = p] = 1 Do {a = rand ()% (W * H);} while (m [a]); while ((I = getch ())! = 27) {if (I = = KEY_UP & & d! = W) {d =-else if (I = = KEY_DOWN & d! =-W) } else if (I = = KEY_LEFT & & d! = 1) {d =-1 jig2;} else if (I = = KEY_RIGHT & & d! =-1) {d = 1 jung3;} if (m [p + = d]) {switch (j) {case 0VOR p = p + (HMU2) * W; break Case 1 break; default p = p-(Hmura2) * W; break; case 2 case 2; break; case 3 Vuitu p = pMaiWmur2; break; default: P = H / 2 * W + (W / 2); break;}} M [Q [t = (t + 1)% (W * H)] = p] = 1; if (p = a) do {a = rand ()% (W * H);} while (m [a]); else m [Q [h = (h + 1)% (W * H)] = 0; for (I = 0; I < W * H) Mvaddstr (I / W, (I% W) * 2, m [I]? "[]": ");} mvaddstr (a / W, (a% W) * 2," () "); refresh (); usleep (100000);} while (getch () = = ERR); endwin ();}
The operation is as follows:
About Linux in how to use curses graphics library to achieve a gluttonous snake game to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.