跳到正文

休闲·娱乐

当蒟蒻OIer因无法摸鱼而养鱼之1.0——未知打怪小游戏

BlachSnake约 33 分钟
原文
文章信息与版本

文章信息

发布
2023年8月1日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 33 分钟

版本

  1. v1当前

本蒟蒻想在搞OI的闲暇之余摸会小鱼。

但是后边有人盯着,摸不了鱼……

所以本蒟蒻决定养鱼(

于是便有了下面这条鱼……

Update: 修复了玩家生成在墙里的Bug。


按 WASD 移动, 按 C 攻击,按 O / P 切换至上一个/下一个武器,按 K 退出游戏。

C++
// Explorer1_3.cpp
//#include <bits/stdc++.h>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <set>
#include <windows.h>
#define Debug(s) MessageBox(nullptr, s, s, MB_OK)
using namespace std;
bool Tick()
{
    static double l = clock();
    double r = clock();
    if (r - l < 100)
        return false;
    l = r;
    return true;
}
const int B = 128;
const int N = 1080, n = 1026;
const int M = 48, m = 45, mm = 22;
const int W = 4, mx[W]{-1, 0, 1, 0}, my[W]{0, -1, 0, 1};
int PlayerHealth = 1500;
enum Way
{
    Up,
    Left,
    Down,
    Right
};
namespace Base
{
    mt19937_64 gen(chrono::system_clock::now().time_since_epoch().count());
    int Rand(int l = 0, int r = 2147483647)
    {
        uniform_int_distribution<> rd(l, r);
        return rd(gen);
    }
    void Move(int x, int y)
    {
        HANDLE hout;
        COORD coord;
        coord.X = y;
        coord.Y = x + 2;
        hout = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleCursorPosition(hout, coord);
    }
    const int FBlack = 0, FBlue = 1, FGreen = 2, FCyan = 3, FRed = 4, FPurple = 5, FYellow = 6, FWhite = 7;
    const int FLBlack = 8, FLBlue = 9, FLGreen = 10, FLCyan = 11, FLRed = 12, FLPurple = 13, FLYellow = 14, FLWhite = 15;
    const int BBlack = 0, BBlue = 16, BGreen = 32, BCyan = 48, BRed = 64, BPurple = 80, BYellow = 96, BWhite = 112;
    const int BLBlack = 128, BLBlue = 144, BLGreen = 160, BLCyan = 176, BLRed = 192, BLPurple = 208, BLYellow = 224, BLWhite = 240;
    void Color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); }
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1 : 0)
    bool Check(char ch)
    {
        if (KEY_DOWN(ch))
            return true;
        else
            return false;
    }
#undef KEY_DOWN
    void Clear()
    {
        system("cls");
    }
    void SlowDisplay(char *p, int x)
    {
        while (1)
        {
            if (*p != 0)
                printf("%c", *p++);
            else
                break;
            Sleep(x);
        }
    }
};
using namespace Base;
namespace Map
{
    int sx = 1, sy = 1;
    class GameMap
    {
    public:
        char ch[N][N];
        int col[N][N];
        // vector<vector<char>> ch;
        // vector<vector<int>> col;

    public:
        // GameMap() { ch.clear(), col.clear(); }
        // void Init(int n, int m)
        // {
            // ch.resize(n, vector<char>(m));
            // col.resize(n, vector<int>(m));
        // }
        void Set(int x, int y, char c, int o) { ch[x][y] = c, col[x][y] = o; }
        void Put(int x, int y) { Move(x, y), Color(col[x][y]), cout << ch[x][y], Color(FWhite); }
    } mp0, mp1, mp2;
    void InitMap()
    {
        // mp0.Init(N, N);
        // mp1.Init(M, M);
        // mp2.Init(M, M);
        for (int i = 0; i < n; ++i)
        {
            mp0.Set(0, i, '#', FWhite);
            mp0.Set(n - 1, i, '#', FWhite);
        }
        for (int i = 1; i < n - 1; ++i)
        {
            mp0.Set(i, 0, '#', FWhite);
            mp0.Set(i, n - 1, '#', FWhite);
            for (int j = 1; j < n - 1; ++j)
                if (Rand(0, 9) == 9)
                    mp0.Set(i, j, '#', FWhite);
                else
                    mp0.Set(i, j, '.', FLBlack);
        }
    }
    void DrawGround(int x, int y, GameMap &a)
    {
        int nx = min(max(x, mm), n - mm - 1);
        int ny = min(max(y, mm), n - mm - 1);
        for (int i = -mm; i <= mm; ++i)
            for (int j = -mm; j <= mm; ++j)
                a.Set(i + mm, j + mm, mp0.ch[nx + i][ny + j], mp0.col[nx + i][ny + j]);
        sx = nx, sy = ny;
    }
    void QPrint(GameMap &a, GameMap &b)
    {
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < m; ++j)
                if (a.ch[i][j] != b.ch[i][j] || a.col[i][j] != b.col[i][j])
                {
                    a.ch[i][j] = b.ch[i][j];
                    a.col[i][j] = b.col[i][j];
                    a.Put(i, j);
                }
    }
}
using namespace Map;
namespace Item
{
    const double Alpha = 3.0, Beta = 1.5;
    class Weapon
    {
    public:
        string Name;
        int Cold, Damage, Range;
        int FreezingTime;
        bool AOE;
        int Color;
        int Critical;
        int HitCount, HitMod;

    public:
        Weapon() {}
        Weapon(string NAME, int COLD, int DAMAGE, int RANGE, bool _AOE, int COLOR, int CRITICAL, int HITMOD) : Name(NAME), FreezingTime(COLD), Cold(COLD), Damage(DAMAGE), Range(RANGE), AOE(_AOE), Color(COLOR), Critical(CRITICAL), HitMod(HITMOD) {}
        bool Freezing() { return FreezingTime > 0; }
        void Freeze() { FreezingTime = Cold; }
        int GetDamage()
        {
            if (Freezing())
                return 0;
            int d = Damage;
            if (Rand(0, 99) < Critical)
                d *= Alpha;
            if (HitMod != 0 && ++HitCount % HitMod == 0)
                d *= Beta;
            return d;
        }
    };
    const Weapon __Claw("Claw", 6, 70, 2, false, FLBlack | BBlack, 0, 0);
    const Weapon __IronSowrd("Iron Sowrd", 8, 85, 3, true, FLBlack | BBlack, 0, 0);
    const Weapon __Axe("Axe", 8, 75, 4, true, FLBlack | BBlack, 0, 0);
    const Weapon Axe("Axe", 8, 18, 4, true, FLWhite | BWhite, 25, 3);
    const Weapon ClayMore("ClayMore", 18, 20, 5, true, FLPurple | BPurple, 10, 4);
    const Weapon Cutlass("Cutlass", 5, 10, 5, false, FLRed | BRed, 0, 0);
    const Weapon Daggers("Daggers", 2, 8, 3, true, FLCyan | BCyan, 25, 0);
    const Weapon DoubleAxe("Double Axe", 8, 18, 7, false, FLYellow | BYellow, 50, 0);
    const Weapon Gauntlets("Gauntlets", 2, 6, 2, true, FLBlue | BBlue, 25, 4);
    const Weapon FightersBlindings("Fighter`s Blindings", 1, 6, 1, true, FLCyan | BCyan, 25, 0);
    const Weapon Glaive("Glaive", 16, 18, 7, true, FLGreen | BGreen, 25, 0);
    const Weapon Pickaxe("Pickaxe", 16, 25, 3, false, FLWhite | BWhite, 0, 0);
    const Weapon DiamindSword("Diamind Sword", 5, 18, 4, true, FLCyan | BCyan, 0, 3);
}
using namespace Item;
namespace Entity
{
    class Mob
    {
    public:
        int x, y, MoveTick;
        int Health;
        bool GHurt;
        Weapon MyWeapon;

    public:
        Mob() {}
    };
    vector<Weapon> Weapons;
    int NowWP = 0;
    Mob Player;
    struct Point
    {
        int x, y;
        Point() {}
        Point(int X, int Y) : x(X), y(Y) {}
        bool operator==(Point b) const { return x == b.x && y == b.y; }
        bool operator<(Point b) const { return x == b.x ? y < b.y : x < b.x; }
    };
    set<Point> mbv;
    void InitWeapons()
    {
        Weapons.push_back(Axe);
        Weapons.push_back(ClayMore);
        Weapons.push_back(Cutlass);
        Weapons.push_back(Daggers);
        Weapons.push_back(DoubleAxe);
        Weapons.push_back(Gauntlets);
        Weapons.push_back(FightersBlindings);
        Weapons.push_back(Glaive);
        Weapons.push_back(Pickaxe);
        Weapons.push_back(DiamindSword);
    }
    bool Address(int x, int y, GameMap &a) { return a.ch[x][y] == '.' && mbv.find(Point(x, y)) == mbv.end(); }
    void InitMob(Mob &mb, GameMap &a, int h, Weapon w)
    {
        do
        {
            mb.x = Rand(1, n - 2);
            mb.y = Rand(1, n - 2);
        } while (Address(mb.x, mb.y, a));
        mb.Health = h;
        mb.GHurt = false;
        mb.MyWeapon = w;
        mb.MoveTick = 0;
        mbv.insert(Point(mb.x, mb.y));
    }
    class MobGroup
    {
    public:
        vector<Mob> Mobs;
        Weapon BasicWeapon;
        char Charater;
        int BasicHealth;

    public:
        MobGroup() { Mobs.clear(); }
        MobGroup(int n, Weapon wp, int h, char c)
        {
            Mobs.clear();
            Mobs.resize(n, Mob());
            BasicWeapon = wp;
            BasicHealth = h;
            for (Mob &e : Mobs)
                InitMob(e, mp0, BasicHealth, BasicWeapon);
            Charater = c;
        }
    };
    MobGroup Zombie(256, __Claw, 50, 'Z');
    MobGroup ExZombie(64, __IronSowrd, 210, 'X');
    MobGroup Vindicator(16, __Axe, 110, 'V');
    vector<MobGroup> Enemy;
    void InitEnemy()
    {
        Enemy.push_back(Zombie);
        Enemy.push_back(ExZombie);
        Enemy.push_back(Vindicator);
    }
    void ClearGHurt()
    {
        Player.GHurt = false;
        for (MobGroup &g : Enemy)
            for (Mob &e : g.Mobs)
                e.GHurt = false;
    }
    void ChangeWeapon()
    {
        bool f = false;
        if (Check('O'))
        {
            if (NowWP-- == 0)
                NowWP = int(Weapons.size()) - 1;
            f = true;
        }
        if (Check('P'))
        {
            if (++NowWP == (int)Weapons.size())
                NowWP = 0;
            f = true;
        }
        if (f)
            Player.MyWeapon = Weapons[NowWP];
    }
    void Colding(Mob &mb)
    {
        if (mb.MyWeapon.Freezing())
            --mb.MyWeapon.FreezingTime;
    }
    int TryMove(Mob &mb, int w, GameMap &a)
    {
        if (!Address(mb.x + mx[w], mb.y + my[w], a))
            return false;
        mbv.erase(Point(mb.x, mb.y));
        mb.x += mx[w], mb.y += my[w];
        mbv.insert(Point(mb.x, mb.y));
        return true;
    }
    void PMove(Mob &mb, GameMap &a)
    {
        if (Check('W'))
            TryMove(mb, Up, a);
        if (Check('A'))
            TryMove(mb, Left, a);
        if (Check('S'))
            TryMove(mb, Down, a);
        if (Check('D'))
            TryMove(mb, Right, a);
    }
    void AMove(Mob &mb, int tx, int ty, GameMap &a)
    {
        if (abs(mb.x - tx) + abs(mb.y - ty) <= 1)
            return;
        if (mb.MoveTick > 0)
        {
            if (TryMove(mb, Rand(0, 3), a))
                --mb.MoveTick;
            return;
        }
        int w;
        if (abs(mb.x - tx) >= abs(mb.y - ty))
            w = mb.x > tx ? Up : Down;
        else
            w = mb.y > ty ? Left : Right;
        if (!TryMove(mb, w, a))
        {
            TryMove(mb, Rand(0, 3), a);
            mb.MoveTick = 5;
        }
    }
    void DrawMob(Mob mb, char c, GameMap &a, int sx, int sy, int col)
    {
        int nx = mb.x - sx + mm, ny = mb.y - sy + mm;
        if (0 <= nx && nx < m && 0 <= ny && ny < m)
        {
            a.ch[nx][ny] = c;
            if (mb.GHurt)
                a.col[nx][ny] = FLRed | BRed;
            else
                a.col[nx][ny] = col;
        }
    }
    void DrawCircle(Mob mb, int r, GameMap &a, int sx, int sy)
    {
        int nx = mb.x - sx + mm, ny = mb.y - sy + mm;
        for (int i = -r; i <= r; ++i)
        {
            if (0 > nx + i || nx + i >= m)
                continue;
            for (int j = -r; j <= r; ++j)
            {
                if (0 > ny + j || ny + j >= m)
                    continue;
                if (int(round(sqrt(i * i + j * j))) == r)
                {
                    a.ch[nx + i][ny + j] = '+';
                    a.col[nx + i][ny + j] = Player.MyWeapon.Color;
                }
            }
        }
    }
    pair<bool, bool> Hit(Mob &mba, Mob &mbb)
    {
        bool f = false;
        if (!mba.MyWeapon.Freezing())
        {
            if (int(round(hypot(abs(mba.x - mbb.x), abs(mba.y - mbb.y)))) <= mba.MyWeapon.Range)
            {
                mbb.Health -= mba.MyWeapon.GetDamage();
                mbb.GHurt = true;
                f = true;
            }
        }
        return make_pair(f, mbb.Health <= 0);
    }
    void RespawnEnemy(Mob &e, Weapon wp, int h)
    {
        mbv.erase(Point(e.x, e.y));
        InitMob(e, mp0, h, wp);
    }
    void PlayerAttack(int &sc)
    {
        if (Check('C'))
        {
            if (Player.MyWeapon.AOE)
            {
                pair<bool, bool> p;
                bool f = false;
                for (MobGroup &g : Enemy)
                    for (Mob &e : g.Mobs)
                    {
                        p = Hit(Player, e);
                        f |= p.first;
                        if (p.second)
                        {
                            ++sc;
                            RespawnEnemy(e, g.BasicWeapon, g.BasicHealth);
                        }
                    }
                if (f)
                    Player.MyWeapon.Freeze();
            }
            else
            {
                pair<bool, bool> p;
                bool f = false;
                for (MobGroup &g : Enemy)
                    for (Mob &e : g.Mobs)
                    {
                        if (f == true)
                            break;
                        p = Hit(Player, e);
                        if (p.second)
                        {
                            ++sc;
                            RespawnEnemy(e, g.BasicWeapon, g.BasicHealth);
                        }
                        if (p.first)
                        {
                            Player.MyWeapon.Freeze();
                            f = true;
                        }
                    }
            }
        }
    }
    void EnemyAttack(MobGroup &mbg)
    {
        for (Mob &e : mbg.Mobs)
            Colding(e);
        for (Mob &e : mbg.Mobs)
            if (Hit(e, Player).first)
                e.MyWeapon.Freeze();
    }
    void PlayerMove() { PMove(Player, mp0); }
    void EnemyMove(MobGroup &mbg)
    {
        for (Mob &e : mbg.Mobs)
            AMove(e, Player.x, Player.y, mp0);
    }
    void GameOver(int sc)
    {
        Clear();
        system("mode con lines=10 cols=20");
        Move(-1, 0);
        Color(FRed);
        cout << "Game Over" << '\n';
        Color(FCyan);
        cout << "Score: " << sc << '\n';
        Color(FWhite);
        system("pause");
    }
}
using namespace Entity;
namespace Data
{
    char bf0[B], bf1[B], bf2[B];
    void FPS(int L, char *pbf, char *nbf)
    {
        for (int i = 0, f = 1; i < B; ++i)
        {
            Move(L, i);
            if (nbf[i] == '\0')
                f = 0;
            if (f == 1)
            {
                if (pbf[i] != nbf[i])
                {
                    if (nbf[i] == '\0')
                        cout << ' ';
                    else
                        cout << nbf[i];
                    pbf[i] = nbf[i];
                }
            }
            else
            {
                if (pbf[i] != '\0')
                {
                    cout << ' ';
                    pbf[i] = '\0';
                }
            }
        }
    }
    void PData(int t, int h, int x, int y, int sc, Weapon wp)
    {
        sprintf(bf2, "Time: %d.%ds | Health: %d | Address: (%d, %d) | Score: %d",
                t / 10, t % 10, h, x, y, sc);
        FPS(-2, bf0, bf2);
        sprintf(bf2, "Weapon: %s | Damage: %d | CD: %d.%ds | Range: %d | CRIT: %d%% | AOE: %c | Freezing: %c",
                wp.Name.c_str(), wp.Damage, wp.Cold / 10, wp.Cold % 10, wp.Range, wp.Critical, wp.AOE ? 'Y' : 'N', wp.Freezing() ? 'Y' : 'N');
        FPS(-1, bf1, bf2);
    }
}
using namespace Data;
namespace Gaming
{
    bool DivTick(int t, int d) { return t % d == 0; }
    void Print(int t, int sc)
    {
        DrawGround(Player.x, Player.y, mp2);
        DrawMob(Player, '@', mp2, sx, sy, FLCyan | BCyan);
        DrawCircle(Player, Player.MyWeapon.Range, mp2, sx, sy);
        for (MobGroup &g : Enemy)
            for (Mob &e : g.Mobs)
                DrawMob(e, g.Charater, mp2, sx, sy, FLGreen | BGreen);
        QPrint(mp1, mp2);
        PData(t, Player.Health, Player.x, Player.y, sc, Player.MyWeapon);
    }
    void Fighting()
    {
        system("mode con lines=50 cols=100");
        Clear();
        InitMap();
        InitWeapons();
        InitMob(Player, mp0, PlayerHealth, Axe);
        InitEnemy();
        int t = 0, sc = 0;
        while (!Check('K'))
            if (Tick())
            {
                ++t;
                ClearGHurt();
                if (Player.Health + 3ll <= PlayerHealth)
                    Player.Health += 3;
                PlayerAttack(sc);
                if (DivTick(t, 2))
                    for (MobGroup &g : Enemy)
                        EnemyAttack(g);
                if (Player.Health <= 0)
                    break;
                Colding(Player);
                ChangeWeapon();
                if (DivTick(t, 2))
                    PlayerMove();
                if (DivTick(t, 4))
                    for (MobGroup &g : Enemy)
                        EnemyMove(g);
                Print(t, sc);
            }
        GameOver(sc);
    }
}
using namespace Gaming;
int main()
{
    Fighting();
    return 0;
}
原文