有没有兄弟知道我这个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;
}