C Language Realization Snake source

Source: Internet
Author: User

First put effect

Source

2016-2-12//zhaoyu//gmail:[email protected]//language:c//platform:code::blocks#include <stdio.h>#    Include <windows.h> #include <stdlib.h> #include <time.h>typedef struct snake{int x;    int y; struct snake *next;} Snake;int X, Y;enum status{up = 1, down, left, right}; Snake *phead, *pbody;//the Head of the Snakeenum STATUS direction;int score=0, scoreperfood=10;int gamestatus = 0;int time    Interval = 200;void gameend (void); void setposition (int x, int y) {COORD pos;    HANDLE Houtput; Pos.    x = x; Pos.    y = y;    Houtput = GetStdHandle (Std_output_handle); SetConsoleCursorPosition (Houtput, POS);} void Hidecursor () {Console_cursor_info cursor_info = {1, 0}; Setconsolecursorinfo (GetStdHandle (std_output_handle), &cursor_info);}    void Creatinterface (void) {int i;        for (i=0;i<58;i+=2) {setposition (i,0);        printf ("¡ö");//a¡öoccupy, character Space SetPosition (i,26);    printf ("¡ö"); } for (i=1;i<26;i++) {        SetPosition (0,i);        printf ("¡ö");        SetPosition (56,i);    printf ("¡ö");    } setposition (65, 10);    printf ("Introduction");    SetPosition (63,12);    printf ("¡ü\tmove up.");    SetPosition (63,14);    printf ("¡ý\tmove down.");    SetPosition (63,16);    printf ("¡û\tmove left.");    SetPosition (63,18);    printf ("¡ú\tmove right.");    SetPosition (63,20);    printf ("F1\tspeed up.");    SetPosition (63, 22);    printf ("F2\tslow down.");    SetPosition (63, 24);    printf ("Space\tgame pause.");    SetPosition (63,26);    printf ("Esc\tquit the game.");    SetPosition (63, 4);    printf ("Score:");    SetPosition (63, 6); printf ("Score Per Food:");}    void Initializesnake (void) {Snake *ptail;    int i;    Ptail = (Snake *) malloc (sizeof (Snake));    Ptail->x = 24;    Ptail->y = 5;    Ptail->next = NULL;        for (i = 1; i < 4; i++) {Phead = (Snake *) malloc (sizeof (Snake));        Phead->next = Ptail;        Phead->x = + 2*i; Phead->y= 5;        Ptail = phead;//important} while (Ptail! = NULL) {setposition (ptail->x, ptail->y);        printf ("¡ö");    Ptail = ptail->next;    }}int biteself (void) {Snake *pself = phead->next; while (pself! = NULL) {if (phead->x==pself->x && phead->y==pself->y) {RE        Turn 1;    } pself = pself->next; } return 0;}    void Creatfood (void) {int flag = 0;    X = Y = 4;    Srand ((unsigned) time (NULL));        do{X = rand ()%52 + 2;        Y = rand ()%24 + 1;        if (x%2! = 0) {flag = 1;        } else {flag = 0;        }//important pbody = Phead;                while (pbody->next! = NULL) {if (pbody->x==x && pbody->y==y) {            flag = 1;        } pbody = pbody->next;    }}while (Flag==1);    SetPosition (X, Y); printf ("¡ö");} void Hitwall (void) {if (phead->x==0 | | phead->x>=56 | | phead->y==0 | |        phead->y>=26) {gamestatus = 1;    Gameend ();    }}void snakemove (void) {Snake *pnexthead;    Hitwall ();    Pnexthead = (Snake *) malloc (sizeof (Snake));    Pnexthead->next = Phead;        Switch (Direction) {Case up:pnexthead->x = phead->x;        Pnexthead->y = phead->y-1;    Break        Case down:pnexthead->x = phead->x;        Pnexthead->y = phead->y + 1;    Break        Case right:pnexthead->x = phead->x + 2;        Pnexthead->y = phead->y;    Break        Case left:pnexthead->x = phead->x-2;        Pnexthead->y = phead->y;    Break    Default:break;    } phead = Pnexthead;    Pbody = Phead; if (pnexthead->x = = x && pnexthead->y = = y) {while (pbody! = NULL) {Setpositio            N (pbody->x, pbody->y);            printf ("¡ö");  Pbody = pbody->next;      } score + = Scoreperfood;    Creatfood ();        } else {setposition (pbody->x, pbody->y);        printf ("¡ö");        while (pbody->next->next! = NULL) {pbody = pbody->next;        } setposition (Pbody->next->x, pbody->next->y);        printf ("");        Free (pbody->next);    Pbody->next = NULL;        } if (biteself () = = 1) {gamestatus = 2;    Gameend ();        }}void pause (void) {while (1) {Sleep (300);        if (Getasynckeystate (Vk_space)) {break;    }}}void gamecircle (void) {Direction = right;        while (1) {SetPosition (72, 4);        printf ("%d", score);        SetPosition (80, 6);        printf ("%d", scoreperfood);//attention space is needed if (Getasynckeystate (vk_up) && Direction!=down)        {Direction = up;        } else if (Getasynckeystate (vk_down) && direction!=up) {    Direction = down;        } else if (Getasynckeystate (vk_left) && direction!=right) {Direction = left;        } else if (Getasynckeystate (vk_right) && direction!=left) {Direction = right;        } else if (Getasynckeystate (vk_space)) {pause ();            } else if (Getasynckeystate (Vk_escape)) {gamestatus = 3;        Break } else if (Getasynckeystate (vk_f1)) {if (timeinterval >=) {Timei            Nterval-= 60;                }} else if (Getasynckeystate (VK_F2)) {if (TimeInterval <= 400) {            TimeInterval + = 60;            }} switch (timeinterval) {case 20:scoreperfood = 20;break;            Case 80:scoreperfood = 15;break;            Case 140:scoreperfood = 12;break;       Case 200:scoreperfood = 10;break;     Case 260:scoreperfood = 8;break;            Case 320:scoreperfood = 6;break;            Case 380:scoreperfood = 4;break;            Case 440:scoreperfood = 2;break;        Default:scoreperfood = 0;break;        } Sleep (TimeInterval);    Snakemove (); }}void welcomepage (void) {setposition (35,2);p rintf ("Welcome to Snake"), SetPosition (5);p rintf ("Rules:");  SetPosition (7);p rintf ("1. Use¡ü.¡ý.¡û.¡úto control the movement of the Snake. ");  SetPosition (9);p rintf ("2. Biting the snake itself is forbidden. ");  SetPosition ();p rintf ("3. Hits the wall is forbidden. ");    SetPosition ();p rintf ("Developeder:zhaoyu.");    SetPosition (15, 15);    printf ("blog:http://blog.csdn.net/sinat_30046339");    SetPosition (28, 23); printf ("Press any key to continue ..."); setposition (0); GetChar (); System ("CLS");    void Gameend (void) {System ("CLS");    SetPosition (32, 10);        Switch (gamestatus) {case 1:printf ("Your Hit the wall!"); Break    Case 2:printf ("You bit yourself!");    Break        Case 3:printf ("Your chose to end the game.");    Break    Default:break;    } setposition (32, 14);    printf ("Your Final score is%d", score);    GetChar ();    SetPosition (0, 25); Exit (0);} void Gamestart (void) {System ("mode con cols=100 lines=30");//no space around Equal Signwelcomepage (); Creatinterface (); Initializesnake (); Creatfood ();} Main Functionint main (void) {hidecursor (); Gamestart (); gamecircle (); Gameend (); return 0;}

  

C Language Realization Snake source

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.