2D方块世界

这个博客展示了如何使用C++编程创建一个2D方块世界,包括不同类型的方块、物品、随机种子生成等。玩家可以通过WASD键移动,1-9键选择物品,鼠标右键放置,E键打开背包。游戏还包括物品栏、工作台和不同的物品合成表。
摘要由CSDN通过智能技术生成

#include <algorithm>
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <iomanip> 
#include <math.h> 
#include <time.h>
#pragma GCC optimize(2)
using namespace std;
const int down = 21, can_in_num = 6, up = 21, item_num = 13, four_compose_num = 7, nine_compose_num = 19, max_int = 2147483000;
int mp[300][300], tree[300], mode = 2, can_in[can_in_num] = {0, 5, 6, 7, 8, 12};
int mp_x, player_x, player_y;
double life = 20;
char t;
CONSOLE_FONT_INFO consoleCurrentFont;
int four_bag_compose[four_compose_num][7] = {
    {0, 7, -1, -1, -1, 10, 4},
    {0, -1, 7, -1, -1, 10, 4},
    {0, -1, -1, 7, -1, 10, 4},
    {0, -1, -1, -1, 7, 10, 4},
    {0, 10, 10, -1, -1, 11, 4},
    {0, -1, -1, 10, 10, 11, 4},
    {0, 10, 10, 10, 10, 12, 1}
};
int nine_bag_compose[nine_compose_num][12] = {
    {0, 7, -1, -1, -1, -1, -1, -1, -1, -1, 10, 4},
    {0, -1, 7, -1, -1, -1, -1, -1, -1, -1, 10, 4},
    {0, -1, -1, 7, -1, -1, -1, -1, -1, -1, 10, 4},
    {0, -1, -1, -1, 7, -1, -1, -1, -1, -1, 10, 4},
    {0, -1, -1, -1, -1, 7, -1, -1, -1, -1, 10, 4},
    {0, -1, -1, -1, -1, -1, 7, -1, -1, -1, 10, 4},
    {0, -1, -1, -1, -1, -1, -1, 7, -1, -1, 10, 4},
    {0, -1, -1, -1, -1, -1, -1, -1, 7, -1, 10, 4},
    {0, -1, -1, -1, -1, -1, -1, -1, -1, 7, 10, 4},
    {0, 10, 10, -1, -1, -1, -1, -1, -1, -1, 11, 4},
    {0, -1, 10, 10, -1, -1, -1, -1, -1, -1, 11, 4},
    {0, -1, -1, -1, 10, 10, -1, -1, -1, -1, 11, 4},
    {0, -1, -1, -1, -1, 10, 10, -1, -1, -1, 11, 4},
    {0, -1, -1, -1, -1, -1, -1, 10, 10, -1, 11, 4},
    {0, -1, -1, -1, -1, -1, -1, -1, 10, 10, 11, 4},
    {0, 10, 10, -1, 10, 10, -1, -1, -1, -1, 12, 1},
    {0, -1, 10, 10, -1, 10, 10, -1, -1, -1, 12, 1},
    {0, -1, -1, -1, 10, 10, -1, 10, 10, -1, 12, 1},
    {0, -1, -1, -1, -1, 10, 10, -1, 10, 10, 12, 1},
};
string mode_name[3] = {"", "1(旁观者模式)", "2(生存模式)"};
string item[item_num] = {
    "\033[48;2;0;255;255m  ",//空气 air
    "\033[48;2;188;188;188m  ",//石头 stone
    "\033[48;2;226;192;170m  ",//铁矿 iron_ore
    "\033[48;2;95;0;0m  ",//泥土 dirt 
    "\033[48;2;196;255;14m  ",//草方块 grass 
    "\033[38;2;255;0;0m\033[48;2;0;255;255m* \033[0m",//玫瑰 rose
    "\033[38;2;27;150;51m\033[48;2;0;255;255mm \033[0m",//矮草丛 short_grass
    "\033[48;2;135;135;0m  ",//原木 wood
    "\033[48;2;14;209;69m  ",//树叶 leaf 
    "\033[48;2;252;238;75m  ",//金矿 gold_ore
    "\033[48;2;192;160;64m  ",//木板 plank
    "此物品无法放置",//木棍 stick
    "\033[48;2;181;147;95m  "//工作台 crafting_table 
};
string h_item[item_num] = {
    "\033[38;2;255;225;200m\033[48;2;0;255;255m()\033[0m",//空气 air
    "\033[38;2;255;225;200m\033[48;2;188;188;188m()\033[0m",//石头 stone
    "\033[38;2;255;225;200m\033[48;2;226;192;170m()\033[0m",//铁矿 iron_ore
    "\033[38;2;255;225;200m\033[48;2;95;0;0m()\033[0m",//泥土 dirt 
    "\033[38;2;255;225;200m\033[48;2;196;255;14m()\033[0m",//草方块 grass 
    "\033[38;2;255;225;200m\033[48;2;0;255;255m()\033[0m",//玫瑰 rose
    "\033[38;2;255;225;200m\033[48;2;0;255;255m()\033[0m",//矮草丛 short_grass
    "\033[38;2;255;225;200m\033[48;2;135;135;0m()\033[0m",//原木 wood
    "\033[38;2;255;225;200m\033[48;2;14;209;69m()\033[0m",//树叶 leaf
    "\033[38;2;255;225;200m\033[48;2;252;238;75m()\033[0m",//金矿 gold_ore
    "\033[38;2;255;225;200m\033[48;2;192;160;64m()\033[0m",//木板 plank
    "此物品无法放置",//木棍 stick
    "\033[38;2;255;225;200m\033[48;2;181;147;95m()\033[0m"//工作台 crafting_table
};
string b_item[item_num] = {
    "\033[38;2;255;0;0m\033[48;2;0;255;255m∏\033[0m",//空气 air
    "\033[38;2;255;0;0m\033[48;2;188;188;188m∏\033[0m",//石头 stone
    "\033[38;2;255;0;0m\033[48;2;226;192;170m∏\033[0m",//铁矿 iron_ore
    "\033[38;2;255;0;0m\033[48;2;95;0;0m∏\033[0m",//泥土 dirt 
    "\033[38;2;255;0;0m\033[48;2;196;255;14m∏\033[0m",//草方块 grass 
    "\033[38;2;255;0;0m\033[48;2;0;255;255m∏\033[0m",//玫瑰 rose
    "\033[38;2;255;0;0m\033[48;2;0;255;255m∏\033[0m",//矮草丛 short_grass
    "\033[38;2;255;0;0m\033[48;2;135;135;0m∏\033[0m",//原木 wood
    "\033[38;2;255;0;0m\033[48;2;14;209;69m∏\033[0m",//树叶 leaf
    "\033[38;2;255;0;0m\033[48;2;252;238;75m∏\033[0m",//金矿 gold_ore
    "\033[38;2;255;0;0m\033[48;2;192;160;64m∏\033[0m",//木板 plank
    "此物品无法放置",//木棍 stick
    "\033[38;2;255;0;200m\033[48;2;181;147;95m∏\033[0m"//工作台 crafting_table
};
string item_name[item_num] = {"空气", "圆石", "铁矿", "泥土", "草方块", "玫瑰", "矮草丛", "原木", "树叶", "金矿", "木板", "木棍", "工作台"};
string item_English_name[item_num] = {"air", "cobblestone", "iron_ore", "dirt", "grass", "rose", "short_grass", "wood", "leaf", "gold_ore", "plank", "stick", "crafting_table"};
string fire[14] = {
    " 1         1  ",
    " 21   1   124 ",
    "  2   2   2 4 ",
    " 134  31  31  ",
    " 234   2  32  ",
    " 3 4  124  34 ",
    "1324  324 231 ",
    "2 14 1 34 1324",
    "3 4  2314   34",
    "  2  2 44 2 34",
    "1 34 3 4  3 14",
    "   4 3 3    4 ",
    "2 24 2  4 2 2 ",
    " 444  444  444",
}; 
long long int seed, old_seed;
const long long int long_long_int_max = 9223372036854775000;
struct player_bag
{
    int id;
    string name, English_name;
    int amount;
}bag[37], player_four_bag[5], player_nine_bag[10];
int randnum(int a, int b, int rand_seed)
{
    int n1 = max(a, b);
    int n2 = min(a, b);
    srand(rand_seed);
    return rand() % (n1 - n2 + 1) + n2;
}
void mineral(int num, int item_num, int x, int y)
{
    int generate = 2;
    if (item_num == 2)
    { 
        generate = 3;
    }
    mp[x][y] = item_num;
    int j = 1;
    while (j <= num)
    {
        int g_long = generate * 2 + 1; 
        int randd = randnum(0, g_long * g_long - 1, seed * 109 + 15028461);
        if (mp[randd / g_long + x - generate][randd % g_long + y - generate] != item_num && randd / g_long + x - generate > 0)
        {
            int wsad[4] = {
                mp[randd / g_long + x - generate - 1][randd % g_long + y - generate],
                mp[randd / g_long + x - generate + 1][randd % g_long + y - generate],
                mp[randd / g_long + x - generate][randd % g_long + y - generate - 1],
                mp[randd / g_long + x - generate][randd % g_long + y - generate + 1]
            };
            if (wsad[0] == item_num || wsad[1] == item_num || wsad[2] == item_num || wsad[3] == item_num)
            {
                j++;
                if (mp[randd / g_long + x - generate][randd % g_long + y - generate] == 0 || mp[randd / g_long + x - generate][randd % g_long + y - generate] == 1)
                {
                    mp[randd / g_long + x - generate][randd % g_long + y - generate] = item_num;
                }
            }
            else
            {
                seed++;
            }
        }
        else
        {
            seed++;
        }
    }
}
void draw_tree(int tree_h, int leaf_h, int leaf_long, int x, int y)
{
    int h = tree_h + leaf_h;
    for (int i = leaf_h; i < h; i++)
    {
        for (int j = h / -2; j <= h / 2; j++)
        {
            if (x + j > 0 && mp[x + j][y + i] != 4)
            {
                mp[x + j][y + i] = 8;    
            }
        }
    }
    for (int i = 0; i < leaf_long; i++)
    {
        mp[x][y + i] = 7;
    }
    mp[x + h / 2][y + h - 1] = 0; 
    if (x + h / -2 > 0)
    {
        mp[x + h / -2][y + h - 1] = 0;    
    }
}
void g_world()
{
    srand(seed);
    int x, y, sea_level_down;
    x = 0;
    y = 0;
    sea_level_down = down;
    for (int i = 0; i < mp_x; i++)
    {
        y = 0;
        sea_level_down = rand() % 3 - 1 + sea_level_down;
        for (int j = 0; j < sea_level_down - 3; j++)
        {
            if (randnum(1, 75, seed * 402 + 458152154) == 57)
            {
                mineral(20, 2, x, y);
            }
            else if (randnum(1, 150, seed * 453 + 7960604) == 48)
            {
                mineral(8, 9, x, y);
            }
            else
            {
                mp[x][y] = 1;
            }
            y++;
            seed++;
        }
        for (int j = 0; j < 2; j++)
        {
            mp[x][y] = 3;
            y++;
        }
        mp[x][y] = 4;
        if ((int)(mp_x / 2.0) == x)
        {
            player_x = x;
            player_y = y; 
        } 
        if (randnum(1, 4, seed) > 3)
        {
            y++; 
            if (mp[x][y] == 0)
            {
                mp[x][y] = 5;            
            }
        }
        else if (randnum(1, 4, seed) > 2)
        { 
            y++; 
            if (mp[x][y] == 0)
            {
                mp[x][y] = 6;
            }
        }
        else if (randnum(1, 10, seed) > 7)
        {
            if (tree[x - 1] == 0 && tree[x - 2] == 0 && tree[x - 3] == 0 && tree[x - 4] == 0 && tree[x - 5] == 0 && tree[x - 6] == 0 && tree[x - 7] == 0)
            {
                y++;
                if (randnum(1, 5, seed * 872 + 570151509) > 2)
                {
                    draw_tree(5, 2, 5, x, y);                
                }
                else
                {
                    draw_tree(4, 1, 3, x, y);
                }
                tree[x] = 1;
            }
        }
        x++;
        seed++;
    }
}
void ncout(string text, bool end = 1, int time = 50)
{
    for (int i = 0; i < text.length(); i++)
    {
        cout << text[i];
        Sleep(time);
    }
    if (end == 1)
    {
        cout << endl;
        Sleep(200);
    }
}
void clear()
{
    system("cls"); 
}
int check_num(int n, int m)
{
    if (n == m)
    {
        return 1;
    }
    else if (n > m)
    {
        cout << "\033[31mError:用户输入的参数过多(" << n - 1 << ">" << m - 1 << ")\033[0m" << endl;
        Sleep(1000); 
        return 0; 
    }
    else
    {
        cout << "\033[31mError:用户输入的参数过少(" << n - 1 << "<" << m - 1 << ")\033[0m" << endl;
        Sleep(1000); 
        return 0; 
    }
}
int string_num(string text, bool out = 1)
{
    int num = 0;
    for (int i = 0; i < text.size(); i++)
    {
        if (text[i] <= '9' && text[i] >= '0')
        {
            num = num + ((int)text[i] - 48) * pow(10, text.size() - i - 1);        
        }
        else
        {
            if (out == 1)
            {
                cout << "\033[31mError:参数类型错误(参数为string类型或者为负数)\033[0m" << endl;
                Sleep(1000);
            }
            return -1;
        }
    } 
    return num;
}
void bag_add(int id, int num)
{
    for (int i = 1; i <= 36; i++)
    {
        if (bag[i].id == id)
        {
            bag[i].amount = bag[i].amount + num;
            return ;
        }
        if (bag[i].id == -1)
        {
            bag[i].id = id;
            bag[i].name = item_name[id];
            bag[i].English_name = item_English_name[id];
            bag[i].amount = num;
            return ;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值