首页 > 文章资讯 > 新闻资讯
C语言基础项目:200 行代码实现贪吃蛇!思路+源码详解
更新时间:2022-08-10 21:37:26 作者:云寺手游


思路分析:

(1)使用该函数首先应在开头包含头文件stdlib.h

#include<stdlib.h>(C++建议使用#include<cstdlib>,下同)

(2)在标准的C库中函数rand()可以生成0~RAND_MAX之间的一个随机数,其中RAND_MAX是stdlib.h 中定义的一个整数,它与系统有关。

(3)rand()函数没有输入参数,直接通过表达式rand()来引用;例如可以用下面的语句来打印两个随机数:

printf(“Random numbers are: %i %in”,rand(),rand());

(4)因为rand()函数是按指定的顺序来产生整数,因此每次执行上面的语句都打印相同的两个值,所以说C语言的随机并不是真正意义上的随机,有时候也叫伪随机数。

(5)为了使程序在每次执行时都能生成一个新序列的随机值,我们通常通过为随机数生成器提供一粒新的随机种子。函数srand()(来自stdlib.h)可以为随机数生成器播散种子。只要种子不同rand()函数就会产生不同的随机数序列。srand()称为随机数生成器的初始化器。

修改部分及bug:

1.速度值反show函数及操作中的bug

2.源代码注释

3.新增最高纪录变量

源码展示:(CSDN:Shawn Hou)

#include <windows.h> #include <stdlib.h> #include <conio.h> #include <time.h> #include <cstring> #include <cstdio> #include <iostream> #define N 25 using namespace std; int gameover;//游戏失败的值 int x1, y1; // 随机生成食物的坐标 int x,y; int record=0; //当前用户最高纪录 long start; //下面定义贪吃蛇的坐标类 class snake_position { public: int 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<N;i++) { cout << 't'; for(j=0;j<N;j++) cout<<s[i][j]<<' '; // 输出贪吃蛇棋盘 if(i==2) cout << "t等级:" << grade; if(i==6) cout << "t得分:" << score << "分" ; if(i==10) cout<<"t最高纪录:"<<record<<"分" ; if(i==14) cout << "t暂停:按一下空格键" ; if(i==18) cout << "t继续:按两下空格键" ; if(i==20) cout<<"t提示:按住方向键可以加速,分数计算公式为个数*等级" ; if(i==24) cout<<"t非空格键结束游戏" ; cout<<endl; } } //输入选择等级 void snake_map::getgrade() { cin>>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 << "ntttt等级:" << grade; cout << "nnntttt得分:" << score << "分" ; cout << "nnntttt最高纪录:"<<record<<"分" ; cout << "nnnttttwww.omegaxyz.com"; } //随机产生米 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*1; //加分 if(snake_map::score>record) record=score; setpoint(); //产生米 } else { // 不吃米 s[position[tail].x][position[tail].y]=' ';//将蛇尾置空 tail= (tail+1) % ( (N-2) * (N-2) );//更新蛇尾坐标 s[position[head].x][position[head].y]='*'; //将蛇头更为蛇身 head= (head+1) % ( (N-2) * (N-2) ); position[head].x = x; position[head].y = y; s[position[head].x][position[head].y]='#'; //更新蛇头 gameover = 1; } return gameover; } //==================================== //主函数部分 //==================================== int main() { char ctn = 'y'; int nodead; cout<<"tt -----------------------------------------------------"; cout<<"ntttt 贪吃蛇"<<endl; cout<<"nnnntttt 感谢Shawn Hou提供源代码"<<endl; cout<<"ntttt xyjigsaw修改"<<endl; cout<<"ntttt 修改内容:ntttt 1.速度bugntttt 2.随机生成选择的固定性ntttt 3.源代码注释ntttt 4.新增最高纪录变量"<<endl; cout<<"ntttt 空格暂停问题未优化,请大家指正"<<endl; cout<<"nntttt 按任意键开始----->>"<<endl; cout<<"nnnntttt www.omegaxyz.com"; cout<<"ntt -----------------------------------------------------"<<endl; getch(); while( ctn=='y' ) { system("cls"); snake_map snake; snake.initialize(); cout << "nn请选择游戏等级:" << endl; cout << "nnnttt1.辣鸡:速度 100 nnttt2.菜鸟:速度 200 nnttt 3.入门:速度 400 "; cout << "nnttt4.中等:速度 600 nnttt5.较快:速度 800 nnttt 6.飞:速度 1000 nnttt7.随机" << endl; snake.getgrade();//获取等级 for(int i=1;i<=4;i++) { position[i].initialize(i);//初始化坐标 } snake.setpoint(); // 产生第一个食物 do { snake.show_game(); nodead = snake.updata_game(); }while(nodead); system("cls"); //清屏 cout<<"ttt -----------------------------------------"; cout << "nnnttttGameover!nn"<<endl; snake.display();//输出等级/得分情况 cout << "nnnttt 是否继续?输入 y 继续,n 退出"<<endl; cout<<"ttt -----------------------------------------"<<endl; cin >> ctn; } return 0; }

效果展示:





希望对大家有帮助,有什么C/C++学习上的问题也可以来和我交流!

写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!

编程学习书籍分享:


编程学习视频分享:


整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)

欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!

对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!

猜你喜欢
最新资讯