2301_79046775 2023-09-21 15:55 采纳率: 0%
浏览 4

关于#c++#的问题:#include <cstdlib>

有没有兄弟知道我这个C++别踩白块代码运行后不管我点哪都直接结束运行了?


#include <cstdlib>
#include <graphics.h>
#include<vector>
#include <conio.h>
#include<iostream>
#include <time.h>
using namespace std;
class piano {
private:
    int width;
    int hight;
    int score;
    int movespeed;
    vector<int> piano_x, piano_y;
public:
    piano();
    void begin();
    void frame();
    void mouseoperate();
    void playgame();
    bool checkwhiteblock(MOUSEMSG a);
    bool checkdepth();
    void showscreen();
};

piano::piano() {
    width = 300;
    hight = 650;
    score = 0;
    movespeed = 2;
    piano_x.resize(5);
    piano_y.resize(5);
}
bool piano::checkwhiteblock(MOUSEMSG mouse) {
    COLORREF color = getpixel(mouse.x, mouse.y);
    if (color == RGB(255,255,255))
        return 1;
    else
        return 0;
}
bool piano::checkdepth() {
    if (piano_y[piano_y.size() - 1] > 650)
        return 0;
    else
        return 1;
}
void piano::begin() {
    initgraph(width, hight);
    setbkcolor(RGB(255,255,255));
    cleardevice();
    for (int i = 0; i < 5; i++) {
        piano_x[i] = rand() % 4;
        if (i == 0)
        {
            piano_y[i] = 110;
        }
        else
            piano_y[i] = piano_y[static_cast<std::vector<int, std::allocator<int>>::size_type>(i) - 1] + 90;
    }
    BeginBatchDraw();
}
void piano::frame() {
    cleardevice();
    setlinecolor(RED);
    for (int i = 0; i < 4; i++) {
        line(i * 75, 50, i * 75, 650);
    }
    line(0, 50, 300, 50);
}
void piano::playgame() {
    //srand((unsigned)time(nullptr));
    if (piano_x.empty()) {
        piano_x.emplace_back(rand() % 4);
        piano_y.emplace_back(50);
    }
    else if (piano_y[0] + movespeed > 140) {
        piano_x.insert(piano_x.begin(), rand() % 4);
        piano_y.insert(piano_y.begin(), piano_y[0] - 90);
    }
    for (int i = 0; i < piano_y.size(); i++)
        piano_y[i] += movespeed;

    showscreen();
}
void piano::mouseoperate() {
    MOUSEMSG m;
    if (MouseHit()) {
        m = GetMouseMsg();
        if (m.uMsg != WM_LBUTTONDOWN)
            return;
        else {
            int n = piano_x.size() - 1;
            while (1) {
                if (n == 0 && m.x >= piano_x[n] && m.x <= piano_x[n] + 75 && m.y <= piano_y[n] && m.y >= 50) {
                    piano_x.erase(piano_x.end() - 1);
                    piano_y.erase(piano_y.end() - 1);
                    break;
                }
                else if (m.x >= piano_x[n] && m.x <= piano_x[n] + 75 && m.y <= piano_y[n] && m.y >= piano_y[n] - 90) {
                    piano_x.erase(piano_x.end() - 1);
                    piano_y.erase(piano_y.end() - 1);
                    break;
                }
                else if (checkwhiteblock(m)) {
                    Sleep(250);
                    exit(0);
                }
                else
                    break;
            }
        }
    }
    
}
void piano::showscreen() {
    //srand((unsigned)time(nullptr));
    //setlinecolor(RGB(rand() % 256, rand() % 256, rand() % 256));
    //setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
    setlinecolor(GREEN);
    setfillcolor(GREEN);
    for (int i = 0; i < piano_x.size(); i++) {
        if (piano_y[i] >= 50) {
            if (piano_y[i] < 140) {
                //    setlinecolor(RGB(rand() % 256,rand()%256,rand()%256));
                //    setfillcolor(RGB(rand()%256,rand()%256,rand()%256));
                fillrectangle(piano_x[i] * 75, 50, piano_x[i] * 75 + 75, piano_y[i]);
            }
            else {
                //setlinecolor(RGB(rand() % 256, rand() % 256, rand() % 256));
                //setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
                fillrectangle(piano_x[i] * 75, piano_y[i] - 90, piano_x[i] * 75 + 75, piano_y[i]);
            }
        }
    }
    FlushBatchDraw();
    Sleep(50);

}
int main() {
    srand((unsigned)time(nullptr));
    piano temp;
    temp.begin();
    while (1) {
        temp.frame();
        temp.mouseoperate();
        temp.playgame();
        if (!temp.checkdepth()) {
            break;
        }
    }
    Sleep(10);
    EndBatchDraw();
    closegraph();
    return 0;
}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-09-21 18:40
    关注

    【相关推荐】



    • 这篇博客: c++各个头文件所包含常用函数中的 1.<cstdlib> 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
      atof(a); // 字符串a转换为double类型
      atoi(a); // 字符串a转换为int类型
      atol(a); // 字符串a转换为long int类型
      atoll(a); // 字符串a转换为long long int类型
      rand(); // 生成一个随机数
      abort(); // 终止程序
      abs(a); // 返回整型a的绝对值
      s=div(a,b); // 返回由结构体div_t变量s接收,s.quot是a,b的商,s.rem是余数
      

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 9月21日