求一个c语言贪吃蛇代码 能在cb下运行
答案:1 悬赏:0 手机版
解决时间 2021-03-22 20:49
- 提问者网友:蓝莓格格巫
- 2021-03-22 05:53
求一个c语言贪吃蛇代码 能在cb下运行
最佳答案
- 五星知识达人网友:千夜
- 2021-03-22 06:48
#include
#include
#include
#include
#include
#define STATIC 0
#define TRUE 1
#define FALSE 0
#define UP 1
#define RIGHT 2
#define DOWN 3
#define LEFT 4
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_ESC 0x011b
int board[22][22];
int snakelength=0;
struct snake{
public:
int x=0;
int y=0;
int direction;
}body[20];
snake food;
void makefood();
int eatfood();
void right();
void down();
void left();
void up();
void getdirection();
move(snake *body)
{int x=body[0].x,y=body[0].y;
if(body->direction==RIGHT&&board[y][x+1]!=1)right();
elseif(body->direction==DOWN&&board[y+1][x]!=1)down();
elseif(body->direction==LEFT&&board[y][x-1]!=1)left();
elseif(body->direction==UP&&board[y-1][x]!=1)up();
return 0;
}
void print()
{int i,j,x=0,y=0;
for(i=1;i<21;i++)
for(j=1;j<21;j++)
board[i][j]=0;
for(i=0;i<20;i++)
{x=body[i].x;
y=body[i].y;
board[y][x]=1;
}
board[food.y][food.x]=2;
for(i=1;i<21;i++)
for(j=1;j<21;j++)
{if(board[i][j]==1)
{setfillstyle(SOLID_FILL,WHITE);
bar(j*15,i*15,j*15+13,i*15+13);
}
if(board[i][j]==0)
{setfillstyle(SOLID_FILL,BLACK);
bar(j*15,i*15,j*15+13,i*15+13);
}
if(board[i][j]==2)
{setfillstyle(SOLID_FILL,RED);
bar(j*15,i*15,j*15+13,i*15+13);
}
}
}
main(int second=0)
{
int gdriver=CGAC0,gmode;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
randomize();
int i,j;
int flag;
longtime=100000;
for(i=0;i<21;i++)
for(j=0;j<21;j++)
board[i][j]=0;
for(i=0;i<22;i++)
{board[0][i]=1;board[21][i]=1;board[i][0]=1;board[i][21]=1;}
snakelength=3;
body[0].x=3,body[0].y=2,body[0].direction=RIGHT;
body[1].x=2,body[1].y=2;
body[2].x=1,body[2].y=2;
makefood();
print();
getch();
for(i=0;i<30000;i++)
{
move(&body[0]);
getdirection();
flag=eatfood();
if(flag==TRUE){print();makefood();};
print();
for(j=0;j }
line(0,0,400,400);
getch();
}
void right()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].x++;
}
void down()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].y++;
}
void left()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].x--;
}
void up()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].y--;
}
void getdirection()
{int key=0;
if(bioskey(1)!=0)
key=bioskey(0);
switch(key)
{case VK_UP: if(body[0].direction!=DOWN)body[0].direction=UP;break;
caseVK_RIGHT: if(body[0].direction!=LEFT)body[0].direction=RIGHT;break;
caseVK_DOWN: if(body[0].direction!=UP)body[0].direction=DOWN;break;
caseVK_LEFT: if(body[0].direction!=RIGHT)body[0].direction=LEFT;break;
case VK_ESC:exit(0);
}
}
void makefood()
{int i,j;
food.x=0;
food.y=0;
print();
food.x=random(20);
food.y=random(20);
for(;;)
{if(board[food.y][food.x]!=0)
{
food.x=random(20);
food.y=random(20);
}
else break;
}
food.direction=body[0].direction;
board[food.y][food.x]=2;
}
int eatfood()
{int i=FALSE;
intx=body[0].x,y=body[0].y;
if(body[0].direction==UP&&board[y-1][x]==2)
{snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==DOWN&&board[y+1][x]==2)
{ snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==LEFT&&board[y][x-1]==2)
{snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==RIGHT&&board[y][x+1]==2)
{snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
return i;
}追问
亲 这个怎么搞
追答头文件用到的是graphics.h,你在TC下试试追问tc没有64位的THANK YOU
#include
#include
#include
#include
#define STATIC 0
#define TRUE 1
#define FALSE 0
#define UP 1
#define RIGHT 2
#define DOWN 3
#define LEFT 4
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_ESC 0x011b
int board[22][22];
int snakelength=0;
struct snake{
public:
int x=0;
int y=0;
int direction;
}body[20];
snake food;
void makefood();
int eatfood();
void right();
void down();
void left();
void up();
void getdirection();
move(snake *body)
{int x=body[0].x,y=body[0].y;
if(body->direction==RIGHT&&board[y][x+1]!=1)right();
elseif(body->direction==DOWN&&board[y+1][x]!=1)down();
elseif(body->direction==LEFT&&board[y][x-1]!=1)left();
elseif(body->direction==UP&&board[y-1][x]!=1)up();
return 0;
}
void print()
{int i,j,x=0,y=0;
for(i=1;i<21;i++)
for(j=1;j<21;j++)
board[i][j]=0;
for(i=0;i<20;i++)
{x=body[i].x;
y=body[i].y;
board[y][x]=1;
}
board[food.y][food.x]=2;
for(i=1;i<21;i++)
for(j=1;j<21;j++)
{if(board[i][j]==1)
{setfillstyle(SOLID_FILL,WHITE);
bar(j*15,i*15,j*15+13,i*15+13);
}
if(board[i][j]==0)
{setfillstyle(SOLID_FILL,BLACK);
bar(j*15,i*15,j*15+13,i*15+13);
}
if(board[i][j]==2)
{setfillstyle(SOLID_FILL,RED);
bar(j*15,i*15,j*15+13,i*15+13);
}
}
}
main(int second=0)
{
int gdriver=CGAC0,gmode;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
randomize();
int i,j;
int flag;
longtime=100000;
for(i=0;i<21;i++)
for(j=0;j<21;j++)
board[i][j]=0;
for(i=0;i<22;i++)
{board[0][i]=1;board[21][i]=1;board[i][0]=1;board[i][21]=1;}
snakelength=3;
body[0].x=3,body[0].y=2,body[0].direction=RIGHT;
body[1].x=2,body[1].y=2;
body[2].x=1,body[2].y=2;
makefood();
print();
getch();
for(i=0;i<30000;i++)
{
move(&body[0]);
getdirection();
flag=eatfood();
if(flag==TRUE){print();makefood();};
print();
for(j=0;j
line(0,0,400,400);
getch();
}
void right()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].x++;
}
void down()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].y++;
}
void left()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].x--;
}
void up()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].y--;
}
void getdirection()
{int key=0;
if(bioskey(1)!=0)
key=bioskey(0);
switch(key)
{case VK_UP: if(body[0].direction!=DOWN)body[0].direction=UP;break;
caseVK_RIGHT: if(body[0].direction!=LEFT)body[0].direction=RIGHT;break;
caseVK_DOWN: if(body[0].direction!=UP)body[0].direction=DOWN;break;
caseVK_LEFT: if(body[0].direction!=RIGHT)body[0].direction=LEFT;break;
case VK_ESC:exit(0);
}
}
void makefood()
{int i,j;
food.x=0;
food.y=0;
print();
food.x=random(20);
food.y=random(20);
for(;;)
{if(board[food.y][food.x]!=0)
{
food.x=random(20);
food.y=random(20);
}
else break;
}
food.direction=body[0].direction;
board[food.y][food.x]=2;
}
int eatfood()
{int i=FALSE;
intx=body[0].x,y=body[0].y;
if(body[0].direction==UP&&board[y-1][x]==2)
{snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==DOWN&&board[y+1][x]==2)
{ snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==LEFT&&board[y][x-1]==2)
{snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==RIGHT&&board[y][x+1]==2)
{snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
return i;
}追问
亲 这个怎么搞
追答头文件用到的是graphics.h,你在TC下试试追问tc没有64位的THANK YOU
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯