c++贪吃蛇,电脑编译正常但运行出错

C++语言 码拜 9年前 (2015-10-22) 1163次浏览
用c++写的贪吃蛇,自己电脑上用codeblocks编译运行一点事没有,编译不出错,运行妥妥的。但是.cpp发给同学后,在他们codeblocks上编译不报错,但是运行就不行。试了好几个人都一样,我们codeblocks版本都一样13.12的。先附上报错图:
c++贪吃蛇,电脑编译正常但运行出错
附上代码:

#include <windows.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include<time.h>
using namespace std;
int s_score,sec;
int food_x,food_y,eaten;//食物x,y坐标,eaten表示食物是否被吃了
const int s_up=1,s_down=2,s_left=3,s_right=4;
int op;
int game_speed=500;
int sx[400]={},sy[400]={};//用于储存蛇的每一点坐标
int b[20][20]={};//存储蛇的状态,0表示"  "(这里是两个空字符),1表示蛇身"实心圆",2表示食物"空心三角形",3表示砖块"实心正方形"(即边界);PS:发帖的时候被系统告知不能包含那些实心圆那些特殊符号,所以用文字替代了=  =
//隐藏光标函数
void HideCursor()
{
 CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//gotoxy()函数的定义
  void gotoxy(int x, int y)
  {
      int xx=0x0b;
      HANDLE hOutput;
      COORD loc;
      loc.X=x;
      loc.Y=y;
      hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleCursorPosition(hOutput, loc);
      return;
  }
class Snake
{
 private:
    int head_x,head_y,tail_x,tail_y;
    int dir;//移动方向
    int len;//蛇长
    int s_prex,s_prey;//前一个蛇尾的x,y坐标;
    int s_latx,s_laty;//后一个蛇头的x,y坐标
 public:
    Snake()
    {
        head_x=9;head_y=9;//注意,此处的x,y轴是根据数组来定义的,x轴垂直向下为正方向,y轴水平向右为正方向.
        tail_x=9;tail_y=9;
        b[head_x][head_y]=1;
        b[tail_x][tail_y]=1;
        dir=s_up;
        len=0;
        sx[len]=head_x;//储存蛇每一点的坐标
        sy[len]=head_y;
        s_prex=tail_x;
        s_prey=tail_y;
    };
//绘制地图
void map_create()
{
    //绘制边界.
    for(int i=0,j=0;j<=19;j++)
    {
        b[i][j]=3;
    }
    for(int i=19,j=0;j<=19;j++)
    {
        b[i][j]=3;
    }
    for(int j=0,i=0;i<=19;i++)
    {
        b[i][j]=3;
    }
    for(int j=19,i=0;i<=19;i++)
    {
        b[i][j]=3;
    }
}
//寻找函数,用于判断下一格是否是蛇身的点或者边界
    int s_search(int dir)//返回s_exit值,取值为0,1
    {
        int s_exit=0;//用于判断游戏是否退出的标准,0不退出,1退出
        switch(dir)
        {
        case s_up:
            if(head_x-1==0)s_exit=1;
            else
            for(int i=0;i<=len;i++)
            {
                if(head_x-1==sx[i]&&head_y==sy[i])s_exit=1;
            };break;
        case s_down:
            if(head_x+1==19)s_exit=1;
            else
            for(int i=0;i<=len;i++)
            {
                if(head_x+1==sx[i]&&head_y==sy[i])s_exit=1;
            };break;
        case s_left:
            if(head_y-1==0)s_exit=1;
            else
            for(int i=0;i<=len;i++)
            {
                if(head_x==sx[i]&&head_y-1==sy[i])s_exit=1;
            };break;
        case s_right:
            if(head_y+1==19)s_exit=1;
            else
            for(int i=0;i<=len;i++)
            {
                if(head_x==sx[i]&&head_y+1==sy[i])s_exit=1;
            };break;
        default:exit(0);break;
        }
        return s_exit;
    }
//显示边界
    void map_show()
    {
        for(int i=0;i<=19;i++)
    {
        for(int j=0;j<=19;j++)
        {
            switch(b[i][j])
            {
            case 0:cout<<"  ";break;
            case 1:cout<<"实心圆";break;
            case 2:cout<<"空心三角行";break;
            case 3:cout<<"实心正方形";break;
            default:cout<<"error";
            }
            if(j==19)cout<<"\n";
        }
    }
    }
//显示蛇,每一次move后,前一个蛇尾和下一格蛇头都被被用来判断,用于描点,其他格子不扫描不判断
    void snake_show()
    {
         gotoxy(2*food_y,food_x);cout<<"空心三角";
         if(b[s_prex][s_prey]==0){gotoxy(2*s_prey,s_prex);cout<<"  ";}
         if(b[s_latx][s_laty]==1){gotoxy(2*s_laty,s_latx);cout<<"实心圆";}
    }
 //随机产生食物
    void food()
    {
        while(1)
        {
            food_x=rand()%19;//这里rand产生的随机数为1到18,不会出现在边界上
            food_y=rand()%19;
            if(b[food_x][food_y]==0){b[food_x][food_y]=2;break;}//当食物坐标所对应的蛇数组表示不为1时,表明此方格可以产生食物.
        }
    }
//蛇根据当前方向进行移动,判断方向后的变化会存在b数组里面.
    int snake_move()
    {
        s_prex=tail_x;
        s_prey=tail_y;
        eaten=0;//用eaten记录是否食物;
        switch(dir)
        {
        case s_up://up
            if(head_x-1==food_x&&head_y==food_y)//如果食物坐标与蛇下一格子冲突,便eat.
                {
                    head_x=head_x-1;
                    b[head_x][head_y]=1;//有食物的时候蛇头部加1个
                    len++;
                    sx[len]=head_x;
                    sy[len]=head_y;
                    eaten=1;
                }
            else
                {
                    head_x=head_x-1;     //蛇信息变化
                    b[head_x][head_y]=1;
                    b[tail_x][tail_y]=0;
                  //重写蛇每一点的坐标
                    for(int i=0;i<=len;i++)
                    {
                        sx[i]=sx[i+1];
                        sy[i]=sy[i+1];
                    }
                    sx[len]=head_x;
                    sy[len]=head_y;
                    tail_x=sx[0];
                    tail_y=sy[0];
                }
            break;//没食物的时候蛇的头部向上,尾巴少一个,在代码最开始定义了up=1,down=2,right=3,left=4;
        case s_down://down
            if(head_x+1==food_x&&head_y==food_y)//如果食物坐标与蛇下一格子冲突,便eat.
                {
                    head_x=head_x+1;
                    b[head_x][head_y]=1;//有食物的时候蛇头部加1个
                    len++;
                    sx[len]=head_x;
                    sy[len]=head_y;
                    eaten=1;
                }
            else
                {
                    head_x=head_x+1;     //蛇信息变化
                    b[head_x][head_y]=1;
                    b[tail_x][tail_y]=0;
                  //重写蛇每一点的坐标
                    for(int i=0;i<=len;i++)
                    {
                        sx[i]=sx[i+1];
                        sy[i]=sy[i+1];
                    }
                    sx[len]=head_x;
                    sy[len]=head_y;
                    tail_x=sx[0];
                    tail_y=sy[0];
                }
            break;
        case s_left://left
            if(head_x==food_x&&head_y-1==food_y)//如果食物坐标与蛇下一格子冲突,便eat.
                {
                    head_y=head_y-1;
                    b[head_x][head_y]=1;//有食物的时候蛇头部加1个
                    len++;
                    sx[len]=head_x;
                    sy[len]=head_y;
                    eaten=1;
                }
            else
                {
                    head_y=head_y-1;     //蛇信息变化
                    b[head_x][head_y]=1;
                    b[tail_x][tail_y]=0;
                  //重写蛇每一点的坐标
                    for(int i=0;i<=len;i++)
                    {
                        sx[i]=sx[i+1];
                        sy[i]=sy[i+1];
                    }
                    sx[len]=head_x;
                    sy[len]=head_y;
                    tail_x=sx[0];
                    tail_y=sy[0];
                }
            break;
        case s_right://right
            if(head_x==food_x&&head_y+1==food_y)//如果食物坐标与蛇下一格子冲突,便eat.
                {
                    head_y=head_y+1;
                    b[head_x][head_y]=1;//有食物的时候蛇头部加1个
                    len++;
                    sx[len]=head_x;
                    sy[len]=head_y;
                    eaten=1;
                }
            else
                {
                    head_y=head_y+1;     //蛇信息变化
                    b[head_x][head_y]=1;
                    b[tail_x][tail_y]=0;
                  //重写蛇每一点的坐标
                    for(int i=0;i<=len;i++)
                    {
                        sx[i]=sx[i+1];
                        sy[i]=sy[i+1];
                    }
                    sx[len]=head_x;
                    sy[len]=head_y;
                    tail_x=sx[0];
                    tail_y=sy[0];
                }
            break;
        default:exit(0);
        }
        s_latx=head_x;
        s_laty=head_y;
        return eaten;
    }
//记录游戏得分
    int getscore()
    {
        return len;
    }
//主体,游戏入口
    void snake_game()
    {
        food();
        while(1)
        {
            snake_show();
            Sleep(game_speed);//此处里面的参数单位为毫秒,可以通过调整参数达到控制游戏速度的效果
            if(kbhit())//kbhit()用于判断是否有键盘输入
            {
                switch(getch())
                {
                    case ""w"":if(dir!=s_down)dir=s_up;break;
                    case ""s"":if(dir!=s_up)dir=s_down;break;
                    case ""a"":if(dir!=s_right)dir=s_left;break;
                    case ""d"":if(dir!=s_left)dir=s_right;break;
                    default:break;
                }
            }
            if(s_search(dir)==1){s_score=getscore();system("cls");cout<<"\n\n\n\n\n\n\n        游戏结束了!你的得分:"<<s_score<<"  points\n\n\n\n\n\n";system("pause");exit(0);}//gameover则下面语句不执行,并推出游戏
            if(snake_move()==1)food();
        }
    }
};
void meun()
{
    int k=0;
    int op=4;
    while(1)
    {
        system("cls");
        cout<<"\n\n\n\n   *****************************\n";
        cout<<"\n\n\n\n            1.进入游戏\n";
        cout<<"\n\n\n\n            2.调节速度\n";
        cout<<"\n\n\n\n            3.退出\n";
        cout<<"\n\n\n\n   *****************************\n\n请选择: ";
        cin>>op;
        switch(op)
        {
        case 1:system("cls");break;
        case 2:
            system("cls");
            cout<<"\n\n\n                 请输入每一步的间隔,只能为整数\n\n\n        Second = ";
            cin>>sec;
            game_speed=sec*100;
            op=4;
            break;
        case 3:
            system("cls");cout<<"\n\n\n                  哦。\n\n\n";exit(0);break;
        default:
            k=3;op=4;break;
        }
        if(op==1)break;
        if(k==3){system("cls");cout<<"\n\n\n           你智商让doge吃了?选项只能输入1、2、3,看不懂?坑比!\n\n\n\n\n";exit(0);}
    }
}
int main()
{
    HideCursor();
    meun();
    Snake A;
    A.map_create();
    A.map_show();
    A.snake_game();
    return 0;
}

哪里出错了

解决方案:40分
引用

int s_latx,s_laty;//后一个蛇头的x,y坐标

没初始化。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c++贪吃蛇,电脑编译正常但运行出错
喜欢 (0)
[1034331897@qq.com]
分享 (0)