| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2207 人关注过本帖
标题:贪吃蛇程序还没动就死了,不知道为什么
只看楼主 加入收藏
yang158
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2019-3-12
结帖率:16.67%
收藏
 问题点数:0 回复次数:5 
贪吃蛇程序还没动就死了,不知道为什么
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define framex 5
#define framey 5
#define wide 25
#define high 25
int i,j,a[2];
using namespace std;

void gotoxy(HANDLE hout,int x,int y)
{
   
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(hout,pos);
}

void cover (HANDLE hout){
    gotoxy(hout,framex+wide,framey);
    cout<<"欢迎使用贪吃蛇游戏1.2";
    gotoxy(hout,framex+wide,framey+5);
    cout<<"开始游戏前请关闭中文输入法";
    gotoxy(hout,framex+wide,framey+8);
    cout<<"按Enter开始";
    gotoxy(hout,framex+wide*2,framey+20);
    cout<<"游戏制作者:";
    gotoxy(hout,framex+wide*2,framey+22);
    cout<<"制作时间:";
    char a;
    a=getchar();
    system("cls");
}  
struct  Snake
{
    int x[100];
    int y[100];
    int speed;
    int length;
    int count;
};
struct Food{
    int x;
    int y;
};
void makeframe(struct Snake snake)
{
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    gotoxy(hout,framex+wide*2+5,framey);
    cout<< "贪吃蛇游戏";
    gotoxy(hout,framex+wide*2+5,framey+3);
    cout<<"使用方向键或wasd移动";
    gotoxy(hout,framex+wide*2+5,framey+5);
    cout<<"长按方向键可加速";
    gotoxy(hout,framex+wide*2+5,framey+7);
    cout<<"按任意键暂停,方向键继续";
    for(i=0;i<wide*2+1;i++){
        gotoxy(hout,framex+i,framey);
        cout<<"*";
    }
    for(i=0;i<wide*2+2;i++){
        gotoxy(hout,framex+i,framey+high);
        cout<<"*";
    }
    for(i=0;i<high;i++){
        gotoxy(hout,framex,framey+i);
        cout<<"*";
    }
    for(i=0;i<high;i++){
        gotoxy(hout,framex+wide*2+1,framey+i);
        cout<<"*";
    }
}
void infor(HANDLE hout,struct Snake*snake)
{
    gotoxy(hout,framex+wide*2+5,framey+15);
        cout<<"dang qian su du"<<snake->speed+500;
    gotoxy(hout,framex+wide*2+5,framey+17);
        cout<<"dang qian de fen"<<snake->count;
    gotoxy(hout,framex+wide*2+5,framey+19);
}        //游戏信息
void initsnake(struct Snake*snake)
{
    snake->x[0]=framex+2;
    snake->y[0]=framey+high/20;
    snake->count=0;
    snake->length=5;
    snake->speed=400;
    for(i=1;i<snake->length;i++)
    {
       snake->x[i]=snake->x[i+1]+1;
       snake->y[i]=snake->y[i+1];   
    }
      

}//初始化蛇

void printsnake(HANDLE hout,struct Snake*snake)
{
    for(i=0;i<snake->length;i++)
    {
        gotoxy(hout,snake->x[i],snake->y[i]);
        if(i==snake->length-1)
            cout<<"■";
        else if(i==0)
            cout<<"■";
        else
            cout<<"■";

    }
}
void movesnake(HANDLE hout,struct Snake*snake)
{
    gotoxy(hout,snake->x[0],snake->y[0]);
    cout<<" ";
    for(i=1;i<snake->length;i++)
    {  snake->x[i-1]=snake->x[i];
       snake->y[i-1]=snake->y[i];
    }
}

void printfood(HANDLE hout,struct Snake*snake,struct Food*food)
{
    srand((unsigned)time(NULL));
    while(1)
    {
        food->x=rand()%(wide-2)+1;
        food->y=rand()%(wide-2)+1;
        if(food->x==0&&food->y==0)
            continue;
        food->x=2*food->x+framex;
        food->y+=framey;
        for(i=0;i<snake->length;i++)
        {
            if(food->x==snake->x[i]&&food->y==snake->y[i])
                break;
        }
        if(i==snake->length)
        {
            gotoxy(hout,food->x,food->y);
            cout<<"●";
            break;
        }
    }
}
void eatfood(HANDLE hout,struct Snake*snake,struct Food*food)
{
    if(snake->x[snake->length-1]==food->x && snake->y[snake->length-1]==food->y)
    {
        snake->length++;
        for(i=snake->length-1;i>0;i--)
        {
            snake->x[i]=snake->x[i-1];
            snake->y[i]=snake->y[i-1];
        }
        snake->x[0]=a[0];
        snake->y[0]=a[1];
        printfood(hout,snake,food);
            snake->count++;
        if(snake->count%5==0)
            snake->speed-=50;
    }
}

int ifdead(struct Snake* snake)
{
    if(snake->x[snake->length-1]==framex)
        return 0;
    if(snake->x[snake->length-1]==framex+wide*2)
        return 0;
    if(snake->y[snake->length-1]==framey)
        return 0;
    if(snake->y[snake->length-1]==framey+high)
        return 0;
    for(i=0; i<snake->length-1; i++)
        if( snake->x[snake->length-1]==snake->x[i] && snake->y[snake->length-1]==snake->y[i] )
            return 0;
    return 1;
}

int main()
{  
    unsigned char ch =77;
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    cover(hout);
    struct Snake s, *snake=&s;
    struct Food  f,  *food=&f;
    makeframe(*snake);
    initsnake(snake);
    printfood(hout,snake,food);
    Sleep(500);
    while(1)
    {
        infor(hout,snake);
        a[0]=snake->x[0];
        a[1]=snake->y[0];
        j=0;
        
        if(kbhit())
        {
            ch=getch();
            if(kbhit())
            {
              Sleep(20);
                j=1;
            }
        }
        switch(ch)
        {
            case'W':
            case 'w' :
            case 72:
                {
                movesnake(hout,snake);
                snake->y[snake->length-1]-=1;
                break;
                }
            case'S':
            case's':
            case 80:
                {
                movesnake(hout,snake);
                snake->y[snake->length-1]+=1;
                break;
            }
            case'A':
            case'a':
            case 75:
                {
                movesnake(hout,snake);
                snake->x[snake->length-1]-=2;
                break;
            }
            case'D':
            case'd':
            case 77:
                {
                movesnake(hout,snake);
                snake->x[snake->length-1]+=2;
                break;
                }
        }
   eatfood(hout,snake,food);
   printsnake(hout,snake);
   if(ifdead(snake)==0||ch==27||snake->speed==0)
   {
       gotoxy(hout,framex+wide/2,framey-2);
         if(snake->speed==0)
            cout<<"恭喜你通关了!!!";
         else
            cout<<"糟糕 T_T";
            break;
        }

        if(j==0)
        Sleep(snake->speed);
        else
            Sleep(20);
    }
    Sleep(3000);
    system("cls");
   if(snake->speed==0)
       cout<<"\n\n\n\n\n\t\t\t哈哈,你赢了\n\n\t\t\t你终于过关了\n\n\n";
    else
        cout<<"\n\n\n\n\n\t\t\t哈哈,你输了\n\n\t\t\t你的最终得分是:"<<snake->count;
    Sleep(3000);
    return 0;
}
搜索更多相关主题的帖子: int cout struct for case 
2019-05-27 16:18
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:53
帖 子:1351
专家分:1200
注 册:2006-12-17
收藏
得分:0 
程序有点问题,前面必须加一句
#include<cstdio>

其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2019-05-30 11:10
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:53
帖 子:1351
专家分:1200
注 册:2006-12-17
收藏
得分:0 
#include<iostream>
#include<cstdio>
 #include<conio.h>
 #include<windows.h>
 #include<time.h>
 #define framex 5
 #define framey 5
 #define wide 25
 #define high 25
 int i,j,a[2];
 using namespace std;

 void gotoxy(HANDLE hout,int x,int y)
 {

     COORD pos;
     pos.X=x;
     pos.Y=y;
     SetConsoleCursorPosition(hout,pos);
 }

 void cover (HANDLE hout){
     gotoxy(hout,framex+wide,framey);
     cout<<"欢迎使用贪吃蛇游戏1.2";
     gotoxy(hout,framex+wide,framey+5);
     cout<<"开始游戏前请关闭中文输入法";
     gotoxy(hout,framex+wide,framey+8);
     cout<<"按Enter开始";
     gotoxy(hout,framex+wide*2,framey+20);
     cout<<"游戏制作者:";
     gotoxy(hout,framex+wide*2,framey+22);
     cout<<"制作时间:";
     char a;
     a=getchar();
     system("cls");
 }
 struct  Snake
 {
     int x[100];
     int y[100];
     int speed;
     int length;
     int count;
 };
 struct Food{
     int x;
     int y;
 };
 void makeframe(struct Snake snake)
 {
     HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
     gotoxy(hout,framex+wide*2+5,framey);
     cout<< "贪吃蛇游戏";
     gotoxy(hout,framex+wide*2+5,framey+3);
     cout<<"使用方向键或wasd移动";
     gotoxy(hout,framex+wide*2+5,framey+5);
     cout<<"长按方向键可加速";
     gotoxy(hout,framex+wide*2+5,framey+7);
     cout<<"按任意键暂停,方向键继续";
     for(i=0;i<wide*2+1;i++){
         gotoxy(hout,framex+i,framey);
         cout<<"*";
     }
     for(i=0;i<wide*2+2;i++){
         gotoxy(hout,framex+i,framey+high);
         cout<<"*";
     }
     for(i=0;i<high;i++){
         gotoxy(hout,framex,framey+i);
         cout<<"*";
     }
     for(i=0;i<high;i++){
         gotoxy(hout,framex+wide*2+1,framey+i);
         cout<<"*";
     }
 }
 void infor(HANDLE hout,struct Snake*snake)
 {
     gotoxy(hout,framex+wide*2+5,framey+15);
         cout<<"dang qian su du"<<snake->speed+500;
     gotoxy(hout,framex+wide*2+5,framey+17);
         cout<<"dang qian de fen"<<snake->count;
     gotoxy(hout,framex+wide*2+5,framey+19);
 }        //游戏信息
void initsnake(struct Snake*snake)
 {
     snake->x[0]=framex+2;
     snake->y[0]=framey+high/20;
     snake->count=0;
     snake->length=5;
     snake->speed=400;
     for(i=1;i<snake->length;i++)
     {
        snake->x[i]=snake->x[i+1]+1;
        snake->y[i]=snake->y[i+1];
     }


 }//初始化蛇

void printsnake(HANDLE hout,struct Snake*snake)
 {
     for(i=0;i<snake->length;i++)
     {
         gotoxy(hout,snake->x[i],snake->y[i]);
         if(i==snake->length-1)
             cout<<"■";
         else if(i==0)
             cout<<"■";
         else
             cout<<"■";

     }
 }
 void movesnake(HANDLE hout,struct Snake*snake)
 {
     gotoxy(hout,snake->x[0],snake->y[0]);
     cout<<" ";
     for(i=1;i<snake->length;i++)
     {  snake->x[i-1]=snake->x[i];
        snake->y[i-1]=snake->y[i];
     }
 }

 void printfood(HANDLE hout,struct Snake*snake,struct Food*food)
 {
     srand((unsigned)time(NULL));
     while(1)
     {
         food->x=rand()%(wide-2)+1;
         food->y=rand()%(wide-2)+1;
         if(food->x==0&&food->y==0)
             continue;
         food->x=2*food->x+framex;
         food->y+=framey;
         for(i=0;i<snake->length;i++)
         {
             if(food->x==snake->x[i]&&food->y==snake->y[i])
                 break;
         }
         if(i==snake->length)
         {
             gotoxy(hout,food->x,food->y);
             cout<<"●";
             break;
         }
     }
 }
 void eatfood(HANDLE hout,struct Snake*snake,struct Food*food)
 {
     if(snake->x[snake->length-1]==food->x && snake->y[snake->length-1]==food->y)
     {
         snake->length++;
         for(i=snake->length-1;i>0;i--)
         {
             snake->x[i]=snake->x[i-1];
             snake->y[i]=snake->y[i-1];
         }
         snake->x[0]=a[0];
         snake->y[0]=a[1];
         printfood(hout,snake,food);
             snake->count++;
         if(snake->count%5==0)
             snake->speed-=50;
     }
 }

 int ifdead(struct Snake* snake)
 {
     if(snake->x[snake->length-1]==framex)
         return 0;
     if(snake->x[snake->length-1]==framex+wide*2)
         return 0;
     if(snake->y[snake->length-1]==framey)
         return 0;
     if(snake->y[snake->length-1]==framey+high)
         return 0;
     for(i=0; i<snake->length-1; i++)
         if( snake->x[snake->length-1]==snake->x[i] && snake->y[snake->length-1]==snake->y[i] )
             return 0;
     return 1;
 }

 int main()
 {
     unsigned char ch =77;
     HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
     cover(hout);
     struct Snake s, *snake=&s;
     struct Food  f,  *food=&f;
     makeframe(*snake);
     initsnake(snake);
     printfood(hout,snake,food);
     Sleep(500);
     while(1)
     {
         infor(hout,snake);
         a[0]=snake->x[0];
         a[1]=snake->y[0];
         j=0;

         if(kbhit())
         {
             ch=getch();
             if(kbhit())
             {
               Sleep(20);
                 j=1;
             }
         }
         switch(ch)
         {

             case 'w' :

                 {
                 movesnake(hout,snake);
                 snake->y[snake->length-1]-=1;
                 break;
                 }

             case's':

                 {
                 movesnake(hout,snake);
                 snake->y[snake->length-1]+=1;
                 break;
             }

             case'a':

                 {
                 movesnake(hout,snake);
                 snake->x[snake->length-1]-=2;
                 break;
             }

             case'd':

                 {
                 movesnake(hout,snake);
                 snake->x[snake->length-1]+=2;
                 break;
                 }
         }
    eatfood(hout,snake,food);
    printsnake(hout,snake);
    if(ifdead(snake)==0||ch==27||snake->speed==0)
    {
        gotoxy(hout,framex+wide/2,framey-2);
          if(snake->speed==0)
             cout<<"恭喜你通关了!!!";
          else
             cout<<"糟糕 T_T";
             break;
         }

         if(j==0)
         Sleep(snake->speed);
         else
             Sleep(20);
     }
     Sleep(3000);
     system("cls");
    if(snake->speed==0)
        cout<<"\n\n\n\n\n\t\t\t哈哈,你赢了\n\n\t\t\t你终于过关了\n\n\n";
     else
         cout<<"\n\n\n\n\n\t\t\t哈哈,你输了\n\n\t\t\t你的最终得分是:"<<snake->count;
     Sleep(3000);
     return 0;
 }


[此贴子已经被作者于2019-5-30 15:35编辑过]


其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2019-05-30 15:29
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:53
帖 子:1351
专家分:1200
注 册:2006-12-17
收藏
得分:0 
能动一下

[此贴子已经被作者于2019-5-30 15:43编辑过]


其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2019-05-30 15:29
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:53
帖 子:1351
专家分:1200
注 册:2006-12-17
收藏
得分:0 
#include<iostream>
#include<cstdio>
 #include<conio.h>
 #include<windows.h>
 #include<time.h>
 #define framex 5
 #define framey 5
 #define wide 25
 #define high 25
 int i,j,a[2];
 using namespace std;

 void gotoxy(HANDLE hout,int x,int y)
 {

     COORD pos;
     pos.X=x;
     pos.Y=y;
     SetConsoleCursorPosition(hout,pos);
 }

 void cover (HANDLE hout){
     gotoxy(hout,framex+wide,framey);
     printf("欢迎使用贪吃蛇游戏1.2");
     gotoxy(hout,framex+wide,framey+5);
     printf("开始游戏前请关闭中文输入法");
     gotoxy(hout,framex+wide,framey+8);
     printf("按Enter开始");
     gotoxy(hout,framex+wide*2,framey+20);
     printf("游戏制作者:");
     gotoxy(hout,framex+wide*2,framey+22);
     printf("制作时间:");
     char a;
     a=getchar();
     system("cls");
 }
 struct  Snake
 {
     int x[100];
     int y[100];
     int speed;
     int length;
     int count;
 };
 struct Food{
     int x;
     int y;
 };
 void makeframe(struct Snake snake)
 {
     HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
     gotoxy(hout,framex+wide*2+5+20,framey);
     printf("贪吃蛇游戏");
     gotoxy(hout,framex+wide*2+5,framey+3);
     printf("使用方向键或wasd移动");
     gotoxy(hout,framex+wide*2+5,framey+5);
     printf("长按方向键可加速");
     gotoxy(hout,framex+wide*2+5,framey+7);
     printf("按任意键暂停,方向键继续");
     for(i=0;i<wide*2+1;i++){
         gotoxy(hout,framex+i,framey);
         cout<<"*";
     }
     for(i=0;i<wide*2+2;i++){
         gotoxy(hout,framex+i,framey+high);
         cout<<"*";
     }
     for(i=0;i<high;i++){
         gotoxy(hout,framex,framey+i);
         cout<<"*";
     }
     for(i=0;i<high;i++){
         gotoxy(hout,framex+wide*2+1,framey+i);
         cout<<"*";
     }
 }
 void infor(HANDLE hout,struct Snake*snake)
 {
     gotoxy(hout,framex+wide*2+5,framey+15);
         printf("dang qian su du");
        printf("%d",snake->speed+500);
     gotoxy(hout,framex+wide*2+5,framey+17);
         printf("dang qian de fen");
         printf("%d",snake->count);
     gotoxy(hout,framex+wide*2+5,framey+19);
 }        //游戏信息
void initsnake(struct Snake*snake)
 {
     snake->x[0]=framex+2;
     snake->y[0]=framey+high/20;
     snake->count=0;
     snake->length=5;
     snake->speed=400;
     for(i=1;i<snake->length;i++)
     {
        snake->x[i]=snake->x[i-1]+1;
        snake->y[i]=snake->y[i-1];
     }


 }//初始化蛇

void printsnake(HANDLE hout,struct Snake*snake)
 {
     for(i=0;i<snake->length;i++)
     {
         gotoxy(hout,snake->x[i],snake->y[i]);
         if(i==snake->length-1)
             printf("■");
         else if(i==0)
             printf("■");
         else
             printf("■");

     }
 }
 void movesnake(HANDLE hout,struct Snake*snake)
 {
     gotoxy(hout,snake->x[0],snake->y[0]);
     cout<<" ";
     for(i=1;i<snake->length;i++)
     {  snake->x[i-1]=snake->x[i];
        snake->y[i-1]=snake->y[i];
     }
 }

 void printfood(HANDLE hout,struct Snake*snake,struct Food*food)
 {
     srand((unsigned)time(NULL));
     while(1)
     {
         food->x=rand()%(wide-2)+1;
         food->y=rand()%(wide-2)+1;
         if(food->x==0&&food->y==0)
             continue;
         food->x=2*food->x+framex;
         food->y+=framey;
         for(i=0;i<snake->length;i++)
         {
             if(food->x==snake->x[i]&&food->y==snake->y[i])
                 break;
         }
         if(i==snake->length)
         {
             gotoxy(hout,food->x,food->y);
             cout<<"●";
             break;
         }
     }
 }
 void eatfood(HANDLE hout,struct Snake*snake,struct Food*food)
 {
     if(snake->x[snake->length-1]==food->x && snake->y[snake->length-1]==food->y)
     {
         snake->length++;
         for(i=snake->length-1;i>0;i--)
         {
             snake->x[i]=snake->x[i-1];
             snake->y[i]=snake->y[i-1];
         }
         snake->x[0]=a[0];
         snake->y[0]=a[1];
         printfood(hout,snake,food);
             snake->count++;
         if(snake->count%5==0)
             snake->speed-=50;
     }
 }

 int ifdead(struct Snake* snake)
 {
     if(snake->x[snake->length-1]==framex)
         return 0;
     if(snake->x[snake->length-1]==framex+wide*2)
         return 0;
     if(snake->y[snake->length-1]==framey)
         return 0;
     if(snake->y[snake->length-1]==framey+high)
         return 0;
     for(i=0; i<snake->length-1; i++)
         if( snake->x[snake->length-1]==snake->x[i] && snake->y[snake->length-1]==snake->y[i] )
             return 0;
     return 1;
 }

 int main()
 {
     unsigned char ch =77;
     HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
     cover(hout);
     struct Snake s, *snake=&s;
     struct Food  f,  *food=&f;
     makeframe(*snake);
     initsnake(snake);
     printfood(hout,snake,food);
     Sleep(500);
     while(1)
     {
         infor(hout,snake);
         a[0]=snake->x[0];
         a[1]=snake->y[0];
         j=0;

         if(kbhit())
         {
             ch=getch();
             if(kbhit())
             {
               Sleep(20);
                 j=1;
             }
         }
         switch(ch)
         {

             case 'w' :

                 {
                 movesnake(hout,snake);
                 snake->y[snake->length-1]-=1;
                 break;
                 }

             case's':

                 {
                 movesnake(hout,snake);
                 snake->y[snake->length-1]+=1;
                 break;
             }

             case'a':

                 {
                 movesnake(hout,snake);
                 snake->x[snake->length-1]-=2;
                 break;
             }

             case'd':

                 {
                 movesnake(hout,snake);
                 snake->x[snake->length-1]+=2;
                 break;
                 }
         }
    eatfood(hout,snake,food);
    printsnake(hout,snake);


         if(j==0)
         Sleep(snake->speed);
         else
             Sleep(20);
     }
     Sleep(3000);
     system("cls");
    if(snake->speed==0)
        cout<<"\n\n\n\n\n\t\t\t哈哈,你赢了\n\n\t\t\t你终于过关了\n\n\n";
     else
         cout<<"\n\n\n\n\n\t\t\t哈哈,你输了\n\n\t\t\t你的最终得分是:"<<snake->count;
     Sleep(3000);
     return 0;
 }

其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2019-06-03 05:27
汪瑞
Rank: 1
等 级:新手上路
帖 子:16
专家分:9
注 册:2018-10-21
收藏
得分:0 
程序有点问题,前面必须加一句
#include<cstdio>
2019-06-04 20:12
快速回复:贪吃蛇程序还没动就死了,不知道为什么
数据加载中...
 
   



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

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