用C++開發一個能在編譯窗口中運行的貪吃蛇小遊戲,實現環境爲:Microsoft visual C++6.0集成開發環境
算法及程序說明:
用類構造蛇
class Snake
{
public:
int head,tail,body[200],length;
};
Snake T;
void init() //初始化函數
{
T.head=3;
T.tail=0;
memset(T.body,0,sizeof(T.body)); // memset(array,值,count)
// 此函數功能:將array數組從頭到count的內容全部 設置爲//這個“值”。
}
繪圖實現
int map[100][100]; // 定義一個整形的二維數組做標記
char maze[100][100]; // 根據map數組的不同數值顯示不同符號, -1表示牆,1表
//示食物,0表示空,以此繪圖
for(i=1;i<20;i++)
for(j=1;j<40;j++){
card[k++]=i*100+j;
}//用card數組來存障礙牆內位置的編號
random_shuffle(card,card+19*39);//打亂編號的次序,用於後//面隨機生成食物
//random_shuffle(card,card+19*39)
//將card數組中前19*39個數據的順序打亂
地圖的預處理
for(i=0;i<=40;i++)
map[i][0]=map[20][i]=map[i][40]=map[0][i]=-1;
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)
for(j=0;j<=40;j++){
if(map[i][j]==-1)
maze[i][j]='#';
else if(map[i][j]==1)
maze[i][j]='o';
}
for(i=T.tail;i!=T.head;){
sum=T.body[i];
maze[sum/100][sum0]='*';
++i;
i%=ML;
}
sum=T.body[(i-1+ML)%ML];
maze[sum/100][sum0]='@';
最後生成圖像
for(i=0;i<=20;i++)
{
for(j=0;j<=40;j++)
{
cout<<maze[i][j];
}
cout<<endl;
}
畫面動態顯示實現
主要運用system("CLS")的刷屏功能來實現畫面的動態效果。
利用臨時無窮循環制作刷屏時間控制
int TM=300; //300毫秒
(TM>100)?(TM=300-T.length*10):(TM=100);
int start=clock();
while(clock()-start<=TM && !kbhit()) //kbhit()檢查當前是否有鍵盤輸入,若有則返回一個非0值,否//則返回0
{
;
}
蛇運動的實現(以向上爲例)
if(order=='w'){
jud=order;
if(map[sum/100-1][sum0]!=-1){
for(i=T.tail;i!=T.head;){
temp=T.body[i];
map[temp/100][temp0]=0;
i++;
i%=ML;
}
up();
}else{
system("CLS");//提示遊戲結束
break;
}
}
UP()函數
void up()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]-100;
if(map[sum/100][sum0]==1){
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum0]=0;
for(i=T.tail;i!=T.head;){
sum=T.body[i];
map[sum/100][sum0]=-1;
i++;
i%=ML;
}
while(1){
sum=getnum();
if(map[sum/100][sum0]==0){
map[sum/100][sum0]=1; break;
}
}
for(i=T.tail;i!=T.head;){
sum=T.body[i];
map[sum/100][sum0]=0;
i++;
i%=ML;
}
}else{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
遊戲開始時,score爲0;小蛇默認從左上角向右走。
每吃一個食物,蛇身長度+1,score+10。
當蛇頭觸碰到四周牆壁或觸碰到蛇身時,遊戲結束,顯示最終得分。
當蛇吃食物的個數到達程序中設定的某個值時,挑戰遊戲成功。
完整代碼: 》僅供參考,如有錯誤盡請諒解《
#include<iostream>
#include<cstdlib>//該函數主要可以提供一些函數與符號常量
#include<algorithm>//提供大量基於迭代器的非成員模版函數
#include<conio.h>//其中定義了通過控制檯進行數據輸入和數據輸出的函數
#include<time.h>//日期和時間頭文件
#include<windows.h>
#define ML 100
using namespace std;
class Snake //聲明一個類Snake
{
public:
int head,tail,body[200],length;
};
Snake T; //定義一個對象T
int map[100][100]; //定義一個整形的二維數組做標記用
char maze[100][100]; //根據map數組的不同數值顯示不同符號,以此繪圖
void init() //初始化函數
{
T.head=3;
T.tail=0;
// T.length=0;
memset(T.body,0,sizeof(T.body));
}
int card[800];
char order; //接收指令
int getnum()
{
static int n=0;
n++;
n=n%800;
return card[n];
}
void up()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]-100;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void down()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]+100;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void right()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]+1;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void left()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]-1;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
int main()
{
memset(map,0,sizeof(map));
memset(maze,' ',sizeof(maze));
int i,j,sum=0,k=0,temp;
for(i=1;i<20;i++)
for(j=1;j<40;j++)
{
card[k++]=i*100+j;
}
srand(time(0));
temp=rand()%10+1;
while(temp--)
random_shuffle(card,card+19*39);
for(i=0;i<=40;i++)
map[i][0]=map[20][i]=map[i][40]=map[0][i]=-1;
init();
T.length=1;
T.body[T.head++]=101;
T.head%=ML;
sum=getnum();
map[sum/100][sum%100]=1;
char jud='d';
int TM=300,start;
while(1)
{
(TM>100)?(TM=300-T.length*60):(TM=50);
start=clock();
//利用臨時無窮循環制作刷屏時間
while(clock()-start<=TM && !kbhit()) //kbhit() 檢查當前是否有鍵盤輸入,若有則返回一個非0值,否則返回0
{
;
}
if(kbhit()&&(order=getch(),order=='w'||order=='s'||order=='a'||order=='d'))
{
sum=T.body[(T.head-1+ML)%ML];
system("CLS");
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=-1;
i++;
i%=ML;
}
if(order=='w')
{
jud=order;
//cout<<"-->w\n";
if(map[sum/100-1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
up();
}
else
{
system("CLS");
cout<<"============================================"<<endl;
cout<<" 很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;
cout<<"============================================"<<endl;
break;
}
}
else if(order=='a')
{
jud=order;
//cout<<"-->a\n";
if(map[sum/100][sum%100-1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
left();
}
else
{
system("CLS");
cout<<"============================================"<<endl;
cout<<" 很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;
cout<<"============================================"<<endl;
break;
}
}
else if (order=='s')
{
jud=order;
//cout<<"-->s\n";
if(map[sum/100+1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
down();
}
else
{
system("CLS");
cout<<"============================================"<<endl;
cout<<" 很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;
cout<<"============================================"<<endl;
break;
}
}
else if(order=='d')
{
jud=order;
//cout<<"-->d\n";
if(map[sum/100][sum%100+1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
right();
}
else
{
system("CLS");
cout<<"============================================"<<endl;
cout<<" 很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;
cout<<"============================================"<<endl;
break;
}
}
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)
for(j=0;j<=40;j++)
{
if(map[i][j]==-1)
maze[i][j]='#';
else if(map[i][j]==1)
maze[i][j]='o';
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
maze[sum/100][sum%100]='*';
++i;
i%=ML;
}
sum=T.body[(i-1+ML)%ML];
maze[sum/100][sum%100]='@';
cout<<"遊戲操作: ↑:w ↓:s ←:a →:d"<<endl;
cout<<"score: "<<T.length*10-10<<endl;
for(i=0;i<=20;i++)
{
for(j=0;j<=40;j++)
{
cout<<maze[i][j];
}
cout<<endl;
}
}
else
{
sum=T.body[(T.head-1+ML)%ML];
system("CLS");
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=-1;
i++;
i%=ML;
}
if(jud=='w')
{
//cout<<"-->w\n";
if(map[sum/100-1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
up();
}
else
{
system("CLS");
cout<<"============================================"<<endl;
cout<<" 很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;
cout<<"============================================"<<endl;
break;
}
}
else if(jud=='a')
{
//cout<<"-->a\n";
if(map[sum/100][sum%100-1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
left();
}
else
{
system("CLS");
cout<<"============================================"<<endl;
cout<<" 很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;
cout<<"============================================"<<endl;
break;
}
}
else if (jud=='s')
{
//cout<<"-->s\n";
if(map[sum/100+1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
down();
}
else
{
system("CLS");
cout<<"============================================"<<endl;
cout<<" 很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;
cout<<"============================================"<<endl;
break;
}
}
else if(jud=='d')
{
//cout<<"-->d\n";
if(map[sum/100][sum%100+1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
right();
}
else
{
system("CLS");
cout<<"============================================"<<endl;
cout<<" 很遺憾,你輸了!!!最後得分爲:"<<T.length*10-10<<endl;
cout<<"============================================"<<endl;
break;
}
}
if(T.length>=20){
system("CLS");
cout<<"============================================"<<endl;
cout<<" 恭喜,過關!!! "<<endl;
cout<<"============================================"<<endl;
break;
}
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)
for(j=0;j<=40;j++)
{
if(map[i][j]==-1)
maze[i][j]='#';
else if(map[i][j]==1)
maze[i][j]='o';
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
maze[sum/100][sum%100]='*';
++i;
i%=ML;
}
sum=T.body[(i-1+ML)%ML];
maze[sum/100][sum%100]='@';
cout<<"遊戲操作: ↑:w ↓:s ←:a →:d"<<endl;
cout<<"score: "<<T.length*10-10<<endl;
for(i=0;i<=20;i++)
{
for(j=0;j<=40;j++)
{
cout<<maze[i][j];
}
cout<<endl;
}
}
}
system("pause");
return 0;
}