logo资料库

贪吃蛇游戏----简易版.docx

第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
资料共14页,剩余部分请下载后查看
小组基本信息 作业题目 贪吃蛇小游戏 功能描述 1、控制游戏:按方向键 W,A,S,D 能控制蛇的移动 2、贪吃的在固定的范围内移动,不能撞到自身,负责,游戏结束 3、固定范围内随机出现食物,蛇每吃一个食物长一格,长度增加 设计思路(或核心流程图) 1.果实出现的设计思路 (1)采用随机数生成果实出现目标 (2)判断当前生成的果实是否再贪吃蛇的身体范围内 (3)如果在,重新生成直到不在为止。如果不在,则把坐标位置返回给调用对象 2.贪吃蛇更新的设计思路 (1)接受玩家按下的方向键消息,并保存到方向向量中。 (2)定义一个时间定时器 (3)当每次时间间隔到达时,则根据方向变量来更新贪吃蛇 bodyx 向量。 (4)判断 bodyx 向量的第一个元素的坐标数是否碰到边界或者蛇身,如果有,转到第 (7)步。 (5)判断 body 向量的第一个元素中的坐标数据是否与当前果实坐标重合,如果有, 表示贪吃蛇已经吃到果实,这时就向贪吃蛇 body 向量添加一个元素,并重新生成一个果 实。、 (6)重绘整个贪吃蛇界面及果实,重复前面步骤(1)~(6) (7)游戏结束时,计算当前游戏得分,并显示游戏所用时间 核心源代码 #include #include #include #include #include #include #include #define N 22 using namespace std; int gameover; int x1, y1; // 随机出米
int x,y; long start; //======================================= //类的实现与应用 initialize //======================================= //下面定义贪吃蛇的坐标类 class snake_position { public: int x,y; //x 表示行,y 表示列 snake_position(){}; void initialize(int &);//坐标初始化 }; snake_position position[(N-2)*(N-2)+1]; //定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标 void snake_position::initialize(int &j) { x = 1; y = j; } //下面定义贪吃蛇的棋盘图 class snake_map { private: char s[N][N];//定义贪吃蛇棋盘,包括墙壁。 int grade, length; int gamespeed; //前进时间间隔
char direction; // 初始情况下,向右运动 int head,tail; int score; bool gameauto; public: snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){} void initialize(); //初始化函数 void show_game(); int updata_game(); void setpoint(); void getgrade(); void display(); }; //定义初始化函数,将贪吃蛇的棋盘图进行初始化 void snake_map::initialize() { int i,j; for(i=1;i<=3;i++) s[1][i] = '*'; s[1][4] = '#'; for(i=1;i<=N-2;i++) for(j=1;j<=N-2;j++)
s[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分 for(i=0;i<=N-1;i++) s[0][i] = s[N-1][i] = '-'; //初始化贪吃蛇棋盘上下墙壁 for(i=1;i<=N-2;i++) s[i][0] = s[i][N-1] = '|'; //初始化贪吃蛇棋盘左右墙壁 } //============================================ //输出贪吃蛇棋盘信息 void snake_map::show_game() { system("cls"); // 清屏 int i,j; cout << endl; for(i=0;i
cout<>grade; while( grade>7 || grade<1 ) { cout << "请输入数字 1-7 选择等级,输入其他数字无效" << endl; cin >> grade; } switch(grade) { case 1: gamespeed = 1000;gameauto = 0;break; case 2: gamespeed = 800;gameauto = 0;break; case 3: gamespeed = 600;gameauto = 0;break; case 4: gamespeed = 400;gameauto = 0;break; case 5: gamespeed = 200;gameauto = 0;break; case 6: gamespeed = 100;gameauto = 0;break; case 7: grade = 1;gamespeed = 1000;gameauto = 1;break; } } //输出等级,得分情况以及称号 void snake_map::display() { cout << "\n\t\t\t\t 等级:" << grade; cout << "\n\n\n\t\t\t\t 速度:" << gamespeed;
cout << "\n\n\n\t\t\t\t 得分:" << score << "分" ; } //随机产生米 void snake_map::setpoint() { srand(time(0)); do { x1 = rand() % (N-2) + 1; y1 = rand() % (N-2) + 1; }while(s[x1][y1]!=' '); s[x1][y1]='*'; } char key; int snake_map::updata_game() { gameover = 1; key = direction; start = clock(); while((gameover=(clock()-start<=gamespeed))&&!kbhit()); //如果有键按下或时间超过自动前进时间间隔则终止循环 if(gameover) { getch(); key = getch();
} if(key == ' ') { while(getch()!=' '){};//这里实现的是按空格键暂停,按空格键继续的功能,但 不知为何原因,需要按两下空格才能继续。这是个 bug。 } else direction = key; switch(direction) { case 72: x= position[head].x-1; y= position[head].y;break; // 向上 case 80: x= position[head].x+1; y= position[head].y;break; // 向下 case 75: x= position[head].x; y= position[head].y-1;break; // 向左 case 77: x= position[head].x; y= position[head].y+1; // 向右 } if(!(direction==72||direction==80||direction==75 ||direction==77)) // 按键非方 向键 gameover = 0; else if(x==0 || x==N-1 ||y==0 || y==N-1) // 碰到墙壁 gameover = 0; else if(s[x][y]!=' '&&!(x==x1&&y==y1)) // 蛇头碰到蛇身 gameover = 0; else if(x==x1 && y==y1) { // 吃米,长度加 1 length ++;
if(length>=8 && gameauto) { } length -= 8; grade ++; if(gamespeed>=200) gamespeed -= 200; // 改变贪吃蛇前进速度 else gamespeed = 100; s[x][y]= '#'; //更新蛇头 s[position[head].x][position[head].y] = '*'; //吃米后将原先蛇头变为蛇身 head = (head+1) % ( (N-2)*(N-2) ); //取蛇头坐标 position[head].x = x; position[head].y = y; show_game(); gameover = 1; score += grade*20; //加分 setpoint(); //产生米 } else { // 不吃米 s[position[tail].x][position[tail].y]=' ';//将蛇尾置空 tail= (tail+1) % ( (N-2) * (N-2) );//更新蛇尾坐标
分享到:
收藏