国产成人AV一区二区三区在线_国产成人精品无码免费看_国产成人无码无卡在线观看_国产精品成人A区在线观看_国产日韩精品欧美一区_国产亚洲精品自在久久蜜TV_久草热久草热线频97精品_久久久噜噜噜久久中文福利_久久婷婷五月综合色国产免费观看_日日狠狠久久偷偷色综合0,九一桃色在线观看,久久97精品久久久久久久不卡,国产成人精品亚洲精品

C++貪吃蛇編程代碼

訪客2025-01-14 14:40:271

這個(gè)可參考的網(wǎng)上很多的,,我收藏的一個(gè)參考:

#include<iostream>#include<windows.h>#include<time.h>#include<conio.h>using namespace std;// 刷新當(dāng)前屏幕inline void Refresh(char q[][22], int grade, int gamespeed){system("cls"); //清屏 int i,j; cout << endl; for(i=0;i<22;i++){ cout << "";for(j=0;j<22;j++) cout<<q[i][j]<<' ';//輸出貪吃蛇棋盤(pán)if(i==0) cout << "等級(jí)為:" << grade;if(i==4) cout << "自動(dòng)前進(jìn)時(shí)間";if(i==6) cout << "間隔為:" << gamespeed << "ms";cout<<endl; }}

int main(){char tcsQipan[22][22]; //貪吃蛇棋盤(pán)是一個(gè)二維數(shù)組(如22*22,,包括墻壁)int i,j;for(i=1;i<=20;i++)for(j=1;j<=20;j++)tcsQipan[i][j]=' '; // 初始化貪吃蛇棋盤(pán)中間空白部分for(i=0;i<=21;i++)tcsQipan[0][i] = tcsQipan[21][i] = '-';//初始化貪吃蛇棋盤(pán)上下墻壁for(i=1;i<=20;i++)tcsQipan[i][0] = tcsQipan[i][21] = '|';//初始化貪吃蛇棋盤(pán)左右墻壁

int tcsZuobiao[2][100]; //蛇的坐標(biāo)數(shù)組for(i=0; i<4; i++){tcsZuobiao[0][i] = 1;tcsZuobiao[1][i] = i + 1;}int head = 3,tail = 0;

for(i=1;i<=3;i++)tcsQipan[1][i]='*';//蛇身tcsQipan[1][4]='#'; //蛇頭

int x1, y1; // 隨機(jī)出米srand(time(0));do{x1=rand()%20+1;y1=rand()%20+1; }while(tcsQipan[x1][y1]!=' '); tcsQipan[x1][y1]='*';

cout<<"貪吃蛇游戲即將開(kāi)始 !"<<endl;//準(zhǔn)備開(kāi)始;; long start;

int grade = 1, length = 4;int gamespeed = 500;//前進(jìn)時(shí)間間隔 for(i=3;i>=0;i--){start=clock();while(clock()-start<=1000);system("cls");if(i>0) cout << "進(jìn)入倒計(jì)時(shí):" << i << endl;else Refresh(tcsQipan,grade,gamespeed); }

int timeover;char direction = 77;// 初始情況下,,向右運(yùn)動(dòng)int x,y;while(1){timeover = 1;start = clock();while((timeover=(clock()-start<=gamespeed))&&!kbhit()); //如果有鍵按下或時(shí)間超過(guò)自動(dòng)前進(jìn)時(shí)間間隔則終止循環(huán)if(timeover){getch();direction = getch();}switch(direction){case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break;// 向下case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break;// 向左case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1; // 向右}if(!(direction==72||direction==80||direction==75 ||direction==77)){ //按鍵非方向鍵cout << "Game over!" << endl;return 0;}if(x==0 || x==21 ||y==0 || y==21){ // 碰到墻壁cout << "Game over!" << endl;return 0;}if(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1)){ // 蛇頭碰到蛇身cout << "Game over!" << endl;return 0;}

if(x==x1 && y==y1){//吃米,,長(zhǎng)度加1length ++;if(length>=8){length -= 8;grade ++;if(gamespeed>=200)gamespeed = 550 - grade * 50; // 改變自動(dòng)前進(jìn)時(shí)間間隔}tcsQipan[x][y]= '#';tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';head = (head+1)%100;tcsZuobiao[0][head] = x;tcsZuobiao[1][head] = y;do{x1=rand()%20+1;y1=rand()%20+1;}while(tcsQipan[x1][y1]!=' ');tcsQipan[x1][y1]='*';Refresh(tcsQipan,grade,gamespeed);}else{ //不吃米tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';tail=(tail+1)%100;tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';head=(head+1)%100;tcsZuobiao[0][head]=x;tcsZuobiao[1][head]=y;tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';Refresh(tcsQipan,grade,gamespeed);}}return 0;}

文章評(píng)論