《软件工程导论》实验报告(8)
时间:2025-07-13
时间:2025-07-13
贪吃蛇程序:贪吃蛇游戏是一个深受人们喜爱的游戏,一条蛇在密闭的围墙内,在围墙内随机出现一个食物,通过按键盘上的四个光标键控制蛇向上下左右四个方向移动,
*");
printf("* * *");
printf("* * *");
printf("* * *");
printf("********************************************************************************"); }
void update_score()//更新分数 {
gotoxy(73,3);
printf("%2d",snake_len); }
void create_food()//产生食物的位置 {
time_t t;
srand(time(&t)); while(1) {
food[0]=rand()%62+1;//生成1~62之间的随机数,其中random函数生成0~77之间的随机数
food[1]=rand()%22+1;//生成1~22之间的随机数,其中random函数生成0~17之间的随机数
if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break; }
gotoxy(food[0],food[1]); printf("*"); }
void direction() {
char keyhit=0,i;
while(kbhit()!=0)keyhit=getch();
if( ((keyhit=='a') || (keyhit=='d') || (keyhit=='w') || (keyhit=='s')) && (abs(snake_direction/16-keyhit/16)==1) )snake_direction=keyhit; else if(keyhit==' ') {
gotoxy(30,24); system("pause"); gotoxy(30,24);