當前位置:吉日网官网 - 傳統美德 - 求C++遊戲源碼~

求C++遊戲源碼~

以下是snake源代碼:

# include & ltiostream.h & gt

# include & ltwindows.h & gt

# include & lttime.h & gt

# include & ltstdlib.h & gt

# include & ltconio.h & gt

#定義N 21

Void gotoxy(int x,int y)//位置函數

{

坐標位置;

位置。X = 2 * x

位置。Y = y

SetConsoleCursorPosition(GetStdHandle(STD _ OUTPUT _ HANDLE),pos);

}

Void color(int a)//顏色函數

{

SetConsoleTextAttribute(GetStdHandle(STD _ OUTPUT _ HANDLE),a);

}

Void init(int apple[2])//初始化函數(初始化柵欄,顯示信息,apple)

{

int i,j;//初始化圍欄

int wall[N+2][N+2]= { { 0 } };

for(I = 1;我& lt= N;i++)

{

for(j = 1;j & lt= N;j++)

wall[I][j]= 1;

}

顏色(11);

for(I = 0;我& ltn+2;i++)

{

for(j = 0;j & ltn+2;j++)

{

if(wall[i][j])

cout & lt& lt"■";

else cout & lt& lt"□" ;

}

cout & lt& ltendl

}

gotoxy(N+3,1);//顯示信息

顏色(20);

cout & lt& lt“向西南方向移動”endl

gotoxy(N+3,2);

顏色(20);

cout & lt& lt"按任意鍵暫停"

gotoxy(N+3,3);

顏色(20);

cout & lt& lt"分數:"

apple[0]= rand()% N+1;//蘋果

apple[1]= rand()% N+1;

gotoxy(蘋果[0],蘋果[1]);

顏色(12);

cout & lt& lt"●" & lt;& ltendl

}

int main()

{

int i,j;

int * * snake = NULL

int apple[2];

int得分= 0;

int tail[2];

int len = 3;

char ch = ' p

srand((無符號)時間(空));

init(蘋果);

snake=(int**)realloc(snake,sizeof(int *)* len);

for(I = 0;我& ltleni++)

snake[I]=(int *)malloc(sizeof(int)* 2);

for(I = 0;我& ltleni++)

{

snake[I][0]= N/2;

snake[I][1]= N/2+I;

gotoxy(snake[i][0],snake[I][1]);

顏色(14);

cout & lt& lt“★”& lt;& ltendl

}

While(1)//進入消息循環。

{

tail[0]= snake[len-1][0];

尾巴[1]=蛇[len-1][1];

gotoxy(tail[0],tail[1]);

顏色(11);

cout & lt& lt"■" & lt;& ltendl

for(I = len-1;我& gt0;我-)

{

蛇[I][0]=蛇[I-1][0];

蛇[I][1]=蛇[I-1][1];

gotoxy(snake[i][0],snake[I][1]);

顏色(14);

cout & lt& lt“★”& lt;& ltendl

}

if(kbhit())

{

gotoxy(0,N+2);

ch = getche();

}

開關(通道)

{

case ' w ':snake[0][1]-;打破;

case ' s ':snake[0][1]++;打破;

案例“a”:snake[0][0]-;打破;

大小寫“d”:snake[0][0]++;打破;

默認:break

}

gotoxy(snake[0][0],snake[0][1]);

顏色(14);

cout & lt& lt“★”& lt;& ltendl

睡眠(ABS(200-0.5 *分));

if(snake[0][0]= = apple[0]& amp;& amp蛇[0] [1] = =蘋果[1])//吃了蘋果之後,蛇的分數增加1,蛇的長度增加1。

{

得分++;

len++;

snake=(int**)realloc(snake,sizeof(int *)* len);

snake[len-1]=(int *)malloc(sizeof(int)* 2);

apple[0]= rand()% N+1;

apple[1]= rand()% N+1;

gotoxy(蘋果[0],蘋果[1]);

顏色(12);

cout & lt& lt"●" & lt;& ltendl

gotoxy(N+5,3);

顏色(20);

cout & lt& lt得分& lt& ltendl

}

if(snake[0][1]= = 0 | | snake[0][1]= = n | | snake[0][0]= = 0 | | snake[0]= = n)//撞上柵欄後失敗。

{

gotoxy(N/2,N/2);

顏色(30);

cout & lt& lt“失敗!!!"& lt& ltendl

for(I = 0;我& ltleni++)

免費(snake[I]);

睡眠(無限);

退出(0);

}

}

返回0;

}

  • 上一篇:探戈的節奏?
  • 下一篇:傳統節日的風俗有哪些
  • copyright 2024吉日网官网