跳到正文

个人记录

猫和老鼠 C++ 游戏

friendk约 17 分钟
原文
文章信息与版本

文章信息

发布
2023年7月19日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 17 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;

int sleepTime = 1, input, rom, tomalive, color, ches;
int jer, tom, skl, scd, ice, score;
string romnam[9] = {"", "庭院", "客厅", "卧室", "阁楼", "楼梯间", "杂物间", "餐厅", "厨房"};
int a[8][8] = {
    {0, 1, 1, 2, 0, 0, 0, 0},
    {1, 0, 0, 0, 1, 0, 0, 0},
    {1, 0, 0, 0, 1, 0, 0, 0},
    {2, 0, 0, 0, 1, 0, 0, 0},
    {0, 1, 1, 1, 0, 2, 1, 1},
    {0, 0, 0, 0, 2, 0, 1, 0},
    {0, 0, 0, 0, 1, 1, 0, 0},
    {0, 0, 0, 0, 1, 0, 0, 0}
};

inline void slowsay(string str) {
    for (unsigned int i = 0; i < str.length(); i++) {
        cout << str[i];
        Sleep(sleepTime * 20);
    }
}
void meet(void);
void cheese(void);
void find(void);
void game(void);
//void hint(void);
void goodEnd1(void);
void goodEnd2(void);
void goodEnd3(void);
void badEnd(void);

int main() {
    color = 1;
    CONSOLE_CURSOR_INFO CursorInfo;
    GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CursorInfo);
    CursorInfo.bVisible = 0;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CursorInfo);
    srand(time(NULL));
    while (1) {
        slowsay("猫和老鼠欢迎你!\n");
        slowsay("\t1:退出游戏.\n");
        slowsay("\t2:开始游戏.\n");
        slowsay("\t3:语速设置.\n");
        while (1) {
            input = _getch() - '0';
            if (input >= 1 && input <= 3)
                break;
        }
        if (input == 1)
			exit(0);
        if (input == 2)
            game();
        if (input == 3) {
            sleepTime ^= 1;
            if (sleepTime)
                slowsay("已关闭快速模式.\n");
            else
                slowsay("已开启快速模式.\n");
        }
        _getch();
        system("cls");
    }
}

void game() {
    system("cls");
    jer = tom = 100;
    tomalive = score = scd = ice = 0;
    rom = rand() % 8 + 1;
    ches = rand() % 8 + 1;
    slowsay("请选择技能:\n\t1:大铁锤\n\t0:鸟哨\n");
    while (1) {
        input = _getch() - '0';
        if (input == 0 || input == 1)
            break;
    }
    skl = input;
    Sleep(500);
    system("cls");

    while (1) {
        system("cls");
        slowsay("当前位置: " + romnam[rom] + "\n");
        slowsay("杰瑞血量: ");
        printf("%d\n", jer);
        slowsay("汤姆血量: ");
        printf("%d\n", tom);
        slowsay("冰块数量: ");
        printf("%d\n", ice);
        slowsay("技能cd: ");
        printf("%d\n", scd);
        slowsay("得分: ");
        printf("%d\n", score);
        slowsay("奶酪位置: " + romnam[ches] + "\n");
        slowsay("你可以:\n\t0: 寻找道具\n");
        for (int i = 1; i <= 8; ++i) {
            if (a[i - 1][rom - 1] == 1) {
                printf("\t%d", i);
                slowsay(": 去" + romnam[i] + "\n");
            }
            if (a[i - 1][rom - 1] == 2) {
                printf("\t%d", i);
                slowsay(": 钻管道去" + romnam[i] + "\n");
            }
        }
        if (ches == rom)
            slowsay("\t9: 推奶酪\n");

        while (1) {
            input = _getch() - '0';
            if (input == 0)
                break;
            if (input == 9 && ches == rom)
                break;
            if (input >= 1 && input <= 8 && a[input - 1][rom - 1])
                break;
        }

        if (input == 0)
            find();
        else if (input == 9)
            cheese();
        else
            rom = input, scd = max(scd - 1, 0);

        if (jer <= 0)
            break;
        if (rand() % 10 == 0)
            meet();
        if (jer <= 0)
            break;
		if (score >= 50)
			goodEnd3();
    }
    system("cls");
}

void find() {
    system("cls");
    int random1 = rand() % 20 + 1;
    slowsay("寻找中...\n");
    Sleep(3000);

    switch (random1) {
        case 1:
            slowsay("找到奶酪!\n");
            cheese();
            break;
        case 2:
        case 3:
        case 4:
			slowsay("什么也没找到,再去找找吗?\n");
			break;
        case 5:
        case 6:
                meet();
            break;
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
        case 12:
        case 13:
            if (color)
                system("color 1");
            slowsay("找到冰块!\n");
            ice++;
            break;
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
        case 19:
        case 20:
            if (color)
                system("color a");
            slowsay("找到牛奶, 血量 + 25!\n");
            jer += 25;
            break;
    }
    _getch();
    if (color)
        system("color 0f");
    system("cls");
}

void cheese(void) {
    system("cls");
    if (color)
        system("color e6");
    slowsay("推奶酪中. . . \n");
    slowsay("________________________________________\n");
    for (int i = 0; i < 40; i++) {
        cout << '*';
        Sleep(400);
    }
    slowsay("\n推入奶酪! \n");
    system("cls");
    if (color)
        system("color 0f");
    score += 10;
    ches = rand() % 8 + 1;
    if (rand() % 10)
        meet();
    system("cls");
}

void meet(void) {
    system("cls");
    if (color) system("color fc");
    slowsay("汤姆出现!\n");
    _getch();
    while (1) {
        slowsay("汤姆血量:");
        cout << tom << endl;
        slowsay("杰瑞血量:");
        cout << jer << endl;
        slowsay("技能:");
        if (skl)
            cout << "大铁锤\n";
        else
            cout << "鸟哨\n";
        slowsay("技能冷却时间:");
        cout << scd << endl;
        slowsay("冰块数量:");
        cout << ice << endl;

        slowsay("汤姆的回合!\n");
        if (tom <= 0) {
            slowsay("汤姆死!\n");
            _getch();
            break;
        }
        int random1 = rand() % 2, random2 = rand() % 3;
        if (random1)
            slowsay("汤姆可能向左攻击!\n");
        else
            slowsay("汤姆可能向右攻击!\n");
        slowsay("你可以\n\t1:向左躲闪\n\t0:向右躲闪\n");
        while (1) {
            input = _getch() - '0';
            if (input == 1 || input == 0)
                break;
        }
        if (!random2) {
            slowsay("汤姆使用转身刀!\n");
            random1 = random1 ^ 1;
        }
        if (input == random1) {
            slowsay("杰瑞中爪刀,血量-50!\n");
            jer -= 50;
        } else
            slowsay("爪刀空!\n");
        _getch();

        slowsay("杰瑞的回合!\n");
        if (jer <= 0) {
            _getch();
            break;
        }
        slowsay("你可以:\n\t0:逃跑\n");
        if (ice)
            slowsay("\t1:扔冰块\n");
        if (scd == 0)
            slowsay("\t2:用技能\n");
        while (1) {
            input = _getch() - '0';
            if (input == 0)
                break;
            if (input == 1 && ice)
                break;
            if (input == 2 && scd == 0)
                break;
        }
        if (input == 0) {
            slowsay("逃跑了...\n");
            Sleep(1500);
            break;
        }
        if (input == 1) {
            random1 = rand() % 10;
            if (random1 < 7) {
                slowsay("冰块打中, 汤姆血量-15!\n");
                tom -= 15;
            } else
                slowsay("冰块没打中!\n");
            ice--;
        }
        if (input == 2) {
            if (skl == 1) {
                slowsay("使用大铁锤, 汤姆血量-10!\n");
                tom -= 10;
            } else {
                random1 = rand() % 2;
                if (random1) {
                    slowsay("鸟哨炸中!\n");
                    tom -= (rand() % 30 + 10);
                } else
                    slowsay("鸟哨没炸中!\n");
            }
            scd = 8;
        }

        _getch();
        system("cls");
    }
	_getch();
    if (jer <= 0) badEnd();
    if (tom <= 0 && jer >= 75) goodEnd1();
    if (tom <= 0 && jer <= 75) goodEnd2();
    system("cls");
    if (color) system("color 0f");
}

void goodEnd1(void) {
	system("color 0f");
	system("cls");
	slowsay("你打败了汤姆, 拯救了全村鼠!\n");
	slowsay("是时候休息一会了.");
	Sleep(500);
	putchar('.');
	Sleep(1000);
	putchar('.');
	Sleep(5000);
	system("color f0");
	printf("\n");
	printf("\t\t\t    _____  .    |\n");
	printf("\t\t\t    / /   --, .-+-.\n");
	printf("\t\t\t   / |=|   /- L=+=|   启动!!!\n");
	printf("\t\t\t  / /,T>  /|\\   |\n");
	_getch();
	exit(0);
}
void goodEnd2(void) {
	system("color 0f");
	system("cls");
	slowsay("你打败了汤姆, 拯救了全村鼠!\n");
	slowsay("是时候休息一会了.");
	Sleep(1000);
	putchar('.');
	Sleep(2000);
	putchar('.');
	Sleep(5000);
    slowsay("但是你用了很多体力, 不能启动原神了!");
    _getch();
	exit(0);
}
void goodEnd3(void) {
	system("color e6");
	system("cls");
	slowsay("你从汤姆手里偷走了 5 块奶酪!\n");
	slowsay("全村鼠都搬到一个新地方.\n");
	Sleep(3000);
	sleepTime = 3;
	slowsay("Gotta make a move to a town that's right for me.\n");
	Sleep(1000);
    slowsay("Town to keep me moving keep me groovin' with some energy...");
    _getch();
	exit(0);
}
void badEnd(void) {
	system("color c0");
	system("cls");
	slowsay("未通过.\n");
	slowsay("运气不好, 再试一次吧!\n");
	_getch();
	exit(0);
}
原文