Greedy snake source program

zhaozj2021-02-16  53

This program is for greedy snake games, I want everyone to play this game, and the program source code is compiled with TC2.0, and the support of the graphics driver file is required, there is in the integrated environment of the TC2.0.

This program uses the linked list in the data structure to connect the snake body while the snake will automatically upgrade when the snake eats a certain number, and the mobile speed will speed up, and some information is displayed on the screen, including the resulting score, How many things to eat can be upgraded, and the player can manually select the game level, the higher the level, the faster the speed of the snake. In addition, this game may be related to the speed of the CPU.

The source code is as follows: ************************************************************ ********************************** / ************** *************************************************** *** // * snake_game.c

* // * it is a game for entermainment.

* // * in the beginning, there is only a snake head, and it will help to eat food * // * to Become Stronger, And it eat a piece of food each time, it will * // * Length it's body, WITHS GoING UP, IT * / / / * WIING BECOME Long More and More, And The Score Will Goes Up Also. * // * There is always. * // * iv The path by which the snake goes to eat food is the shortest, the * // * score will add up a double.

* // *

* // * Enjoy yourself, and anyproblem, contact * // *************************************** *************************************************** /

/ * All Head File That Will BE Used * / # include #include #include #include #include #include

/ * Useful MACRO * / # define FOOD_SIZE 8 # define SCALE 8 # define UP_KEY 0x4800 # define DOWN_KEY 0x5000 # define LEFT_KEY 0x4b00 # define RIGHT_KEY 0x4d00 # define MOVE_UP 1 # define MOVE_LEFT 2 # define MOVE_DOWN 3 # define MOVE_RIGHT 4 # define INVALID_DIRECTION 0 #define QUIT_KEYC 0x1051 # define QUIT_KEY 0x1071 # define SELECT_KEYC 0x1f53 # define SELECT_KEY 0x1f73 # define PAUSE_KEYC 0x1950 # define PAUSE_KEY 0x1970 # define DEFAULT_LEVEL 1 # define HELP_COLOR WHITE # define WELCOME_COLOR WHITE # define DEFAULT_COLOR GREEN / * define the macro as follows to improve the Game in future * / # define food_color yellow # define snake_head_color red # define default_snake_color yellow # define exit_color white # define score_color yellow

/ * Sturcture for snake body mainly, and food also * / typedef struct food_infor * FOOD_INFOR_PTR; typedef struct food_infor {int posx; / * position for each piece of snake body * / int posy; int next_move; / * next move direction * / int pre_move; / * previous move direction, seems unuseful * / int beEaten; / * xidentifier for snake body or food * / FOOD_INFOR_PTR next; / * pointer to next piece of snake body * / FOOD_INFOR_PTR pre; / * pointer to previous piece of Snake body * /} food_infor;

/ * Structure for snake head * / typedef struct _snake_head {int posx; int posy; int next_move; int pre_move; int eatenC; / * number of food that have been eaten * / int hop; / * number of steps to eat food * / FOOD_INFOR_PTR next; / * POINTER TO THE FIRST PIECE OF Eaten Food * /} Snake_HEAD;

/ * The Left-Up Corner And Right-Down Corner * / Typef Struct Point {Int x; Int Y;} POINT;

/ * Standards for Game Speed

* // * Before Level 5, The Time Interval IS Level_B [Level - 1] / 10, Andreat * // * Level 5, The Time Interval IS 1.00 / Level_B [Level - 1] * / float level_b [9] = {10.0,8.0,6.0,3.0,1.0,20.0,40.0,160.0,640.0}; / * available varary * / sNAKE_HEAD snake_head; FOOD_INFOR * current; / * always point to food * / POINT border_LT, border_RB; int driver, mode ; / * For graphics driver * / int maxx, maxy; / * max length and width of screen, in pixel * / int eaten; / * identifier if the food is eaten * / int score = 0; / * Total score * / int level = DEFAULT_LEVEL; / * level or speed * / float interval; / * based on speed * / int snake_color = DEFAULT_SNAKE_COLOR; / * snake body color * / int hopcount = 0; / * the shortest number of steps for snake * / / * to eat food * /

/ * All sub function * / void init_graphics (); void generate_first_step (); int judge_death (); int willeatfood (); void generate_food (); void addonefood (); void redrawsnake (); void show_all (); void sort_all ( ); void change_direction (); void help (); void show_score (int); void change_level (); void show_level (); void release (SNAKE_HEAD); int can_promote (); void win (); void show_infor_to_level (); void Show_eaten (); void cagculate_hop ();

/ * main function or entry * / void main () {char str [50] = "you lose !!!"; / * fail information * / clock_t start; int querykey; int tempx, tempy;

/ * if Fail and Want to Resume Game, Go Here * / Retry: init_graphics (); show_all (); / * show wall * / generate_first_step (); / * generate food and snake head * / show_score (score); / * Show score to player * / eaten = 0;

/ * Begin to Play Game * / while (1) {if (Judge_Death () == 1) / * DIE * / BREAK;

IF (WilleATFOOD () == 1) {Eaten = 1; addonefood (); snake_head.hop ;

IF (snake_head.hop == hopcount) score = level * 2; Else Score = level;

CAN_promote ();

Show_score (score);

}

sort_all ();

Redrawsnake ();

Snake_Head.hop ;

Show_infor_to_level ();

SHOW_EATEN ();

SHOW_ALL ();

Change_direction ();

IF (eaten == 1) {generate_food ();

Calculate_hop (); snake_head.hop = 0;

Eaten = 0;}

IF (Level <= 5) Interval = level_b [level - 1] / 10.0; else interval = 1.00 / level_b [level - 1]; start = clock (); while ((clock () - start) / clk_tck

DEFAULT: BREAK;}}}}

SetTextStyle (Default_Font, 0, 2); setColor (exit_color);

Tempx = border_lt.x (border_rb.x - border_lt.x - textwidth (STR)) / 2; tempy = border_lt.y (border_rb.y - border_lt.y) / 2; OutTextxy (Tempx, Tempy, Str);

STRCPY (STR, "Press to Retry, or Enter Key To EXIT");

TEMPY = TextHeight (STR) * 2; setTextStyle (default_font, 0,1); tempx = border_lt.x (border_rb.x - border_lt.x - textwidth (STR)) / 2; OutTextxy (Tempx, Tempy, STR) SELECT:

While (! Bioskey (1));

QueryKey = BIOSKEY (0);

IF ((QueryKey == 0x1372) || (queryy == 0x1352)) {level = default_level; score = 0;

Release (Snake_Head);

Closegraph (); goto retry;

}

IF (QueryKey! = 0x1c0d) goto select;

Closegraph (); Return;

EXIT_GAME: Release (snake_head); closegraph ();

}

/ * SUB FUNCTION SHOW_EAN () * // * Function: To show the total number application of food * // * That Have been eaten by snake any time * / void show_eaten () {int tempx, tempy; int size; void * BUF; char STR [15];

SetTextStyle (default_font, 0,1); setColor (default_color);

Sprintf (STR, "Eaten:% D", Snake_Head.eatenc);

Tempx = 0; Tempy = Border_lt.y textHeight (STR) * 6;

Size = imagesize (Tempx, Tempx Textwidth (STR) TextWidth ("a"), Tempy TextHeight (STR); BUF = Malloc (size); GetImage (Tempx, Tempy, Tempx TextWidth (STR) TextWidth ("a"), Tempy TextHeight (STR), BUF); PUTIMAGE (Tempx, Tempy, BUF, XOR_PUT); OUTTEXTXY (Tempx, Tempy, Str);

Free (BUF);

}

/ * SUB FUNCTION: SHOW_INFOR_TO_LEVEL * / / * Function: Show Information To Player That How Many Pieces * /// f food to been eaten to get to next level * /// *, and this is not related with score, but only * // * Eaten Number Of Food

* // *

* // * Level Standard: let highlevel standard for the number of * // * Pieces of food That Can Be Put INT the * // * Vertical Direction of Play Area, and * // * Before Level 5, As long as the Snake eat * // * a quarter of highlevel, it will go to next * /// * Level, And Between Level 5 and 7, as long * // * as the snake eat one thirds of highlevel, * // * IT Will Go to Next Level, And Between * /// * Level 8 and 9, The Snake Will Go to Next * /// * Level As Long As It Eat Half of HighLevel * // * Note: Level IS Between 1 to 9. * / Void show_infor_to_level () {int highlevel; int code; int Tempx, Tempy; int toeat; void * buf; char str [50]; highlevel = (border_rb.y - border_lt.y) / scale;

Switch (Level) {Case 1: Case 2: Case 3: Case 4: TOEAT = (HighLevel / 4) * Level - Snake_Head.eatenc; Break;

Case 5: Case 6: Case 7: TOEAT = (Highlevel HighLevel / 3 * (Level - 4)) - Snake_Head.eatenc; Break;

Case 8: Case 9: TOEAT = (Highlevel * 2 HighLevel / 2 * (Level - 7)) -

Snake_head.eatenc;

DEFAULT: BREAK;

SetTextStyle (default_font, 0,1); setColor (default_color);

IF (snake_head.next == null) {sprintf (STR, "Next Level");

Tempx = 0; TEMPY = Border_lt.y textHeight (STR) * 2;

Outtextxy (Tempx, Tempy, Str);

IF (TOEAT <0) TOEAT = 0;

Sprintf (STR, "% D:% D", Level 1, TOEAT; TEMPX = 0; Tempy = Border_lt.y TextHeight (STR) * 4; size = imagesize (Tempx, Tempy, Tempx TextWidth (STR) TEXTWIDTH ("a"), TEMPY

TextHeight (STR); buf = malloc (size); GetImage (Tempx, Tempx TextWidth (STR) TextWidth ("A"), TEMPY

TextHeight (STR), BUF; PUTIMAGE (Tempx, Tempy, BUF, XOR_PUT); OUTTEXTXY (Tempx, Tempy, Str); Free (BUF);}

/ * SUB FUNCTION: WIN () * // * Function: if The Player Pass Level 9, this function * // * Will be called, to show "you win information * // * and after a key ispessed, the game Will Go * // * ON, But All is Back to begin, Excepte the * // * snake body length.

* / void win () {char str [] = "you win"; int Tempx, Tempy;

SetTextStyle (Default_Font, 0, 8); setColor (Welcome_color);

Tempx = border_lt.x (border_rb.x - border_lt.x - textwidth (STR)) / 2; tempy = border_lt.y (border_rb.y - border_lt.y - textheight (str)) / 2;

OutTextxy (Tempx, Tempy, Str);

While (! Bioskey (1));

}

/ * SUB FUNCTION: CAN_PROMOTE () * // * function: See eti the snake can go to next level on basis * //// of the snake length.

* // *

* // * NOTE: Standards of promote level is instruction above * / int CAN_promote () {/ * compare score with standard level score * /

INT high_level; static int last_score = 0;

HIGH_LEVEL = (border_rb.y - border_lt.y) / scale;

Switch (Level) {Case 1: Case 2: Case 3: Case 4: IF (Snake_Head.eatenc == (High_level / 4 * Level)) Level ; Last_Score = Score; Break;

Case 5: Case 6: Case 7: IF (Snake_Head.eatenc == (High_LEVEL HIGH_LEVEL / 3 * (Level - 4)))] Level ; Last_Score = Score; Break;

Case 8: IF (Snake_Head.eatenc == (High_level * 2 High_level / 2)) Level ; Last_Score = Score; Break;

Case 9: IF (Snake_Head.eatenc == (High_level * 3)) {WIN ();

Score = 0; last_score = 0; Level = default_level;

} Break;

DEFAULT: BREAK;

Show_level ();

/ * Sub function: calulate_hop () * // * function:. Calculate the shortest path from snake head to * // * the food it will eaten * / void calculate_hop () {hopcount = (snake_head.posx> = current-> POSX)? (Snake_Head.Posx - Current-> POSX) / scale): (Current-> POSX - Snake_Head.POSX) / scale); HopCount = (Snake_Head.Posy> = Current-> Posy)? ((( Snake_head.posy - current-> posy) /

Scale): ((Current-> Posy - Snake_Head.Posy) / scale);

}

/ * SUB FUNCTION: Release ()

* // * function: free memory * / void release (snake_head snake_head) {food_infor_ptr traceon, last;

Traceon = snake_head.next; snake_head.eatenc = 0; snake_head.next = null; snake_head.hop = 0;

While (traceon) if (traceon-> next! = null) traceon = traceon-> next; else break; while (traceon) {Last = traceon-> pre; free (traceon); traceon = last;}

}

/ * SUB FUNCTION: Show_level () x * // * function: show level information to player anytime * / void show_level () {char STR [20]; int size; void * buf;

SetTextStyle (default_font, 0,1); setColor (default_color);

Sprintf (STR, "Level:% D", Level); size = imagesize (0, border_lt.y, textwidth, border_lt.y textheight (str)); buf = malloc (size); getImage (0, Border_lt.y, Textwidth (STR), Border_lt.y TextHeight (Str), BUF); PUTIMAGE (0, Border_Lt.y, BUF, XOR_PUT);

Free (BUF);

Outtextxy (0, border_lt.y, str);

/ * SUB FUNCTION: CHANGE_LEVEL () * // * function: if The Play Choose "SELECT Level " Item, * // * This function will be called * / void change_level () {int C; int size Void * buf; int Tempx, Tempy; char str [] = "new level (1--9):";

SetTextStyle (DEFAULT_FONT, 0, 1); setColor (default_color); tempx = 0; tempx = border_lt.y - textHeight ("a") * 3/2; OutTextxy (Tempx, Tempy, Str); Goon: While (! Bioskey (1)); c = bioskey (0); IF ((c == 0x1051) || (c == 0x1071)) goto exit; if (isdigit (c & 0x00f)) level = (C & 0x00FF) - 48; Else Goto Goon ; exit: size = imagesize (tempx, tempy, tempx textwidth (str), tempy textheight (str)); buf = malloc (size); getimage (tempx, tempy, tempx textwidth (str), tempy textheight ( STR), BUF); PUTIMAGE (Tempx, Tempy, BUF, XOR_PUT); Free (BUF);

}

/ * SUB FUNCTION: Show_Score () * // * function: show score information to player anytime * / void show_score (int count) {int th; int size; void * buf; char STR [20];

SetTextStyle (Default_Font, 0, 2); setColor (score_color);

Sprintf (STR, "Score:% D", Count; TH = TextHeight ("Hello");

IF ((count == 0) && (snake_head.next == null) {OutTextxy (border_lt.x (border_rb.x - border_lt.x) / 4, border_lt.y - 2 * th, str);} Else {Size = imagesize (border_lt.x (border_rb.x - border_lt.x) / 4, border_lt.y - 2 * th, border_lt.x (border_rb.x - border_lt.x) / 4

TextWidth (STR) TEXTWIDTH ("100"), Border_lt.y - 2 * TH TH); BUF = Malloc (size); GetImage (border_lt.x (border_rb.x - border_lt.x) / 4, border_lt. Y - 2 * TH, Border_lt.x (border_rb.x - border_lt.x) / 4 TextWidth (STR)

TextWidth, Border_Lt.Y - 2 * TH TH, BUF); PUTIMAGE (Border_Lt.x (Border_RB.x - Border_lt.x) / 4, Border_lt.y - 2 * TH, BUF, XOR_PUT) ; Outtextxy (border_lt.x (border_rb.x - border_lt.x) / 4, border_lt.y - 2 * th, str); free (buf);}}

/ * SUB FUNCTION: HELP ()

* // * Function: Show help information at the beginning of game * //////////////////////// char STR [100]; int th;

SetTextStyle (Default_Font, 0, 1);

SetColor (help_color);

TH = TEXTHEIGHT ("Hello");

Sprintf (STR, "Move Left:% C", 27); Outtextxy (Border_lt.x, Border_RB.Y, STR);

Sprintf (STR, "Move Up:% C", 24); OUTTEXTXY (Border_Lt.x (border_rb.x - border_lt.x) / 2, Border_RB.Y, STR);

Sprintf (STR, "Move Down:% C", 25); OUTTEXTXY (Border_lt.x, Border_RB.Y TH 2, STR);

Sprintf (STR, "Move Right:% C", 26); OUTTEXTXY (border_lt.x (border_rb.x - border_lt.x) / 2, Border_RB.Y TH 2, STR);

Outtextxy (border_lt.x, border_rb.y tH * 2 4, "quit "); OUTTEXTXY (Border_lt.x textwidth ("Quit ") * 3/2, Border_RB. Y TH * 2 4, "Pause

"); Outtextxy (border_lt.x (border_rb.x - border_lt.x) / 2, border_rb.y tH * 2 4, "SELECT Level < S / S> ");

}

/ * SUB FUNCTION: SHOW_ALL ()

* // * function: Redraw the Play Area, Means Show wall * / void show_all () {

INT I, J;

SetColor (Default_Color); / * for (i = border_lt.x; i <= border_rb.x; i = scale) for (j = border_lt.y; j <= border_rb.y; j = scale) Rectangle (i , J, I Scale, J Scale); * /

Rectangle (border_lt.x, border_lt.y, border_rb.x, border_rb.y);} / * sub fullction: generate_food ()

* // * function: after the food is eaten by snake, the function will * /// _ be called to generate Another food, and it will * // * EnSure That the generated food shouldn't APPEARE * / / * in the Snake body.

* / void generate_food () {food_infor_ptr traceon; int tempx, tempy;

Generate: current-> pOSX = random (border_rb.x - scale / 2); while (current-> poSX <= border_lt.x) || ((current-> poosx - border_lt.x)% scale == 0) || (Current-> POSX - Border_lt.x)% scale% (Scale / 2)! = 0)) Current-> POSX ;

Current-> Posy = Random (border_rb.y - scale / 2); while ((current-> Posy <= border_lt.y) || ((current-> Posy - border_lt.y)% scale == 0) || ((Current-> Posy - Border_lt.y)% scale% (scale / 2)! = 0)) Current-> Posy ;

Traceon = snake_head.next; while (traceon) {i ((traceon-> poSX == current-> posx) && (traceon-> posy == current-> Posy)) goto generate; traceon = traceon-> next;}

IF (current-> poSX - border_lt.x == scale / 2) Current-> POSX = Scale; if (border_rb.x - current-> poSX == scale / 2) Current-> POSX - = scale; if ( Current-> Posy - border_lt.y == scale / 2) Current-> POSY = Scale; if (border_rb.y - current-> Posy == scale / 2) Current-> Posy - = scale;

SetColor (Default_color);

Rectangle (current-> POSX - Scale / 2, Current-> Posy - Scale / 2, Current-> POSX Scale / 2, Current-> Posy Scale / 2); setFillStyle (Solid_Fill, Yellow); FloodFill (Current- > POSX, Current-> Posy, Default_color;

/ * SUB Function: init_graphics ()

* // * function: Initialize the game interface * / void init_graphics () {driver = detect; mode = 0;

INITGRAPH (& Driver, & Mode, "*. BGI"); MAXX = getmaxx (); maxy = getMaxy ();

Border_lt.x = maxx / scale; border_lt.y = maxy / scale;

Border_rb.x = maxx * (scale - 1) / scale; border_rb.y = maxy * (scale - 1) / scale;

While (border_rb.x - border_lt.x)% food_size) (border_rb.x) ; while ((border_rb.y - border_lt.y)% food_size) (border_rb.y) ;

While (Border_RB.Y - Border_lt.y)% (12 * scale)) Border_rb.y = scale;

SetColor (Default_color);

Rectangle (border_lt.x, border_lt.y, border_rb.x, border_rb.y);

Help ();

Show_level ();

}

/ * Sub function: generateX_first_step () * // * function: generate snake head and first food to prepare for * // * game to start, and this function will also initialize * // * the move direction of snake head, and show Welcome * // * Information to Player.

* / void generate_first_step () {char str [] = "Welcome to Snake Game, Press Enter Key to Start"; int size; int Tempx, Tempy; Void * buf; randomize ();

/ * generate snake head * / snake_head.posx = random (border_rb.x - scale / 2); while ((snake_head.posx <= border_lt.x) || ((Snake_Head.Posx - Border_lt.x)% scale == 0)

|| ((Snake_Head.posx - Border_lt.x)% scale% (Scale / 2)! = 0)) Snake_Head.POSX ;

Snake_head.posy = random (border_rb.y - scale / 2); while ((snake_head.posy <= border_lt.y) || ((Snake_Head.Posy - Border_lt.y)% Scale == 0) || (((Snake_Head) .posy - border_lt.y)% scale% (Scale / 2)! = 0)) Snake_Head.Posy ;

SetColor (Default_color);

rectangle (snake_head.posx - SCALE / 2, snake_head.posy - SCALE / 2, snake_head.posx SCALE / 2, snake_head.posy SCALE / 2); setfillstyle (SOLID_FILL, SNAKE_HEAD_COLOR); floodfill (snake_head.posx, snake_head. Posy, default_color;

/ * Generate first food * /

Current = (Food_INFOR_PTR) Malloc (SIZEOF (Food_INFOR));

Goon_Generate:

Current-> POSX = Random (border_rb.x - scale / 2); while (current-> poSX <= border_lt.x) || ((current-> poosx - border_lt.x)% scale == 0) || (Current-> POSX - BORDER_LT.X)% scale% (scale / 2)! = 0)) Current-> POSX ;

Current-> Posy = Random (border_rb.y - scale / 2); while ((current-> Posy <= border_lt.y) || ((current-> Posy - border_lt.y)% scale == 0) || ((Current-> Posy - Border_lt.y)% scale% (scale / 2)! = 0)) Current-> Posy ;

IF ((Current-> POSX == Snake_Head.posx) && (current-> Posy == snake_head.posy)) goto goon_generate;

Rectangle (Current-> POSX - Scale / 2, Current-> Posy - Scale / 2, Current-> POSX Scale / 2, Current-> Posy Scale / 2); setFillStyle (Solid_Fill, Food_Color); floodfill (Current- > POSX, CURRENT-> POSY, Default_color;

Calculate_hop ();

Snake_Head.next = NULL; Snake_Head.eatenc = 0; snake_head.hop = 0;

current-> next = NULL; current-> pre = NULL; current-> beEaten = 0; current-> next_move = INVALID_DIRECTION; current-> pre_move = INVALID_DIRECTION; if (snake_head.posx == current-> posx) {if ( snake_head.posy> current-> posy) snake_head.next_move = mOVE_UP; else snake_head.next_move = mOVE_DOWN;} else {if (snake_head.posx posx) snake_head.next_move = MOVE_RIGHT; else snake_head.next_move = MOVE_LEFT;}

Snake_head.pre_move = snake_head.next_move;

SetTextStyle (default_font, 0, 1); setColor (Welcome_color);

Tempx = border_lt.x (border_rb.x - border_lt.x - textwidth (STR)) / 2; tempy = border_lt.y - textheight ("a") * 6/2;

OutTextxy (Tempx, Tempy, Str);

size = imagesize (tempx, tempy, tempx textwidth (str), tempy textheight (str)); buf = malloc (size); getimage (tempx, tempy, tempx textwidth (str), tempy textheight (str), BUF); while (bioskey (0)! = 0x1c0d);

PUTIMAGE (Tempx, Tempy, BUF, XOR_PUT); Free (BUF);

}

/ * SUB FUNCTION: JUDGE_DEATH ()

* // * function: Judge if the snake will die because, there is, there is, there is the will result * // * the snake to death: first, it run keto the wall * /// *, and and Second, IT Run Into ITS Body.

* / int Judge_death () {/ * return 1 means will die, and return 0 means will survive * /

Int Tempx, Tempy;

switch (snake_head.next_move) {case MOVE_UP: tempx = snake_head.posx; tempy = snake_head.posy - SCALE; break; case MOVE_LEFT: tempx = snake_head.posx - SCALE; tempy = snake_head.posy; break; case MOVE_DOWN: tempx = snake_head.posx; tempy = snake_head.posy SCALE; break; case MOVE_RIGHT: tempx = snake_head.posx SCALE; tempy = snake_head.posy; break; default: break;} if ((tempx border_rb.x) || (TEMPY BORDER_RB.Y)) Return 1;

IF (getPixel (Tempx, Tempy) == snake_color) {Food_infor_ptr traceon; traceon = snake_head.next; while (traceon! = null) {= ((traceon-> posx == Tempx) && (traceon-> Posy == TEMPY )) RETURN 1;

Traceon = traceon-> next;}}

Return 0; / * survive * /

}

/ * SUB FUNCTION: WILLEATFOOD () * // * function: Judge if the Sanke can * /// * this: provided That the snake move a step based * // * on its next move direction, and IF this place * // * Have food, Then the snake can eat food. * / int willeatfood () {/ * 1 means will eat food, and 0 means won't eat food * /

Int Tempx, Tempy;

switch (snake_head.next_move) {case MOVE_UP: tempx = snake_head.posx; tempy = snake_head.posy - SCALE; break; case MOVE_LEFT: tempx = snake_head.posx - SCALE; tempy = snake_head.posy; break; case MOVE_DOWN: tempx = Snake_head.posx; tempy = snake_head.posy scale; break; casser_right: tempx = snake_head.posx scale; tempy = snake_head.posy; break; default: Break;

IF (getPixel (Tempx, Tempy) == Food_Color) Return 1;

Return 0;}

/ * Sub function: addonefood () * // * function: this function will lengthen the snake body * // * this function is important because it will * // * not only locate memory for new snake body, * // * but Also Handle The Relationship of Pointer * // * Between The New Snake Body and ITS Previous * // * snake body.

* / void addonefood () {Food_infor_ptr traceon;

Snake_Head.eatenc ; traceon = snake_head.next;

IF (Snake_Head.Next == NULL) / * Haven't Eaten Any Food * / {Traceon = (Food_INFOR_PTR) Malloc (Sizeof (Food_INFOR));

switch (snake_head.next_move) {case MOVE_UP: traceon-> posx = snake_head.posx; traceon-> posy = snake_head.posy SCALE; break; case MOVE_LEFT: traceon-> posx = snake_head.posx SCALE; traceon-> posy = snake_head.posy; break; case mOVE_DOWN: traceon-> posx = snake_head.posx; traceon-> posy = snake_head.posy - SCALE; break; case MOVE_RIGHT: traceon-> posx = snake_head.posx - SCALE; traceon-> posy = Snake_Head.Posy; Break; default: BREAK;

Traceon-> next_move = snake_head.next_move; traceon-> pre_move = snake_head.next = null; traceon-> pre = null; traceon-> beeaten = 1;

Snake_head.next = traceon;} else {while (traceon-> next! = null) traceon = traceon-> next; else break;

Traceon-> Next = (Food_INFOR_PTR) Malloc (SIZEOF (Food_INFOR));

Traceon-> Next-> Next = null; traceon-> next-> pre = traceon; traceon = traceon-> next

Switch (traceon-> pre-> next_move) {copy move_up: traceon-> poSX = traceon-> pre-> POSX; traceon-> Posy = traale; break; casser_left: traceon-> POSX = Traceon-> pre-> POSX Scale; Traceon-> Posy = Traceon-> pre-> Posy; Break; Case Move_Down: Traceon-> POSX = traceon-> pre-> POSX; traceon-> Posy = traceon- > pre-> Posy - Scale; Break; Case Move_right: Traceon-> POSX = Traceon-> pre-> POSX - Scale; Traceon-> Posy = Traceon-> pre-> Posy; Break; default: Break;} traceon- > next_move = traceon-> pre-> prep_move = traceon-> pre-> next_move; traceon-> beeaten = 1;}

}

/ * SUB FUNCTION: SORT_ALL ()

* // * Function: this function Will Calculate the next position of snake * /// * and it is associan the snake has move to next position, but * // * Haven't APPEARED YET.

* / void sort_all () {/ * Sort All Food, include snake head, and virtual place * /

Food_infor_ptr traceon; void * buf; int size;

SIZE = ImageSize (snake_head.posx - scale / 2, snake_head.posy - scale / 2, snake_head.posx scale / 2, snake_head.posy scale /

2); buf = malloc (size); getImage (snake_head.posx - scale / 2, snake_head.posy - scale / 2, snake_head.posx scale / 2, snake_head.posy scale / 2, buf); PUTIMAGE (SNAKE_HEAD .posx - scale / 2, snake_head.posy - scale / 2, buf, xor_put;

switch (snake_head.next_move) {case MOVE_UP: snake_head.posy - = SCALE; break; case MOVE_LEFT: snake_head.posx - = SCALE; break; case MOVE_DOWN: snake_head.posy = SCALE; break; case MOVE_RIGHT: snake_head.posx = Scale; Break; default: Break;} traceon = snake_head.next; while (traceon) {getImage (traale / 2, traceon-> Posy - scale / 2, traceon-> POSX Scale / 2, Traceon-> Posy Scale / 2, BUF); PUTIMAGE (Traceon-> POSX - Scale / 2, Traceon-> Posy - Scale / 2, BUF, XOR_PUT);

Switch (traceon-> next_move) {Case Move_up: Traceon-> Posy - = Scale; Break; Case Move_LEFT: TRACEON-> POSX - = Scale; Break; Case Move_Down: Traceon-> Posy = Scale; Break; Case Move_right: Traceon-> POSX = Scale; Break; Default: Break;} traceon = traceon-> next;

Free (BUF);

}

/ * SUB FUNCTION: Redrawsnake ()

* // * function: The function will redraw the snake based on function * /// * sort_all ().

* / void redrawsnake () {Food_infor_ptr traceon;

setcolor (DEFAULT_COLOR); / * redraw snake head * / setfillstyle (SOLID_FILL, SNAKE_HEAD_COLOR); rectangle (snake_head.posx - SCALE / 2, snake_head.posy - SCALE / 2, snake_head.posx SCALE / 2, snake_head.posy SCALE / 2); floodfill (snake_head.posx, snake_head.posy, default_color);

/ * Redraw all eaten foodd * / setfillstyle (Solid_fill, snake_color); traceon = snake_head.next;

While (traceon) {Rectangle (Traceon-> POSX - Scale / 2, Traceon-> Posy - Scale / 2, Traceon-> POSX Scale / 2, Traceon-> Posy Scale / 2); FloodFill (Traceon-> POSX , Traceon-> Posy, default_color; traceon = traceon-> next;}

}

/ * Sub function: change_direction () * // * function: this function will assign the next move direction * // * of snake, it calculate on basis of the information * // * of snake body structure * / void change_direction (. ) {FOOD_INFOR_PTR TRACEON, TEMP1, TEMP2;

Traceon = snake_head.next;

/ * if (traceon == null) * / / * Only Have Snake Head * / snake_head.pre_move = snake_head.next_move;

IF (traceon! = null) {while (traceon) {if (traceon-> Next! = null) Traceon = traceon-> next; else break;} while (traceon-> pre! = null) {traceon-> pre_move = traceon-> next_move; traceon-> next_move = traceon-> pre-> next_move; / *** / traceon = traceon-> pre;} traceon-> pre_move = traceon-> next_move; traceon-> next_move = snake_head.pre_move;

}

}

/ * SOCRE CODE Ended * /// * Date: Jul 2th, 01 * / ************************************************* *********************************************************** ********** Complete programs and source code and related files can be downloaded at http://blldw.myetang.com/c/snake.zip

转载请注明原文地址:https://www.9cbs.com/read-26833.html

New Post(0)