跳到正文

个人记录

摸摸鱼

Smile_Hammer约 18 分钟
原文
文章信息与版本

文章信息

发布
2025年9月22日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 18 分钟

版本

  1. v1当前

和deepseek一起做的超难小游戏,a和s表示左右移动,w表示跳跃,重点来了,s代表下劈,向下下劈时向上移动,向左下劈时向右上移动,向右下劈时向左上移动。 P表示你自己,^表示刺,=表示可以站的平台,Q表示终点。 第一个通过第一关的在10点之前成功给3r,九点半之前成功给5r。 code:

C++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
#include <cstdlib>
#include <ctime>



using namespace std;

const int WIDTH = 50;
const int HEIGHT = 15;
const char PLAYER = 'P';
const char PLATFORM = '=';
const char EMPTY = ' ';
const char SPIKE = '^';
const char GOAL = 'G'; // 终点标志

void cls() {
	COORD pos;
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	pos.X = 0;
	pos.Y = 0;
	SetConsoleCursorPosition(hOut, pos);
}

class Game {
private:
	struct Position {
		int x, y;
		Position(int x = 0, int y = 0) : x(x), y(y) {}
	};

	Position player;
	Position goal; // 终点位置
	vector<Position> platforms;
	vector<Position> spikes;
	float verticalVelocity;
	bool canJump;
	bool gameOver;
	bool gameWin; // 游戏胜利标志
	bool canDownSlash;
	bool canSideSlash;
	string slashDirection;

public:
	Game() : verticalVelocity(0), canJump(true), gameOver(false), gameWin(false),
	canDownSlash(false), canSideSlash(false), slashDirection("") {
		player = Position(5, HEIGHT - 3);
		goal = Position(WIDTH - 5, HEIGHT - 12); // 终点放在右上角高处
		initializeGame();
		srand(time(0));
	}

	void initializeGame() {
		platforms.clear();
		spikes.clear();

		// 地面
		for (int i = 0; i < WIDTH; i++) {
			platforms.push_back(Position(i, HEIGHT - 1));
		}

		// 平台 - 设计一条通往终点的路径
		createPlatform(5, HEIGHT - 3, 8);    // 起始平台
//		createPlatform(15, HEIGHT - 6, 6);   // 第一个跳跃平台
//		createPlatform(25, HEIGHT - 7, 5);   // 需要下劈的平台
//		createPlatform(35, HEIGHT - 10, 4);   // 高平台
		createSpikes(40, HEIGHT - 11, 3);  // 接近终点的平台
		createSpikes(44, HEIGHT - 12, 1);  // 终点前平台

		// 连接平台
		createSpikes(10, HEIGHT - 11, 4);   // 左侧连接平台
		createSpikes(20, HEIGHT - 6, 5);   // 中间连接平台
		createSpikes(30, HEIGHT - 8, 6);   // 右侧连接平台

		// 刺 - 设计成障碍,需要下劈技巧通过
		createSpikes(0, HEIGHT - 2, 100);     // 地面刺
		createSpikes(22, HEIGHT - 2, 4);
		createSpikes(40, HEIGHT - 2, 2);

		// 平台上的刺
		createSpikes(16, HEIGHT - 6, 2);     // 需要跳过
		createSpikes(28, HEIGHT - 5, 2);     // 需要下劈
		createSpikes(36, HEIGHT - 7, 4);

		// 空中刺(用于下劈)
//		createSpikes(8, HEIGHT - 4, 1);
		createSpikes(18, HEIGHT - 6, 2);
		createSpikes(33, HEIGHT - 8, 1);
		createSpikes(45, HEIGHT - 5, 1);

		// 侧面下劈专用刺
//		createSpikes(3, HEIGHT - 4, 1);      // 左侧刺
//		createSpikes(48, HEIGHT - 7, 1);     // 右侧刺
//		createSpikes(14, HEIGHT - 3, 1);     // 玩家右侧刺
//		createSpikes(26, HEIGHT - 6, 1);     // 玩家左侧刺

		// 墙面刺
//		createSpikes(2, HEIGHT - 5, 1);
		createSpikes(2, HEIGHT - 6, 1);
		createSpikes(47, HEIGHT - 8, 1);
		createSpikes(47, HEIGHT - 9, 1);

		// 终点周围的刺(增加挑战性)
		createSpikes(42, HEIGHT - 11, 2);
		createSpikes(46, HEIGHT - 13, 1);
	}

	void createPlatform(int startX, int y, int length) {
		for (int i = 0; i < length; i++) {
			platforms.push_back(Position(startX + i, y));
		}
	}

	void createSpikes(int startX, int y, int length) {
		for (int i = 0; i < length; i++) {
			spikes.push_back(Position(startX + i, y));
		}
	}

	void draw() {
		cls();

		cout << "目标: 到达终点(G) | 控制: A(左) D(右) W(跳跃) S(下劈) Q(退出) R(重开)" << endl;
		cout << "提示: 避开刺(^),使用下劈技巧到达终点" << endl;

		if (canDownSlash || canSideSlash) {
			cout << "*** 下劈可用!";
			if (canDownSlash) cout << " 下方有刺";
			if (canSideSlash) cout << " " << slashDirection << "侧有刺";
			cout << ",按S键触发 ***" << endl;
		} else {
			cout << "下劈: 需要下方或侧面3格内有刺才能使用" << endl;
		}

		// 绘制游戏区域
		for (int y = 0; y < HEIGHT; y++) {
			for (int x = 0; x < WIDTH; x++) {
				char ch = EMPTY;

				// 检查终点(最高优先级)
				if (goal.x == x && goal.y == y) {
					ch = GOAL;
				}
				// 检查刺
				else if (ch == EMPTY) {
					for (const auto& spike : spikes) {
						if (spike.x == x && spike.y == y) {
							ch = SPIKE;
							break;
						}
					}
				}

				// 检查平台
				if (ch == EMPTY) {
					for (const auto& platform : platforms) {
						if (platform.x == x && platform.y == y) {
							ch = PLATFORM;
							break;
						}
					}
				}

				// 检查玩家
				if (x == player.x && y == player.y) {
					ch = PLAYER;
				}

				cout << ch;
			}
			cout << endl;
		}

		if (gameOver) {
			cout << "!!! 碰到刺了!游戏结束 !!!" << endl;
			cout << "按R重新开始,按Q退出" << endl;
		} else if (gameWin) {
			cout << " 恭喜!你到达了终点!" << endl;
			cout << "按R再玩一次,按Q退出" << endl;
		}
	}

	// 检查下方是否有刺(3格范围内)
	bool checkSpikeBelow() {
		for (const auto& spike : spikes) {
			if (spike.x == player.x) {
				int distance = spike.y - player.y;
				if (distance > 0 && distance <= 3) {
					return true;
				}
			}
		}
		return false;
	}

	// 检查侧面是否有刺(3格范围内)
	bool checkSpikeSide() {
		canSideSlash = false;
		slashDirection = "";

		for (const auto& spike : spikes) {
			if (spike.y == player.y) {
				int leftDistance = player.x - spike.x;
				int rightDistance = spike.x - player.x;

				if (leftDistance > 0 && leftDistance <= 3) {
					canSideSlash = true;
					slashDirection = "左";
					return true;
				}
				if (rightDistance > 0 && rightDistance <= 3) {
					canSideSlash = true;
					slashDirection = "右";
					return true;
				}
			}

			// 检查斜向的刺
			int xDistance = abs(spike.x - player.x);
			int yDistance = abs(spike.y - player.y);

			if (xDistance <= 3 && yDistance <= 3 && xDistance + yDistance <= 4) {
				if (spike.x < player.x) {
					canSideSlash = true;
					slashDirection = "左";
					return true;
				} else if (spike.x > player.x) {
					canSideSlash = true;
					slashDirection = "右";
					return true;
				}
			}
		}
		return false;
	}

	void updatePhysics() {
		// 检查是否到达终点
		if (player.x == goal.x && player.y == goal.y) {
			gameWin = true;
			return;
		}

		// 更新下劈状态
		canDownSlash = checkSpikeBelow();
		checkSpikeSide();

		// 应用重力
		verticalVelocity += 0.3f;

		// 保存旧位置
		Position oldPosition = player;

		// 尝试移动
		player.y += (int)verticalVelocity;

		// 检查平台碰撞
		bool collision = false;
		for (const auto& platform : platforms) {
			if (player.x == platform.x && player.y == platform.y) {
				collision = true;
				break;
			}
		}

		// 处理平台碰撞
		if (collision) {
			if (verticalVelocity > 0) {
				player.y = oldPosition.y;
				verticalVelocity = 0;
				canJump = true;
			} else if (verticalVelocity < 0) {
				player.y = oldPosition.y;
				verticalVelocity = 0;
			}
		}

		// 检查是否站在地面上
		bool onGround = false;
		for (const auto& platform : platforms) {
			if (player.x == platform.x && player.y + 1 == platform.y) {
				onGround = true;
				canJump = true;
				verticalVelocity = 0;
				break;
			}
		}

		// 边界检查
		if (player.x < 0) player.x = 0;
		if (player.x >= WIDTH) player.x = WIDTH - 1;
		if (player.y < 0) {
			player.y = 0;
			verticalVelocity = 0;
		}
		if (player.y >= HEIGHT - 1) {
			player.y = HEIGHT - 2;
			verticalVelocity = 0;
			canJump = true;
		}

		// 检查刺碰撞 - 碰到就游戏结束
		for (const auto& spike : spikes) {
			if (player.x == spike.x && player.y == spike.y) {
				gameOver = true;
				return;
			}
		}

		// 游戏结束条件:掉出屏幕底部
		if (player.y >= HEIGHT) {
			gameOver = true;
		}
	}

	void jump() {
		if (canJump) {
			verticalVelocity = -2.0f;
			canJump = false;
		}
	}

	void downSlash() {
		if (canDownSlash || canSideSlash) {
			if (canDownSlash) {
				verticalVelocity = -1.5f;
				cout << "下方下劈!垂直跳跃!" << endl;
			} else if (canSideSlash) {
				verticalVelocity = -2.0f;
				if (slashDirection == "左") {
					player.x = min(WIDTH - 1, player.x + 2);
					cout << "左侧下劈!向右斜跳!" << endl;
				} else if (slashDirection == "右") {
					player.x = max(0, player.x - 2);
					cout << "右侧下劈!向左斜跳!" << endl;
				}
			}

			canJump = false;
			canDownSlash = false;
			canSideSlash = false;
			Sleep(100);
		}
	}

	void handleInput() {
		if (_kbhit()) {
			char input = _getch();
			input = tolower(input);

			switch (input) {
			case 'a':
				player.x = max(0, player.x - 1);
				break;
			case 'd':
				player.x = min(WIDTH - 1, player.x + 1);
				break;
			case 'w':
				jump();
				break;
			case 's':
				downSlash();
				break;
			case 'q':
				gameOver = true;
				break;
			case 'r':
				if (gameOver || gameWin) reset();
				break;
			}
		}
	}

	void reset() {
		player = Position(5, HEIGHT - 3);
		verticalVelocity = 0;
		canJump = true;
		gameOver = false;
		gameWin = false;
		canDownSlash = false;
		canSideSlash = false;
		slashDirection = "";
		initializeGame();
	}

	bool isGameEnd() {
		return gameOver || gameWin;
	}

	void run() {
		while (!isGameEnd()) {
			draw();
			handleInput();
			updatePhysics();
			Sleep(50);
		}
		draw();

		// 等待重新开始
		while (true) {
			if (_kbhit()) {
				char input = tolower(_getch());
				if (input == 'r') {
					reset();
					run();
					break;
				} else if (input == 'q') {
					break;
				}
			}
			Sleep(100);
		}
	}
};

int main() {
	cout << "=== 终点挑战平台跳跃游戏 ===" << endl;
	cout << "控制: A(左) D(右) W(跳跃) S(下劈)" << endl;
	cout << "目标: 避开刺(^),到达终点(G)" << endl;
	cout << "下劈机制: " << endl;
	cout << "  - 下方3格内有刺:垂直高跳" << endl;
	cout << "  - 左侧3格内有刺:向右斜跳" << endl;
	cout << "  - 右侧3格内有刺:向左斜跳" << endl;
	cout << "提示: 终点在右上角高处,需要巧妙使用下劈技巧才能到达" << endl;
	cout << "按任意键开始..." << endl;
	_getch();

	Game game;
	game.run();

	cout << "谢谢游玩!" << endl;
	return 0;
}
原文