控制台c++贪吃蛇的简单实现

本文深入探讨了游戏开发领域的关键技术,包括Unity3D和Cocos2dX引擎的应用,以及如何利用AI和音视频技术实现更丰富的交互体验。详细介绍了图像处理、AR特效、语音识别和深度学习在游戏中的应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Body.h头文件

#ifndef _BODY_H_
#define _BODY_H_
#include<iostream>
class Body
{
public:
	Body* next;
	int x;
	int y;
	Body();
	Body(int h,int z);
};

#endif

Body.cpp文件

#include"Body.h"

Body::Body()
{
	next=NULL;
}

Body::Body(int h,int z)
{
	next=NULL,x=h,y=z;
}

Snake.h文件

#pragma once
#include<iostream>
#include"Body.h"

class Snake
{
private:
	Body* head;
	Body* tail;
	Body* curr;
	int length;
	int direction;
public:
	Snake();
	~Snake();

	void grow();
	bool die(); 
	void move();
	void setdir(const int& in);
	const int& getdir();
	const int& getlengh();
	const Body* gethead();
	const Body* gettail();
	const Body* getcurr();
	void addcurr();
	void backcurr();
};

Snake.cpp文件

#include"Snake.h"

Snake::Snake()
{
	curr=head=new Body(4,7);
	curr->next=new Body(4,6);
	curr->next->next=new Body(4,5);
	tail=curr->next->next;
	tail->next=NULL;
	length=3;
	direction=4;
}

Snake::~Snake()
{
	curr=head;
	while(curr!=NULL)
	{
		head=head->next;
		delete curr;
		curr=head;
	}
}

void Snake::grow()
{
	curr=head;
	Body* temp=new Body;
	while(curr->next!=tail)
	{
		curr=curr->next;
	}
	if(curr->x==tail->x)
	{
		if(curr->y>tail->y)
		{
			temp->x=tail->x;
			temp->y=tail->y+1;
		}
		else
		{
			temp->x=tail->x;
			temp->y=tail->y-1;
		}
	}
	else if(curr->y==tail->y)
	{
		if(curr->x>tail->x)
		{
			temp->x=tail->x-1;
			temp->y=tail->y;
		}
		else
		{
			temp->x=tail->x+1;
			temp->y=tail->y;
		}
	}
	tail=temp;
	tail->next=NULL;
	curr->next->next=tail;
	curr=head;
	length++;
}

const int& Snake::getdir()
{
	return direction;
}

bool Snake::die()
{
	if(((head->x==tail->x)&&(head->y=tail->y))||head->x>19||head->x<0||head->y>39||head->y<0)
	{
		return true;
	}
	else return false;
}

void Snake::move()
{
	curr=head;
	Body* temp=new Body;
	switch(direction)
	{
	case 1:
		temp->x=head->x-1;
		temp->y=head->y;
	    head=temp;
	    head->next=curr;
		break;
	case 2:
		temp->x=head->x+1;
		temp->y=head->y;
	    head=temp;
	    head->next=curr;
		break;
	case 3:
		temp->x=head->x;
		temp->y=head->y-1;
	    head=temp;
	    head->next=curr;
		break;
	case 4:
		temp->x=head->x;
		temp->y=head->y+1;
	    head=temp;
	    head->next=curr;
		break;
	}
	while(curr->next!=tail)
	{
		curr=curr->next;
	}
	delete tail;
	tail=curr;
	tail->next=NULL;
	curr=head;
}

void Snake::setdir(const int& dir)
{
	direction=dir;
}

const int& Snake::getlengh()
{
	return length;
}

const Body* Snake::gethead()
{
    return head;
}

const Body* Snake::gettail()
{
	return tail;
}

const Body* Snake::getcurr()
{
	return curr;
}

void Snake::addcurr()
{
	if(curr==NULL)return;
    curr=curr->next;
}

void Snake::backcurr()
{
	curr=head;
}

main.cpp主文件

#include<iostream>
#include<windows.h>
#include<string>
#include<time.h>
#include<conio.h>
#include"Body.h"
#include"Snake.h"
int map[20][40]={0};
enum{up=1,down=2,left=3,right=4};
void print(int map1[20][40],int h,int s)
{
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<s;j++)
		{
			if(0==map1[i][j])
			{
			    std::cout<<" ";
			}
			if(1==map1[i][j])
			{
				std::cout<<"*";
			}
			if(2==map1[i][j])
			{
				std::cout<<"o";
			}
		}
		std::cout<<std::endl;
	}
}

int main()
{
	srand(time(0));
	Body food(rand()%20,rand()%40);
	bool pause=false;
	Snake brick;
	while(1)
	{
		if(kbhit())
		{
			char ch=getch();
			if(ch=='w')
			{
				if(brick.getdir()!=down)
				brick.setdir(up);
			}
			else if(ch=='s')
			{
				if(brick.getdir()!=up)
				brick.setdir(down);
			}
			else if(ch=='a')
			{
				if(brick.getdir()!=right)
				brick.setdir(left);
			}
			else if(ch=='d')
			{
				if(brick.getdir()!=left)
			    brick.setdir(right);
			}
		}
		for(int i=0;i<20;i++)
		{
			for(int j=0;j<40;j++)
			{
		        map[i][j]=0;
			}
		}
	    for(int i=0;i<brick.getlengh();i++)
	    {
	    	map[brick.getcurr()->x][brick.getcurr()->y]=1;
		    brick.addcurr();
	    }
		map[food.x][food.y]=2;
		if(brick.gethead()->x==food.x&&brick.gethead()->y==food.y)
		{
			srand(time(0));
			map[food.x][food.y]=0;
			brick.grow();
			food.x=rand()%20;
			food.y=rand()%40;
			brick.backcurr();
		}
		else
		{
		       brick.backcurr();
    	               print(map,20,40);
		       brick.move();
	               Sleep(500);
		}
		system("cls");
	}
	system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值