C++ 贪吃蛇小游戏(CLI)

前言

我大二的时候用c写过一个跑出五格就越界的贪吃蛇,现在看来简直是一堆垃圾。
我现在用cplusplus重写它,那就叫它垃圾plusplus好了(狗头)。

设计思路

贪吃蛇行为过程:
1.蛇吃果子
2.蛇以speed运动
3.蛇在ground上运动
4.果子以frquency为间隔出现
5.果子出现在在ground上
6.蛇的头撞到了空气和果子之外的物体会死

实现过程

backg.h文件:

#include<vector>
#include<windows.h>
#include<iostream>
#include<conio.h>
#include<queue>
#include<thread>
#include<random>
#include<time.h>
class backg {
public:
	std::vector<std::vector<int>> ground;
	HANDLE hout;
	backg();
	void jump(int x,int y);
	void plant();
	void startplant();
};

backg.c文件:

#include<backg.h>
backg::backg()
{
	int m=29;/*高*/int n=91;/*长*/
	std::vector<std::vector<int>> t(m, std::vector<int>(n));
	ground = t;
	hout = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(hout, &cursor_info);
	int i = 0;
	while (i < m || i < n)
	{
		if (i < m) 
		{ ground[i][0] = ground[i][n - 1] = 1; jump(0, i); std::cout << '|'; jump(n - 1, i); std::cout << '|'; }
		if (i < n) 
		{ ground[0][i] = ground[m - 1][i] = 1; jump(i, 0); std::cout << '-'; jump(i, m - 1); std::cout << '-'; }
		i++;
	}
}
void backg::jump(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(hout, coord);
}
void backg::plant()
{
	srand(time(NULL));
	while (1)
	{
		int n = rand() % (89 * 27);
		int i = 1; int j = 1;
		while (1)
		{
			if (ground[i][j] == 0)
				n--; if (n == 0)break;
			i++; j++;
			if (j == 90)j = 1;
			if (i == 28)i = 1;
		}
		ground[i][j] = 2; jump(j, i); std::cout << '$';
		Sleep(4000);
	}
}
void backg::startplant()
{
	std::thread t2(&backg::plant, this);
	t2.detach(); 
}

snake.h文件

#include<backg.h>
class snake :public backg {
public:
	std::queue<std::vector<int>> body;
	char dir;
	double speed;
	snake();
	void walk();
	void startwalk();
};

snake.cpp文件

#include<snake.h>
snake::snake()
{
	dir = 77;
	std::vector<int> t(2);
	std::queue<std::vector<int>> b;
	t[0] = ground.size() / 2; t[1] = ground[0].size() / 2; 
	for (int i = 0; i < 4; i++)
	{	
		ground[t[0]][t[1]] = 1;
		jump(t[1], t[0]); 
		if (i != 3)
			std::cout << '*'; 
		else std::cout << '@'; b.push(t);
		t[1]++;
	}
	body = b;
	speed = 0.3;
	jump(91, 4);
	std::cout << "speed:" << speed;
}
void snake::walk()
{
	std::vector<int> t;
	while (1)
	{
		t = body.back(); jump(t[1], t[0]); std::cout << '*';
		if (dir == 72)
		{
			t[0]--; body.push(t); jump(t[1], t[0]); std::cout << '@';
		}else
		if (dir == 80)
		{
			t[0]++; body.push(t); jump(t[1], t[0]); std::cout << '@';
		}else
		if (dir == 75)
		{
			t[1]--; body.push(t); jump(t[1], t[0]); std::cout << '@';
		}else
		if (dir == 77)
		{
			t[1]++; body.push(t); jump(t[1], t[0]); std::cout << '@';
		}
		if (ground[t[0]][t[1]] == 2)
		{
			ground[t[0]][t[1]] = 1;		
			if (body.size() % 10 == 0) 
			{ jump(91, 4); std::cout << "                 ";			jump(91, 4); speed -= 0.001; }
			std::cout << "speed:" << speed;
		}
		else
		{
			if (ground[t[0]][t[1]])//error
				break;
			else {
				ground[t[0]][t[1]] = 1;
				t = body.front();
				jump(t[1], t[0]); std::cout << ' ';
				ground[t[0]][t[1]] = 0;
				body.pop();
			}
		}
		jump(91, 3);
		std::cout << "                 "; jump(91, 3);
		std::cout << "score:" << body.size();

		Sleep(speed * 1000);
	}
	jump(91, 4);
	std::cout << "                                       ";
	jump(91, 4);
	std::cout << "byebye";
}
void snake::startwalk()
{
	std::thread t1(&snake::walk, this);
	t1.detach();
}
int main()
{
	snake a;
	//a.walk();
	int b;
	int i = 0;
	a.startwalk();
	Sleep(2000);
	a.startplant();
	while (1)
	{
		b = _getch();
		if ((a.dir == 72 || a.dir == 80) && (b == 75 || b == 77))a.dir = b;
		else if ((a.dir == 75 || a.dir == 77) && (b == 72 || b == 80)) a.dir = b;;
		b = 0;
	}
}

运行测试

在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36389986/article/details/111242889