小恐龙跳跳跳devc++(升级版)

本文详细描述了一段C++代码,实现了一个简单的控制台游戏,主角小恐龙需要在地图上跳跃避开障碍物,用户可以输入颜色和跳跃高度。游戏通过控制台输出图形和处理玩家输入来实现。
摘要由CSDN通过智能技术生成

小恐龙跳跳跳

#include<bits/stdc++.h>
#include<conio.h>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<windows.h>
#define set_color(d) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|d)
#define r FOREGROUND_RED
#define b FOREGROUND_BLUE
#define g FOREGROUND_GREEN
using namespace std;
//定义 
int x=10,now_x=10,now,len,now_old,low_len,flat=0;
string col,man[5];
//游戏主地图 
string a[15]={
    {""},
    {""},
    {""},
    {""},
    {""},
    {""},
    {""},
    {""},
    {""},
    {""},
    {""},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉"},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉"},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉"},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉"}
};
//加载界面定义 
string beginmap[40]={
    {"▉                                       "},
    {"▉▉                                      "},
    {"▉▉▉                                     "},
    {"▉▉▉▉                                    "},
    {"▉▉▉▉▉                                   "},
    {"▉▉▉▉▉▉                                  "},
    {"▉▉▉▉▉▉▉                                 "},
    {"▉▉▉▉▉▉▉▉                                "},
    {"▉▉▉▉▉▉▉▉▉                               "},
    {"▉▉▉▉▉▉▉▉▉▉                              "},
    {"▉▉▉▉▉▉▉▉▉▉▉                             "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉                            "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉                           "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉                          "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                         "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                        "}, 
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                       "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                      "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                     "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                    "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                   "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                  "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                 "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉                "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉               "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉              "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉             "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉            "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉           "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉          "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉         "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉        "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉       "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉      "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉     "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉    "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉   "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉  "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ "},
    {"▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉"}
};
//障碍物定义 
string zai={"▉"};
int now_zai=63;
int now_zai_speed=1,now_zai_speed_begin=70;
void game();
void print(); 
void print_begin();
int main()
{
    system("mode con cols=63 lines=20");
    print_begin();
    game();
    return 0;
}
void game()
{
    system("cls");
    cout<<"Who are you?\n";
    do{
        getline(cin,man[len++]);
    }while(len<5&&man[len-1]!="end");
    man[len-1]={""};
    low_len=man[len-2].size();
    len-=2;
    system("cls");
    cout<<"What is the color of your body?\n";
    cin>>col;
    system("cls");
    cout<<"How high can you jump?\n";
    cin>>now_old;    
    while(now_zai_speed_begin++){
        if(now>0)
        {
            x--;
            now--;
        }
        else if(x<now_x)
        {
            x++;
        }
        else if(_kbhit()&&x==now_x){
            char ch=getch();
            if(ch==' '){
                x--;
                now=now_old;
            }
        }
        print();
        Sleep(100);
        now_zai-=now_zai_speed;
        if(now_zai<0&&flat==1)
        {
            flat=0;
            now_zai=63;
            srand(time(NULL));
            now_zai_speed=rand()%5+now_zai_speed_begin/70;
        }
        else if(now_zai<0&&flat==0)
        {
            flat=1;
        }
    }    
}

void print()
{
    system("cls");
    cout<<"height="<<x<<" "<<'\n';
    for(int i=0;i<=10;i++)
    {
        if(i+len<2*x&&i+len>=x)
        {
            if(col=="red"||col=="RED")
                set_color(r);
            else if(col=="green"||col=="GREEN")
                set_color(g);
            else if(col=="blue"||col=="BLUE")
                set_color(b);
            cout<<man[i+len-x];
            set_color(r|g|b);
        }
        if(i==10)
        {
            //障碍物判断 
            set_color(r|g|b);
            if(now_zai<=low_len&&x==10)
            {
                
                system("cls");
                system("color 40");
                cout<<"\n\n\n\n\n\n\n\n                         GAMEOVER";
                Sleep(5000);
                system("cls");
                system("color 07");
                cout<<"go back turn '1':else turn '2'\n";
                int aaa;
                cin>>aaa;
                if(aaa==1)
                    game();
                else
                {
                    exit(1);
                }
            }
            else if(now_zai>low_len)
            {
                if(x==10)
                {
                    for(int i=low_len;i<now_zai;i++){
                        cout<<" ";
                    }
                }
                else
                {
                    for(int i=0;i<now_zai;i++){
                        cout<<" ";
                    }
                }
                cout<<zai;
            }
            else if(now_zai<low_len)
            {
                for(int i=0;i<now_zai;i++){
                    cout<<" ";
                }    
                cout<<zai;            
            }
            else if(now_zai==low_len&&x!=10)
            {
                for(int i=0;i<now_zai;i++){
                    cout<<" ";
                }
                cout<<zai;                    
            }
        }
        cout<<"\n";
    }
    set_color(g|r|b);
    for(int i=11;i<=14;i++){
        cout<<a[i]<<"\n";
    }
}
void print_begin()
{
    system("color E0");
    for(int i=0;i<40;i++){
        system("cls");
        cout<<"                   ___      ___  ___          ___   |      "<<"\n";
        cout<<"        \      /|__  |   |    |   | |\/| |__    |       "<<"\n";
        cout<<"         \/\/ |___ |__ |___ |___| |    | |___   .       "<<"\n\n";
        cout<<"                ___      _    _    _  ___                       "<<"\n";
        cout<<"               |  __   /_\  |\/| |__                       "<<"\n";
        cout<<"               |___|  /   \ |    | |___                         "<<"\n";
        cout<<"\n\n                    name:jump!jump!jump!!!\n\n          -----------------------------------------\n"<<"         |"<<beginmap[i]<<" |\n          -----------------------------------------\n\n\n\ncreato:Jason   ";
        srand(time(NULL));
        Sleep(rand()%500);
    }
    Sleep(500);
}

以下是谷歌小恐龙的简单实现代码(使用C++和SDL库): ```c++ #include <iostream> #include <SDL.h> using namespace std; const int SCREEN_WIDTH = 600; const int SCREEN_HEIGHT = 200; const int GROUND_HEIGHT = 180; int main(int argc, char* args[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("Google Dino", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Texture* dinoTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("dino.bmp")); SDL_Texture* cactusTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("cactus.bmp")); SDL_Texture* groundTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("ground.bmp")); bool quit = false; SDL_Event e; int dinoX = 0; int dinoY = GROUND_HEIGHT; int cactusX = SCREEN_WIDTH; int cactusY = GROUND_HEIGHT - 20; int groundX = 0; int groundY = GROUND_HEIGHT + 20; int dinoVelY = 0; while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_SPACE) { if (dinoY == GROUND_HEIGHT) { dinoVelY = -10; } } } } dinoY += dinoVelY; if (dinoY > GROUND_HEIGHT) { dinoY = GROUND_HEIGHT; dinoVelY = 0; } else if (dinoY < 0) { dinoY = 0; dinoVelY = 0; } cactusX -= 5; if (cactusX < -20) { cactusX = SCREEN_WIDTH; } groundX -= 5; if (groundX < -SCREEN_WIDTH) { groundX = 0; } SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderClear(renderer); SDL_Rect dinoRect = { dinoX, dinoY, 48, 48 }; SDL_RenderCopy(renderer, dinoTexture, NULL, &dinoRect); SDL_Rect cactusRect = { cactusX, cactusY, 20, 40 }; SDL_RenderCopy(renderer, cactusTexture, NULL, &cactusRect); SDL_Rect groundRect1 = { groundX, groundY, SCREEN_WIDTH, 20 }; SDL_RenderCopy(renderer, groundTexture, NULL, &groundRect1); SDL_Rect groundRect2 = { groundX + SCREEN_WIDTH, groundY, SCREEN_WIDTH, 20 }; SDL_RenderCopy(renderer, groundTexture, NULL, &groundRect2); SDL_RenderPresent(renderer); SDL_Delay(17); } SDL_DestroyTexture(dinoTexture); SDL_DestroyTexture(cactusTexture); SDL_DestroyTexture(groundTexture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` 这只是一个简单的实现,你可以根据需要进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值