回复 7楼 a1017075043
程序代码:
#include<stdio.h> //你看一下这个的食物产生函数,food_1->x = rand()%38, 已经随机产生了事物的坐标了,这就算是初始化了吧
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
struct Snake
{
int x;
int y;
struct Snake * pnext;
};
struct Food
{
int x;
int y;
};
struct Direction
{
char direction;
};
char ch = 1;
char tn = 3;
int stop = 0;
int TIME = 200;
int SPEED = 400;
char direction;
struct Food * food;
struct Direction * headir;
struct Snake * head,* tail; //建立头指针
struct Snake * p; //遍历指针
void ZHUYE(); //绘制主界面函数
void HideCursor(); //隐藏光标函数
void gotoxy(int x,int y); //移动光标函数
void creat_snake(); //初始化蛇身函数
void draw_snake(); //绘制蛇身
void creat_food(); //产生食物函数
void creat_node(); //添加头结点函数
void shanchu_tail(); //删除尾结点函数
void gameover(); //游戏结束判断函数
int selfpan(); //判断是否咬到自身
void snake_move(); //蛇身移动函数
void keyjiance(); //按键检测函数
void main()
{
ZHUYE(); //主界面
while(!stop)
{
keyjiance(); //检测按键
snake_move(); //蛇身移动
draw_snake(); //重绘蛇身
Sleep(TIME);
gameover(); //判断结束
}
}
void ZHUYE() //绘制主页面函数
{
HideCursor();
int i,j;
for(i = 0; i < 40; i++)
{
gotoxy(i,0);
printf("-");
gotoxy(i,14);
printf("-");
}
for(j = 0; j < 15; j++)
{
gotoxy(0,j);
printf("|");
gotoxy(39,j);
printf("|");
}
gotoxy(42,2);
printf("按任意键开始!w,s,a,d控制方向");
gotoxy(42,4);
printf("o键加速 p键减速\n");
gotoxy(42,6);
printf("按空格键暂停");
gotoxy(42,8);
printf("SPEED : %d",SPEED);
_getch();
creat_snake();
creat_food();
}
void HideCursor() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x,int y) //移动光标
{
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
COORD loc={x,y};
SetConsoleCursorPosition(hOut,loc);
}
void creat_snake() //蛇身初始化
{
int i;
struct Snake * tail;
tail = (struct Snake *)malloc(sizeof(struct Snake));
headir = (struct Direction *)malloc(sizeof(struct Direction));
food = (struct Food *)malloc(sizeof(struct Food));
if(tail == NULL)
{
exit (-1);
}
tail -> x = 10;
tail -> y = 6;
tail -> pnext = NULL;
headir -> direction = 'd';
for(i = 1; i <= 4; i++)
{
head = (struct Snake *)malloc(sizeof(struct Snake));
head -> pnext = tail;
head -> x = 10 + i;
head -> y = 6;
tail = head;
}
head = tail;
draw_snake();
}
void draw_snake() //绘制蛇身结点函数
{
struct Snake * temp;
temp = head;
while(temp != NULL)
{
gotoxy(temp -> x,temp -> y);
printf("%c",ch);
temp = temp -> pnext;
}
}
void creat_food()
{
struct Snake * temp;
temp = head;
srand(time(NULL));
food -> x = rand()%38+1;
food -> y = rand()%13+1;
while(temp != NULL)
{
if(food -> x == temp -> x&&food -> y == temp -> y)
{
free(food);
creat_food();
}
temp = temp -> pnext;
}
gotoxy(food -> x,food -> y);
printf("%c",tn);
}
void creat_node(int x,int y) //增添头结点
{
struct Snake * p2;
p2 = (struct Snake *)malloc(sizeof(struct Snake));
p2 -> pnext = head;
p2 -> x = x;
p2 -> y = y;
head = p2;
gameover();
}
void shanchu_tail() //删除尾结点函数定义
{
struct Snake * temp;
p = head;
while(p -> pnext != NULL)
{
temp = p;
p = p -> pnext;
}
gotoxy(p -> x,p -> y);
printf(" ");
temp -> pnext = NULL;
free(p);
}
int selfpan() //是否咬到自身
{
struct Snake * temp;
temp = head -> pnext;
while(temp != NULL)
{
if(temp -> x == head -> x&& temp -> y == head -> y)
return 1;
temp = temp -> pnext;
}
}
void gameover() //判断游戏结束
{
struct Snake * temp;
temp = head -> pnext;
if(selfpan() == 1)
{
stop = 1;
system("cls");
gotoxy(18,7);
printf("游戏结束!");
gotoxy(8,18);
printf(" ");
}
if(head -> x <1 || head -> x > 38 || head -> y <1 || head -> y >13)
{
stop = 1;
system("cls");
gotoxy(18,7);
printf("游戏结束!");
gotoxy(8,18);
printf(" ");
}
}
void snake_move() //蛇身移动函数定义
{
if(headir -> direction == 'w')
{
if(food -> x == head -> x && food -> y == head -> y)
{
gotoxy(food -> x,food -> y);
printf(" ");
creat_node(head -> x,head -> y -1);
creat_food();
}
else
{
creat_node(head -> x,head -> y -1);
shanchu_tail();
}
}
if(headir -> direction == 's')
{
if(food -> x == head -> x && food -> y == head -> y)
{
gotoxy(food -> x,food -> y);
printf(" ");
creat_node(head -> x,head -> y + 1);
creat_food();
}
else
{
creat_node(head -> x,head -> y + 1);
shanchu_tail();
}
}
if(headir -> direction == 'd')
{
if(food -> x == head -> x && food -> y == head -> y)
{
gotoxy(food -> x,food -> y);
printf(" ");
creat_node(head -> x + 1,head -> y);
creat_food();
}
else
{
creat_node(head -> x + 1,head -> y);
shanchu_tail();
}
}
if(headir -> direction == 'a')
{
if(food -> x == head -> x && food -> y == head -> y)
{
gotoxy(food -> x,food -> y);
printf(" ");
creat_node(head -> x -1,head -> y);
creat_food();
}
else
{
creat_node(head -> x - 1,head -> y);
shanchu_tail();
}
}
gotoxy(42,8);
printf("SPEED : %d",SPEED);
}
void keyjiance() //按键检测函数定义
{
char key;
if(_kbhit())
{
key = getch();
switch(key)
{
case 'w' : if(headir -> direction == 's')
break;
else
headir -> direction = 'w';break;
case 's' : if(headir -> direction == 'w')
break;
else
headir -> direction = 's';break;
case 'd' : if(headir -> direction == 'a')
break;
else
headir -> direction = 'd';break;
case 'a' : if(headir -> direction == 'd')
break;
else
headir -> direction = 'a';break;
case 'o' : if(TIME > 50)
{
TIME -= 50;SPEED += 50; break;
}
case 'p' : if(TIME < 400)
{
TIME += 50;SPEED -= 50; break;
}
case ' ' : gotoxy(6,15);
printf("游戏已暂停,按任意键恢复游戏");
system("pause>nul");
gotoxy(6,15);
printf(" ");
break;
default : break;
}
}
}