日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C语言贪吃蛇如何让蛇一直前进,c++贪吃蛇代码中,哪条代码是让蛇知道前进的

發布時間:2023/12/19 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言贪吃蛇如何让蛇一直前进,c++贪吃蛇代码中,哪条代码是让蛇知道前进的 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該樓層疑似違規已被系統折疊?隱藏此樓查看此樓

#include

#include

#include

#include

using namespace std;

// 刷新當前屏幕

inline void Refresh(char q[][22], int grade, int gamespeed){

system("cls"); // 清屏

int i,j;

cout << endl;

for(i=0;i<22;i++){

cout << "\t";

for(j=0;j<22;j++)

cout<

if(i==0) cout << "\t等級為:" << grade;

if(i==4) cout << "\t自動前進時間";

if(i==6) cout << "\t間隔為:" << gamespeed << "ms";

cout<

}

}

int main(){

char tcsQipan[22][22]; // 貪吃蛇棋盤是一個二維數組(如22*22,包括墻壁)

int i,j;

for(i=1;i<=20;i++)

for(j=1;j<=20;j++)

tcsQipan[i][j]=' '; // 初始化貪吃蛇棋盤中間空白部分

for(i=0;i<=21;i++)

tcsQipan[0][i] = tcsQipan[21][i] = '-'; //初始化貪吃蛇棋盤上下墻壁

for(i=1;i<=20;i++)

tcsQipan[i][0] = tcsQipan[i][21] = '|'; //初始化貪吃蛇棋盤左右墻壁

int tcsZuobiao[2][100]; //蛇的坐標數組

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; // 隨機出米

srand(time(0));

do{

x1=rand()%20+1;

y1=rand()%20+1;

}while(tcsQipan[x1][y1]!=' ');

tcsQipan[x1][y1]='*';

cout<

long start;

int grade = 1, length = 4;

int gamespeed = 500; //前進時間間隔

for(i=3;i>=0;i--){

start=clock();

while(clock()-start<=1000);

system("cls");

if(i>0)

cout << "\n\n\t\t進入倒計時:" << i << endl;

else

Refresh(tcsQipan,grade,gamespeed);

}

int timeover;

char direction = 77; // 初始情況下,向右運動

int x,y;

while(1){

timeover = 1;

start = clock();

while((timeover=(clock()-start<=gamespeed))&&!kbhit());

//如果有鍵按下或時間超過自動前進時間間隔則終止循環

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 << "\tGame over!" << endl;return 0;

}

if(x==0 || x==21 ||y==0 || y==21){ // 碰到墻壁

cout << "\tGame over!" << endl;return 0;

}

if(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1)){ // 蛇頭碰到蛇身

cout << "\tGame over!" << endl;return 0;

}

if(x==x1 && y==y1){ // 吃米,長度加1

length ++;

if(length>=8){

length -= 8;

grade ++;

if(gamespeed>=200)

gamespeed = 550 - grade * 50; // 改變自動前進時間間隔

}

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;

}

總結

以上是生活随笔為你收集整理的C语言贪吃蛇如何让蛇一直前进,c++贪吃蛇代码中,哪条代码是让蛇知道前进的的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。