C/C++貪吃蛇遊戲,zjlj,2015.3.16
*/
#定義?調試?0?//程序在調試階段的時候?調試是?1
# include & ltiostream & gt
# include & ltwindows.h & gt
# include & lttime.h & gt
# include & ltconio.h & gt
使用?命名空間?std
作廢?readini(文件?**fphead,?int?*分數,?夏爾?*argv[])?//創建或打開運行文件對應的ini文件,讀取最高記錄。
{
夏爾?文件名[200],* pfilename
int?flag=-1,I;
strcpy(文件名,argv[0]);
for(I = 0;文件名[i]!='\0';i++)
{
如果?('.'= = filename[I])flag = 1;
}
if(1==flag)
{
filename[I-1]= ' I ';
文件名[I-2]= ' n ';
文件名[I-3]= ' I ';
}
其他
{
文件名[i]= ' . ';
filename[I+1]= ' I ';
文件名[I+2]= ' n ';
文件名[I+3]= ' I ';
文件名[I+4]= ' \ 0 ';
}
for(;文件名[i]!= ' \ \ ' & amp& amp我& gt=0;I-)pfilename = & amp;文件名[I];
如果?(?(*fphead=fopen(pfilename," r b+ ")= = NULL)
{
如果?(?(*fphead=fopen(pfilename," WB+")= = NULL)
{
Printf("無法創建或打開\"%s\ "文件\n ",pfilename);
系統(“暫停”);
退出(0);
}
}
其他
{
fread(score,sizeof(int),1,* fphead);
}
}
作廢?writeini(文件?**fphead,?int?*分數,?夏爾?*argv[])//打開運行文件對應的ini文件,寫入最高記錄。
{
夏爾?文件名[200],* pfilename
int?flag=-1,I;
strcpy(文件名,argv[0]);
for(I = 0;文件名[i]!='\0';i++)
{
如果?('.'= = filename[I])flag = 1;
}
if(1==flag)
{
filename[I-1]= ' I ';
文件名[I-2]= ' n ';
文件名[I-3]= ' I ';
}
其他
{
文件名[i]= ' . ';
filename[I+1]= ' I ';
文件名[I+2]= ' n ';
文件名[I+3]= ' I ';
文件名[I+4]= ' \ 0 ';
}
for(;文件名[i]!= ' \ \ ' & amp& amp我& gt=0;I-)pfilename = & amp;文件名[I];
如果?(?(*fphead=fopen(pfilename," WB+")= = NULL)
{
Printf("無法寫入\"%s\ "文件,磁盤被寫保護!\n ",pfilename);
系統(“暫停”);
退出(0);
}
其他
{
倒帶(* fphead);
fwrite(score,sizeof(int),1,* fphead);
fclose(* FP head);
}
}
作廢?gotoxy(int?x,int?Y)//光標定位,光標定位函數SetConsoleCursorPosition是左上角位置為0,然後向左下方延伸。
{
坐標?pos
位置。x = 2 * y;
位置。y = x;
SetConsoleCursorPosition(GetStdHandle(STD _ OUTPUT _ HANDLE),pos);
}
作廢?顏色(int?A)//顏色函數
{
SetConsoleTextAttribute(GetStdHandle(STD _ OUTPUT _ HANDLE),a);
}
作廢?刷新(int?q[][22],?int?等級,?int?gamespeed?int?長度,int?分數)?//輸出蛇板
{
int?I,j;
for(I = 0;我& lt22;i++)
{
for(j = 0;j & lt22;j++)
{
If(q[i][j]==0)//輸出棋盤空白。
{
gotoxy(i,j);
顏色(11);
cout & lt& lt"■";
}
if(q[I][j]= = 1 | | q[I][j]= = 2)//輸出棋盤墻
{
gotoxy(i,j);
顏色(11);
cout & lt& lt"□";
}
If(q[i][j]==3)//輸出黑魚
{
gotoxy(i,j);
顏色(14);
cout & lt& lt"★";
}
If(q[i][j]==4)//輸出蛇體。
{
gotoxy(i,j);
顏色(12);
cout & lt& lt"◆";
}
If(q[i][j]==5)//輸出果實。
{
gotoxy(i,j);
顏色(12);
cout & lt& lt"●";
}
}
if(i==0)?cout?& lt& lt?”\ t * * * * * * * * * * * * * * * * * * * * * *”;
if(i==1)?cout?& lt& lt?" \t等級:"?& lt& lt?等級;//顯示級別
if(i==3)?cout?& lt& lt?”\t自動提前時間”;
if(i==4)?cout?& lt& lt?" \t間隔是:"?& lt& lt?gamespeed?& lt& lt?”女士”;//顯示時間
if(i==6)?cout?& lt& lt?" \ t歷史最高分是"?& lt& lt?得分?& lt& lt?“分”;
if(i==7)?cout?& lt& lt?\t妳現在的分數是多少?& lt& lt?(長度+(等級-1)*8)*10?& lt& lt?“分”;
if(i==8)?cout?& lt& lt?" \ t * * * * * * * * * * * * * * * * * * * * * * * *;
if(i==9)?cout?& lt& lt?" \ t遊戲描述:";
if(i==10)?cout?& lt& lt?”\t(1)由鍵盤控制”;
if(i==11)?cout?& lt& lt?" \ t蛇頭的運動方向;";
if(i==12)?cout?& lt& lt?" \t(2)蛇每吃壹個果子,蛇身";
if(i==13)?cout?& lt& lt?" t添加壹個節;";
if(i==14)?cout?& lt& lt?“\t(3)蛇咬自己或撞墻”;
if(i==15)?cout?& lt& lt?"墻壁遊戲結束了。";
if(i==18)?cout?& lt& lt?" \ t * * * * * * * * * * * * * * * * * * * * * * * *;
if(i==19)?cout?& lt& lt?" \tC/C++語言作業:";
如果(i==20)?cout?& lt& lt?\tzjlj,2015.03.16?;
}
}
int?main(int?argc,?夏爾?*argv[]){
int?tcsQipan[22][22];?//蛇棋盤是二維數組(比如22*22,包括墻壁)。
int?I,j,score,directiontemp
FILE * fpini//*fpini?信息文件
readini(& amp;fpini,?& amp分數,?argv);//讀取ini文件的最高記錄。
如果?(分數& lt0)//如果最高分小於零,設置為零,則初始文件為負數。
得分= 0;
while(1)
{
for(I = 1;我& lt=20;i++)
for(j = 1;j & lt=20;j++)
tcsQipan[I][j]= 0;//蛇棋盤對應的坐標用中間空白部分的符號0標註。
for(I = 0;我& lt=21;i++)
tcsQipan[0][i]?=?tcsQipan[21][i]?=?1;//蛇棋盤對應坐標標有上下壁1的標誌。
for(I = 1;我& lt=20;i++)
tcsQipan[i][0]?=?tcsQipan[i][21]?=?2;//蛇棋盤的對應坐標在左右兩面墻上用標誌標出2。
int?tcszoo Biao[2][500];?//snake坐標數組
for(I = 0;?我& lt4;?i++)
{
tcszoobao[0][I]?=?1;//蛇身和蛇頭的X坐標
tcszoobao[1][I]?=?我?+?1;//蛇身和蛇頭的Y坐標
}
int?頭?=?3、尾巴?=?0;//表示蛇頭和蛇頭的數組偏移量。
for(I = 1;我& lt=3;i++)
tcsQipan[1][I]= 4;//蛇身
tcsQipan[1][4]= 3;?//蛇頭
int?x1,?y 1;?//?隨機水果
srand(time(0));//設置隨機種子
做
{
x 1 = rand()% 20+1;
y 1 = rand()% 20+1;
}
while(tcsQipan[x 1][y 1]!=0);//如果妳不在空白處重新生產水果,
tcsQipan[x 1][y 1]= 5;//蛇棋盤對應的坐標標有水果5的符號。
顏色(12);
cout & lt& lt" \ n \ n \ t \ t \ t \貪吃蛇遊戲就要開始了?!"& lt& ltendl//準備開始
龍?start,starttemp
int?年級?=?1,?長度?=?4;//設置蛇的初始級別和初始長度。
int?gamespeed?=?500;//設置初始轉發時間間隔
for(I = 3;我& gt=0;我-)
{
start =時鐘();
while(clock()-start & lt;=1000);
系統(“cls”);
如果(i & gt0)
cout?& lt& lt?\ n \ n \ t \ t \ t \輸入倒計時?& lt& lt?我?& lt& lt?endl//倒計時顯示
其他
刷新(tcsQipan,等級,gamespeed,長度,分數);//初始棋盤顯示
}
int?timeover=1,other key = 1;//初始化超時和關鍵判斷參數。
夏爾?方向?=?77;//?在初始設置中,向右移動。
int?x = tcszuobao[0][head],y = tcszuobao[1][head];//將黑魚的坐標保存到x,y變量中。
While(1)//運行壹個遊戲。
{
開始?=?clock();
while((time over =((start temp = clock())-start & lt;= gamespeed))& amp;& amp!kb hit());//如果按下某個鍵或時間超過自動前進間隔,則循環終止。
if(方向= = 72 | |方向= = 80 | |方向==75?| |方向==77)
directiontemp =方向;//保留最後壹個方向鍵。
//start temp = gamespeed+start-start temp;//保留時間
如果(超時)
{
#如果?(調試==1)
方向?=?getch();//調試代碼
#否則
如果((方向?=getch())==-32)
方向?=?getch();
#endif
}
#如果?(DEBUG==1)//調試代碼
start =時鐘();
while(clock()-start & lt;=2000);
gotoxy(24,4);
cout?& lt& lt?" \ t鍵入ASCII代碼"
#endif
如果(!(方向= = 72 | |方向= = 80 | |方向==75?| |方向==77))
{?
other key = 0;//該鍵不是方向鍵,otherkey設置為0。
}
其他
{
other key = 1;//鍵是方向鍵,otherkey設置為1。
}
如果(方向==72?& amp& amp?Directiontemp==80)//忽略反向鍵。
{
方向= 32°;
other key = 0;
//開始?=?clock();
//while(clock()-start & lt;= start temp);
}
不然呢?如果(方向==80?& amp& amp?directiontemp==72)
{
方向= 32°;//將鍵設置為非方向鍵。
other key = 0;//該鍵為非方向鍵,otherkey設置為0。
//?開始?=?clock();
//while(clock()-start & lt;= start temp);//補償等待時間
}
不然呢?如果(方向==75?& amp& amp?directiontemp==77)
{
方向= 32°;
other key = 0;
//開始?=?clock();
//while(clock()-start & lt;= start temp);
}
不然呢?如果(方向==77?& amp& amp?directiontemp==75)
{
方向= 32°;
other key = 0;
//開始?=?clock();
//while(clock()-start & lt;= start temp);
}
開關(方向)//確定方向鍵
{
案子?72:?x=?tcszuobao[0][head]-1;?y=?tcszuobao[1][head];打破;//?向上
案子?80:?x=?tcszuobao[0][head]+1;?y=?tcszuobao[1][head];打破;//?向下
案子?75:?x=?tcszoobao[0][head];?y=?tcszuobao[1][head]-1;打破;//?向左
案子?77:?x=?tcszoobao[0][head];?y=?tcszuobao[1][頭]+1;打破;//?向右走
默認:?打破;
}
if(x==0?||?x==21?||y==0?||?y==21)//?蛇頭撞墻,結束這局比賽。
{
gotoxy(22,12);
cout?& lt& lt?" \ t遊戲結束了!"?& lt& lt?endl
if(score & gt;=(length+(grade-1)* 8)* 10)//判斷是否破紀錄。
{
gotoxy(10,7);
顏色(12);
cout?& lt& lt?“沒能打通?加油!”?& lt& lt?endl
fclose(fpini);//關閉ini文件
}
其他
{
gotoxy(10,7);
顏色(12);
cout?& lt& lt?“恭喜破紀錄”?& lt& lt?endl
得分=(長度+(年級-1)* 8)* 10;
write ini(& amp;fpini,?& amp分數,?argv);ini文件中寫入的最高記錄
}
gotoxy(23,12);
cout?& lt& lt?“按回車重啟,按ESC退出遊戲”?& lt& lt?endl//顯示提示
打破;//退出遊戲
}
if(tcsQipan[x][y]!= 0 & amp& amp!(x = = x 1 & amp;& ampy = = y 1)& amp;& amptcsQipan[x][y]!=3)?//?蛇頭碰到蛇身,結束此局遊戲。
{
gotoxy(22,12);
cout?& lt& lt?" \ t遊戲結束了!"?& lt& lt?endl
if(score & gt;=(length+(grade-1)* 8)* 10)//判斷是否破紀錄。
{
gotoxy(10,7);
顏色(12);
cout?& lt& lt?“沒能打通?加油!”?& lt& lt?endl
fclose(fpini);//關閉ini文件
}
其他
{
gotoxy(10,7);
顏色(12);
cout?& lt& lt?“恭喜破紀錄”?& lt& lt?endl
得分=(長度+(年級-1)* 8)* 10;
write ini(& amp;fpini,?& amp分數,?argv);ini文件中寫入的最高記錄
}
gotoxy(23,12);
cout?& lt& lt?“按回車重啟,按ESC退出遊戲”?& lt& lt?endl//顯示提示
打破;//退出遊戲
}
/*
遊戲運行時核心算法開始。
*/
if(x==x1?& amp& amp?y==y1)?//吃水果,長度加1
{?
長度?++;
if(長度& gt=8)//如果長度大於等於8,則重新計算長度,並將1加到等級上。
{
長度?-=?8;//重新計算長度
年級?++;//年級加1
if(gamespeed & gt;50)//最快控制速度50。
gamespeed?=?550?-?年級?*?50;?//?更改自動推進時間間隔
}
tcsQipan[x][y]=?3;//貪吃蛇棋盤對應坐標現在改為蛇頭符號3。
tcsQipan[tcszuobao[0][head]][tcszuobao[1][head]]?=?4;//貪吃蛇棋盤對應坐標由原來的蛇頭符號改為蛇身符號4。
頭?=?(head+1)% 400;//防止數組越界
tcszoobao[0][head]?=?x;//蛇頭的X坐標
tcszoobao[1][head]?=?y;//蛇頭的Y坐標
做//隨機水果
{
x 1 = rand()% 20+1;
y 1 = rand()% 20+1;
}
while(tcsQipan[x 1][y 1]!=0);//如果妳不在空白處重新生產水果,
tcsQipan[x 1][y 1]= 5;//蛇棋盤對應的坐標標有水果5的符號。
gotoxy(22,12);
cout?& lt& lt?" \ t遊戲正在進行中!"?& lt& lt?endl
刷新(tcsQipan,等級,gamespeed,長度,分數);
}
Else//不吃水果
{
if(其他鍵)
{
tcsQipan?[tcszuobao[0][tail]][tcszuobao[1][tail]]= 0;
tail =(tail+1)% 400;//防止數組越界
tcsQipan?[tcszuobao[0][head]][tcszuobao[1][head]]= 4;
head =(head+1)% 400;//防止數組越界
tcszuobao[0][head]= x;//蛇頭的X坐標
tcszuobao[1][head]= y;//蛇頭的Y坐標
tcsQipan[tcszuobao[0][head]][tcszuobao[1][head]]= 3;
gotoxy(22,12);
cout?& lt& lt?" \ t遊戲正在進行中!"?& lt& lt?endl
刷新(tcsQipan,等級,gamespeed,長度,分數);
}
其他
{
gotoxy(22,12);
cout?& lt& lt?" \ t遊戲暫停!"?& lt& lt?endl
}
}
/*
遊戲運行時的核心算法結束。
*/
}
while(1)
{
而(!kb hit());
如果((方向?= getch()= = 13)//回車開始下壹局。
打破;
如果(方向?==27)//按ESC退出遊戲。
退出(0);
}
系統(“cls”);//清空屏幕,重新開始。
}
回歸?0;
}