Simple snake in VC Environment

Source: Internet
Author: User

In the afternoon, I was bored and did not know what to do. I rummaged through the code I had previously written. I accidentally saw this simple greedy snake I wrote. I decided to send it to my blog after I had played a few tricks. (Boring, pass the time)

Note: It is fully based on the VC ++ 6.0 environment and does not support running Terminal in Linux. In addition, there is basically no code style, and a lot of code is written in the main () function. The switch function is used to implement two levels. One is to hit the wall over, of course, the other one can penetrate the wall. Code reuse should not be mentioned. Because the two levels are very similar, the code is basically the same. Because they are just playing, there is no public call interface. Alas, looking at the previous code, it seems that style is nonsense.


"Start text 」:

When the code starts, it is actually used to install force, set the user name, the Code finally restarts the game, set the speed (implemented by sleep time), and so on, and prepare for future modification, perfect, but now I feel that I have no time to play these things, and I have never changed it.

Since I have basically written comments and English notes for printing in the Code, the general idea of code implementation is that the snake body is implemented based on a two-way linked list, the snake's movement is to read the value of the key from the buffer in a non-blocking manner, so that the control direction (kbhit () function can detect whether the keyboard is pressed, read data from the buffer in a non-blocking manner), and the snake moves by detecting the position of the used snake body, through screen Flushing

(System ("CLS");) to continuously print the position of the snake body, so as to achieve the effect of snake Movement (when moving, you must ensure that the direction of the snake body is consistent with that of the Snake Head ). In fact, everything is the defined array.

Int A [20] [20] = {0}; // defines the layout range size

The key to control is the character style (marked) at each position in the array. The snake header prints the object box, And the snake body is a virtual body box, food is a random English. Every time the Snake Head and food are in the same coordinate, a new node is created and added to the chain table of the snake body. The logo of the game's end is actually to detect the coordinate relationship between the Snake Head and the snake body, as well as the coordinate relationship with the edge to determine whether it is self-defeating or hitting the wall.

「 No more nonsense 」

Code:

<Head. h> struct node {int X; int y; // data type of the snake node position, struct node * pre; struct node * Next; // bidirectional linked list, consistency of snake body movement control}; struct food {int X; int y; char C; // data type of the food seeds .};


This is two simple struct. The overall implementation is actually based on a two-way linked list.

The header file in the win environment is required:


#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <time.h>            //key for snake food !#include <conio.h>#include "head.h" 

Due to the comparison, the following implementation code is in the main function, and I am too lazy to change it now. Let's take a look.

Int main (INT argc, char * argv []) {int A [20] [20] = {0}; // defines the layout range. Char user_name [15]; int I, j, flag = 0; int speed; // control of the running speed of the snake, implemented by sleep (), is actually the intermittent speed of the screen .. Char c = 'D', C1 = 'D'; // initialize char key in the direction of motion; char speed_key, user_key, stage_key; struct food = {9, 15 ,'*'}; // The initial position of the food is int stage; // select the level int gameover = 0; // key data struct node * head, * P, * rear, * PT; int score; printf ("************************ <! Careful!> * *************************** \ N "); puts ("\ n <*> * <*> every time when you touch foods and you will get five points! <*> \ N when you want to stop at once, please press 'T' \ n \ NWhen you want to have a pause, please press '0' \ n "); puts (" Direction Control help: ** <up: W> <domn: S> <left: A> <right: d> ** \ n "); printf ("************************************* * *** \ n "); printf ("Please input the speed of this snake (between 0 ~~ 300): "); scanf (" % d ", & speed); printf (" \ n \ nplease input your user_name: "); scanf (" % s ", user_name); Key = 'y'; printf ("\ n \ nplease choose which stage to start game (input the int value bellow :)? \ N [(* 0 *): without the wall to stop game, (* 1 *): With the wall to stop your touch] \ n "); flushall (); scanf ("% d", & stage); While (Key = 'y') {score = 0; head = (struct node *) malloc (sizeof (struct node); head-> X = 10; head-> Y = 10; // the initial position of the Snake head node head-> pre = NULL; head-> next = NULL; Rear = head; srand (unsigned) Time (null); // The generation of Random Functions: [Food seed] switch (stage) {Case 0: While (1) {If (food. X = head-> X & food. y = head-> Y) {P = (struct node *) m Alloc (sizeof (struct node); // generates a long time segment. PT = head; while (Pt-> next! = NULL) Pt = Pt-> next; // locate the position where the snake grows. PT is the tail pointer p-> pre = pt; Pt-> next = P; p-> next = NULL; Rear = P; // The Last pointer score + = 5; // The score increases by food. X = rand () % 20; // random location algorithm generated by food. y = rand () % 20; food. C = 65 + rand () % 26; flag = 1;} Pt = rear; If (kbhit () // key point: [check whether the keyboard is pressed .. Enter any character to continue, or the program can only be executed once, that is, playing {// kbhit () can ensure that the characters can be read from the buffer continuously in a non-blocking manner during the loop .. C1 = getch (); // clear the cache and read one character from the buffer.] If (C1 = '0') {system ("pause ");} if (C1 = 'T') {exit (0);} If (C! = 'D' & C1 = 'A') C = C1; else if (C! = 'A' & C1 = 'D') // make sure that the game ends when the snake goes forward in a straight line and presses the key opposite to the straight line .. C = C1; else if (C! = 'W' & C1 = 's') C = C1; else if (C! = 'S' & C1 = 'W') C = C1;} while (PT! = Head) {// controls the movement of each snake body to be consistent with that of the head. Pt-> X = Pt-> pre-> X; pt-> Y = Pt-> pre-> Y; Pt = Pt-> pre;} If (C = 'D ') // direction control {head-> Y + = 1; if (Head-> Y> = 20) Head-> Y-= 20 ;} else if (C = 'A') {head-> Y-= 1; if (Head-> Y <0) Head-> Y + = 20 ;} else if (C = 'W') {head-> X-= 1; if (Head-> x <0) Head-> X + = 20 ;} else if (C ='s ') {head-> X + = 1; if (Head-> x> = 20) Head-> X-= 20 ;} PT = head-> next; while (PT! = NULL) {If (Head-> X = Pt-> X & head-> Y = Pt-> Y) {gameover = 1; // exit the game break when the snake head and body match;} Pt = Pt-> next;} If (gameover = 1) break; System ("CLS "); // clear the screen each time... Printf ("************************ <! Game start!> * *************************** \ N "); printf ("Your user_name: % s \ n \ nthe current speed is % d \ n", user_name, speed ); printf ("the current stage is % d \ n", stage ); printf ("************************************* * *** \ n "); for (I = 0; I <20; I ++) // print the start of the loop, which is also the visual layout of the game {printf ("|"); For (j = 0; j <20; j ++) {flag = 0; Pt = head; while (PT! = NULL) {if (I = Pt-> X & J = Pt-> Y) {If (Pt = head) printf ("■ "); // when Pt = head, set the title in that position, elseprintf ("□"); // either flag = 1; break ;} PT = Pt-> next; // keep searching until the end of the snake ends printing} If (flag = 0) {if (I = food. X & J = food. y) {putchar (food. c); // if this position is the position of food, the characters of food are printed; otherwise, spaces are printed (square layout must be filled ). Putchar (food. c); continue; // execute the next loop judgment} printf (""); // print the space if it is not a snake.} printf ("| \ n ");} printf ("************************************* * *** \ n "); _ sleep (speed);} break; Case 1: While (1) {If (food. X = head-> X & food. y = head-> Y) {P = (struct node *) malloc (sizeof (struct node); // generates a long time segment. PT = head; while (Pt-> next! = NULL) Pt = Pt-> next; // locate the position where the snake grows. PT is the tail pointer p-> pre = pt; Pt-> next = P; p-> next = NULL; Rear = P; // end pointer score + = 5; // score food. X = rand () % 20; // random location algorithm generated by food. y = rand () % 20; food. C = 65 + rand () % 26; flag = 1;} Pt = rear; If (kbhit () // key point: [check whether the keyboard is pressed .. Enter any character to continue, or the program can only be executed once, that is, playing {// kbhit () can ensure that the characters can be read from the buffer continuously in a non-blocking manner during the loop .. C1 = getch (); // clear the cache and read one character from the buffer.] If (C1 = '0') {system ("pause ");} if (C1 = 'T') {exit (0);} If (C! = 'D' & C1 = 'A') C = C1; else if (C! = 'A' & C1 = 'D') // make sure that the game ends when the snake goes forward in a straight line and presses the key opposite to the straight line .. C = C1; else if (C! = 'W' & C1 = 's') C = C1; else if (C! = 'S' & C1 = 'W') C = C1;} while (PT! = Head) {// controls the movement of each snake body to be consistent with that of the head. Pt-> X = Pt-> pre-> X; pt-> Y = Pt-> pre-> Y; Pt = Pt-> pre;} If (C = 'D ') // direction control {head-> Y + = 1; if (Head-> Y> = 20) {gameover = 1; break ;}} else if (C = 'A') {head-> Y-= 1; if (Head-> Y <0) {gameover = 1; break ;}} else if (C = 'W') {head-> X-= 1; if (Head-> x <0) {gameover = 1; break ;}} else if (C ='s ') {head-> X + = 1; if (Head-> x> = 20) {gameover = 1; break ;}} PT = head-> next; while (PT! = NULL) {If (Head-> X = Pt-> X & head-> Y = Pt-> Y) {gameover = 1; // exit the game break when the snake head and body match;} Pt = Pt-> next;} If (gameover = 1) break; System ("CLS "); // clear the screen each time... Printf ("************************ <! Game start!> * *************************** \ N "); printf ("Your user_name: % s \ n \ nthe current speed is % d \ n", user_name, speed ); printf ("************************************* * *** \ n "); for (I = 0; I <20; I ++) {printf ("|"); For (j = 0; j <20; j ++) {flag = 0; Pt = head; while (PT! = NULL) {if (I = Pt-> X & J = Pt-> Y) {If (Pt = head) printf ("■ "); // when Pt = head, set else printf ("□"); // either flag = 1; break ;} PT = Pt-> next; // keep searching until the end of the snake ends printing} If (flag = 0) {if (I = food. X & J = food. y) {putchar (food. c); // if this position is the position of food, the characters of food are printed; otherwise, spaces are printed (square layout must be filled ). Putchar (food. c); continue; // execute the next loop judgment} printf (""); // print the space if it is not a snake.} printf ("| \ n ");} printf ("************************************* * *** \ n "); _ sleep (speed);} break;} If (gameover = 1) {puts ("************************* now the game over!" * *************************** \ N ******* *! <_>! * ****** \ N "); printf (" \ n your score is: % d \ n ", score ); puts ("if you want to continue? (Y/n) \ n "); flushall (); scanf (" % C ", & Key); If (Key = 'y') {gameover = 0; flushall (); printf ("\ NIF you want to change speed? (Y/N): "); scanf (" % C ", & speed_key); If (speed_key = 'y') {printf (" input speed value: \ n "); scanf (" % d ", & speed) ;}elseprintf (" \ n "); flushall (); printf (" \ NIF you want to change user_name? (Y/N): "); scanf (" % C ", & user_key); If (user_key = 'y') {flushall (); printf ("input new user_name: \ n"); scanf ("% s", user_name);} elseprintf ("\ n"); flushall (); printf ("\ n if you want to change the stage? (Y/N): "); scanf (" % C ", & stage_key); If (stage_key = 'y') {flushall (); printf ("\ nplease input the stage value:"); scanf ("% d", & stage);} elseprintf ("\ n"); continue ;} else exit (0) ;}} return 0 ;}


Write it here. Alas, the Code style is terrible.

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.