From 60353170f8f377c3e4190f6afe4cf2dd6430a774 Mon Sep 17 00:00:00 2001 From: InkSoul Date: Mon, 10 Nov 2025 22:28:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=8E=A9=E5=AE=B6=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mazzGame/CMakeLists.txt | 4 +- mazzGame/src/Game.cpp | 57 +++++++++++++++++++++++++-- mazzGame/src/Game.h | 7 +++- mazzGame/src/Maze.h | 12 +----- mazzGame/src/Player.cpp | 82 +++++++++++++++++++++++++++++++++++++++ mazzGame/src/Player.h | 25 ++++++++++++ mazzGame/src/Position.cpp | 13 +++++++ mazzGame/src/Position.h | 12 ++++++ mazzGame/src/main.cpp | 5 +-- 9 files changed, 196 insertions(+), 21 deletions(-) create mode 100644 mazzGame/src/Position.cpp create mode 100644 mazzGame/src/Position.h diff --git a/mazzGame/CMakeLists.txt b/mazzGame/CMakeLists.txt index 253e83b..7f8667e 100644 --- a/mazzGame/CMakeLists.txt +++ b/mazzGame/CMakeLists.txt @@ -1,7 +1,7 @@ # CMakeList.txt: mazzGame 的 CMake 项目,在此处包括源代码并定义 # 项目特定的逻辑。 # -cmake_minimum_required (VERSION 3.8) +cmake_minimum_required (VERSION 3.12) # 如果支持,请为 MSVC 编译器启用热重载。 if (POLICY CMP0141) @@ -24,4 +24,4 @@ if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET mazzGame PROPERTY CXX_STANDARD 20) endif() -# TODO: 如有需要,请添加测试并安装目标。 +# TODO: 如有需要,请添加测试并安装目标。 \ No newline at end of file diff --git a/mazzGame/src/Game.cpp b/mazzGame/src/Game.cpp index b8d5ff5..e55f0ea 100644 --- a/mazzGame/src/Game.cpp +++ b/mazzGame/src/Game.cpp @@ -1,6 +1,9 @@ -#include "Game.h" +#include "Game.h" +#include // 用于时间控制 +#include // 用于_kbhit和_getch #include +#include // 用于Sleep Game::Game() { @@ -21,11 +24,58 @@ void Game::initMaze() m_maze.setMazeData(m_mazeData); m_maze.generate(); } + void Game::run() { - Position player(1, 1); + m_player.SetPosition(1, 1); std::vector monster{Position(2, 2), Position(4, 4)}; - m_maze.draw(player,monster); + + bool gameRunning = true; + + if (!m_initialized) + { + // 初始化时绘制一次迷宫 + system("cls"); // 清屏 + m_maze.draw(m_player.GetPosition(), monster); + m_initialized = true; + } + + while (gameRunning) + { + // 处理输入(非阻塞) + + // 更新玩家状态(处理输入和移动) + Position oldPos = m_player.GetPosition(); + m_player.Update(); + Position newPos = m_player.GetPosition(); + + // 检查新位置是否可行走,如果不可行走则恢复原位置 + if (!m_maze.isWalkable(newPos)) + { + m_player.SetPosition(oldPos); + } + + // 检查ESC键是否被按下,如果按下则退出游戏 + if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) + { + gameRunning = false; + } + + // 更新游戏状态 + // 这里可以添加怪物AI逻辑等 + + // 只在玩家位置发生变化时重绘 + bool playerMoved = (oldPos.x != newPos.x) || (oldPos.y != newPos.y); + if (playerMoved) + { + // 重绘游戏画面 + system("cls"); // 清屏 + m_maze.draw(m_player.GetPosition(), monster); + } + + // 控制游戏循环速度 + Sleep(50); // 降低CPU占用,但保持输入响应速度 + } } void Game::printConfig() @@ -43,5 +93,4 @@ void Game::printConfig() std::cout << std::endl; std::cout << "迷宫大小: " << m_config.getMazeWidth() << "x" << m_config.getMazeHeight() << std::endl; std::cout << "怪物AI难度: " << m_config.getDifficulty() << std::endl; - } \ No newline at end of file diff --git a/mazzGame/src/Game.h b/mazzGame/src/Game.h index 34a5746..1edfaa4 100644 --- a/mazzGame/src/Game.h +++ b/mazzGame/src/Game.h @@ -1,8 +1,9 @@ -#pragma once +#pragma once #include "Config.h" -#include "MazeData.h" #include "Maze.h" +#include "MazeData.h" +#include "Player.h" #include @@ -12,8 +13,10 @@ private: ConfigReader m_config; MazeData m_mazeData; Maze m_maze; + Player m_player; bool m_isRunning = true; + bool m_initialized = false; public: Game(); diff --git a/mazzGame/src/Maze.h b/mazzGame/src/Maze.h index 074446b..51127f7 100644 --- a/mazzGame/src/Maze.h +++ b/mazzGame/src/Maze.h @@ -1,19 +1,11 @@ #pragma once #include "MazeData.h" +#include "Position.h" #include #include -struct Position -{ - int x, y; - bool operator==(const Position &other) const - { - return x == other.x && y == other.y; - } -}; - class Maze { public: @@ -33,4 +25,4 @@ private: int m_width, m_height; std::vector m_grid; MazeData m_symbols; -}; +}; \ No newline at end of file diff --git a/mazzGame/src/Player.cpp b/mazzGame/src/Player.cpp index e69de29..a2cdff8 100644 --- a/mazzGame/src/Player.cpp +++ b/mazzGame/src/Player.cpp @@ -0,0 +1,82 @@ +#include "Player.h" +#include // 添加Windows API头文件 + +Player::Player() +{ + m_position.x = 0; + m_position.y = 0; +} + +Player::~Player() +{ +} + +void Player::Update() +{ + // 捕捉方向键输入并调用相应的移动方法 + // 使用Windows API检测键盘输入 + + // 检测上方向键是否被按下 + if (GetAsyncKeyState(VK_UP) & 0x8000) + { + MoveUp(); + } + + // 检测下方向键是否被按下 + if (GetAsyncKeyState(VK_DOWN) & 0x8000) + { + MoveDown(); + } + + // 检测左方向键是否被按下 + if (GetAsyncKeyState(VK_LEFT) & 0x8000) + { + MoveLeft(); + } + + // 检测右方向键是否被按下 + if (GetAsyncKeyState(VK_RIGHT) & 0x8000) + { + MoveRight(); + } +} + +void Player::SetPosition(int x, int y) +{ + m_position.x = x; + m_position.y = y; +} + +void Player::SetPosition(const Position &pos) +{ + m_position = pos; +} + +Position Player::GetPosition() const +{ + return m_position; +} + +void Player::MoveUp() +{ + // 实现向上移动的逻辑 + m_position.y -= 1; +} + +void Player::MoveDown() +{ + // 实现向下移动的逻辑 + m_position.y += 1; +} + +void Player::MoveLeft() +{ + // 实现向左移动的逻辑 + m_position.x -= 1; +} + +void Player::MoveRight() +{ + // 实现向右移动的逻辑 + m_position.x += 1; +} \ No newline at end of file diff --git a/mazzGame/src/Player.h b/mazzGame/src/Player.h index 73b4b86..a968fb7 100644 --- a/mazzGame/src/Player.h +++ b/mazzGame/src/Player.h @@ -1 +1,26 @@ #pragma once + +#include "Position.h" + +class Player +{ +public: + Player(); + ~Player(); + + void Update(); + + void SetPosition(int x, int y); + void SetPosition(const Position &pos); + + // 移动方法,处理方向键输入 + void MoveUp(); + void MoveDown(); + void MoveLeft(); + void MoveRight(); + + Position GetPosition() const; + +private: + Position m_position; +}; \ No newline at end of file diff --git a/mazzGame/src/Position.cpp b/mazzGame/src/Position.cpp new file mode 100644 index 0000000..b8c5369 --- /dev/null +++ b/mazzGame/src/Position.cpp @@ -0,0 +1,13 @@ +#include "Position.h" + +Position::Position() + : x(0), y(0) {} + +Position::Position(int x, int y) + : x(x), y(y) {} + +bool Position::operator==(const Position &other) const +{ + return x == other.x && y == other.y; +} + diff --git a/mazzGame/src/Position.h b/mazzGame/src/Position.h new file mode 100644 index 0000000..206055d --- /dev/null +++ b/mazzGame/src/Position.h @@ -0,0 +1,12 @@ +#pragma once + +class Position +{ +public: + int x, y; + + Position(); + Position(int x, int y); + + bool operator==(const Position &other) const; +}; \ No newline at end of file diff --git a/mazzGame/src/main.cpp b/mazzGame/src/main.cpp index 892bf81..a9eaddb 100644 --- a/mazzGame/src/main.cpp +++ b/mazzGame/src/main.cpp @@ -1,7 +1,6 @@ -#include "main.h" +#include "main.h" #include -#include int main() { @@ -11,7 +10,7 @@ int main() Game game; game.loadConfig("config.ini"); - //game.printConfig(); + // game.printConfig(); game.initMaze(); game.run(); system("pause");