完成玩家移动逻辑

main
InkSoul 2025-11-10 22:28:06 +08:00
parent fba5e3b2ab
commit 60353170f8
9 changed files with 196 additions and 21 deletions

View File

@ -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:

View File

@ -1,6 +1,9 @@
#include "Game.h"
#include "Game.h"
#include <chrono> // 用于时间控制
#include <conio.h> // 用于_kbhit和_getch
#include <iostream>
#include <windows.h> // 用于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<Position> 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;
}

View File

@ -1,8 +1,9 @@
#pragma once
#pragma once
#include "Config.h"
#include "MazeData.h"
#include "Maze.h"
#include "MazeData.h"
#include "Player.h"
#include <string>
@ -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();

View File

@ -1,19 +1,11 @@
#pragma once
#include "MazeData.h"
#include "Position.h"
#include <string>
#include <vector>
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<std::string> m_grid;
MazeData m_symbols;
};
};

View File

@ -0,0 +1,82 @@
#include "Player.h"
#include <windows.h> // 添加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;
}

View File

@ -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;
};

View File

@ -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;
}

View File

@ -0,0 +1,12 @@
#pragma once
class Position
{
public:
int x, y;
Position();
Position(int x, int y);
bool operator==(const Position &other) const;
};

View File

@ -1,7 +1,6 @@
#include "main.h"
#include "main.h"
#include <windows.h>
#include <consoleapi2.h>
int main()
{
@ -11,7 +10,7 @@ int main()
Game game;
game.loadConfig("config.ini");
//game.printConfig();
// game.printConfig();
game.initMaze();
game.run();
system("pause");