学C++ 写面向对象贪吃蛇(作风一贯::懒)

#include <iostream>
#include <ctime>
#include <conio.h>
using namespace std;

class data
{
private:
	int x;
	int  y;
public:
	data(int i, int ff) :x(i), y(ff){ ; }
	virtual~data(){ cout << "data................\n"; }
	virtual int getInt(){ return x; }
	virtual int getY(){ return y; }
	virtual void setint(int i){ x = i; }
	virtual void setY(int ff){ y = ff; }
	virtual void print() = 0;
};
class she_s :public data
{
public:
	she_s(int x, int ff) :data(x,ff){}
	~she_s(){ cout << "she_s...........\n"; }
	void print()
	{
		cout << "x==" << getInt() << "y==" << getY();
	}
};
class shiru:public data
{
public:
	shiru(int x, int y) :data(x,y){}
	~shiru(){ cout << "shiru...............\n"; }
	void print()
	{
		cout << "x==" << this->getInt() << "y ==" <<getY();;
	}
};
class node
{
public:
	node(data *pdata) :Data(pdata), isNode(0), L(0),  conut(0){}
	~node(){
		cout << "node............\n";
		delete Data;
		Data = 0;
		if (conut)
		{
			delete conut;
			conut = 0;
		}
	}
	node* getNode(){ return isNode; }
	data *getData(){ return Data; }
	node *getL(){ return L; }
	void initConut()
	{
		if (!conut)
			conut = new int(0);
		return;
	}
	void SetL(node *p){ L = p; }
	void Setdata(data *pdata){ Data = pdata; }
	void SetNode(node *p){ isNode = p; }
	int getConut(){ return *conut; }
	void SetConut(int i){ *conut = i; }
	void ADDConut(){ *conut+=1; }
	void SunConut(){ *conut-=1; }
private:
	data *Data;
	node *isNode;
	node *L;
	int *conut;
};
class l
{
public:
	l() :head(0),end(0),shi(0){}
	void init_she()
	{
		for (int i = 0; i < 3; i++)
		{
			data *p = new she_s(11,i+1 );
			if (!head)
			{
				head = new node(p);
				head->SetL(0);
				head->SetNode(0);
				end = head;
				head->initConut();
				head->ADDConut();
			}
			else
			{
				node *pnew = new node(p);
				end->SetNode(pnew);
				pnew->SetL(end);
				end = pnew;
				head->ADDConut();
			}		
		}
	}
	node *getShiTou(){ return head; }
	node *getWei(){ return end; }
	node * getshi(){ return shi; }
	void Setweiba(node *p){ end = p; }
	void init_shiwu()
	{
		if (!shi)
		{
			data *d = new shiru(7, 9);
			shi = new node(d);
			shi->initConut();
			shi->ADDConut();
			return;
		}
	}
	
	

	~l()
	{
		cout << "L........." << endl;
		if (head)
		{
			while (head->getNode() != NULL)
			{
				head = head->getNode();
				delete (head->getL());
				head->SetL(0);
			}
			delete head;
			head = end = NULL;
		}
		if (shi)
		{
			delete shi;
			shi = NULL;
		}
	}
private:
	node *head;
	node *end;
	node *shi;
	
};
class re
{
public:
	re() :map(0), sp(300), ch(0){ quit = false; }
	void initmap()
	{
		map = (char **)calloc(21, sizeof(char*));
		for (int i = 0; i < 21; i++)
		{
			map[i] = (char*)calloc(21, sizeof(char));
		}
		if (!map)
			exit(0);
		for (int i = 0; i < 20; i++)
		{
			map[0][i] = '1';
		}
		for (int i = 0; i < 20; i++)
		{
			map[i][0] = '1';
		}
		for (int i = 0; i < 20; i++)
		{
			map[i][19] = '1';
		}
		for (int i = 0; i < 20; i++)
		{
			map[19][i] = '1';
		}
	}
	int getsp(){ return sp; }
	void Setsp(int i){ sp = i; }
	void show_map()
	{
		cout << "\t C++太吃蛇大战!!" << endl;
		cout << "\n\n";
		for (int i = 0; i < 20; i++)
		{
			for (int j = 0; j < 20; j++)
			{
				if (map[i][j] == '1')
					cout << "■";
				 if (map[i][j] == '2')
					cout << "○";
				 if (map[i][j] == '3')
					cout << "●"; 
				if (map[i][j] == '6')
					cout << "◎";
				else if (!map[i][j])
					cout << "  ";
			}
			switch (i)
			{
			case 3:
				cout << "\t蛇等级为:" << p.getShiTou()->getConut()/5;
				break;
			case 5:
				cout << "\t蛇的长度:" << p.getShiTou()->getConut();
				break;
			case 7:
				cout << "\t蛇的速度:" << getsp();
				break;
			case 9:
				cout << "\t得分:" <<( p.getShiTou()->getConut() -3)* 10;
				break;
			case 11:
				cout << "\t食物的坐标:X" << p.getshi()->getData()->getInt() << "  Y的坐标:"<<p.getshi()->getData()->getY();
				break;
			case 17:
				cout << "\t游戏结束请按ESL";
				break;
			case 18:
				cout << "\t长按加速";
				break;
			}
			cout << endl;
		}
	}
	void init_shiwu()
	{
		srand(time(0));
		while (1)
		{
			int x = rand() % 18 + 1;
			int y = rand() % 18 + 1;
			if (map[x][y] != '1')
			{
				node *temp = p.getWei();
				while (temp)
				{
					if (x == temp->getData()->getInt() && y == temp->getData()->getY())
						break;
					temp = temp->getL();
				}
				if (temp)
					continue;
				else
				{
					p.getshi()->getData()->setint(x);
					p.getshi()->getData()->setY(y);
					map[p.getshi()->getData()->getInt()][p.getshi()->getData()->getY()] = '3';
					return;
				}
			}
		}
		
	}
	void read_map()
	{
		node *ps =p.getWei();
		int numer = p.getShiTou()->getConut();
		int conut=0;
		while (ps)
		{
			if (!conut)
			{
				++conut;
				map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
				
			}
			else if (conut == numer-1)
			{
				map[ps->getData()->getInt()][ps->getData()->getY()] = '2';
				conut++;
			}
				
			else
			{
				++conut;
				map[ps->getData()->getInt()][ps->getData()->getY()] = '3';
				
			}
			ps = ps->getL();
		}
		map[p.getshi()->getData()->getInt()][p.getshi()->getData()->getY()] = '3';
	}

	void Sethead(const int i)
	{
		node *ps =p.getShiTou() ;
		
		if (!ps)
			return;
		map[ps->getData()->getInt()][ps->getData()->getY()] = '\0';
		ps->getData()->setint(ps->getNode()->getData()->getInt());
		ps->getData()->setY(ps->getNode()->getData()->getY());
		map[ps->getData()->getInt()][ps->getData()->getY()] = '2';
		int x = p.getWei()->getData()->getInt();
		int y = p.getWei()->getData()->getY();
		switch (i)
		{
			case 1:
			case 2:
				while (1)
				{
					ps = ps->getNode();
					if (ps->getNode() == NULL)
						break;
					ps->getData()->setint(ps->getNode()->getData()->getInt());
					ps->getData()->setY(ps->getNode()->getData()->getY());
					map[ps->getData()->getInt()][ps->getData()->getY()] = '3';
				}
				if (i == 1)
				{
					ps->getData()->setint(x-1);
					ps->getData()->setY(y);
					map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
				}
				else
				{
					ps->getData()->setint(x + 1);
					ps->getData()->setY(y);
					map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
				}
			break;
			case 3:
			case 4:
				while(1)
				{
					ps = ps->getNode();
					if (ps->getNode() == NULL)
						break;
					ps->getData()->setint(ps->getNode()->getData()->getInt());
					ps->getData()->setY(ps->getNode()->getData()->getY());
					map[ps->getData()->getInt()][ps->getData()->getY()] = '3';
				}
				if (i == 3)
				{
					ps->getData()->setint(x);
					ps->getData()->setY(y-1);
					map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
				}
				else
				{
					ps->getData()->setint(x );
					ps->getData()->setY(y+1);
					map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
				}
				break;
		}

	}
	void chishiwu()
	{
		node*ps = p.getWei();
		if (ps->getData()->getInt() == p.getshi()->getData()->getInt() && ps->getData()->getY() == p.getshi()->getData()->getY())
		{
			p.getShiTou()->ADDConut();
			init_shiwu();//布置食物
			addshe_s();
			if ((p.getShiTou()->getConut() - 3) % 5 == 0)
			{
				Setsp(sp - 50);
			}

		}
	}
	void addshe_s()
	{
		int x = p.getWei()->getData()->getInt();
		int y = p.getWei()->getData()->getY();
		data *pp = new she_s(x, y);
		node*ps = new node(pp);
		node *temp = p.getWei();
		temp->SetNode(ps);
		ps->SetL(temp);
		p.Setweiba(ps);
		ps = ps->getL();
		while (ps!=p.getShiTou())
		{
			ps->getData()->setint(ps->getL()->getData()->getInt());
			ps->getData()->setY(ps->getL()->getData()->getY());
			ps = ps->getL();
		}
	}
	bool simang()
	{
		int x = p.getWei()->getData()->getInt();
		int y = p.getWei()->getData()->getY();
		if (x==0 || x == 19 || y == 0 || y == 19)
			return true;
		node *ps = p.getWei()->getL();
		node *pa= p.getShiTou();
		while (ps != pa)
		{
			if (ps->getData()->getInt() == x&&ps->getData()->getY() == y)
				return true;
			if (pa->getData()->getInt() == x&&pa->getData()->getY() == y)
				return true;
			ps = ps->getL();
			if (ps == pa)
				break;
			pa = pa->getNode();
			if (ps = ps)
				break;
		}
		return false;
	}
	void Run();
	
	void Sleep(int num)
	{
		clock_t st, en;
		st = clock();
		while (1)
		{
			en = clock();
			if ((double)(en - st) >= num)
			{
				break;
			}
		}
	}
	~re()
	{
		for (int i = 0; i < 21; i++)
		{
			delete []map[i];
		}
		delete []map;
		cout << "re......." << endl;
	}
private:
	char **map; //■●○
	l p;
	int sp;
	bool quit;
	 unsigned char ch;
};
void re::Run()
{
	system("color 02");
	
	initmap();//树池地图
	p.init_she();//舒适化蛇
	p.init_shiwu();//刷护士食物
	read_map();//写入地图
	show_map();//显示地图
	while (1)
	{
		
		if (_kbhit())
		{
			ch = _getch();
			if (_kbhit())
			{
				quit = false;
				Sleep(20);
			}
		}
		switch (ch)
		{
		case 'H'://上H
			Sethead(1);
			break;
		case 'P'://下P
			Sethead(2);
			break;
		case 'K'://左K
			Sethead(3);
			break;
		case 'M'://右M
			Sethead(4);
			break;
		}
		chishiwu();
		system("cls");
		show_map();//显示
		if (simang() || ch == 27 )
		{
			cout << "游戏结束\n";
			break;
		}
		if (quit)
		{
			Sleep(getsp());
		}
		
		quit = true;

	}

}
int main111()
{
	while (1)
	{
		re *r=new re;
		r->Run();
		delete r;
		r = NULL;
		fflush(stdin);
		system("pause");
	}
	
	
	system("pause");
     return 0;
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的C++面向对象贪吃蛇游戏的完整代码: ```cpp #include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <conio.h> using namespace std; class Snake { public: vector<pair<int, int>> body; int length; int direction; Snake(int x, int y, int len, int dir) { length = len; direction = dir; for (int i = 0; i < len; i++) { body.push_back(make_pair(x - i, y)); } } void move() { pair<int, int> head = body[0]; switch (direction) { case 0: // Up head.second--; break; case 1: // Down head.second++; break; case 2: // Left head.first--; break; case 3: // Right head.first++; break; default: break; } body.insert(body.begin(), head); body.pop_back(); } bool is_dead(int width, int height) { pair<int, int> head = body[0]; if (head.first < 0 || head.first >= width || head.second < 0 || head.second >= height) { return true; } for (int i = 1; i < body.size(); i++) { if (head == body[i]) { return true; } } return false; } void eat() { pair<int, int> head = body[0]; switch (direction) { case 0: // Up head.second--; break; case 1: // Down head.second++; break; case 2: // Left head.first--; break; case 3: // Right head.first++; break; default: break; } body.insert(body.begin(), head); length++; } }; class Food { public: pair<int, int> position; Food() { position = make_pair(0, 0); } void generate(int width, int height) { srand(time(NULL)); position.first = rand() % width; position.second = rand() % height; } }; class Game { public: int width; int height; Snake snake; Food food; Game(int w, int h) { width = w; height = h; snake = Snake(width / 2, height / 2, 3, 3); food = Food(); } void init() { snake = Snake(width / 2, height / 2, 3, 3); food.generate(width, height); } void run() { char key = ' '; while (key != 'q') { system("cls"); // Draw the map for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == 0 || i == height - 1 || j == 0 || j == width - 1) { cout << "*"; } else if (snake.body[0] == make_pair(j, i)) { cout << "O"; } else if (food.position == make_pair(j, i)) { cout << "X"; } else { bool is_body = false; for (int k = 1; k < snake.body.size(); k++) { if (snake.body[k] == make_pair(j, i)) { cout << "o"; is_body = true; break; } } if (!is_body) { cout << " "; } } } cout << endl; } // Handle user input if (_kbhit()) { key = _getch(); switch (key) { case 'w': if (snake.direction != 1) { snake.direction = 0; } break; case 's': if (snake.direction != 0) { snake.direction = 1; } break; case 'a': if (snake.direction != 3) { snake.direction = 2; } break; case 'd': if (snake.direction != 2) { snake.direction = 3; } break; default: break; } } // Move the snake snake.move(); // Check if the snake is dead if (snake.is_dead(width, height)) { cout << "Game over!" << endl; break; } // Check if the snake eats the food if (snake.body[0] == food.position) { snake.eat(); food.generate(width, height); } // Sleep for a while Sleep(100); } } }; int main() { Game game(20, 10); game.init(); game.run(); return 0; } ``` 这个代码实现了一个基本的贪吃蛇游戏,包含了移动、判断死亡、吃到食物等功能。你可以根据需要进一步扩展和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值