求C++小游戏源代码啊~
的有关信息介绍如下:问题补充说明:坦克啊,不用搞得太好的,贪吃蛇这种小游戏就行,要那种在黑色窗口或白色窗口里玩的小游戏
以下是贪吃蛇源代码:
#include<iost360问答ream.h>
#includ进思班纸科e<windows.h>
#include<time.h>
#include<stdlib.h>坦数
#include<conio.h>
#defineN21
voidgotoxy(intx,inty)//位置函数
{
COORDpos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_O美读持守群防UTPUT_HANDLE),pos);
}
voidcolor(inta)//颜色函数
{
SetCo伯nsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
voidinit(intapple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
inti,j;背县块尼核食且检著角//初始化围墙
intwall[N+2][N+2]={{0}};
for(i怀领=1;i<=N;i++)
{
for(j=1;j<=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;i<N+2;i++)
{
for(j=0;j<N+2;j++)
{
if(wall[i][j])
cout<<"■";
elsecout<<"□";
}
cout<<endl;
}
g季始应东出还友预织觉otoxy(N+3,1);//让差首显示信息
color(20);
cout<<庆宴"按WSAD移动方向"<<endl;
gotoxy(N+3,2);
color(20);
cout<<"按任意键暂停"<<endl;
gotoxy(N+3,3);
color(20);
cout<<"得执消前队脚丝养建明分:"<<endl;
apple[0]=ra轮于团动nd()%N+1;//苹果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
}
intmain()
{
inti,j;
int**snake=NULL;
intapple[2];
intscore=0;
inttail[2];
intlen=3;
charch正='p';
srand((unsigned训帮系京距取)time(NU研害坚植先LL));
init(apple);
snake厚类模检=(int**)realloc(snake,sizeof(int*)*len);
仅世艺日岁相序创游for(i=0;i<len;息光货司却i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i独卫办=0;i<len;i++)
属却互粉派{
snake[i][0板政会洋刚万收静甲务升]=N/2;
snake[i][1]=N/2+i;
g夜义otoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout<<"■"<<endl;
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case'w':snake[0][1]--;break;
case's':snake[0][1]++;break;
case'a':snake[0][0]--;break;
case'd':snake[0][0]++;break;
default:break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout<<"★"<<endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
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(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
gotoxy(N+5,3);
color(20);
cout<<score<<endl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout<<"失败!!!"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return0;
}