贪吃蛇代码c语言100行,贪吃蛇代码-C语言.doc

/*这是一个贪吃蛇代码,运行环境VC++6.0,Dev-C也可*/

/*该程序由C语言小方贡献,谢谢您的支持*/

#include

#include

#include

#include

#include

#include

#define N 21

int apple[3];

char score[3];

char tail[3];

void gotoxy(int x, int y) //输出坐标

{

COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}

void color(int b) //颜色函数

{

HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;

SetConsoleTextAttribute(hConsole,b) ;

}

int Block(char head[2]) //判断出界

{

if ((head[0] < 1) || (head[0] > N) || (head[1] < 1) || (head[1] > N))

return 1;

return 0;

}

int Eat(char snake[2]) //吃了苹果

{

if ((snake[0] == apple[0]) && (snake[1] == apple[1]))

{

apple[0] = apple[1] = apple[2] = 0;

gotoxy(N+44,10);

color(13);

printf("%d",score[0]*10);

color(11);

return 1;

}

return 0;

}

void Draw(char **snake, int len) //蛇移动

{

if (apple[2]) {

gotoxy(apple[1] * 2, apple[0]);

color(12);

printf("●");

color(11);

}

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

if (tail[2])

{ color(14);

printf("★");

color(11);

}

else

printf("■");

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

color(14);

printf("★");

color(11);

putchar('\n');

}

char** Move(char **snake, char dirx, int *len) //控制方向

{

int i, full = Eat(snake[0]);

memcpy(tail, snake[(*len)-1], 2);

for (i = (*len) - 1; i > 0; --i)

memcpy(snake[i], snake[i-1], 2);

switch (dirx)

{

case 'w': case 'W': --snake[0][0]; break;

case 's': case 'S': ++snake[0][0]; break;

case 'a': case 'A': --snake[0][1]; break;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值