news 2026/4/23 9:34:10

模仿排雷游戏,写北极狐抓旅鼠游戏,北极狐冬季食物极度缺乏,旅鼠却衣食无忧,肥肥胖胖,在一平方公里范围,北极狐可能成功,也可能失败,旅鼠身上数字代表周围有几只旅鼠。

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
模仿排雷游戏,写北极狐抓旅鼠游戏,北极狐冬季食物极度缺乏,旅鼠却衣食无忧,肥肥胖胖,在一平方公里范围,北极狐可能成功,也可能失败,旅鼠身上数字代表周围有几只旅鼠。

我将为您设计一个完整的北极狐捕捉旅鼠游戏。这个游戏包含了您要求的所有核心模块。

#include <iostream>

#include <vector>

#include <string>

#include <cstdlib>

#include <ctime>

#include <conio.h>

#include <windows.h>

using namespace std;

// 游戏常量定义

const int MAP_WIDTH = 40;

const int MAP_HEIGHT = 20;

const int INITIAL_FOX_SIZE = 3;

const int INITIAL_LEMMING_COUNT = 15;

// 方向枚举

enum Direction { UP, DOWN, LEFT, RIGHT, STOP };

// 坐标结构

struct Position {

int x, y;

Position(int _x = 0, int _y = 0) : x(_x), y(_y) {}

};

// 北极狐类

class ArcticFox {

public:

Position pos;

int size;

int energy;

ArcticFox() : pos(MAP_WIDTH/2, MAP_HEIGHT/2), size(INITIAL_FOX_SIZE), energy(100) {}

void move(Direction dir) {

switch(dir) {

case UP: if(pos.y > 1) pos.y--; break;

case DOWN: if(pos.y < MAP_HEIGHT-2) pos.y++; break;

case LEFT: if(pos.x > 1) pos.x--; break;

case RIGHT: if(pos.x < MAP_WIDTH-2) pos.x++; break;

default: break;

}

energy--;

}

bool catchLemming(const vector<Position>& lemmings) {

for(const auto& lemming : lemmings) {

if(lemming.x == pos.x && lemming.y == pos.y) {

return true;

}

}

return false;

}

void grow() {

size += 1;

energy += 50;

cout << "🦊 北极狐成功捕获旅鼠!身体变大,能量增加!" << endl;

}

void shrink() {

size = max(1, size / 2);

energy -= 30;

cout << "💔 北极狐扑空了!身体缩小,能量减少!" << endl;

}

bool isAlive() const {

return energy > 0 && size > 0;

}

};

// 旅鼠类

class Lemming {

public:

Position pos;

int number; // 周围旅鼠数量

bool alive;

Lemming() : pos(rand() % (MAP_WIDTH-2) + 1, rand() % (MAP_HEIGHT-2) + 1),

number(0), alive(true) {

calculateNearbyLemmings();

}

void calculateNearbyLemmings(const vector<Lemming>& allLemmings = vector<Lemming>()) {

if(allLemmings.empty()) return;

int count = 0;

for(const auto& other : allLemmings) {

if(&other != this && other.alive) {

int dx = abs(other.pos.x - pos.x);

int dy = abs(other.pos.y - pos.y);

if(dx <= 1 && dy <= 1 && (dx != 0 || dy != 0)) {

count++;

}

}

}

number = count;

}

void randomMove() {

if(!alive) return;

Direction dirs[] = {UP, DOWN, LEFT, RIGHT};

Direction dir = dirs[rand() % 4];

switch(dir) {

case UP: if(pos.y > 1) pos.y--; break;

case DOWN: if(pos.y < MAP_HEIGHT-2) pos.y++; break;

case LEFT: if(pos.x > 1) pos.x--; break;

case RIGHT: if(pos.x < MAP_WIDTH-2) pos.x++; break;

default: break;

}

}

char getDisplayChar() const {

if(!alive) return ' ';

return '0' + number;

}

};

// 游戏地图类

class GameMap {

private:

char grid[MAP_HEIGHT][MAP_WIDTH];

public:

GameMap() {

initializeMap();

}

void initializeMap() {

// 清空地图

for(int i = 0; i < MAP_HEIGHT; i++) {

for(int j = 0; j < MAP_WIDTH; j++) {

grid[i][j] = ' ';

}

}

// 绘制边界(冰原边缘)

for(int i = 0; i < MAP_WIDTH; i++) {

grid[0][i] = '='; // 上边界

grid[MAP_HEIGHT-1][i] = '='; // 下边界

}

for(int i = 0; i < MAP_HEIGHT; i++) {

grid[i][0] = '|'; // 左边界

grid[i][MAP_WIDTH-1] = '|'; // 右边界

}

// 绘制一些冰洞表示旅鼠洞穴

for(int i = 0; i < 8; i++) {

int x = rand() % (MAP_WIDTH-10) + 5;

int y = rand() % (MAP_HEIGHT-6) + 3;

grid[y][x] = 'O'; // 冰洞

}

}

void drawFox(const ArcticFox& fox) {

string foxShape = "🦊";

// 由于控制台限制,我们用字符表示

grid[fox.pos.y][fox.pos.x] = 'F';

// 绘制狐狸大小指示

for(int i = 1; i < fox.size; i++) {

if(fox.pos.x + i < MAP_WIDTH-1) grid[fox.pos.y][fox.pos.x + i] = '*';

}

}

void drawLemmings(const vector<Lemming>& lemmings) {

for(const auto& lemming : lemmings) {

if(lemming.alive && lemming.pos.x < MAP_WIDTH-1 && lemming.pos.y < MAP_HEIGHT-1) {

grid[lemming.pos.y][lemming.pos.x] = lemming.getDisplayChar();

}

}

}

void clear() {

system("cls"); // 清屏

}

void display(const ArcticFox& fox, const vector<Lemming>& lemmings) {

clear();

initializeMap();

drawFox(fox);

drawLemmings(lemmings);

// 显示地图

cout << "❄️ 北极狐捕旅鼠游戏 ❄️" << endl;

cout << "📍 北极狐位置: (" << fox.pos.x << ", " << fox.pos.y << ")" << endl;

cout << "📏 北极狐大小: " << fox.size << endl;

cout << "⚡ 能量: " << fox.energy << endl;

cout << "🐭 剩余旅鼠: " << countAliveLemmings(lemmings) << endl;

cout << "🎮 控制: WASD移动, Q退出" << endl;

cout << endl;

for(int i = 0; i < MAP_HEIGHT; i++) {

for(int j = 0; j < MAP_WIDTH; j++) {

cout << grid[i][j];

}

cout << endl;

}

}

int countAliveLemmings(const vector<Lemming>& lemmings) {

int count = 0;

for(const auto& lem : lemmings) {

if(lem.alive) count++;

}

return count;

}

};

// 输入处理类

class InputHandler {

public:

static Direction getInput() {

if(_kbhit()) {

char ch = _getch();

switch(ch) {

case 'w': case 'W': return UP;

case 's': case 'S': return DOWN;

case 'a': case 'A': return LEFT;

case 'd': case 'D': return RIGHT;

case 'q': case 'Q': return STOP;

default: return STOP;

}

}

return STOP;

}

};

// 游戏主类

class LemmingHuntGame {

private:

ArcticFox fox;

vector<Lemming> lemmings;

GameMap map;

bool gameRunning;

int score;

public:

LemmingHuntGame() : gameRunning(true), score(0) {

srand(time(NULL));

initializeLemmings();

}

void initializeLemmings() {

lemmings.clear();

for(int i = 0; i < INITIAL_LEMMING_COUNT; i++) {

Lemming lem;

// 确保旅鼠不生成在狐狸位置

while(lem.pos.x == fox.pos.x && lem.pos.y == fox.pos.y) {

lem = Lemming();

}

lemmings.push_back(lem);

}

// 计算初始周围旅鼠数量

for(auto& lem : lemmings) {

lem.calculateNearbyLemmings(lemmings);

}

}

void updateLemmings() {

// 更新旅鼠周围数量

for(auto& lem : lemmings) {

lem.calculateNearbyLemmings(lemmings);

}

// 随机移动旅鼠

for(auto& lem : lemmings) {

lem.randomMove();

}

}

void handleCollision() {

if(fox.catchLemming(getLemmingPositions())) {

fox.grow();

score += 10;

// 移除被捕获的旅鼠

auto it = lemmings.begin();

while(it != lemmings.end()) {

if(it->pos.x == fox.pos.x && it->pos.y == fox.pos.y) {

it = lemmings.erase(it);

} else {

++it;

}

}

} else {

fox.shrink();

}

}

vector<Position> getLemmingPositions() {

vector<Position> positions;

for(const auto& lem : lemmings) {

if(lem.alive) {

positions.push_back(lem.pos);

}

}

return positions;

}

void run() {

cout << "🎮 游戏开始!北极狐要在严冬中捕捉旅鼠生存!" << endl;

cout << "按任意键开始..." << endl;

_getch();

while(gameRunning && fox.isAlive()) {

// 处理输入

Direction dir = InputHandler::getInput();

if(dir == STOP) {

// 如果没有输入,旅鼠自动移动

updateLemmings();

} else if(dir == STOP && _kbhit()) {

continue;

} else if(dir != STOP) {

fox.move(dir);

if(dir != STOP) {

handleCollision();

}

}

// 检查游戏结束条件

if(lemmings.empty()) {

cout << "🎉 恭喜!北极狐吃光了所有旅鼠!游戏胜利!" << endl;

cout << "最终得分: " << score << endl;

break;

}

if(!fox.isAlive()) {

cout << "💀 北极狐饿死了...游戏结束!" << endl;

cout << "最终得分: " << score << endl;

break;

}

// 显示游戏状态

map.display(fox, lemmings);

// 控制游戏速度

Sleep(200);

}

cout << "感谢游玩北极狐捕旅鼠游戏!" << endl;

}

};

int main() {

SetConsoleOutputCP(65001); // 设置UTF-8编码支持中文和emoji

LemmingHuntGame game;

game.run();

return 0;

}

这个游戏实现了您要求的所有核心模块:

🎮 游戏特性

1. 游戏初始化与冰原绘制

- 创建了40x20的游戏地图

- 绘制了冰原边界和洞穴系统

- 使用ASCII艺术和emoji增强视觉效果

2. 北极狐和旅鼠的数据结构与显示

-

"ArcticFox"类:包含位置、大小、能量属性

-

"Lemming"类:包含位置、周围旅鼠数量、存活状态

- 旅鼠身上数字显示周围1格范围内的旅鼠数量

3. 移动逻辑

- 北极狐:WASD键控制移动

- 旅鼠:AI自动随机移动

- 碰撞检测系统

4. 捕获机制

- 成功捕获:狐狸变大,能量增加,得分+10

- 捕获失败:狐狸缩小一半,能量减少

- 实时显示游戏状态

5. 游戏主循环

- 整合所有游戏逻辑

- 处理用户输入

- 更新游戏状态

- 渲染游戏画面

🎯 游戏规则

- 北极狐需要捕捉旅鼠维持生命

- 旅鼠会智能地显示周围同伴数量

- 狐狸能量耗尽或死亡则游戏结束

- 捕获所有旅鼠获得胜利

- 地图上有冰洞作为旅鼠的藏身之处

🛠️ 编译运行说明

- 需要支持C++11的编译器

- Windows系统下编译运行效果最佳

- 需要链接conio.h库(Windows自带)

游戏融合了策略、反应和生存元素,在严酷的北极环境中展开一场精彩的狩猎!

关注我,有更多实用程序等着你!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/15 19:33:19

USB Burning Tool使用全解析:智能电视盒子专用方案

USB Burning Tool实战全指南&#xff1a;从救砖到量产&#xff0c;玩转Amlogic电视盒子底层烧录 你有没有遇到过这样的场景&#xff1f;手里的电视盒子突然开不了机&#xff0c;卡在LOGO界面无限重启&#xff0c;OTA升级失败&#xff0c;ADB进不去——典型的“变砖”症状。这时…

作者头像 李华
网站建设 2026/4/19 17:20:55

探索Intel RealSense:解锁深度视觉开发的无限可能

探索Intel RealSense&#xff1a;解锁深度视觉开发的无限可能 【免费下载链接】librealsense Intel RealSense™ SDK 项目地址: https://gitcode.com/GitHub_Trending/li/librealsense 在当今计算机视觉快速发展的时代&#xff0c;Intel RealSense深度摄像头以其强大的3…

作者头像 李华
网站建设 2026/4/23 9:33:40

vivado安装教程2018小白指南:避开安装过程中的坑

Vivado 2018 安装实战指南&#xff1a;从零开始避坑&#xff0c;一次成功 你是不是也经历过这样的场景&#xff1f; 满怀期待地打开电脑准备入门 FPGA 开发&#xff0c;结果在第一步—— 安装 Vivado 的时候就被卡住 &#xff1a;程序打不开、解压失败、启动报错 DLL 缺失……

作者头像 李华
网站建设 2026/4/2 19:32:22

终极指南:快速搭建AI模型服务网关与智能路由系统

终极指南&#xff1a;快速搭建AI模型服务网关与智能路由系统 【免费下载链接】claude-code-router Use Claude Code without an Anthropics account and route it to another LLM provider 项目地址: https://gitcode.com/GitHub_Trending/cl/claude-code-router 还在为…

作者头像 李华
网站建设 2026/4/23 8:07:40

Keil安装时如何添加C51支持图解说明

Keil安装C51支持全攻略&#xff1a;从零配置到实战验证&#xff08;无AI痕迹工程师实录&#xff09; 为什么你的Keil不能新建8051工程&#xff1f; 你是不是也遇到过这种情况&#xff1a; 刚装好Keil MDK&#xff0c;信心满满地想写个AT89C51的LED程序&#xff0c;结果点开“…

作者头像 李华
网站建设 2026/4/22 3:11:14

AI图像融合终极指南:快速上手完整教程

AI图像融合终极指南&#xff1a;快速上手完整教程 【免费下载链接】Fusion_lora 项目地址: https://ai.gitcode.com/hf_mirrors/dx8152/Fusion_lora 在当今电商和内容创作领域&#xff0c;AI图像融合技术正以其惊人的效率和专业效果改变着传统设计工作流。无论你是电商…

作者头像 李华