Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C language to realize gluttonous Snake

Shulou Source: shulou.com Published: 2022-06-01 19:11:22 09月15日 Update

Editor to share with you how to use C language to achieve the gluttonous Snake game, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!

First, realize the effect

Key position: use wasd four keys to control the direction, press Q key to exit (pay attention to the final use of English input method to achieve keying)

Rule: snakes get 10 points for every bean they eat, and their sides grow and move faster at the same time.

When the snake touches the wall or bites itself, the game ends and the game score is output.

Second, part of the code interpretation

(1) define snake and bean by structure.

Typedef struct Snakes {int x; int y; struct Snakes * next;} snake; snake * head,*tail; struct Food {int x; int y;} food

(2) print walls

Void creatgraph () {int i; for (I = 0; ix = 16; head- > y = 15; / gotoprint (head- > x, head- > y); tail = (snake*) malloc (sizeof (snake)); snake* p = (snake*) malloc (sizeof (snake)); snake* Q = (snake*) malloc (sizeof (snake)); p-> x = 16 P-> y = 16; Q-> x = 16; Q-> y = 17; head- > next = p; p-> next = Q; Q-> next = tail; / / gotoprint (p-> x, p-> y); / / gotoprint (Q-> x, Q-> y); tail- > next = NULL; tail- > x = 4; tail- > y = 2 } void gotoxy (int x, int y) {COORD pos; HANDLE hOutput; pos.X = x; pos.Y = y; hOutput = GetStdHandle (STD_OUTPUT_HANDLE); SetConsoleCursorPosition (hOutput, pos);} void gotoprint (int x, int y) {gotoxy (x, y); printf ("■");} void gotodelete (int x, int y) {gotoxy (x, y) Printf (");}

(3) growing beans.

Void creatfood () {srand ((int) time (NULL)); lable: food.y = rand ()% (25-1 + 1) + 1; food.x = rand ()% (54-2 + 1) + 2; if (food.x% 2! = 0) {food.x = food.x+1;} snake * judge = head While (1) {if (judge- > next = = NULL) break; if (food.x = = judge- > x&&food.y = = judge- > y) {goto lable;} judge = judge- > next;} gotoxy (food.x, food.y) Printf ("⊙");}

(4) Click the control function

Int ClickControl () {char c; while (1) {if (Judge () = = 0) return 0; if (_ kbhit ()) {click = _ getch ();} MovingBody (); Eating ();} return 1;}

(5) Mobility control

Void MovingBody () {int count = 0; int a = head- > x, b = head- > y; snake * p = head; while (1) {if (p-> next = = NULL) break; gotodelete (p-> x, p-> y); count++; p = p-> next } switch (click) {case up: head- > y-= 1; ChangeBody (ameme b); break; case down: head- > y + = 1; ChangeBody (ameme b) Break; case left: head- > x-= 2; ChangeBody (aformab); break; case right: head- > x + = 2; ChangeBody (arecom b) Break; case stop: _ getch (); break;} p = head; while (1) {if (p-> next = = NULL) break; gotoprint (p-> x, p-> y) P = p-> next;} p = head; gotoxy (0,28); if (count 10 & & count 20 & & count next; int mid1, mid2,_mid1,_mid2; mid1 = p-> x; mid2 = p-> y; while (1) {if (p-> next- > next = = NULL) break _ mid1 = p-> next- > x; _ mid2 = p-> next- > y; p-> next- > x = mid1; p-> next- > y = mid2; mid1 = _ mid1; mid2 = _ mid2; p = p-> next;} p = head- > next {p-> x = a; p-> y = b;}} these are all the contents of this article entitled "how to use C language to realize the Snake Game". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Articles controls language content at the same time walls keys up and down not much code functions most scores effects directions more knowledge structure industry rules Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple Shulou Information OPPO Reno Huawei