游戏引擎

· · 个人记录

渲染引擎(WinGu补充版)

#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
namespace WinGUI{   
    void Ccout(char text, int textColor, int bgColor = 0) {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hConsole, (bgColor << 4) | textColor);
        std::cout << text;
        SetConsoleTextAttribute(hConsole, 7); // 恢复默认颜色
    }

    struct Texture{
        char _text;
        int _background_color;
        int text_color;
    };

    struct Image{
        Texture _image[210][210];
        int width;
        int height;

        void init(int _w,int _h){
            width=_w;
            height=_h;
        } 

        void set_image(int _width,int _height,char _im[][210],int _color[][210],int _back_color[][210]){
            width=_width;
            height=_height;
            for(int i=1;i<=height;i++){
                for(int j=1;j<=width;j++){
                    _image[i][j]._text=_im[i][j];
                    _image[i][j].text_color=_color[i][j];
                    _image[i][j]._background_color=_back_color[i][j];
                }
            }
        } 

        void set_image(int _width,int _height,char _im[][210],int _color[][210]){
            width=_width;
            height=_height;
            for(int i=1;i<=height;i++){
                for(int j=1;j<=width;j++){
                    _image[i][j]._text=_im[i][j];
                    _image[i][j].text_color=_color[i][j];
                }
            }
        } 

        void set_image(int _width,int _height,char _im[][210]){
            width=_width;
            height=_height;
            for(int i=1;i<=height;i++){
                for(int j=1;j<=width;j++){
                    _image[i][j]._text=_im[i][j];
                }
            }
        } 

        void fill(char _text,int color,int back_color) {
            for(int i=1;i<=height;i++){
                for(int j=1;j<=width;j++){
                    _image[i][j]._text=_text;
                    _image[i][j].text_color=color;
                    _image[i][j]._background_color=back_color;
                }
            } 
        }

        void fill(char _text,int color) {
            for(int i=1;i<=height;i++){
                for(int j=1;j<=width;j++){
                    _image[i][j]._text=_text;
                    _image[i][j].text_color=color;
                }
            } 
        }

        void fill(char _text) {
            for(int i=1;i<=height;i++){
                for(int j=1;j<=width;j++){
                    _image[i][j]._text=_text;
                }
            } 
        }
    };

    struct BackGround{
        Texture _map[510][510];
        int _width;
        int _height;
        void clear(){
            for(int i=1;i<=_height;i++){
                for(int j=1;j<=_width;j++){
                    _map[i][j]._text=' ';
                    _map[i][j]._background_color=0;
                    _map[i][j].text_color=1;
                }
            }
        }

        void init(int width,int height){
            _width=width;
            _height=height;
            clear();
        }

        void print(){
            system("cls");
            for(int i=1;i<=_height;i++){
                for(int j=1;j<=_width;j++){
                    Ccout(_map[i][j]._text,_map[i][j].text_color,_map[i][j]._background_color);//4红,2绿,7蓝,1白 
                }
                cout<<'\n'; 
            }
        }

        int _add_image(Image _image,int x,int y){
            if(_image.width+x>500 or _image.height+y>500)return 'x'; 
            for(int i=1;i<=_image.height;i++){
                for(int j=1;j<=_image.width;j++){
                    _map[i+y+1][j+x+1]=_image._image[i][j];
                } 
            }
            return 1;
        }
    };

    struct Sprite_info {
        Image _texture;
        int x;
        int y;
        double d; 
        void init(int w, int h, int xx = 0, int yy = 0, double dd = 0) {
            _texture.init(w, h);
            x = xx;
            y = yy;
            d = dd;
        }
        void move_with_dir(int g) {
            x += static_cast<int>(g * sin(d));
            y += static_cast<int>(g * cos(d));
        }

        void render_sprite(BackGround &bg) {
            bg._add_image(_texture, x, y);
        }
        void setTexture(const Image &img) {
            _texture = img;
        }
        void setTexture(int w, int h, char im[][210], int color[][210], int bgColor[][210]) {
            _texture.set_image(w, h, im, color, bgColor);
        }
        void setTexture(int w, int h, char im[][210], int color[][210]) {
            _texture.set_image(w, h, im, color);
        }
        void setTexture(int w, int h, char im[][210]) {
            _texture.set_image(w, h, im);
        }
        void setPos(int nx, int ny) { x = nx; y = ny; }
        int getX() const { return x; }
        int getY() const { return y; }
        void setDirection(double rad) { d = rad; }
        double getDirection() const { return d; }
        void move(int dx, int dy) { x += dx; y += dy; }
        void rotate(double delta) { d += delta; }
        bool isColliding(const Sprite_info &other) const {
            int x1 = x, y1 = y, w1 = _texture.width, h1 = _texture.height;
            int x2 = other.x, y2 = other.y, w2 = other._texture.width, h2 = other._texture.height;
            return (x1 < x2 + w2 && x1 + w1 > x2 &&
                    y1 < y2 + h2 && y1 + h1 > y2);
        }
        void getRect(int &outX, int &outY, int &outW, int &outH) const {
            outX = x; outY = y; outW = _texture.width; outH = _texture.height;
        }
    };

    struct Sprite_Group {
        vector<Sprite_info> group;
        void push_back(Sprite_info sprite) {
            group.push_back(sprite);
        }

        void render(BackGround &bg) {
            for (size_t i = 0; i < group.size(); i++) {
                group[i].render_sprite(bg);
            }
        }
        void clear() {
            group.clear();
        }
        bool erase(int index) {
            if (index < 0 || index >= (int)group.size()) return false;
            group.erase(group.begin() + index);
            return true;
        }
        size_t size() const {
            return group.size();
        }
        Sprite_info& get(int index) {
            return group[index];
        }
        const Sprite_info& get(int index) const {
            return group[index];
        }
        void moveAll(int distance) {
            for (auto &s : group) {
                s.move_with_dir(distance);
            }
        }
        void moveAllBy(int dx, int dy) {
            for (auto &s : group) {
                s.move(dx, dy);
            }
        }
        int findCollision(const Sprite_info &target) const {
            for (size_t i = 0; i < group.size(); i++) {
                if (group[i].isColliding(target)) {
                    return (int)i;
                }
            }
            return -1;
        }
        Sprite_info& operator[](size_t idx) {
            return group[idx];
        }
        const Sprite_info& operator[](size_t idx) const {
            return group[idx];
        }
    };

}

int main() {
    /*
    环境配置

    操作系统:仅支持 Windows(需要 windows.h)。

    编译器:C++14 或更高(MinGW、MSVC 均可)。

    编译选项(以 MinGW 为例):
    g++ -std=c++14 -Wl,--stack=52428800 你的文件.cpp -o 游戏.exe
    增大栈空间是因为内部数组较大(Image 210x210,BackGround 510x510)。

    内存:建议至少 50 MB 空闲内存。

    在代码开头包含头文件并引入命名空间:
    #include <bits/stdc++.h>
    #include <windows.h>
    using namespace std;
    using namespace WinGUI; // 引入引擎所有功能

    核心概念

    2.1 像素(Texture)
    每个像素包含三个属性:

    char _text :显示的字符(如 'A', '#')。

    int text_color :字体颜色(0~15,见颜色表)。

    int _background_color:背景颜色(0~15)。

    颜色对照表(常用):
    0=黑色 1=蓝色 2=绿色 3=青色 4=红色 5=紫色 6=黄色 7=白色
    8=灰色 9=亮蓝 10=亮绿 11=亮青 12=亮红 13=亮紫 14=亮黄 15=亮白

    2.2 图像(Image)

    是一个二维像素矩阵,最大尺寸 210×210。

    坐标原点在左上角,行和列的下标从 1 开始(重要!)。

    提供填充、设置像素、从二维数组导入等功能。

    2.3 背景(BackGround)

    整个控制台窗口的画布,最大尺寸 510×510。

    可以粘贴图像到指定位置(左上角对齐)。

    提供清屏和渲染(print)方法。

    2.4 精灵(Sprite_info)

    由图像 + 坐标 (x, y) + 方向角 d(弧度)组成。

    支持独立移动、旋转、碰撞检测(矩形重叠)。

    2.5 精灵组(Sprite_Group)

    管理多个精灵的容器,支持批量渲染和移动。

    基础操作

    3.1 创建并初始化背景
    BackGround bg;
    bg.init(宽度, 高度); // 例如 bg.init(80, 25);
    bg.clear(); // 可选,清空所有像素(填充空格)

    3.2 创建图像
    Image myImage;
    myImage.init(宽度, 高度); // 例如 myImage.init(10, 10);

    // 填充整个图像(三种重载)
    myImage.fill('填充字符', 字体颜色, 背景颜色); // 全参数
    myImage.fill('*', 4); // 只指定字体颜色,背景默认黑色(0)
    myImage.fill('#'); // 只指定字符,颜色默认白色(7),背景黑色

    3.3 从二维数组导入图像(下标从1开始)
    char tex[210][210];
    int colors[210][210];
    int bgColors[210][210];
    // ... 填充数据(注意从[1][1]开始)...
    myImage.set_image(宽度, 高度, tex, colors, bgColors);

    // 也可以只传字符和字体颜色(背景色默认0)
    myImage.set_image(宽度, 高度, tex, colors);

    // 或只传字符(颜色默认白色)
    myImage.set_image(宽度, 高度, tex);

    3.4 将图像绘制到背景
    bg._add_image(myImage, x坐标, y坐标);
    // 图像左上角对齐到 (x, y)
    // 如果超出背景边界,返回 'x' 表示失败

    3.5 渲染到屏幕
    bg.print(); // 清屏并显示当前画布

    精灵与动画

    4.1 创建精灵
    Sprite_info sprite;
    sprite.init(图像宽度, 图像高度, 初始x, 初始y, 初始方向弧度);
    // 方向角 d:0 表示正下方(因为屏幕 y 向下),π/2 表示正右方。
    // 如果不提供坐标和方向,默认为 (0,0,0)。

    4.2 设置精灵的纹理(三种方式)
    // 方式1:复制另一个 Image 对象
    Image img;
    img.init(5,5);
    img.fill('@', 2);
    sprite.setTexture(img);

    // 方式2:从二维数组(下标从1开始)
    char tex[210][210];
    int colors[210][210], bgColors[210][210];
    // ... 填充数据 ...
    sprite.setTexture(宽, 高, tex, colors, bgColors);

    // 方式3:只传字符和字体颜色(背景黑色)
    sprite.setTexture(宽, 高, tex, colors);

    4.3 移动和旋转
    sprite.move(dx, dy); // 绝对偏移(像素)
    sprite.move_with_dir(步长); // 沿当前方向移动指定步长
    sprite.rotate(角度增量); // 旋转方向(弧度)
    sprite.setDirection(新角度); // 直接设置方向
    sprite.setPos(新x, 新y); // 直接设置位置

    4.4 碰撞检测(矩形重叠检测)
    if (sprite.isColliding(anotherSprite)) {
    // 发生碰撞,处理逻辑
    }
    // 基于轴对齐包围盒(AABB),适用于矩形精灵。

    4.5 获取属性
    int x = sprite.getX();
    int y = sprite.getY();
    double d = sprite.getDirection();
    int outX, outY, outW, outH;
    sprite.getRect(outX, outY, outW, outH); // 获取矩形边界

    精灵组管理

    5.1 创建组并添加精灵
    Sprite_Group group;
    group.push_back(sprite1);
    group.push_back(sprite2);
    // 也可以用索引访问:group[0] = sprite1;

    5.2 批量操作
    group.render(bg); // 将所有精灵绘制到背景
    group.moveAll(步长); // 所有精灵沿自己的方向移动相同步长
    group.moveAllBy(dx, dy); // 所有精灵整体平移 (dx, dy)

    5.3 查找和删除
    int idx = group.findCollision(target); // 查找与 target 碰撞的第一个精灵索引,返回 -1 表示无
    group.erase(index); // 删除指定索引的精灵(成功返回 true)
    group.clear(); // 清空所有精灵
    size_t cnt = group.size(); // 获取精灵数量

    5.4 访问精灵
    Sprite_info &s = group[0]; // 可修改
    const Sprite_info &s = group[0]; // 只读
*/
    return 0;
}
} 

概率引擎

#include <random>
#include <chrono>

class ProbabilityEngine {
private:
    std::mt19937 rng;

public:
    ProbabilityEngine() {
        auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
        rng = std::mt19937(static_cast<unsigned int>(seed));
    }
    ProbabilityEngine(unsigned int seed) : rng(seed) {}
    bool chance(int x, int y) {
        if (x <= 0) return false;
        if (x >= y) return true;
        if (y <= 0) return false;

        std::uniform_int_distribution<int> dist(1, y);
        return dist(rng) <= x;
    }
    bool percent(int p) {
        return chance(p, 100);
    }
    bool permille(int p) {
        return chance(p, 1000);
    }
    bool floatChance(double probability) {
        if (probability <= 0.0) return false;
        if (probability >= 1.0) return true;

        std::uniform_real_distribution<double> dist(0.0, 1.0);
        return dist(rng) < probability;
    }
    int multiChance(int x, int y, int attempts) {
        int success = 0;
        for (int i = 0; i < attempts; i++) {
            if (chance(x, y)) success++;
        }
        return success;
    }
    double getTheoreticalProbability(int x, int y) {
        if (y == 0) return 0.0;
        return (static_cast<double>(x) / y) * 100.0;
    }
};
bool probabilityCheck(int x, int y) {
    static ProbabilityEngine engine;
    return engine.chance(x, y);
}
int main(){
    cout<<probabilityCheck(50,100);
}

键盘引擎

#include <windows.h>
#include <string>
#include <iostream>

// 功能:等待任意按键释放,返回该键的字符或名称(例如 "A", "F1", "Enter")
// 注意:该函数会阻塞当前线程,直到有键释放
std::string WaitForKeyRelease() {
    bool prevState[256] = {false};

    while (true) {
        for (int key = 8; key < 256; ++key) {
            bool currState = (GetAsyncKeyState(key) & 0x8000) != 0;

            // 检测到释放事件(之前按下,现在松开)
            if (prevState[key] && !currState) {
                // 获取该键的扫描码
                UINT scanCode = MapVirtualKey(key, MAPVK_VK_TO_VSC);
                char buffer[256];
                LONG lParam = scanCode << 16;
                int len = GetKeyNameText(lParam, buffer, sizeof(buffer));
                if (len > 0) {
                    return std::string(buffer);
                } else {
                    // 回退:返回 "VK+数字" 形式,使用 std::to_string 确保跨编译器
                    return std::string("VK") + std::to_string(key);
                }
            }

            prevState[key] = currState;
        }
        Sleep(10);
    }
}
/*
使用示例
int main() {
    while(1){
        std::cout << "请按任意键,松手后我将返回键名..." << std::endl;
        std::string keyName = WaitForKeyRelease();
        std::cout << "您松开的是: " << keyName << std::endl;
    }

    return 0;
}
*/

逐字输出

void print(string a){
    int i=0;
    while(i<a){
        cout<<a[i];
        Sleep(70);
    }
    return ;
}
int main(){
  //print(需要输出的字符)
}