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

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

#include<bits/stdc++.h>
#include <Windows.h>
#include <conio.h>
#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<<"                                                                                         "<<endl;
	cout<<"                       __________       ___                                              "<<endl;
	cout<<"                      /          \\     / \\ \\    |____      __\\__                     "<<endl;
	cout<<"                     /  ________  \\   / ___ \\  _/ __     | |   /                       "<<endl;
	cout<<"                     |  |      |__|     _/_   |_|  /    [|] |/                           "<<endl;
	cout<<"                     |  |              | | |      /     _|_ \\__/                        "<<endl;
	cout<<"                     \\  \\_______        / \\      |___/        ____                    "<<endl;
	cout<<"                      \\         \\    ____ ____      ____   __ |  |  ___   ______       "<<endl;
	cout<<"                       \\_______  \\   |  |/    \\    /    \\_/ / |  | /  /  /      \\   "<<endl;
	cout<<"                               \\  \\  |    ___  \\  / ____   /  |  |/  /  /  ____  \\   "<<endl;
	cout<<"                     __        |  |  |   /   \\  \\ | |  |  /   |     /  |  /____\\  |   "<<endl;
	cout<<"                    \\  \\_______|  |  |  |    |  | | |__|  |   |     \\  |  ________/   "<<endl;
	cout<<"                     \\            /  |  |    |  |  \\       \\  |  |\\  \\  \\  \\____  "<<endl;
	cout<<"                      \\__________/   |__|    |__|   \\___/\\__\\ |__| \\__\\  \\______/ "<<endl;	
}

void initSnake()//初始化蛇 
{
    snake_head->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;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(i==0||i==N-1||j==0||j==N-1)
            {
            	m[i][j]=1;
            	gotoxy(j*2,i);//光标跳转() 
            	cout<<"■";
			}
            else
            {
            	gotoxy(j*2,i);
                cout<<"  ";
			}
        }
        cout<<endl;
    }
    gotoxy(snake_head->y*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<<"切~你好菜哦,才吃了"<<grade<<"个";
			else if(grade<20)
			cout<<"一般般而已,就吃了"<<grade<<"个";
			else
			cout<<"你好强呀,吃了"<<grade<<"个呢!";
            getchar();
        }
        if(m[p->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<<"切~你好菜哦,才吃了"<<grade<<"个";
			else if(grade<20)
			cout<<"一般般而已,就吃了"<<grade<<"个";
			else
			cout<<"你好强呀,吃了"<<grade<<"个呢!";
            getchar();
        }
        p=p->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<<"当前得分:"<<grade;
            addSnakeNode(cur_tail_x,cur_tail_y);
            createFood();
        }
        else
        {
            gotoxy(cur_tail_y*2,cur_tail_x);
            cout<<"  ";
        }
        Sleep(TIME);
 		if(GetAsyncKeyState(VK_RIGHT))
        {
            control(RIGHT);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_LEFT))
        {
            control(LEFT);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_UP))
        {
            control(UP);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_DOWN))
        {
            continue;
        }
    }
}
 
void moveDown()
{
    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<<"当前得分:"<<grade;
            addSnakeNode(cur_tail_x,cur_tail_y);
            createFood();
        }
        else
        {
            gotoxy(cur_tail_y*2,cur_tail_x);
            cout<<"  ";
        }
        Sleep(TIME);
 		if (GetAsyncKeyState(VK_RIGHT))
        {
            control(RIGHT);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_LEFT))
        {
            control(LEFT);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_UP))
        {
            continue;
        }
        else if(GetAsyncKeyState(VK_DOWN))
        {
            control(DOWN);
            Sleep(TIME);
        }
    }
}
 
void moveLeft()
{
    while(1)
    {
        next_head_x=snake_head->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<<"当前得分:"<<grade;
            addSnakeNode(cur_tail_x,cur_tail_y);
            createFood();
        }
        else
        {
            gotoxy(cur_tail_y*2,cur_tail_x);
            printf("  ");
        }
        Sleep(TIME);
 		if(GetAsyncKeyState(VK_RIGHT))
        {
            continue;
        }
        else if(GetAsyncKeyState(VK_LEFT))
        {
            control(LEFT);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_UP))
        {
            control(UP);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_DOWN))
        {
            control(DOWN);
            Sleep(TIME);
        }
    }
}
 
void moveRight()
{
    while(1)
    {
        next_head_x=snake_head->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<<"当前得分:"<<grade;
            addSnakeNode(cur_tail_x,cur_tail_y);
            createFood();
        }
        else
        {
            gotoxy(cur_tail_y*2,cur_tail_x);
            printf("  ");
        }
        Sleep(TIME);
 		if(GetAsyncKeyState(VK_RIGHT))
        {
            control(RIGHT);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_LEFT))
        {
            continue;
        }
        else if(GetAsyncKeyState(VK_UP))
        {
            control(UP);
            Sleep(TIME);
        }
        else if(GetAsyncKeyState(VK_DOWN))
        {
            control(DOWN);
            Sleep(TIME);
        }
    }
}

void control(int d)
{
    if(d==UP) 
	{
		moveUp();
		Sleep(TIME);
	}
	else if(d==DOWN)
	{
		moveDown();
		Sleep(TIME);
	}
    else if(d==LEFT)
    {
		moveLeft();
		Sleep(TIME);
	}
    else if(d==RIGHT)
	{
		moveRight();
		Sleep(TIME);
	}     
}

void gameRun()
{
 	control(UP);
 	n=getch();
 	while(1)
    {
        if(n==RIGHT)
        {
        	control(RIGHT);
        	Sleep(TIME);
		}
        else if(n==LEFT)
        {
        	control(LEFT);
        	Sleep(TIME);
		}
        else if(n==UP)
        {
        	control(UP);
        	Sleep(TIME);
		}
        else if(n==DOWN)
        {
        	control(DOWN);
        	Sleep(TIME);
		}
    }
}
signed main()
{
	system("title 贪吃蛇"); //设置cmd窗口的名字
	system("mode con cols=95 lines=32"); //设置cmd窗口的大小
	grade=0;
	HideCursor(); //隐藏光标
	initSnake(); //初始化蛇 
	initMap();//初始化地图 
	srand((unsigned int)time(NULL)); //设置随机数生成起点
	createFood();
	gameRun(); 
	return 0;
}

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值