标题:请教各位大神,我用C语言写了一个贪吃蛇,但运行的时候蛇总是不动,且闪退
取消只看楼主
alter黑贞
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-7-25
结帖率:0
已结贴  问题点数:20 回复次数:2 
请教各位大神,我用C语言写了一个贪吃蛇,但运行的时候蛇总是不动,且闪退
#include<stdafx.h>
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
//宏变量
#define U 1
#define D 2
#define L 3
#define R 4
//定义链表(全局变量)
typedef struct snake
{
    int x;
    int y;
    struct snake *next;
} snake;
int score = 0,add = 10;//初始分数为0,成功一次增加10分
int Highscore= 0;//初始最高分为0分
int status,sleeptime = 200;//蛇的状态,每运行一次要的时间间隔
snake *head,*food;//定义指针指向蛇的头部和食物
snake *q;//定义指针指向所需的蛇身的变量
int endgamestatus = 0;//定义结束游戏时蛇的状态
HANDLE hOut;
//函数声明
void gotoxy(int x,int y);//设置光标位置
int color(int c);//更改文字颜色
void printsnake();//描绘蛇
void welcometogame();//游戏开始界面
void createMap();//创建游戏地图
void scoreandtips();//右侧的得分和游戏提示
void initsnake();//蛇身初始化,创建形体
void createfood();//创建和随机出现食物
int biteself();//判定蛇是否咬到自己
void cantcrosswall();//设置蛇撞墙的判定
void speedup();//对蛇加速
void speeddown();//对蛇减速
void snakemove();//控制蛇前进方向
void keyboardcontrol();//控制键盘按钮
void lostdrew();// 游戏结束界面
void endgame();//游戏结束
void choose();//游戏失败之后的选择
void File_out();//在文件中读取最高分
void File_in();//存储最高分进文件
void explation();//游戏说明


//设置文字颜色
int color(int c)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
    return 0;
}
//获取控制台的坐标位置
void gotoxy(int x,int y)
{
    COORD c;
    c.X = x;
    c.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
//字符蛇
 void printsnake()
 {
 
     gotoxy(25,1);
     printf(" /^\\/^\\");
     gotoxy(24,2);
     printf("_|__|  o|");
     gotoxy(16,3);
     printf("\\/     /~    \\_/ \\");
     gotoxy(17,4);
     printf("\\___|________/  \\");
     gotoxy(23,5);
     printf("\\______    \\");
     gotoxy(31,6);
     printf("\\   \\");
     gotoxy(33,7);
     printf("|  |                              \\");
     gotoxy(32,8);
     printf("/   /                               \\");
     gotoxy(31,9);
     printf("/   /                                \\ \\");
     gotoxy(30,10);
     printf("/   /                                  \\  \\");
     gotoxy(29,11);
     printf("/   /               _----_              \\   \\");
     gotoxy(28,12);
     printf("/   /            _-~       ~-_           |   |");
     gotoxy(27,13);
     printf("〔   〔        _-~    _--_     ~-_      _/   |");
     gotoxy(28,14);
     printf("\\     ~-___-~     _-~     ~-_     ~-_-~    /");
     gotoxy(29,15);
     printf("~-_           _-~           ~-_        _-~");
     gotoxy(30,16);
     printf(" ~--_____-~                    ~-___-~");
 }
 //欢迎界面
 void welcometogame()
 {
     int n;
     int i,j = 1;
     gotoxy(43,18);
     printf("贪吃蛇大作战");
     for(i = 20;i<=26;i++)
     {
         for(j = 27;j<=74;j++)
         {
             gotoxy(j,i);
         if(i==20||i==26)
         {
             printf("-");
         }else if(j==27||j==74)
         {
             printf("|");
         }
         }
     }
    gotoxy(35,22);
    printf("1.开始游戏");
    gotoxy(55,22);
    printf("2.游戏说明");
    gotoxy(35,24);
    printf("3.退出游戏");
    gotoxy(29,27);
    printf("请选择[1 2 3]:[ ]\b\b");
    scanf("%d",&n);
    switch(n)
    {
        case 1:
            system("cls");
            createMap();
            initsnake();
            scoreandtips();
            createfood();
            
            break;
        case 2:
            explation();
                break;
        case 3:
                exit(0);break;
        
    };
 }
 //创建游戏地图
void createMap()
{
    int i,j;
    for(i=0;i<58;i+=2)
    {
        gotoxy(i,0);
        printf("□");
        gotoxy(i,26);
        printf("□");
    }
    for(i=1;i<26;i++)
    {
        gotoxy(0,i);
        printf("□");
        gotoxy(56,i);
        printf("□");
    }
    for(i=2;i<56;i+=2)
    {
        for(j=1;j<26;j++)
        {
            gotoxy(i,j);
            printf("■\n");
        }
    }
}
//游戏分数和提示
void scoreandtips()
{
    File_out();
    gotoxy(64,4);
    printf("☆最高记录☆:%d",Highscore);
    gotoxy(64,8);
    printf("得分:%d",score);
    gotoxy(73,12);
    printf("小 提 示");
    gotoxy(60,13);
    printf("╬-----------------------------╬");
    gotoxy(64,15);
    printf("每个食物得分:10分");
    gotoxy(64,17);
    printf("不能穿墙,不能咬到自己");
    gotoxy(64,19);
    printf("用 ↑ ↓ ← → 分别控制蛇的移动");
    gotoxy(64,21);
    printf("F1 为加速,F2 为减速");
    gotoxy(64,23);
    printf("space :暂停游戏");
    gotoxy(64,25);
    printf("ESC :退出游戏");
    gotoxy(60,25);
    printf("╬-----------------------------╬");
     
}
//在文件中读取最高分
void File_out()
{
    FILE *fp;
    fp=fopen("save.text ","a+");
    fscanf(fp,"%d",&Highscore);
    fclose(fp);
}
//初始化蛇身
void initsnake()
{
    snake *tail;
    int i;
    tail = (snake*)malloc(sizeof(snake));
    tail->x = 24;
    tail->y = 5;
    for(i=1;i<=2;i++)
    {
        head = (snake*)malloc(sizeof(snake));
        head->next =tail;
        head->x = 24+i*2;
        head->y = 5;
        tail = head;
    }
    while(tail!=NULL)
    {
    gotoxy(tail->x,tail->y);
    printf("★");
    tail = tail->next;
    }
}
//绘制食物并随机显现
void createfood()
{
    snake *food_1;
    food_1=(snake*)malloc(sizeof(snake));
    srand((unsigned)time(NULL));
    while((food_1->x%2)!=0)               //保证食物随机生成在方框中间和划定生成范围
        {
           food_1->x=rand()%52+2;
         }
           food_1->y=rand()%24+1;
     
    q=head;
    while(q->next==NULL)
    {
        if(q->x==food_1->x&&q->y==food_1->y)
             {   
                 free(food_1);
                 createfood();
             }
             q=q->next;
    }
    gotoxy(food_1->x,food_1->y);
    food=food_1;
    printf("●");
}
//判断蛇是否咬到自己
int biteself()
{
    snake *self;
    self = head->next;
    while(self!=NULL)
    {
        if(self->x==head->y&&self->y==head->y)
        {
            return 1;
        }
        self=self->next;
    }
    return 0;
}
// 判断蛇是否撞墙
void cantcrosswall()
{
    if(head->x==0 || head->x==56 || head->y==0 || head->y==26)
    {
        endgamestatus = 1;
        endgame();
    }
   
}

//蛇减速
void speeddown()
{
    if(sleeptime<350)
    {
        sleeptime = sleeptime -10;
        add=add-2;
        if(sleeptime==350)
        {
            add=1;
        }
    }
}
//蛇加速
void speedup()
{
    if(sleeptime<=50)
    {
        sleeptime = sleeptime +30;
        add=add+2;

    }
}
//控制方向
void snakemove()
{
    snake *nexthead;
    nexthead=(snake*)malloc(sizeof(snake));
    cantcrosswall();
    if(status==U)
    {
        head->x = head->x;
        head->y = head->y-1;
        nexthead->next=head;
        head=nexthead;
        q=head;
        if(nexthead->x==food->x&&nexthead->y==food->y)
        {
            while(q!=NULL)
            {
                gotoxy(q->x,q->y);
                printf("★");
                q = q->next;
               
            }
            score=score+add;
            speedup();
            createfood();
        }
        else
        {
            while(q->next->next!=NULL)
            {
                gotoxy(q->x,q->y);
                printf("★");
                q = q->next;   
            }
            gotoxy(q->next->x,q->next->y);
            printf("■");
            free(q->next);
            q->next=NULL;
        }
    }
    if(status==D)
        {
            head->x = head->x;
            head->y = head->y+1;
            nexthead->next=head;
            head=nexthead;
            q=head;
            if(nexthead->x==food->x&&nexthead->y==food->y)
            {
                while(q!=NULL)
                {
                    gotoxy(q->x,q->y);
                    printf("★");
                    q = q->next;
                    
                }
                score=score+add;
                speedup();
                createfood();
            }
            else
            {
                while(q->next->next!=NULL)
                {
                    gotoxy(q->x,q->y);
                    printf("★");
                    q = q->next;   
                }
                gotoxy(q->next->x,q->next->y);
                printf("■");
                free(q->next);
                q->next=NULL;
            }
        }
        if(status==L)
            {
                head->x = head->x-1;
                head->y = head->y;
                nexthead->next=head;
                head=nexthead;
                q=head;
                if(nexthead->x==food->x&&nexthead->y==food->y)
                {
                    while(q!=NULL)
                    {
                        gotoxy(q->x,q->y);
                        printf("★");
                        q = q->next;
                        
                    }
                    score=score+add;
                    speedup();
                    createfood();
                }
                else
                {
                    while(q->next->next!=NULL)
                    {
                        gotoxy(q->x,q->y);
                        printf("★");
                        q = q->next;   
                    }
                    gotoxy(q->next->x,q->next->y);
                    printf("■");
                    free(q->next);
                    q->next=NULL;
                }
            }
            if(status==R)
                {
                    head->x = head->x+1;
                    head->y = head->y;
                    nexthead->next=head;
                    head=nexthead;
                    q=head;
                    if(nexthead->x==food->x&&nexthead->y==food->y)
                    {
                        while(q!=NULL)
                        {
                            gotoxy(q->x,q->y);
                            printf("★");
                            q = q->next;
                           
                        }
                        score=score+add;
                        speedup();
                        createfood();
                    }
                    else
                    {
                        while(q->next->next!=NULL)
                        {
                            gotoxy(q->x,q->y);
                            printf("★");
                            q = q->next;   
                        }
                        gotoxy(q->next->x,q->next->y);
                        printf("■");
                        free(q->next);
                        q->next=NULL;
                    }
                }
    if(biteself()==1)
    {
        endgamestatus = 2;
        endgame();
    }
}
//控制键盘按键
void keyboardcontrol()
{
    status = R;
    while(1)
    {
        scoreandtips();
        if(GetAsyncKeyState(VK_UP)&&status!=D)
            {
                status=U;
            }
        else if(GetAsyncKeyState(VK_DOWN)&&status!=U)
            {
                status=D;
             }
        else if(GetAsyncKeyState(VK_LEFT)&&status!=R)
            {
                    status=L;
            }
        else if(GetAsyncKeyState(VK_RIGHT)&&status!=L)
            {
                    status=R;
            }
        if(GetAsyncKeyState(VK_SPACE))
           {
            Sleep(300);
            if(GetAsyncKeyState(VK_SPACE))
                 {
                     break;
                 }
            }
        if(GetAsyncKeyState(VK_ESCAPE))
             {
                endgamestatus = 3;
                break;
             }
        if(GetAsyncKeyState(VK_F1))
             {
            speedup();
             }
        if(GetAsyncKeyState(VK_F2))
            {
                speeddown();
            }
        Sleep(sleeptime);
        snakemove();
    }
}
//失败界面
void lostdrew()
{
    system("cls");
    int i;
    gotoxy(45,2);
    color(6);
    printf("\\\\\\|///");
    gotoxy(43,3);
    printf("\\\\");
    color(15);
    printf(".-.-");
    color(6);
    gotoxy(54,3);
    printf("//");
    color(14);
    gotoxy(44,4);
    printf("(");
    color(15);
    gotoxy(47,4);
    printf(".@.@");
    color(14);
    gotoxy(54,4);
    printf(")");
    color(11);
    gotoxy(17,5);
    printf("+--------------------");
    color(14);
    gotoxy(39,5);
    printf("oOOo");
    color(11);
    gotoxy(43,5);
    printf("----------");
    color(14);
    gotoxy(53,5);
    printf("<_>");
    color(11);
    gotoxy(56,5);
    printf("----------");
    color(14);
    gotoxy(66,5);
    printf("oOOo");
    color(11);
    gotoxy(70,5);
    printf("----------+");
    for(i=6;i<=19;i++)
    {
        gotoxy(17,i);
        printf("|");
        gotoxy(82,i);
        printf("|");
    }
    gotoxy(17,20);
    printf("+----------------------------------");
    gotoxy(52,20);
    color(14);
    printf("☆☆☆∮");
    gotoxy(60,20);
    color(11);
    printf("--------------------+");
   
}
//结束界面
void endgame()
{
    system("cls");
    if(endgamestatus==1)
    {
        lostdrew();
        gotoxy(35,9);
        color(12);
        printf("对不起,您撞到了墙,游戏结束");     
    }
    else if(endgamestatus==2)
    {
        lostdrew();
        gotoxy(35,9);
        color(12);
        printf("对不起,您咬到了自己,游戏结束");
    }
    else if(endgamestatus==3)
    {
        lostdrew();
        gotoxy(40,9);
        color(12);
        printf("您自己结束了游戏,是太难了吗");
    }
    color(13);
    gotoxy(43,12);
    printf("您的得分是:%d",score);
    if(score>=Highscore)
    {
        color(10);
        gotoxy(33,16);
        printf("创纪录了,你很勇哦");
        File_in();
    }
    else
    {
        color(10);
        gotoxy(33,16);
        printf("弱诶,这个玩家就是逊啦,离最高分还差:%d",Highscore-score);
    }
    choose();
}
//将最高分存储进文件
void File_in()
{
    FILE *fp;
    fp = fopen("save.txt","w+");
    fprintf(fp,"%d",score);
    fclose(fp);
}
//边框下的分支选项
void choose()
{
    int n;
    color(12);
    gotoxy(25,23);
    printf("我想再战一局");
    gotoxy(52,23);
    color(12);
    printf("不玩了,毁灭吧");
    color(11);
    gotoxy(46,25);
    printf("选择");
    scanf("%d",&n);
    switch (n) {
        case 1:
            system("cls");
            score = 0;
            sleeptime=200;
            add = 10;
            printsnake();
            welcometogame();
            break;
            
        case 2:
            exit(0);
            break;
        default:
            color(12);
            gotoxy(35,27);
            printf("※※您的输入有误,请重新输入※※");
            system("pause >nul");
            endgame();
            choose();
            break;
    }
     
}
//游戏说明
void explation()
{
    int i,j=1;
    system("cls");
    color(13);
    gotoxy(44,3);
    printf("游戏说明");
    color(2);
    for(i=6;i<=22;i++)
    {
        for(j=20;j<=75;j++)
        {
            gotoxy(j,i);
            if(i==6||i==22)
            {
                printf("=");
            }
            if(j==20||j==75)
            {
                printf("||");
            }
        }
    }
    color(3);
    gotoxy(30,8);
    printf("tip 1: 不能穿墙,不能咬到自己");
    color(10);
    gotoxy(30,11);
    printf("tip 2:用↑↓←→分别控制蛇的移动");
    color(14);
    gotoxy(30,14);
    printf("tip 3: F1为加速,F2为减速");
    color(11);
    gotoxy(30,17);
    printf("tip 4: 按空格键暂停游戏,再按继续");
    color(4);
    gotoxy(30,20);
    printf("tip 5: ESC:退出游戏,space:暂停游戏");
    getch();
    system("cls");
    printsnake();
    welcometogame();
}
 //主函数
 int main()
 {
     system("mode con cols=100 lines=30");
     printsnake();
     welcometogame();
     File_out();
     keyboardcontrol();
     endgame();
     return 0;
 }
搜索更多相关主题的帖子: printf next color void int 
2021-07-25 22:11
alter黑贞
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-7-25
得分:0 
回复 2楼 rjsp
tail=head是将蛇头的赋给蛇尾,然后再进行循环,因为下面一个循环输出的是蛇尾的坐标,我感觉我错的是随机食物那,initsnake这个应该没错,但我不知道错在哪,因为我把createfood这个函数换顺序后,其他函数就能运行起来了

[此贴子已经被作者于2021-7-26 22:05编辑过]

2021-07-26 22:02
alter黑贞
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-7-25
得分:0 
回复 4楼 diycai
谢谢大佬,膜拜膜拜
2021-07-31 21:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506329-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016965 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved