C語言基礎項目:200 行代碼實現貪吃蛇!思路+源碼詳解

c語言編程 發佈 2022-03-10T15:00:46+00:00

思路分析:(1)使用該函數首先應在開頭包含頭文件stdlib.h#include<stdlib.h>(C++建議使用#include<cstdlib>,下同)(2)在標準的C庫中函數rand()可以生成0~RAND_MAX之間的一個隨機數,其中RAND_MAX是stdlib.

思路分析:

(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 %i\n」,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 << "\n\t\t\t\t等級:" << grade;
cout << "\n\n\n\t\t\t\t得分:" << score << "分" ;
cout << "\n\n\n\t\t\t\t最高紀錄:"<<record<<"分" ;
cout << "\n\n\n\t\t\t\twww.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<<"\t\t -----------------------------------------------------";
cout<<"\n\t\t\t\t 貪吃蛇"<<endl;
cout<<"\n\n\n\n\t\t\t\t 感謝Shawn Hou提供原始碼"<<endl;
cout<<"\n\t\t\t\t xyjigsaw修改"<<endl;
cout<<"\n\t\t\t\t 修改內容:\n\t\t\t\t 1.速度bug\n\t\t\t\t
2.隨機生成選擇的固定性\n\t\t\t\t 3.原始碼注釋\n\t\t\t\t 4.新增最高紀錄變量"<<endl;
cout<<"\n\t\t\t\t 空格暫停問題未優化,請大家指正"<<endl;
cout<<"\n\n\t\t\t\t 按任意鍵開始----->>"<<endl;
cout<<"\n\n\n\n\t\t\t\t www.omegaxyz.com";
cout<<"\n\t\t -----------------------------------------------------"<<endl;
getch();
while( ctn=='y' )
{
system("cls");
snake_map snake;
snake.initialize();
cout << "\n\n請選擇遊戲等級:" << endl;
cout << "\n\n\n\t\t\t1.辣雞:速度 100 \n\n\t\t\t2.菜鳥:速度 200 \n\n\t\t\t
3.入門:速度 400 ";
cout << "\n\n\t\t\t4.中等:速度 600 \n\n\t\t\t5.較快:速度 800 \n\n\t\t\t
6.飛:速度 1000 \n\n\t\t\t7.隨機" << 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<<"\t\t\t -----------------------------------------";
cout << "\n\n\n\t\t\t\tGameover!\n\n"<<endl;
snake.display();//輸出等級/得分情況
cout << "\n\n\n\t\t\t 是否繼續?輸入 y 繼續,n 退出"<<endl;
cout<<"\t\t\t -----------------------------------------"<<endl;
cin >> ctn;
}
return 0;
}

效果展示:


希望對大家有幫助,有什麼C/C++學習上的問題也可以來和我交流!

寫在最後:對於準備學習C/C++編程的小夥伴,如果你想更好的提升你的編程核心能力(內功)不妨從現在開始!

編程學習書籍分享:

編程學習視頻分享:

整理分享(多年學習的源碼、項目實戰視頻、項目筆記,基礎入門教程)

歡迎轉行和學習編程的夥伴,利用更多的資料學習成長比自己琢磨更快哦!

對於C/C++感興趣可以關注小編在後台私信我:【編程交流】一起來學習哦!可以領取一些C/C++的項目學習視頻資料哦!已經設置好了關鍵詞自動回復,自動領取就好了!

關鍵字: