关于C++编写的贪吃蛇的问题

Q405620495 2012-10-12 05:40:36
各位高手,我是新手,在编写贪吃蛇的过程中出现了很多的问题,运行得不到想要的结果。现把我的代码贴出来,希望大家能

够指出我的错误之处。非常感谢!

#include <iostream>
#include <ctime>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;
#define N 100
enum dir{up,down,left,right}; //枚举类型enum dir;
class Snake
{ public:
void move();//移动
void getdirection(char a);//改变方向
void showsnake();
bool iseatbody(); //是否咬到自己的身体
void drawme();//显示
bool ishitground(); //是否碰到墙壁
bool iseatfood();//吃到食物否
private:
int x[N];
int y[N];
int length;//蛇长
};

class Ground
{
public :

void drawme();//显示墙壁
void setdrawme();//设置墙壁的长宽
public :
char game [20][30]; //设置墙壁的长宽

}ground;

class Food
{
public:
void getfood();//获得随机食物
int x;
int y;//食物的坐标
}food;

void Snake::move()
{ int i;
cout<<"Snake move"<<endl;
for(i=length-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
{
x[i]=x[i-1];
y[i]=y[i-1];
}

}
void Snake::getdirection(char a)
{
/*w,a,s,d表示上,下,左,右四个方向,通过这个判断来移动蛇头*/
switch(a)
{
case 'w':x[0]--;break;
case 's': x[0]++;break;
case 'a': y[0]--;break;
case 'd': y[0]++;break;
}


}
//显示蛇
void Snake::showsnake()
{ int i;
for (i=0;i<length;i++)
ground.game[x[i]][y[i]]='@';
}

bool Snake::iseatbody()
{
int i;
cout<<"Snake iseatbody"<<endl;
for(i=3;i<length;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/
{
if(x[i]==x[0]&&y[i]==y[0])
{
cout<<"Game over"<<endl;/*显示失败*/
return 0;
}
}
return 1;
}
void Snake::drawme()
{
cout<<"Snake drawme"<<endl;
x[0]=10;y[0]=10;/*蛇头*/
x[1]=11;y[1]=10;
length=2;/*节数*/
ground.game[x[0]][y[0]]='@';
ground.game[x[1]][y[1]]='@';
}


//是否碰到墙壁
bool Snake::ishitground()
{
cout<<"is Snake hitground ?"<<endl;
if(x[0]<0||x[0]>19||y[0]<0||y[0]>19)/*蛇是否撞到墙壁*/
{
cout<<"Game over"<<endl;/*本次游戏结束*/
return 0;
}
else return 1;
}

//是否吃到食物
bool Snake::iseatfood()
{
if (x[0]==food.x&&y[0]==food.y)
return 1;
else return 0;
}

//设置墙壁的长和宽
void Ground::setdrawme()
{ int i,j;
for( i=0; i<20; i++)
for( j=0; j<30; j++)
{
if(i==0||i==19||j==0||j==29)
game[i][j]= '*';
else game[i][j]= ' ';
}

}

//显示出墙壁
void Ground::drawme()
{ int i,j;
for( i=0; i<20; i++)
{
for(j=0; j<30; j++)
cout<<game[i][j]<<" ";
cout<<endl;
}
}

void Food::getfood ()
{
cout<<"Snake getfood "<<endl;
srand((unsigned int) time(NULL)); //做种子(程序运行时间);
x= rand()%18+1;
y= rand()%28+1;
ground.game[x][y]='*';

}


int main ()
{
Snake snake;
ground.setdrawme();
snake.drawme();
food.getfood();
ground.drawme();
while (true )
{
char a=getch();//getch()返回键盘上读取的字符;包含头文件<conio.h>
snake.getdirection(a);
while (!kbhit())//判断有没有按键落下;
{
system("cls");//清屏函数;
snake.move();
snake.showsnake();
ground.drawme();
if( snake.iseatfood()==1)
food.getfood();
else if( snake.iseatbody()==0)
exit (0);
else if (snake.ishitground()==0)
exit (0);
Sleep(2500);
}
}
// ground.drawme();
return 0;
}




...全文
874 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bear234 2015-01-14
  • 打赏
  • 举报
回复
贪吃蛇很简单吧...... 我留言只是想说...........楼主,你英语不好还不如直接用拼音命名.......iseatbody是什么东西....
hlx_beat 2015-01-13
  • 打赏
  • 举报
回复
这设计有问题,蛇、食物和墙,都应该继承于同一个父类,只是继承后各自所具有的属性有点不同。 他们都有位置:共同属性(POINT pos),同时能够打印到屏幕(void print()), 蛇会多一个属性:记录上一个位置(下一节蛇节点将要设置的位置)。 吃食物和碰墙:应该一个游戏管理决定,蛇就尽管吃碰到东西就吃(不管是食物还是墙壁),管理者去检测蛇是长一节还是死亡, 管理者是用检测碰撞方式检测。 怎么会有这东西: snake. ishitground(); snake.iseatbody(); 按照常理来说如果蛇能判断是否是墙和自身,我为什么要吃要杀自己的东西,why? 所以蛇是贪(无脑)碰到东西就吃,吃东西是活是死蛇自己不能决定。
龍将 2014-12-24
  • 打赏
  • 举报
回复
请问怎么没有刷频让蛇一直移动?
川黑今 2013-06-25
  • 打赏
  • 举报
回复
最近也想写个这个,但好多问题都没解决
Naugo 2013-05-30
  • 打赏
  • 举报
回复
那么蛇尾移到蛇头的时间怎么实现的呢?
Q405620495 2012-10-14
  • 打赏
  • 举报
回复
谢谢,谢谢你们!
lnjhh 2012-10-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <ctime>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;
#define N 100
enum dir{up,down,left,right}; //枚举类型enum dir;
class Snake
{
public:
void move(char direction);//移动
bool isdirection(char a);//改变方向
void showsnake();
bool iseatbody(); //是否咬到自己的身体
void drawme();//显示
bool ishitground(); //是否碰到墙壁
bool iseatfood();//吃到食物否
private:
int x[N];
int y[N];
int length;//蛇长
};

class Ground
{
public :

void drawme();//显示墙壁
void setdrawme();//设置墙壁的长宽
public :
char game [20][30]; //设置墙壁的长宽

}ground;

class Food
{
public:
void getfood();//获得随机食物
int x;
int y;//食物的坐标
}food;

void Snake::move(char direction)
{
int i;
int xx, yy;
//cout<<"Snake move"<<endl;

xx = x[length-1];
yy = y[length-1];

/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
for (i = length - 1; i > 0; i--)
{
x[i]=x[i-1];
y[i]=y[i-1];
}

/*w,a,s,d表示上,下,左,右四个方向,通过这个判断来移动蛇头*/
switch(direction)
{
case 'w':
x[0]--;
break;
case 's':
x[0]++;
break;
case 'a':
y[0]--;
break;
case 'd':
y[0]++;
break;
}

if (iseatfood() == 1)
{
// 吃到食物变长
x[length] = xx;
y[length] = yy;
length++;
}
else
{
// 前移之后,尾部清空
ground.game[xx][yy]=' ';
}

}

bool Snake::isdirection(char a)
{
switch(a)
{
case 'w':
case 's':
case 'a':
case 'd':
return true;
default:
return false;
}
}
//显示蛇
void Snake::showsnake()
{
int i;
for (i=0;i<length;i++)
ground.game[x[i]][y[i]]='@';
}

bool Snake::iseatbody()
{
int i;
cout<<"Snake iseatbody"<<endl;
for(i=3;i<length;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/
{
if(x[i]==x[0]&&y[i]==y[0])
{
cout<<"Game over"<<endl;/*显示失败*/
return 0;
}
}
return 1;
}
void Snake::drawme()
{
//dcout<<"Snake drawme"<<endl;
x[0]=10;y[0]=10;/*蛇头*/
x[1]=11;y[1]=10;
length=2;/*节数*/
ground.game[x[0]][y[0]]='@';
ground.game[x[1]][y[1]]='@';
}


//是否碰到墙壁
bool Snake::ishitground()
{
cout<<"is Snake hitground ?"<<endl;
if(x[0]<=0||x[0]>=19||y[0]<=0||y[0]>=29)/*蛇是否撞到墙壁*/
{
cout<<"Game over"<<endl;/*本次游戏结束*/
return 0;
}
else return 1;
}

//是否吃到食物
bool Snake::iseatfood()
{
if (x[0]==food.x&&y[0]==food.y)
return 1;
else return 0;
}

//设置墙壁的长和宽
void Ground::setdrawme()
{
int i,j;
for( i=0; i<20; i++)
for( j=0; j<30; j++)
{
if(i==0||i==19||j==0||j==29)
game[i][j]= '*';
else game[i][j]= ' ';
}

}

//显示出墙壁
void Ground::drawme()
{
int i,j;
for( i=0; i<20; i++)
{
for(j=0; j<30; j++)
cout<<game[i][j]<<" ";
cout<<endl;
}
}

void Food::getfood ()
{
//cout<<"Snake getfood "<<endl;
srand((unsigned int) time(NULL)); //做种子(程序运行时间);
x= rand()%18+1;
y= rand()%28+1;
ground.game[x][y]='*';

}

int main ()
{
Snake snake;
ground.setdrawme();
snake.drawme();
food.getfood();
ground.drawme();
while (true )
{
char a=getch();//getch()返回键盘上读取的字符;包含头文件<conio.h>
bool bl = snake.isdirection(a);
if (bl)//判断有没有按键落下;
{
system("cls");//清屏函数;
snake.move(a);
snake.showsnake();
if( snake.iseatfood()==1)
food.getfood();
ground.drawme();

if( snake.iseatbody()==0)
exit (0);
else if (snake.ishitground()==0)
exit (0);
Sleep(100);
}
}
// ground.drawme();
return 0;
}

主要是移动算法有问题,可以先在纸上计算一下移动的规律,然后再编码思路就比较清晰。
A28496647 2012-10-13
  • 打赏
  • 举报
回复
关于这一句我想说一下:"/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/",为什么是每个环节向前移动,你真的把你的代码当成蛇啦,只需将蛇的最后一节移到最前边来就实现了蛇的走动,根据按下的方向键判断具体移到头部的哪个位置,同时只需判断这一节蛇有没有撞墙、吃到食物等等。
皮特尔 2012-10-13
  • 打赏
  • 举报
回复
网上有源码的,可以参考一下。

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧