(C语言基础版)贪吃蛇-支持滚键盘

来源:互联网 发布:玉溪香烟软件 编辑:程序博客网 时间:2024/04/29 06:58

对于一个刚学C不久的人,最好自己尝试写写像成绩管理系统,贪吃蛇这些简单的小项目,来提升自己的代码水平

不多说了,下面就直接上代码了

#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <time.h>#include <conio.h>#include <string.h>#include <stdbool.h>#define N 100int x[N],y[N]; //蛇的身体int xi,yi; //食物的坐标int score[4]= {0}; //得分  (前三名和本局游戏的得分)int len; //蛇的长度bool exist_food; //当前是否存在食物bool fag; //食物是否与蛇身重合void InitSnake(){    x[0]=20;    y[0]=10;    score[3]=0;    len=1;    exist_food=0;}void food() // 生成食物   x坐标为偶数{    int i;    fag=0;    srand((unsigned)time( NULL ));    xi=rand()%29+1;    xi*=2;    yi=rand()%20+1;    for(i=0; i<len; i++)    {        if(xi==x[i]&&yi==y[i])            fag=1; //1表示重合    }    if(fag==0)    {        gotoxy(xi,yi);        printf("*");    }    else food();}void create_window()//创建窗口{    printf(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │  得分: 0     │\n");    printf("│                                                           │              │\n");    printf("│                                                           │  按键说明:  │\n");    printf("│                                                           │  W A S D     │\n");    printf("│                                                           │  控制移动    │\n");    printf("│                                                           │              │\n");    printf("│                                                           │  退出:Esc键 │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf("│                                                           │              │\n");    printf(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");}void gotoxy(int x, int y)//定位光标,x为行坐标,y为列坐标{    COORD pos = {x,y};    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleCursorPosition(hOut, pos);}void hidden()//隐藏光标{    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);    CONSOLE_CURSOR_INFO cci;    GetConsoleCursorInfo(hOut,&cci);    cci.bVisible=0;//赋1为显示,赋0为隐藏    SetConsoleCursorInfo(hOut,&cci);}bool run() //游戏过程{    x[0]=20,y[0]=10; //初始蛇头的位置    int i;    char m,n;    while(1)    {        if(exist_food==0)        {            food();            exist_food=1;        }        if(x[0]==xi&&y[0]==yi)        {            exist_food=0;            score[3]++;            len++;            gotoxy(71,3);            printf("%3d",score[3]);        }        for(i=len-1; i>0; i--)        {            x[i]=x[i-1];            y[i]=y[i-1];        }        if(kbhit())            n=getch();        if(n=='w'||n=='a'||n=='s'||n=='d'||n==27)        {            if((n=='w'&&m!='s')||(n=='a'&&m!='d')||(n=='s'&&m!='w')||(n=='d'&&m!='a')||n==27)                m=n;        }        switch (m)        {        case 'w':            y[0]-=1;            break;        case 's':            y[0]+=1;            break;        case 'a':    // 纵坐标之间的距离大约是横坐标之间的距离的两倍            x[0]-=2;            break;        case 'd':            x[0]+=2;            break;        case 27:            return false;        }        if(x[0]>60)            x[0]=2;        if(x[0]<2)            x[0]=60;        if(y[0]<1)            y[0]=21;        if(y[0]>21)            y[0]=1;        for(i=3; i<len; i++)            if(x[0]==x[i]&&y[0]==y[i])                return false;        for(i=0; i<len; i++)        {            gotoxy(x[i],y[i]);            printf("%c",2);        }        _sleep(300);        gotoxy(x[len-1],y[len-1]);        printf(" ");    }    return false;  //false代表蛇已死亡.}int main(){    system("color 09");    int i,j,temp;    FILE *fp;    fp=fopen("得分.txt","r");    if(fp!=NULL)    {        for(i=0; i<3; i++)        {            fscanf(fp,"%d",&score[i]);        }        fclose(fp);    }    char m;    printf("\t\t【贪吃蛇】[可以穿墙]\n\t请选择:\n\n");    printf("\t1.开始游戏.\n\t2.分数排名.【前三名】\n\t0.退出程序.\n\n\t");    scanf("%s",&m);    system("cls");    if(m=='1')    {        InitSnake();        create_window();        hidden();        if(run()==false)        {            system("cls");            printf("\t游戏结束,得分为:%3d\n\n\t",score[3]);        }        if(score[3]>score[2])            score[2]=score[3];        for(i=0; i<3; i++)            for(j=0; j<2; j++)                if(score[j]<score[j+1])                {                    temp=score[j];                    score[j]=score[j+1];                    score[j+1]=temp;                }        fp=fopen("得分.txt","w");        for(i=0; i<3; i++)            fprintf(fp,"%d ",score[i]);        fclose(fp);        system("pause");        system("cls");        return main();    }    else if(m=='2')    {        printf("\t前三名分数为:\n\t");        for(i=0; i<3; i++)            printf("%3d",score[i]);        printf("\n\n\t");        system("pause");        system("cls");        return main();    }    else if(m=='0')    {        int i;        printf("use:   退出中...........\n\n\t");        for(i=3; i>0; i--)        {            printf("\b%d",i);            _sleep(1000);        }        return 0;    }    else    {        printf("输入错误!\n");        return main();    }    return 0;}




2 0