C++命令行贪吃蛇

闲来无事,用c++写了个命令行贪吃蛇玩玩,发现刷新太快,光标跑来跑去的很难受,不过勉强能“冲”。下面放上代码。DALAO勿喷。

代码

#include<iostream>
#include<string>
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
const int top = 0;
const int feet = 20;
typedef struct {
	int x;int y;
}BodyPos;
typedef struct {
	int dx;int dy;
}Direction;
class Snake
{
private:
	int length;
	Direction dir;
public:
	Snake(int Length, int Dx, int Dy){
		this->length = Length;this->dir.dx = Dx;this->dir.dy = Dy;
	}
	~Snake() { cout << "你蛇没了" <<endl; }
	void Get_food() { this->length++; }
	int Get_length() { return length; }
	void Put_deirection(int Dx, int Dy) { this->dir = { Dx,Dy }; }
	Direction Get_direction() { return dir; }
};
class Map {
private:
	char map[21][21] = { ' ' };
	char food = '*';
	char body = 'o';
	char TransverseBondary = '-';
	char VerticalBondary = '|';
	BodyPos bodypos[300] = { 0,0 };
	BodyPos foodpos;
public:
	Map(Snake& s) {
		for (int i = top;i <= feet;i++) {
			this->map[top][i] = this->TransverseBondary;this->map[feet][i] = this->TransverseBondary;
			//this->map[i][top] = this->VerticalBondary;this->map[i][feet] = this->VerticalBondary;
		}
		for (int i = top + 1;i < feet;i++) {
			this->map[i][top] = this->VerticalBondary;this->map[i][feet] = this->VerticalBondary;
		}
		int j = 5;
		for (int i = 2;i >= 0;i--) {
			bodypos[i] = { 7,j };
			j++;
		}
		this->GetFoodPos();
		SetSnake(s);
	}
	void SetSnake(Snake& s) {
		int length = s.Get_length();
		this->DeleteMap();
		for (int i = 0;i < length;i++) {
			map[bodypos[i].x][bodypos[i].y] = body;
		}
		this->map[foodpos.x][foodpos.y] = '*';
	}
	void DeleteMap() {
		for (int i = 0;i <= feet;i++) {
			for (int j = 0;j <= feet;j++) {
				map[i][j] = ' ';
			}
		}
		for (int i = top;i <= feet;i++) {
			this->map[top][i] = this->TransverseBondary;this->map[feet][i] = this->TransverseBondary;
			//this->map[i][top] = this->VerticalBondary;this->map[i][feet] = this->VerticalBondary;
		}
		for (int i = top + 1;i < feet;i++) {
			this->map[i][top] = this->VerticalBondary;this->map[i][feet] = this->VerticalBondary;
		}
	}
	void PrintMap() {
		for (int i = top;i <= feet;i++) {
			for (int j = top;j <= feet;j++)
				printf("%c ", map[i][j]);
			printf("\n");
		}
	}
	/*void GetFood(Snake &s) {
		s.Get_food();
		this->bodypos[s.Get_length()];
	}*/
	void GetFoodPos() {
		srand(time(0));
		this->foodpos.x = rand() % 19 + 1;
		this->foodpos.y = rand() % 19 + 1;
	}
	void Move(Snake& s) {
		int dx = s.Get_direction().dx;
		int dy = s.Get_direction().dy;
		int length = s.Get_length();
		if (this->map[bodypos[0].x][bodypos[0].y] == '*') { s.Get_food();this->GetFoodPos(); }
		for (int i = length-1;i > 0;i--) {
			bodypos[i].x = bodypos[i - 1].x;
			bodypos[i].y = bodypos[i - 1].y;
		}		
		if (this->bodypos[0].x + dx > feet) { this->bodypos[0].x = top;this->SetSnake(s); return; }
		if (this->bodypos[0].x + dx < top) { this->bodypos[0].x = feet;this->SetSnake(s);return; }
		if (this->bodypos[0].y + dy > feet) { this->bodypos[0].y = top;this->SetSnake(s); return; }
		if (this->bodypos[0].y + dy < top) { this->bodypos[0].y = feet;this->SetSnake(s);return; }		
		this->bodypos[0].x += dx, this->bodypos[0].y += dy;
		this->SetSnake(s);
	}
	bool HoldJudge(int Dx,int Dy) {
		if (this->bodypos[0].x + Dx == this->bodypos[1].x && this->bodypos[0].y + Dy == this->bodypos[1].y)return false;
		if (this->bodypos[0].x + Dx > feet) {  return false; }
		if (this->bodypos[0].x + Dx < top) { return false; }
		if (this->bodypos[0].y + Dy > feet) {  return false; }
		if (this->bodypos[0].y + Dy < top) { return false; }
		else return true;
	}
	bool TKJudge(Snake &s) {
		int length = s.Get_length();
			if (map[this->bodypos[0].x+s.Get_direction().dx][this->bodypos[0].y+s.Get_direction().dy]=='o')return false;
			else return true;
	}
};
int main() {
	Snake s(3,0,1);
	Map m(s);
	int ch;
	while (1) {
		cout << "score:" << s.Get_length() << endl;
		m.PrintMap();
		if (m.TKJudge(s) == false) { cout << "No!";break; }
		
		if (_kbhit()) {		//如果有按键按下,则_kbhit()函数返回真
			ch = _getch();	//使用_getch()函数获取按下的键值
			if (ch == 75) { if(m.HoldJudge(0,-1))s.Put_deirection(0,-1); }
			if (ch == 72) { if(m.HoldJudge(-1,0))s.Put_deirection(-1,0); }
			if (ch == 77) { if(m.HoldJudge(0,1))s.Put_deirection(0,1); }
			if (ch == 80) { if(m.HoldJudge(1,0))s.Put_deirection(1,0); }
		}
		m.Move(s);
		Sleep(50);
		system("cls");
	}
	system("pause");
	return 0;
}

运行截图

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cgxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值