C++/*
版本:5.11
玩法:
按下Ctrl+G停止
左右键移动鼠标111111
空格键跳跃
W键在脚下生成一个箱子(垫脚石)
C键清除所有箱子
S键保存当前箱子布局(存档)
L键读取保存的箱子布局(读档)
H键隐藏/显示所有箱子(同时解锁/锁定鼠标控制)
D键删除脚下的箱子
J键发射激光(手动)
在连接器命令行加入以下命令:-static-libgcc -lcomdlg32 -lshell32 -lole32 -lgdi32 -fexec-charset=utf-8
*/
#include<iostream>
#include<windows.h>
#include<vector>
#include<string>
#include<fstream>
#include<commdlg.h> // 文件对话框头文件
#include <shlobj.h> // SHGetFolderPath
#include <cstdlib> // for rand()
#include <ctime> // for time()
// 链接必要的库
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "ole32.lib") // 为了 CoInitializeEx
#define MOVEUP VK_UP
#define MOVELEFT VK_LEFT
#define MOVERIGHT VK_RIGHT
#define CREATEBOX 'W'
#define CLEARBOX 'C'
#define SAVEGAME 'S'
#define LOADGAME 'L'
#define HIDEBOX 'H'
#define DELETEBOX 'D'
#define LASERKEY 'J' // 新增激光键
using namespace std;
// 玩家(鼠标)的虚拟半径 - 这样玩家就有"身体"了
const double PLAYER_RADIUS = 5.0;
double posx, posy, xspeed, yspeed;
POINT p;
const double g=0.25, pluspeed=0.95;
int maxx, maxy;
bool jumping=0;
int bcnt;
bool pressw, presss, pressl, pressh, pressd, pressj; // 新增:pressj
// 箱子隐藏状态和鼠标控制状态
bool boxesHidden = false;
bool mouseControlEnabled = true;
// 箱子窗口类名
const char BOX_CLASS_NAME[] = "BoxWindowClass";
// 激光相关定义
const char LASER_CLASS_NAME[] = "LaserWindowClass";
struct box{
HWND hwnd;
int x, y; // w=100,h=100
bool hidden; // 记录箱子是否被隐藏
} bx[222];
struct Laser {
HWND hwnd;
int x, y; // 激光线的位置
int length; // 激光线长度
bool isHorizontal; // true为横向,false为纵向
bool active; // 是否激活(显示)
int direction; // 激光方向:1向右/下,-1向左/上
};
vector<Laser> lasers; // 存储所有激光
// 存档数据结构
struct SaveData {
double playerX, playerY;
int boxCount;
vector<pair<int, int>> boxPositions; // 箱子位置
};
// 存档文件默认扩展名
const string SAVE_FILE_EXT = ".boxsav";
// 前向声明函数
HWND CreateBoxWindow(int x, int y);
HWND CreateLaserWindow(int x, int y, int width, int height);
// 激光窗口消息处理函数
LRESULT CALLBACK LaserWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 绘制激光线
HBRUSH brush = CreateSolidBrush(RGB(255, 0, 0)); // 红色激光
SelectObject(hdc, brush);
RECT rect;
GetClientRect(hwnd, &rect);
// 绘制激光线 - 使用Rectangle绘制一个细长的矩形
FillRect(hdc, &rect, brush);
DeleteObject(brush);
EndPaint(hwnd, &ps);
return 0;
}
case WM_ERASEBKGND:
return 1; // 告诉Windows我们已经处理了背景擦除
case WM_NCHITTEST:
return HTTRANSPARENT; // 让鼠标点击穿透激光
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// 箱子窗口消息处理函数
LRESULT CALLBACK BoxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 绘制箱子(白色矩形)
HBRUSH brush = CreateSolidBrush(RGB(255, 255, 255)); // 白色
SelectObject(hdc, brush);
RECT rect;
GetClientRect(hwnd, &rect);
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
// 绘制箱子边框
HPEN pen = CreatePen(PS_SOLID, 2, RGB(101, 67, 33)); // 深棕色边框
SelectObject(hdc, pen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
DeleteObject(brush);
DeleteObject(pen);
EndPaint(hwnd, &ps);
return 0;
}
case WM_ERASEBKGND:
return 1; // 告诉Windows我们已经处理了背景擦除
case WM_NCHITTEST:
return HTTRANSPARENT; // 让鼠标点击穿透箱子
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// 注册激光窗口类
void RegisterLaserWindowClass() {
static bool registered = false;
if(registered) return;
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = LaserWindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH); // 透明背景
wc.lpszClassName = LASER_CLASS_NAME;
RegisterClassEx(&wc);
registered = true;
}
// 注册箱子窗口类
void RegisterBoxWindowClass() {
static bool registered = false;
if(registered) return;
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = BoxWindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH); // 透明背景
wc.lpszClassName = BOX_CLASS_NAME;
RegisterClassEx(&wc);
registered = true;
}
// 创建激光窗口
HWND CreateLaserWindow(int x, int y, int width, int height) {
RegisterLaserWindowClass();
HWND hwnd = CreateWindowEx(
WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW,
LASER_CLASS_NAME,
"Laser",
WS_POPUP | WS_VISIBLE,
x, y, width, height,
NULL, NULL, GetModuleHandle(NULL), NULL
);
// 设置窗口透明度和点击穿透
SetLayeredWindowAttributes(hwnd, 0, 200, LWA_ALPHA); // 半透明效果
return hwnd;
}
// 创建箱子窗口
HWND CreateBoxWindow(int x, int y) {
RegisterBoxWindowClass();
HWND hwnd = CreateWindowEx(
WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW,
BOX_CLASS_NAME,
"Box",
WS_POPUP | WS_VISIBLE,
x, y, 100, 100,
NULL, NULL, GetModuleHandle(NULL), NULL
);
// 设置窗口透明度和点击穿透
SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
return hwnd;
}
void CreateBox(){
bx[++bcnt].hwnd = CreateBoxWindow(posx-50, posy+5);
bx[bcnt].x = posx-50;
bx[bcnt].y = posy+5;
bx[bcnt].hidden = false; // 默认不隐藏
ShowWindow(bx[bcnt].hwnd, SW_SHOW);
UpdateWindow(bx[bcnt].hwnd);
}
// 创建指定位置的箱子
void CreateBoxAt(int x, int y) {
bx[++bcnt].hwnd = CreateBoxWindow(x, y);
bx[bcnt].x = x;
bx[bcnt].y = y;
bx[bcnt].hidden = false; // 默认不隐藏
ShowWindow(bx[bcnt].hwnd, SW_SHOW);
UpdateWindow(bx[bcnt].hwnd);
}
// 创建激光
void CreateLaser(bool isHorizontal, int position, int direction = 1) {
int laserWidth, laserHeight;
int x, y;
if (isHorizontal) {
// 横向激光
laserWidth = maxx; // 横跨整个屏幕宽度
laserHeight = 5; // 高度为5像素
x = 0;
y = position;
} else {
// 纵向激光
laserWidth = 5; // 宽度为5像素
laserHeight = maxy; // 高度为整个屏幕高度
x = position;
y = 0;
}
Laser newLaser;
newLaser.hwnd = CreateLaserWindow(x, y, laserWidth, laserHeight);
newLaser.x = x;
newLaser.y = y;
newLaser.length = isHorizontal ? maxx : maxy;
newLaser.isHorizontal = isHorizontal;
newLaser.active = true;
newLaser.direction = direction;
lasers.push_back(newLaser);
}
// 显示激光预警
void ShowLaserWarning(const char* message) {
HWND msgBox = CreateWindowEx(
WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
"STATIC",
message,
WS_POPUP | WS_VISIBLE | WS_BORDER | SS_CENTER,
GetSystemMetrics(SM_CXSCREEN) / 2 - 150,
GetSystemMetrics(SM_CYSCREEN) / 2 - 50,
300, 100,
NULL, NULL, GetModuleHandle(NULL), NULL
);
// 设置字体
HFONT hFont = CreateFont(24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, "Arial");
SendMessage(msgBox, WM_SETFONT, (WPARAM)hFont, TRUE);
SetTimer(msgBox, 1, 1500, NULL); // 1.5秒后自动关闭
// 处理消息直到定时器触发
MSG msg;
DWORD startTime = GetTickCount();
while(GetMessage(&msg, msgBox, 0, 0)) {
if(msg.message == WM_TIMER || (GetTickCount() - startTime) > 1500) {
DestroyWindow(msgBox);
DeleteObject(hFont);
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// 改进的碰撞检测函数
bool CheckCollision(double px, double py, double nextY, int boxIndex) {
// 如果箱子被隐藏,不进行碰撞检测
if (bx[boxIndex].hidden) {
return false;
}
// 箱子的边界
double boxLeft = bx[boxIndex].x;
double boxRight = bx[boxIndex].x + 100;
double boxTop = bx[boxIndex].y;
double boxBottom = bx[boxIndex].y + 100;
// 玩家的边界(考虑玩家半径)
double playerLeft = px - PLAYER_RADIUS;
double playerRight = px + PLAYER_RADIUS;
double playerTop = py - PLAYER_RADIUS;
double playerBottom = py + PLAYER_RADIUS;
double playerNextBottom = nextY + PLAYER_RADIUS;
// 检查是否在箱子水平范围内
if (playerRight > boxLeft && playerLeft < boxRight) {
// 检查是否从上方接近箱子
if (playerBottom <= boxTop && playerNextBottom >= boxTop && yspeed >= 0) {
// 检查垂直重叠范围 - 放宽条件
if (playerNextBottom - boxTop <= 20) { // 允许最多20像素的穿透深度
return true;
}
}
// 额外检查:如果玩家已经在箱子内部,把他推出来
if (playerBottom > boxTop && playerTop < boxBottom &&
playerRight > boxLeft && playerLeft < boxRight) {
// 检查是从哪个方向进入的
if (py < boxTop) { // 从上方进入
return true;
}
}
}
return false;
}
// 检测玩家与激光的碰撞
bool CheckLaserCollision(double px, double py) {
for(size_t i = 0; i < lasers.size(); i++) {
if (!lasers[i].active) continue;
if (lasers[i].isHorizontal) {
// 横向激光
if (py >= lasers[i].y && py <= lasers[i].y + 5) { // 激光高度为5像素
if (px >= lasers[i].x && px <= lasers[i].x + maxx) {
return true;
}
}
} else {
// 纵向激光
if (px >= lasers[i].x && px <= lasers[i].x + 5) { // 激光宽度为5像素
if (py >= lasers[i].y && py <= lasers[i].y + maxy) {
return true;
}
}
}
}
return false;
}
// 显示简短消息
void ShowBriefMessage(const char* message) {
HWND msgBox = CreateWindowEx(
WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
"STATIC",
message,
WS_POPUP | WS_VISIBLE | WS_BORDER | SS_CENTER,
GetSystemMetrics(SM_CXSCREEN) / 2 - 150,
GetSystemMetrics(SM_CYSCREEN) / 2 - 50,
300, 100,
NULL, NULL, GetModuleHandle(NULL), NULL
);
// 设置字体
HFONT hFont = CreateFont(24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, "Arial");
SendMessage(msgBox, WM_SETFONT, (WPARAM)hFont, TRUE);
SetTimer(msgBox, 1, 1500, NULL); // 1.5秒后自动关闭
// 处理消息直到定时器触发
MSG msg;
DWORD startTime = GetTickCount();
while(GetMessage(&msg, msgBox, 0, 0)) {
if(msg.message == WM_TIMER || (GetTickCount() - startTime) > 1500) {
DestroyWindow(msgBox);
DeleteObject(hFont);
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// 新增:检查玩家是否站在某个箱子上
int GetBoxUnderPlayer() {
for(int i=1;i<=bcnt;i++){
if(bx[i].hidden) continue; // 跳过隐藏的箱子
// 箱子的边界
double boxLeft = bx[i].x;
double boxRight = bx[i].x + 100;
double boxTop = bx[i].y;
double boxBottom = bx[i].y + 100;
// 玩家的边界
double playerLeft = posx - PLAYER_RADIUS;
double playerRight = posx + PLAYER_RADIUS;
double playerTop = posy - PLAYER_RADIUS;
double playerBottom = posy + PLAYER_RADIUS;
// 检查玩家是否在箱子正上方且接触
if (playerRight > boxLeft && playerLeft < boxRight &&
__builtin_fabs(playerBottom - boxTop) <= 5) { // 接触容差5像素
return i;
}
}
return 0; // 0表示没有箱子在脚下
}
// 新增:删除指定索引的箱子
void DeleteBox(int index) {
if (index < 1 || index > bcnt) return;
// 销毁窗口
DestroyWindow(bx[index].hwnd);
// 将后面的箱子前移
for(int i = index; i < bcnt; i++) {
bx[i] = bx[i+1];
}
bcnt--;
}
// 新增:删除脚下的箱子
void DeleteBoxUnderPlayer() {
int boxIndex = GetBoxUnderPlayer();
if (boxIndex > 0) {
// 如果玩家站在这个箱子上,让他掉落
if (!jumping) {
// jumping = true; // 让玩家开始下落
}
DeleteBox(boxIndex);
// ShowBriefMessage("脚下的箱子已删除");
} else {
// ShowBriefMessage("脚下没有箱子");
}
}
// 打开保存文件对话框
string OpenSaveDialog() {
char savePath[MAX_PATH]="D:\\";
// 获取"我的文档"文件夹路径
// if (SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, 0, savePath) != S_OK) {
// strcpy(savePath, "C:\\");
// }
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Box Game Save Files (*.boxsav)\0*.boxsav\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrInitialDir = savePath;
ofn.lpstrTitle = "保存游戏";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR;
ofn.lpstrDefExt = "boxsav";
if (GetSaveFileName(&ofn) == TRUE) {
return string(szFileName);
}
return "";
}
// 打开加载文件对话框
string OpenLoadDialog() {
char savePath[MAX_PATH]="D:\\";
// 获取"我的文档"文件夹路径
// if (SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, 0, savePath) != S_OK) {
// strcpy(savePath, "C:\\");
// }
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Box Game Save Files (*.boxsav)\0*.boxsav\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrInitialDir = savePath;
ofn.lpstrTitle = "加载游戏";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
ofn.lpstrDefExt = "boxsav";
if (GetOpenFileName(&ofn) == TRUE) {
return string(szFileName);
}
return "";
}
// 保存游戏状态
void SaveGame() {
// 打开保存文件对话框
string filename = OpenSaveDialog();
if (filename.empty()) {
ShowBriefMessage("保存已取消");
return;
}
// 确保文件扩展名
if (filename.find(".boxsav") == string::npos) {
filename += ".boxsav";
}
SaveData save;
save.playerX = posx;
save.playerY = posy;
save.boxCount = bcnt;
// 保存所有箱子位置
for(int i = 1; i <= bcnt; i++) {
save.boxPositions.push_back(make_pair(bx[i].x, bx[i].y));
}
// 写入文件
ofstream outFile(filename, ios::binary);
if(outFile.is_open()) {
// 保存玩家位置
outFile.write(reinterpret_cast<char*>(&save.playerX), sizeof(save.playerX));
outFile.write(reinterpret_cast<char*>(&save.playerY), sizeof(save.playerY));
// 保存箱子数量
outFile.write(reinterpret_cast<char*>(&save.boxCount), sizeof(save.boxCount));
// 保存箱子位置
for(auto& pos : save.boxPositions) {
outFile.write(reinterpret_cast<char*>(&pos.first), sizeof(pos.first));
outFile.write(reinterpret_cast<char*>(&pos.second), sizeof(pos.second));
}
outFile.close();
// 显示保存成功消息
ShowBriefMessage("游戏已保存!");
} else {
ShowBriefMessage("保存失败!");
}
}
// 加载游戏状态
void LoadGame() {
// 打开加载文件对话框
string filename = OpenLoadDialog();
if (filename.empty()) {
ShowBriefMessage("加载已取消");
return;
}
ifstream inFile(filename, ios::binary);
if(!inFile.is_open()) {
ShowBriefMessage("无法打开存档文件!");
return;
}
SaveData save;
// 读取玩家位置
inFile.read(reinterpret_cast<char*>(&save.playerX), sizeof(save.playerX));
inFile.read(reinterpret_cast<char*>(&save.playerY), sizeof(save.playerY));
// 读取箱子数量
inFile.read(reinterpret_cast<char*>(&save.boxCount), sizeof(save.boxCount));
// 清除当前所有箱子
for(int i=1;i<=bcnt;i++){
DestroyWindow(bx[i].hwnd);
}
bcnt = 0;
// 读取箱子位置并创建箱子
for(int i = 0; i < save.boxCount; i++) {
int x, y;
inFile.read(reinterpret_cast<char*>(&x), sizeof(x));
inFile.read(reinterpret_cast<char*>(&y), sizeof(y));
save.boxPositions.push_back(make_pair(x, y));
// 创建箱子
CreateBoxAt(x, y);
}
inFile.close();
// 设置玩家位置
posx = save.playerX;
posy = save.playerY;
SetCursorPos(posx, posy);
ShowBriefMessage("游戏已加载!");
}
// 隐藏/显示所有箱子
void ToggleBoxesVisibility() {
boxesHidden = !boxesHidden;
mouseControlEnabled = !boxesHidden; // 当箱子隐藏时禁用鼠标控制
if (boxesHidden) {
// 隐藏所有箱子
for(int i=1;i<=bcnt;i++){
ShowWindow(bx[i].hwnd, SW_HIDE);
bx[i].hidden = true;
}
// ShowBriefMessage("箱子已隐藏\n鼠标控制已禁用");
} else {
// 显示所有箱子
for(int i=1;i<=bcnt;i++){
ShowWindow(bx[i].hwnd, SW_SHOW);
bx[i].hidden = false;
}
// ShowBriefMessage("箱子已显示\n鼠标控制已启用");
}
}
// 更新激光系统
void UpdateLasers() {
static DWORD lastLaserTime = GetTickCount();
static bool warningShown = false;
static DWORD warningStartTime = 0;
static DWORD laserActivationTime = 0;
DWORD currentTime = GetTickCount();
// 检查是否到了生成新激光的时间
if (currentTime - lastLaserTime > (rand() % 5000 + 10000)) { // 10-15秒
if (!warningShown) {
// 显示预警
bool horizontal = rand() % 2 == 0; // 随机选择方向
if (horizontal) {
ShowLaserWarning("横向激光即将出现!");
} else {
ShowLaserWarning("纵向激光即将出现!");
}
warningShown = true;
warningStartTime = currentTime;
}
// 等待1.5秒后激活激光
if (warningShown && currentTime - warningStartTime > 1500) {
bool horizontal = rand() % 2 == 0; // 随机选择方向
int position;
if (horizontal) {
position = rand() % maxy; // 在屏幕高度内随机选择y坐标
} else {
position = rand() % maxx; // 在屏幕宽度内随机选择x坐标
}
CreateLaser(horizontal, position);
laserActivationTime = currentTime;
lastLaserTime = currentTime; // 重置计时器
warningShown = false;
}
}
// 检查激光是否应该消失(持续5秒)
for(size_t i = 0; i < lasers.size(); i++) {
if (currentTime - laserActivationTime > 5000) { // 5秒后消失
if (lasers[i].active) {
DestroyWindow(lasers[i].hwnd);
lasers[i].active = false;
}
}
}
// 清理已失效的激光
for(int i = lasers.size() - 1; i >= 0; i--) {
if (!lasers[i].active && GetTickCount() - laserActivationTime > 5500) {
lasers.erase(lasers.begin() + i);
}
}
}
int main(){
// 初始化COM(某些对话框可能需要)
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
// 初始化随机数种子
srand(time(NULL));
HWND conso=GetConsoleWindow();
ShowWindow(conso, SW_HIDE);
GetCursorPos(&p);
posx = p.x;
posy = p.y;
RECT scr;
SystemParametersInfo(SPI_GETWORKAREA, 0, &scr, 0);
maxx = scr.right;
maxy = scr.bottom;
maxx -= 10;
maxy -= 10;
// 注册箱子窗口类
RegisterBoxWindowClass();
// 处理Windows消息
MSG msg;
// 显示欢迎消息
ShowBriefMessage("Box Game 已启动!\nW:生成箱子 D:删除脚下箱子 J:发射激光\nC:清除所有 S:保存 L:加载\nH:隐藏/显示箱子 Ctrl+G:退出");
while(1){
// 处理Windows消息
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 如果鼠标控制被禁用,跳过所有游戏逻辑
if (!mouseControlEnabled) {
// 只检查H键来恢复控制
if((GetAsyncKeyState(HIDEBOX)&0x8000)&&!pressh){
ToggleBoxesVisibility();
pressh = true;
}
if(!(GetAsyncKeyState(HIDEBOX)&0x8000)){
pressh = false;
}
// 检查退出快捷键(即使在鼠标控制禁用时也能退出)
if((GetAsyncKeyState(VK_CONTROL)&0x8000)&&(GetAsyncKeyState('G')&0x8000)){
// 清理所有箱子窗口
for(int i=1;i<=bcnt;i++){
DestroyWindow(bx[i].hwnd);
}
// 清理所有激光窗口
for(size_t i=0; i<lasers.size(); i++){
DestroyWindow(lasers[i].hwnd);
}
CoUninitialize(); // 清理COM
break;
}
Sleep(10);
continue;
}
// 正常的游戏逻辑(鼠标控制启用时)
if(GetAsyncKeyState(MOVELEFT)&0x8000){
xspeed = -5;
}
if(GetAsyncKeyState(MOVERIGHT)&0x8000){
xspeed = 5;
}
if((GetAsyncKeyState(MOVEUP)&0x8000)&&(jumping==false)){
yspeed = -10;
jumping = true;
}
// 应用重力
yspeed += g;
// 计算下一帧的位置
double nextY = posy + yspeed;
// 改进的碰撞检测(自动跳过隐藏的箱子)
bool onGround = false;
for(int i=1;i<=bcnt;i++){
if(CheckCollision(posx, posy, nextY, i)){
// 调整位置到箱子顶部
posy = bx[i].y - PLAYER_RADIUS;
yspeed = 0;
jumping = false;
onGround = true;
break;
}
}
if(!onGround) {
posy = nextY;
}
// 边界检查
if(posy > maxy - PLAYER_RADIUS){
posy = maxy - PLAYER_RADIUS;
yspeed = 0;
jumping = false;
}
if(posx < PLAYER_RADIUS){
posx = PLAYER_RADIUS;
}
if(posx > maxx - PLAYER_RADIUS){
posx = maxx - PLAYER_RADIUS;
}
if(posy < PLAYER_RADIUS){
posy = PLAYER_RADIUS;
yspeed = 0;
}
// 水平移动
if(jumping){
xspeed *= pluspeed;
}
posx += xspeed;
if(!jumping) xspeed = 0;
SetCursorPos(posx, posy);
// 检测激光碰撞
if (CheckLaserCollision(posx, posy)) {
// 玩家碰到激光,可以添加相应处理,比如重置位置
// 这里简单地将玩家传送到左上角
posx = PLAYER_RADIUS;
posy = PLAYER_RADIUS;
SetCursorPos(posx, posy);
// 可以添加其他反应,如闪烁屏幕或播放音效
}
// 更新激光系统
UpdateLasers();
// 存档功能
if((GetAsyncKeyState(SAVEGAME)&0x8000)&&!presss){
SaveGame();
presss = true;
}
if(!(GetAsyncKeyState(SAVEGAME)&0x8000)){
presss = false;
}
// 读档功能
if((GetAsyncKeyState(LOADGAME)&0x8000)&&!pressl){
LoadGame();
pressl = true;
}
if(!(GetAsyncKeyState(LOADGAME)&0x8000)){
pressl = false;
}
// 隐藏/显示箱子功能
if((GetAsyncKeyState(HIDEBOX)&0x8000)&&!pressh){
ToggleBoxesVisibility();
pressh = true;
}
if(!(GetAsyncKeyState(HIDEBOX)&0x8000)){
pressh = false;
}
// 新增:删除脚下箱子功能
if((GetAsyncKeyState(DELETEBOX)&0x8000)&&!pressd){
DeleteBoxUnderPlayer();
pressd = true;
}
if(!(GetAsyncKeyState(DELETEBOX)&0x8000)){
pressd = false;
}
// 新增:手动发射激光功能
if((GetAsyncKeyState(LASERKEY)&0x8000)&&!pressj){
// 随机生成一个激光
bool horizontal = rand() % 2 == 0;
int position = horizontal ? (rand() % maxy) : (rand() % maxx);
CreateLaser(horizontal, position);
pressj = true;
}
if(!(GetAsyncKeyState(LASERKEY)&0x8000)){
pressj = false;
}
// 其他控制
if((GetAsyncKeyState(VK_CONTROL)&0x8000)&&(GetAsyncKeyState('G')&0x8000)){
// 清理所有箱子窗口
for(int i=1;i<=bcnt;i++){
DestroyWindow(bx[i].hwnd);
}
// 清理所有激光窗口
for(size_t i=0; i<lasers.size(); i++){
DestroyWindow(lasers[i].hwnd);
}
CoUninitialize(); // 清理COM
break;
}
if((GetAsyncKeyState(CREATEBOX)&0x8000)&&!pressw){
CreateBox();
pressw = true;
}
if(!(GetAsyncKeyState(CREATEBOX)&0x8000)){
pressw = false;
}
if(GetAsyncKeyState(CLEARBOX)&0x8000){
for(int i=1;i<=bcnt;i++){
DestroyWindow(bx[i].hwnd);
}
// 清理所有激光窗口
for(size_t i=0; i<lasers.size(); i++){
DestroyWindow(lasers[i].hwnd);
}
lasers.clear();
bcnt = 0;
}
Sleep(10);
}
return 0;
}