数据结构大作业:贪吃蛇游戏

 记录一下大一下写的数据结构大作业

#include
#include 
#include 
#define N 30
#define UP 72 //方向键:上
#define DOWN 80 //方向键:下
#define LEFT 75 //方向键:左
#define RIGHT 77 //方向键:右
int grade,n,m[N][N];
int next_head_x,next_head_y;
int cur_tail_x,cur_tail_y;
int TIME=301;
using namespace std;

void control(int d);
void createFood();

typedef struct SnakeNode
{
	int x;
	int y;
	struct SnakeNode *pre;
	struct SnakeNode *next;
}*snakeNode;

typedef struct FoodNode 
{
    int x;
    int y;
}*foodNode;

snakeNode snake_head=new SnakeNode;
snakeNode snake_tail=new SnakeNode;
foodNode food=new FoodNode;

void HideCursor()//隐藏光标
{
	CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量
	curInfo.dwSize=1; //如果没赋值的话,光标隐藏无效
	curInfo.bVisible=FALSE; //将光标设置为不可见
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
	SetConsoleCursorInfo(handle,&curInfo); //设置光标信息
}

void gotoxy(int x, int y)//光标跳转 
{
    COORD pos;
    HANDLE hOutput;
    pos.X=x;
    pos.Y=y;
    hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput,pos);
}

void printsnake()
{
	system("color F3");
	cout<<"                                                                                         "<x=15;
    snake_head->y=15;
    snake_head->pre=NULL;
    snake_head->next=snake_tail;
 
    snake_tail->pre=snake_head;
    snake_tail->x=snake_head->x+1;
    snake_tail->y=snake_head->y;
    snake_tail->next=NULL;
}

void initMap()//初始化地图 
{
	system("color F3"); 
    for(int i=0;iy*2,snake_head->x);
    cout<<"●";
    gotoxy(snake_tail->y*2,snake_tail->x);
    cout<<"●";
    gotoxy(N*2,0);
    cout<<"当前得分:0";
}

void updateSnake(int next_head_x,int next_head_y)//更新蛇 
{
    snakeNode p=snake_tail;
    while(p!=snake_head)
    {
        p->x=p->pre->x;
        p->y=p->pre->y;
        p=p->pre;
    }
    p->x=next_head_x;
    p->y=next_head_y;
}

void crashTest(int head_x,int head_y)
{
    snakeNode p=snake_head->next;
    while (p!=NULL)
    {
        if(p->x==head_x&&p->y==head_y)
        {
        	system("cls"); //清空屏幕
			gotoxy(N,3);
			printsnake();
			gotoxy(N,21);
            cout<<"GAME OVER......";
            gotoxy(N,22);
            cout<<"嘤嘤嘤~,咬到自己了";
            gotoxy(N,20);
            if(grade<10)
			cout<<"切~你好菜哦,才吃了"<x][p->y]==1)
        {
        	system("cls");
			gotoxy(N,3);
			printsnake();
			gotoxy(N,20);
            cout<<"GAME OVER......";
            gotoxy(N,21);
            cout<<"QAQ,撞到墙了好痛哦";
            gotoxy(N,22);
            if(grade<10)
			cout<<"切~你好菜哦,才吃了"<next;
    }
}

void addSnakeNode(int cur_tail_x,int cur_tail_y)
{
    snakeNode t=new SnakeNode;
    t->x=cur_tail_x;
    t->y=cur_tail_y;
    t->next=NULL;
    snake_tail->next=t;
    t->pre=snake_tail;
    snake_tail=t;
}

void createFood()
{
    food->x=rand()%23+1;
    food->y=rand()%23+1;
    gotoxy(food->y*2,food->x);
    cout<<"◇";
}

void moveUp()
{
    while(1) 
    {
        next_head_x=snake_head->x-1;
        next_head_y=snake_head->y;
        cur_tail_x=snake_tail->x;
        cur_tail_y=snake_tail->y;
        updateSnake(next_head_x,next_head_y);
        gotoxy(next_head_y*2,next_head_x);
        cout<<"●";
        crashTest(next_head_x,next_head_y);
        if(next_head_x==food->x&&next_head_y==food->y)
        {
        	grade++;
        	if(TIME>1) TIME-=15;
        	gotoxy(N*2,0);
        	cout<<"当前得分:"<x+1;
        next_head_y=snake_head->y;
        cur_tail_x=snake_tail->x;
        cur_tail_y=snake_tail->y;
        updateSnake(next_head_x,next_head_y);
        gotoxy(next_head_y*2,next_head_x);
        cout<<"●";
        crashTest(next_head_x,next_head_y);
        if(next_head_x==food->x&&next_head_y==food->y)
        {
        	grade++;
        	if(TIME>1) TIME-=15;
        	gotoxy(N*2,0);
        	cout<<"当前得分:"<x;
        next_head_y=snake_head->y-1;
        cur_tail_x=snake_tail->x;
        cur_tail_y=snake_tail->y;
        updateSnake(next_head_x,next_head_y);
        gotoxy(next_head_y*2,next_head_x);
        printf("●");
        crashTest(next_head_x,next_head_y);
        if(next_head_x==food->x&&next_head_y==food->y)
        {
        	grade++;
        	if(TIME>1) TIME-=15;
        	gotoxy(N*2,0);
        	cout<<"当前得分:"<x;
        next_head_y=snake_head->y+1;
        cur_tail_x=snake_tail->x;
        cur_tail_y=snake_tail->y;
        updateSnake(next_head_x,next_head_y);
        gotoxy(next_head_y*2,next_head_x);
        printf("●");
        crashTest(next_head_x,next_head_y);
        if(next_head_x==food->x&&next_head_y==food->y)
        {
        	grade++;
        	if(TIME>1) TIME-=15;
        	gotoxy(N*2,0);
        	cout<<"当前得分:"<

你可能感兴趣的:(数据结构,游戏,c++)