C++実現テラリアを考えてもいいですか?


V1.0
#include
#include
#include"game.h"
#include
#include
#include
using namespace std;
const int WIN_SIZE = 24;
SYSTEMTIME sys;

/*                                                      -------------WELCOME--------------                                                          */

void project() {
    SetConsoleTitle("Floatiy World");
    system("mode con cols=49 lines=31");
    srand(time(NULL));
}
void welcome_print() {
    cout<<"                                                "<cout<<"  □      □              □                □  "<cout<<"                                  □            "<cout<<"          M A D E   B Y   F L O A T I Y   □    "<cout<<"    □                          □              "<//5
    cout<<"                                                "<cout<<"    □□□□□          □□□          □□□  "<cout<<"    □                    □              □    "<cout<<"    □                    □              □    "<cout<<"    □                      □    □    □      "<//10
    cout<<"    □□□□      □□      □    □    □      "<cout<<"    □            □□      □    □    □      "<cout<<"    □                      □  □  □  □      "<cout<<"    □                      □□      □□      "<cout<<"    □                      □          □      "<//15
    cout<<"                                                "<cout<<"                                                "<cout<<"            □                □                "<cout<<"                                    □          "<cout<<"      □          LODING......                  "<//20
    cout<<"□                                          □  "<cout<<"                                      □        "<cout<<"□□□□□□□□□□□□□□□□□□□□□□□□"<cout<<"□□□□□□□□□□□□□□□□□□□□□□□□"<//24
    cout<<"================================================"<//25
    cout<<"=====   If you have any questions about   ======"<cout<<"=====   this game, please send a message  ======"<cout<<"=====   to me at blog.csdn.net/floatiy    ======"<cout<<"=====        HOPE  YOU  HAVE  FUN!        ======"<cout<<"================================================"<//30
    Sleep(1500);
    return;
}

/*                                                      -------------INIT--------------                                                         */

string world[100][100];
struct Player {
    int hp;
    int hungry;
    int bag[100];
    int x,y;
    int jump;
    string hand;
} player;
map<string,int> item;
map<int,string> id;

void map_create();
void player_create();
void items_create();
void tree_create();
void init() {
    map_create();
    items_create();
    player_create();
}
void map_create() {
    int last = WIN_SIZE - 4;
    for(int i = 1; i <= WIN_SIZE; i++) {
        int tmp = last + rand()%3 - 1;
        last = tmp;
        int j;
        for(j = WIN_SIZE; j >= tmp; j--) world[j][i] = "stone";
        j++;
        world[j-1][i] = world[j-2][i] = "dirt";
        world[j-3][i] = "gress";
        world[WIN_SIZE][i] = "bedstone";
    }
    tree_create();
}
int jd(int x) {
    if(x) return x;
    return -x+1;
}
void tree_create() {
    for(int iu = 1; iu <= 3; iu++) {
        int opt = rand() % WIN_SIZE;
        int high = 5 + rand()%3 - 1;
        int i;
        for(i = 1; i <= WIN_SIZE; i++) {
            if(world[i][opt] == "gress") break;
        }
        int j;
        for(j = 1; j <= high && (i - j - 1); j++) {
            world[i-j][opt] = "treewood";
        }
        for(int a = -2; a <= 2; a++) {
            for(int b = -2; b <= 2; b++) {
                if(rand()%jd(a+b) <= 2) {
                    if((a==-2||a==2)&&(b==-2||b==2)) continue;
                    if(j + a <= 0 || j + a >= WIN_SIZE+1 || opt + b <= 0 || opt + b >= WIN_SIZE+1) continue;
                    world[i-j + a][opt + b] = "leaf";
                }
            }
        }
    }
}
void items_create() {
    item["nothing"] = 0;
    item["pickaxe"] = 1;
    item["axe"] = 2;
    item["sword"] = 3;
    item["bow"] = 4;
    item["apple"] = 5;
    item["meat"] = 6;
    item["arrow"] = 7;
    item["clip"] = 8;
    item["stone"] = 9;
    item["dirt"] = 10;
    item["seed"] = 11;
    item["wood"] = 12;
    item["tree_seed"] = 13;
    item["boom"] = 14;

    id[0] = "nothing";
    id[1] = "pickaxe";
    id[2] = "axe";
    id[3] = "sword";
    id[4] = "bow";
    id[5] = "apple";
    id[6] = "meat";
    id[7] = "arrow";
    id[8] = "clip";
    id[9] = "stone";
    id[10] = "dirt";
    id[11] = "seed";
    id[12] = "wood";
    id[13] = "tree_seed";
    id[14] = "boom";
}
void player_create() {
    player.hp = 100;
    player.hungry = 100;
    for(int i = 1; i <= WIN_SIZE; i++) {
        if(world[i][12] == "gress") {
            player.x = i-1;
            player.y = 12;
            break;
        }
    }
    player.hand = "nothing";
    player.bag[0] = 1;
}
/*                                                      -------------PRINT--------------                                                            */
void print_stone(int x,int y);
void print_dirt(int x,int y);
void print_gress(int x,int y);
void print_treewood(int x,int y);
void print_leaf(int x,int y);
void print_bedstone(int x,int y);
void print_seting();
void world_print() {
    system("cls");
    for(int i = 1; i <= WIN_SIZE; i++) {
        for(int j = 1; j <= WIN_SIZE; j++) {
            if(world[i][j] == "stone") print_stone(i,j);
            else if(world[i][j] == "dirt") print_dirt(i,j);
            else if(world[i][j] == "gress") print_gress(i,j);
            else if(world[i][j] == "treewood") print_treewood(i,j);
            else if(world[i][j] == "leaf") print_leaf(i,j);
            else if(world[i][j] == "bedstone") print_bedstone(i,j);
        }
    }
    print_seting();
}
void print_seting() {
    Locate(25,1);
    printf("================================================
"
); if(player.hand == "nothing") printf("=== Item in your hand: nothing ===
"
); if(player.hand == "pickaxe") printf("=== Item in your hand: pickaxe ===
"
); if(player.hand == "axe") printf("=== Item in your hand: axe ===
"
); if(player.hand == "sword") printf("=== Item in your hand: sword ===
"
); if(player.hand == "bow") printf("=== Item in your hand: bow ===
"
); if(player.hand == "apple") printf("=== Item in your hand: apple ===
"
); if(player.hand == "meat") printf("=== Item in your hand: meat ===
"
); if(player.hand == "arrow") printf("=== Item in your hand: arrow ===
"
); if(player.hand == "clip") printf("=== Item in your hand: clip ===
"
); if(player.hand == "stone") printf("=== Item in your hand: stone ===
"
); if(player.hand == "dirt") printf("=== Item in your hand: dirt ===
"
); if(player.hand == "seed") printf("=== Item in your hand: seed ===
"
); if(player.hand == "wood") printf("=== Item in your hand: wood ===
"
); if(player.hand == "tree_seed") printf("=== Item in your hand: tree_seed ===
"
); if(player.hand == "boom") printf("=== Item in your hand: boom ===
"
); Locate(27,1); printf("=== Bag: ===
"
); printf("=== ===
"
); printf("=== ===
"
); if(player.bag[item["pickaxe"]]) Locate(28,7/2),printf("pickaxe"); if(player.bag[item["axe"]]) Locate(28,17/2),printf("axe"); if(player.bag[item["sword"]]) Locate(28,23/2),printf("sword"); // if(player.bag[item[4]]) Locate(28,7),printf("apple"); if(player.bag[item["apple"]]) Locate(28,31/2),printf("apple"); // if(player.bag[item[6]]) Locate(28,7),printf("pickaxe"); // if(player.bag[item[7]]) Locate(28,7),printf("pickaxe"); // if(player.bag[item[8]]) Locate(28,7),printf("pickaxe"); if(player.bag[item["stone"]]) Locate(28,7/2),printf("stone"); if(player.bag[item["dirt"]]) Locate(28,15/2),printf("dirt"); if(player.bag[item["wood"]]) Locate(28,22/2),printf("wood"); /* item["nothing"] = 0; item["pickaxe"] = 1; item["axe"] = 2; item["sword"] = 3; item["bow"] = 4; item["apple"] = 5; item["meat"] = 6; item["arrow"] = 7; item["clip"] = 8; item["stone"] = 9; item["dirt"] = 10; item["seed"] = 11; item["wood"] = 12; item["tree_seed"] = 13; item["boom"] = 14; */ // printf("=== pickaxe, axe, sword, apple, meat ===
");
// printf("=== stone, dirt, wood, seed, treeseed ===
");
printf("================================================
"
); } void print_stone(int x,int y) { Setcolor(GRAY); Locate(x,y); printf("■"); Setcolor(GRAY); } void print_dirt(int x,int y) { Setcolor(DARKYELLOW); Locate(x,y); printf("■"); Setcolor(GRAY); } void print_gress(int x,int y) { Setcolor(DARKGREEN); Locate(x,y); printf("■"); Setcolor(GRAY); } void print_treewood(int x,int y) { Setcolor(DARKGREEN); Locate(x,y); printf("■"); Setcolor(GRAY); } void print_leaf(int x,int y) { Setcolor(GREEN); Locate(x,y); printf("■"); Setcolor(GRAY); } void print_bedstone(int x,int y) { Setcolor(DARKGRAY); Locate(x,y); printf("■"); Setcolor(GRAY); } void print_wood(int x,int y) { Setcolor(DARKRED); Locate(x,y); printf("■"); Setcolor(GRAY); } void print_player(int x,int y) { // cout< Setcolor(TEAL); Locate(x,y); printf("♀"); Setcolor(GRAY); } void clean_player(int x,int y) { if(world[x][y] == "treewood") { print_treewood(x,y); return; } else if(world[x][y] == "leaf") { print_leaf(x,y); return; } Locate(x,y); printf(" "); } void clean_block(int x,int y) { Locate(x,y); printf(" "); } /* -------------Playing-------------- */ bool judge(int x,int y) { if(world[x][y] == "stone") return false; else if(world[x][y] == "dirt") return false; else if(world[x][y] == "gress") return false; else if(world[x][y] == "wood") return false; else if(world[x][y] == "bedstone") return false; if(x <= 0 || x >= WIN_SIZE+1 || y <= 0 || y >= WIN_SIZE+1) return false; return true; } void go_left() { if(judge(player.x,player.y-1)) { clean_player(player.x,player.y); player.y--; print_player(player.x,player.y); } } void go_right() { if(judge(player.x,player.y+1)) { clean_player(player.x,player.y); player.y++; print_player(player.x,player.y); } } void jump() { if(!player.jump&&!judge(player.x+1,player.y)) player.jump = 3; } judge_hit(int x,int y) { if(world[x][y] == "stone") return true; else if(world[x][y] == "dirt") return true; else if(world[x][y] == "gress") return true; else if(world[x][y] == "treewood") { // cout< return true; } else if(world[x][y] == "leaf") return true; else if(world[x][y] == "wood") return true; else if(world[x][y] == "bedstone") return false; if(x <= 0 || x >= WIN_SIZE+1 || y <= 0 || y >= WIN_SIZE+1) return false; return false; } void hit_left() { if(judge_hit(player.x,player.y-1)) { if(world[player.x][player.y-1] == "stone") Sleep(700); if(world[player.x][player.y-1] == "treewood") Sleep(500); else Sleep(300); clean_block(player.x,player.y-1); if(world[player.x][player.y-1]=="treewood") world[player.x][player.y-1]="wood"; player.bag[item[ world[player.x][player.y-1] ]]++; world[player.x][player.y-1] = " "; } } void hit_right() { if(judge_hit(player.x,player.y+1)) { if(world[player.x][player.y+1] == "stone") Sleep(700); if(world[player.x][player.y+1] == "treewood") Sleep(500); else Sleep(300); clean_block(player.x,player.y+1); if(world[player.x][player.y+1]=="treewood") world[player.x][player.y+1]="wood"; player.bag[item[ world[player.x][player.y+1] ]]++; world[player.x][player.y+1] = " "; } } void hit_down() { if(judge_hit(player.x+1,player.y)) { if(world[player.x+1][player.y] == "stone") Sleep(700); if(world[player.x+1][player.y] == "treewood") Sleep(500); else Sleep(300); clean_block(player.x+1,player.y); if(world[player.x+1][player.y]=="treewood") world[player.x+1][player.y] = "wood"; player.bag[item[ world[player.x+1][player.y] ]]++; world[player.x+1][player.y] = " "; } } bool judge_put(int x,int y) { if(world[x][y] == "stone") return false; else if(world[x][y] == "dirt") return false; else if(world[x][y] == "gress") return false; else if(world[x][y] == "wood") return false; // else if(world[x][y] == "leaf") return false; else if(world[x][y] == "treewood") return false; else if(world[x][y] == "bedstone") return false; if(!player.bag[item[player.hand]]) return false; if(player.hand != "stone"&&player.hand != "dirt"&&player.hand != "wood") return false; if(x <= 0 || x >= WIN_SIZE+1 || y <= 0 || y >= WIN_SIZE+1) return false; if(judge(x+1,y) && judge(x,y-1) && judge(x,y+1)) return false; return true; } void put_left() { if(judge_put(player.x,player.y-1)) { player.bag[item[player.hand]]--; if(player.hand == "stone") print_stone(player.x,player.y-1),world[player.x][player.y-1] = "stone"; else if(player.hand == "dirt") print_dirt(player.x,player.y-1),world[player.x][player.y-1] = "dirt"; else if(player.hand == "wood") print_wood(player.x,player.y-1),world[player.x][player.y-1] = "wood"; } } void put_right() { if(judge_put(player.x,player.y+1)) { player.bag[item[player.hand]]--; if(player.hand == "stone") print_stone(player.x,player.y+1),world[player.x][player.y+1] = "stone"; else if(player.hand == "dirt") print_dirt(player.x,player.y+1),world[player.x][player.y+1] = "dirt"; else if(player.hand == "wood") print_wood(player.x,player.y+1),world[player.x][player.y+1] = "wood"; } } void put_down() { if(judge_put(player.x+1,player.y)) { player.bag[item[player.hand]]--; if(player.hand == "stone") print_stone(player.x+1,player.y),world[player.x+1][player.y] = "stone"; else if(player.hand == "dirt") print_dirt(player.x+1,player.y),world[player.x+1][player.y] = "dirt"; else if(player.hand == "wood") print_wood(player.x+1,player.y),world[player.x+1][player.y] = "wood"; } } void takeout_item_1() { for(int i = 1; i <= 50; i++) { if(player.bag[i] && id[i] != player.hand) { player.hand = id[i]; break; } if(i == 50) i = -1; } } void takeout_item_2() { for(int i = 50; i > -1; i--) { if(player.bag[i] && id[i] != player.hand) { player.hand = id[i]; break; } if(i == 0) i = 50; } } void update_bag() { // if(player.bag[item["wood"]]) printf("88888"); if(!player.bag[item[player.hand]]) player.hand = "nothing"; Locate(25,1); printf("================================================
"
); if(player.hand == "nothing") printf("=== Item in your hand: nothing ===
"
); if(player.hand == "pickaxe") printf("=== Item in your hand: pickaxe ===
"
); if(player.hand == "axe") printf("=== Item in your hand: axe ===
"
); if(player.hand == "sword") printf("=== Item in your hand: sword ===
"
); if(player.hand == "bow") printf("=== Item in your hand: bow ===
"
); if(player.hand == "apple") printf("=== Item in your hand: apple ===
"
); if(player.hand == "meat") printf("=== Item in your hand: meat ===
"
); if(player.hand == "arrow") printf("=== Item in your hand: arrow ===
"
); if(player.hand == "clip") printf("=== Item in your hand: clip ===
"
); if(player.hand == "stone") printf("=== Item in your hand: stone ===
"
); if(player.hand == "dirt") printf("=== Item in your hand: dirt ===
"
); if(player.hand == "seed") printf("=== Item in your hand: seed ===
"
); if(player.hand == "wood") printf("=== Item in your hand: wood ===
"
); if(player.hand == "tree_seed") printf("=== Item in your hand: tree_seed ===
"
); if(player.hand == "boom") printf("=== Item in your hand: boom ===
"
); Locate(27,1); printf("=== Bag: ===
"
); printf("=== ===
"
); printf("=== ===
"
); if(player.bag[item["pickaxe"]]) Locate(28,7/2),printf("pickaxe"); if(player.bag[item["axe"]]) Locate(28,17/2),printf("axe"); if(player.bag[item["sword"]]) Locate(28,23/2),printf("sword"); // if(player.bag[item[4]]) Locate(28,7),printf("apple"); if(player.bag[item["apple"]]) Locate(28,31/2),printf("apple"); // if(player.bag[item[6]]) Locate(28,7),printf("pickaxe"); // if(player.bag[item[7]]) Locate(28,7),printf("pickaxe"); // if(player.bag[item[8]]) Locate(28,7),printf("pickaxe"); if(player.bag[item["stone"]]) Locate(28,5),printf("stone"); if(player.bag[item["dirt"]]) Locate(28,9),printf("dirt"); if(player.bag[item["wood"]]) Locate(28,12),printf("wood"); } int t; int last_time = sys.wMilliseconds + sys.wSecond * 1000 + sys.wMinute * 1000*60 + sys.wHour * 1000*3600; void world_modify() { GetLocalTime( &sys ); t = sys.wMilliseconds + sys.wSecond * 1000 + sys.wMinute * 1000*60 + sys.wHour * 1000*3600; if(t - last_time >= 75) { last_time = t; update_bag(); if(player.jump) { player.jump--; if(judge(player.x-1,player.y)) { clean_player(player.x,player.y); player.x--; print_player(player.x,player.y); } } else if(!player.jump) { if(judge(player.x+1,player.y)) { clean_player(player.x,player.y); player.x++; print_player(player.x,player.y); } } } } void playing() { char ch; while(true) { if(kbhit()) { ch=getch(); if(ch == 'a') go_left(); else if(ch == 'd') go_right(); else if(ch == ' ') jump(); else if(ch == '1') hit_left(); else if(ch == '3') hit_right(); else if(ch == '2') hit_down(); else if(ch == '4') put_left(); else if(ch == '6') put_right(); else if(ch == '5') put_down(); else if(ch == '7') takeout_item_1(); else if(ch == '9') takeout_item_2(); // else if(ch == '0') use_item(); } world_modify(); } } int main() { project(); welcome_print(); init(); world_print(); playing(); Sleep(10000); }