LZT 贪吃蛇2

LZT  •  1个月前


include

include <windows.h>

include <conio.h>

include

include

include

using namespace std;

// 光标操作函数 void gotoxy(int x, int y) {

COORD pos = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}

void hidecursor() {

CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);

}

// 游戏常量 const int WIDTH = 25; const int HEIGHT = 15; const int SPEED = 120;

// 游戏类 class SnakeGame {

private:
	struct Point {
		int x, y;
	};

	deque<Point> snake;
	Point food;
	int direction; // 0:右, 1:下, 2:左, 3:上
	int score;
	bool gameover;

	void drawBorder() {
		// 上边框
		gotoxy(0, 0);
		for (int i = 0; i < WIDTH + 2; i++)
			cout << "#";

		// 左右边框
		for (int i = 1; i <= HEIGHT; i++) {
			gotoxy(0, i);
			cout << "#";
			gotoxy(WIDTH + 1, i);
			cout << "#";
		}

		// 下边框
		gotoxy(0, HEIGHT + 1);
		for (int i = 0; i < WIDTH + 2; i++)
			cout << "#";
	}

	void generateFood() {
		bool onSnake;
		do {
			onSnake = false;
			food.x = rand() % WIDTH;
			food.y = rand() % HEIGHT;

			for (const auto &segment : snake) {
				if (segment.x == food.x && segment.y == food.y) {
					onSnake = true;
					break;
				}
			}
		} while (onSnake);

		gotoxy(food.x + 1, food.y + 1);
		cout << "@";
	}

public:
	SnakeGame() {
		// 初始化蛇
		snake.push_back({WIDTH / 2, HEIGHT / 2});
		direction = 0;
		score = 0;
		gameover = false;

		// 初始化随机种子
		srand(time(NULL));

		// 设置控制台
		system("mode con cols=40 lines=25");
		hidecursor();
		system("cls");

		// 绘制边框
		drawBorder();

		// 生成第一个食物
		generateFood();

		// 绘制初始蛇
		gotoxy(snake[0].x + 1, snake[0].y + 1);
		cout << "O";

		// 显示分数
		gotoxy(0, HEIGHT + 3);
		cout << "分数: " << score;
		gotoxy(0, HEIGHT + 4);
		cout << "控制: WAZS 移动, X 退出";
	}

	void processInput() {
		if (_kbhit()) {
			char key = _getch();
			switch (key) {
				case 'w':
				case 'W':
					if (direction != 1)
						direction = 3;
					break;
				case 'z':
				case 'Z':
					if (direction != 3)
						direction = 1;
					break;
				case 'a':
				case 'A':
					if (direction != 0)
						direction = 2;
					break;
				case 's':
				case 'S':
					if (direction != 2)
						direction = 0;
					break;
				case 'x':
				case 'X':
					gameover = true;
					break;
			}
		}
	}

	void update() {
		// 计算新的蛇头位置
		Point newHead = snake.front();
		switch (direction) {
			case 0:
				newHead.x++;
				break;
			case 1:
				newHead.y++;
				break;
			case 2:
				newHead.x--;
				break;
			case 3:
				newHead.y--;
				break;
		}

		// 边界检查(穿墙)
		if (newHead.x < 0)
			newHead.x = WIDTH - 1;
		else if (newHead.x >= WIDTH)
			newHead.x = 0;
		if (newHead.y < 0)
			newHead.y = HEIGHT - 1;
		else if (newHead.y >= HEIGHT)
			newHead.y = 0;

		// 检查是否撞到自己
		for (const auto &segment : snake) {
			if (segment.x == newHead.x && segment.y == newHead.y) {
				gameover = true;
				return;
			}
		}

		// 移动蛇
		snake.push_front(newHead);

		// 绘制新蛇头
		gotoxy(newHead.x + 1, newHead.y + 1);
		cout << "O";

		// 检查是否吃到食物
		if (newHead.x == food.x && newHead.y == food.y) {
			score += 10;
			gotoxy(7, HEIGHT + 3);
			cout << score;
			generateFood();
		} else {
			// 如果没有吃到食物,移除蛇尾
			Point tail = snake.back();
			snake.pop_back();
			gotoxy(tail.x + 1, tail.y + 1);
			cout << " ";
		}

		// 将旧蛇头位置改为蛇身
		if (snake.size() > 1) {
			gotoxy(snake[1].x + 1, snake[1].y + 1);
			cout << "o";
		}
	}

	bool isGameOver() const {
		return gameover;
	}
	int getScore() const {
		return score;
	}

};

int main() {

SnakeGame game;

while (!game.isGameOver()) {
	game.processInput();
	game.update();
	Sleep(SPEED);
}

system("cls");
cout << "===== 游戏结束 =====" << endl;
cout << "最终分数: " << game.getScore() << endl;
cout << "\n按任意键退出...";
_getch();

return 0;

}


评论:

请先登录,才能进行评论