#include #include <conio.h> #include <windows.h> #include #include #include

using namespace std;

#define WIDTH 20 #define HEIGHT 10

int pacmanX, pacmanY; int score; char board[HEIGHT][WIDTH]; int ghostX, ghostY; // 新增:幽灵的位置

// 初始化游戏板 void init() { score = 0; pacmanX = WIDTH / 2; pacmanY = HEIGHT / 2; ghostX = 3; // 初始化幽灵的位置 ghostY = 3;

for (int y = 0; y < HEIGHT; y++) {
    for (int x = 0; x < WIDTH; x++) {
        if (x == 0 || x == WIDTH - 1 || y == 0 || y == HEIGHT - 1) {
            board[y][x] = '#';
        } else {
            board[y][x] = '.';
        }
    }
}
board[pacmanY][pacmanX] = 'C';
board[ghostY][ghostX] = 'G';  // 显示幽灵

}

// 绘制游戏板 void draw() { system("cls"); for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { cout << board[y][x]; } cout << endl; } cout << "Score: " << score << endl; }

// 处理幽灵移动 void moveGhost(int randomMoveProbability) { // 根据传入的概率决定是否随机移动 if (rand() % 100 < randomMoveProbability) { int direction = rand() % 4; switch (direction) { case 0: // 上 if (board[ghostY - 1][ghostX] != '#') { board[ghostY][ghostX] = '.'; ghostY--; board[ghostY][ghostX] = 'G'; } break; case 1: // 下 if (board[ghostY + 1][ghostX] != '#') { board[ghostY][ghostX] = '.'; ghostY++; board[ghostY][ghostX] = 'G'; } break; case 2: // 左 if (board[ghostY][ghostX - 1] != '#') { board[ghostY][ghostX] = '.'; ghostX--; board[ghostY][ghostX] = 'G'; } break; case 3: // 右 if (board[ghostY][ghostX + 1] != '#') { board[ghostY][ghostX] = '.'; ghostX++; board[ghostY][ghostX] = 'G'; } break; } } else { int dx = pacmanX - ghostX; int dy = pacmanY - ghostY;

    if (abs(dx) > abs(dy)) {
        if (dx > 0 && board[ghostY][ghostX + 1] != '#') {
            board[ghostY][ghostX] = '.';
            ghostX++;
            board[ghostY][ghostX] = 'G';
        } else if (dx < 0 && board[ghostY][ghostX - 1] != '#') {
            board[ghostY][ghostX] = '.';
            ghostX--;
            board[ghostY][ghostX] = 'G';
        }
    } else {
        if (dy > 0 && board[ghostY + 1][ghostX] != '#') {
            board[ghostY][ghostX] = '.';
            ghostY++;
            board[ghostY][ghostX] = 'G';
        } else if (dy < 0 && board[ghostY - 1][ghostX] != '#') {
            board[ghostY][ghostX] = '.';
            ghostY--;
            board[ghostY][ghostX] = 'G';
        }
    }
}

}

// 处理输入 void input() { if (_kbhit()) { char key = _getch(); switch (key) { case 'w': if (board[pacmanY - 1][pacmanX] != '#') { board[pacmanY][pacmanX] = '.'; pacmanY--; if (board[pacmanY][pacmanX] == '.') { score++; } else if (board[pacmanY][pacmanX] == 'G') { // 处理吃幽灵的逻辑 score += 10; // 吃幽灵加10分 // 重新随机生成幽灵的位置 ghostX = rand() % (WIDTH - 2) + 1; ghostY = rand() % (HEIGHT - 2) + 1; board[ghostY][ghostX] = 'G'; } board[pacmanY][pacmanX] = 'C'; } break; case 's': if (board[pacmanY + 1][pacmanX] != '#') { board[pacmanY][pacmanX] = '.'; pacmanY++; if (board[pacmanY][pacmanX] == '.') { score++; } else if (board[pacmanY][pacmanX] == 'G') { // 处理吃幽灵的逻辑 score += 10; // 吃幽灵加10分 // 重新随机生成幽灵的位置 ghostX = rand() % (WIDTH - 2) + 1; ghostY = rand() % (HEIGHT - 2) + 1; board[ghostY][ghostX] = 'G'; } board[pacmanY][pacmanX] = 'C'; } break; case 'a': if (board[pacmanY][pacmanX - 1] != '#') { board[pacmanY][pacmanX] = '.'; pacmanX--; if (board[pacmanY][pacmanX] == '.') { score++; } else if (board[pacmanY][pacmanX] == 'G') { // 处理吃幽灵的逻辑 score += 10; // 吃幽灵加10分 // 重新随机生成幽灵的位置 ghostX = rand() % (WIDTH - 2) + 1; ghostY = rand() % (HEIGHT - 2) + 1; board[ghostY][ghostX] = 'G'; } board[pacmanY][pacmanX] = 'C'; } break; case 'd': if (board[pacmanY][pacmanX + 1] != '#') { board[pacmanY][pacmanX] = '.'; pacmanX++; if (board[pacmanY][pacmanX] == '.') { score++; } else if (board[pacmanY][pacmanX] == 'G') { // 处理吃幽灵的逻辑 score += 10; // 吃幽灵加10分 // 重新随机生成幽灵的位置 ghostX = rand() % (WIDTH - 2) + 1; ghostY = rand() % (HEIGHT - 2) + 1; board[ghostY][ghostX] = 'G'; } board[pacmanY][pacmanX] = 'C'; } break; } } }

int main() { srand(time(NULL)); // 初始化随机数种子 init(); int randomMoveProbability = 20; // 可以修改这个值来控制随机移动的概率 while (true) { draw(); input(); moveGhost(randomMoveProbability); Sleep(100); } return 0; }

0 条评论

目前还没有评论...

信息

ID
1976
时间
1000ms
内存
256MiB
难度
5
标签
递交数
64
已通过
25
上传者