c语言贪吃蛇小游戏的心得体会,【C语言小游戏】 贪吃蛇

这是一个使用C++编写的DOS版贪吃蛇游戏,适用于VC++6.0编译环境。游戏在控制台运行,无需额外图形库。玩家通过键盘控制蛇移动,吃到苹果则得分,碰到边界或自身则游戏结束。程序包含初始化、绘制、移动、判断等功能,并支持读写文件来记录最高分。
摘要由CSDN通过智能技术生成

windows编译运行

3bc62c147b16cfa3ce42ce7dc6ae4204.png

源代码:/*这是一个贪吃蛇代码,运行环境VC++6.0(亲测完美运行)*/

/*该程序在dos系统下运行,不需要graphics.h头文件*/

#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]  N) || (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;

case 'd': case 'D': ++snake[0][1]; break;

default: ;

}

if (full)

{

snake = (char **)realloc(snake, sizeof(char *) * ((*len) + 1));

snake[(*len)] = (char *)malloc(sizeof(char) * 2);

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

++(*len);

++score[0];

if(score[3] 

++score[3];

tail[2] = 1;

}

else

tail[2] = 0;

return snake;

}

void init(char plate[N+2][N+2], char ***snake_x, int *len)  //初始化

{

int i, j;

char **snake = NULL;

*len = 3;

score[0] = score[3] =3;

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

for (i = 0; i 

snake[i] = (char *)malloc(sizeof(char) * 2);

for (i = 0; i 

{

snake[i][0] = N/2 + 1;

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

}

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

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

plate[i][j] = 1;

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

apple[2] = 1;

for (i = 0; i 

{

gotoxy(0, i);

for (j = 0; j 

{

switch (plate[i][j])

{

case 0:

color(12);printf("□");color(11); continue;

case 1: printf("■"); continue;

default: ;

}

}

putchar('\n');

}

for (i = 0; i 

{

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

printf("★");

}

putchar('\n');

*snake_x = snake;

}

void Manual()

{

gotoxy(N+30,2);

color(10);

printf("按 W S A D 移动方向");

gotoxy(N+30,4);

printf("按 space 键暂停");

gotoxy(N+30,8);

color(11);

printf("历史最高分为: ");

color(12);

gotoxy(N+44,8);

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

color(11);

gotoxy(N+30,12);

printf("你现在得分为: 0");

}

int File_in()     //取记录的分数

{

FILE *fp;

if((fp = fopen("C:\\tcs.txt","a+")) == NULL)

{

gotoxy(N+18, N+2);

printf("文件不能打开\n");

exit(0);

}

if((score[1] = fgetc(fp)) != EOF);

else

score[1] = 0;

return 0;

}

int File_out()    //存数据

{

FILE *fp;

if(score[1] > score[0])

{gotoxy(10,10);

color(12);

puts("闯关失败 加油耶");

gotoxy(0,N+2);

return 0;

}

if((fp = fopen("C:\\tcs.txt","w+")) == NULL)

{

printf("文件不能打开\n");

exit(0);

}

if(fputc(--score[0],fp)==EOF)

printf("输出失败\n");

gotoxy(10,10);

color(12);

puts("恭喜您打破记录");

gotoxy(0,N+2);

return 0;

}

void Free(char **snake, int len)    //释放空间

{

int i;

for (i = 0; i 

free(snake[i]);

free(snake);

}

int main(void)

{

int len;

char ch = 'g';

char a[N+2][N+2] = `0`;

char **snake;

srand((unsigned)time(NULL));

color(11);

File_in();

init(a, &snake, &len);

Manual();

while (ch != 0x1B)   // 按 ESC 结束

{

Draw(snake, len);

if (!apple[2]) {

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

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

apple[2] = 1;

}

Sleep(200-score[3]*10);

setbuf(stdin, NULL);

if (kbhit())

{

gotoxy(0, N+2);

ch = getche();

}

snake = Move(snake, ch, &len);

if (Block(snake[0])==1)

{

gotoxy(N+2, N+2);

puts("你输了");

File_out();

Free(snake, len);

getche();

exit(0);

}

}

Free(snake, len);

exit(0);

}

编译:

0b0614a2afdd9e4c576c8f7411231b94.png

C语言实训心得总结.doc C语言实训心得 10903090113 李新程 在初学C语言的一个学期后,我们进行了C语言实训阶段,尝试编写一个比较复杂的程序系统。在为期两周的时间中,我们同组的同学共同的感受是:C语言实训和平时上课所接触的程序是有很大不同的,所经受的考验和克服的困难是平时所无法比拟的。好在同组的搭档们精诚合作,分工明确,有问题共同解决,攻克了C语言实训的复杂程序。在这里,我作为其中的参与者,感触良多。 在这次实训中,我对对C语言有了一个更深的了解认识,也对这个学期学的知识得到巩固,还尝试运行编程,每次运行程序成功,让我对下面的项目就充满信心。通过自己与同学合作编写程序,最终把最初的理论知识转化基本技能。这次的实训,使我对C语言的学习产生浓厚的兴趣。 还是这次实训,最令人激动的就是合作做项目,虽然那只是一个很小很小的项目。每天大家来得很早,大家在一起学习,取长补短,我们很好的在实训中长知识,提高我们的学习热情。实训中深切体会到了老师认真负责的伟大的精神和热情为同学指导的促学方式,虽然对有些时候老师没给我们指出解决问题的方法有些小抱怨,但是到了结束时才知道,这种教学让我们自己学会了自学,学会了去看懂别人的代码。更多是老师给的感动,每天在我们来之前就到了教室,在讲课中海给我们分享他在公司上班的一些心得和体会,还有那些我们应该注意的事项,这些是平时上课时无法学到的,是更深层次的巨大收获。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值