C++快板程序包围盒碰撞关闭 我正在使用AlelGro图形库在C++中使用一个游戏菜单。我正在使用代码块和mingw。有许多按钮使用修改的边界框,如果鼠标位于按钮上方,该边界框将返回true。除了一个按钮外,它对所有按钮都很好,我不知道为什么会发生这种情况。我花了整整一个小时寻找,什么也没找到。在断开的按钮上,当鼠标悬停在按钮的右侧和下方以及上方的某些位置时,该按钮的响应为真。:/任何帮助都将不胜感激。请随意指出我的代码中的任何其他错误,或者如果我正在以愚蠢的方式做某事。我在教自己的C++,如果代码不好,我不会感到惊讶。谢谢

C++快板程序包围盒碰撞关闭 我正在使用AlelGro图形库在C++中使用一个游戏菜单。我正在使用代码块和mingw。有许多按钮使用修改的边界框,如果鼠标位于按钮上方,该边界框将返回true。除了一个按钮外,它对所有按钮都很好,我不知道为什么会发生这种情况。我花了整整一个小时寻找,什么也没找到。在断开的按钮上,当鼠标悬停在按钮的右侧和下方以及上方的某些位置时,该按钮的响应为真。:/任何帮助都将不胜感激。请随意指出我的代码中的任何其他错误,或者如果我正在以愚蠢的方式做某事。我在教自己的C++,如果代码不好,我不会感到惊讶。谢谢,c++,allegro,bounding,C++,Allegro,Bounding,main.cpp #include<allegro.h> #include<fstream> #include<stdlib.h> #include<stdio.h> #include<time.h> #include"menu.h" using namespace std; //external functions extern void setup(); extern void evil_sock_puppet_intro(BI

main.cpp

#include<allegro.h>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include"menu.h"

using namespace std;

//external functions
extern void setup();
extern void evil_sock_puppet_intro(BITMAP*);
extern void version_number(BITMAP*);
extern bool bounding_box_collision(int, int, int, int, int, int, int, int);
extern bool check_button(button);
extern void credits(BITMAP*, BITMAP*, BITMAP*, button);
extern void instructions(BITMAP*, BITMAP*, BITMAP*, button);
extern void highscores(BITMAP*, BITMAP*, BITMAP*, button);


int main(){
//loops
int i, n;
bool loop;

//file input output streams
int save_return;
ifstream load_highscores;

//set up allegro
setup();
//create bitmaps
BITMAP *buffer = create_bitmap(640, 480);
//menu
BITMAP *menu_background = load_bitmap("placeholder_sprites\\menu_background.bmp", NULL);
BITMAP *title = load_bitmap("placeholder_sprites\\title.bmp", NULL);
BITMAP *play_preview = load_bitmap("placeholder_sprites\\play_preview.bmp", NULL);
BITMAP *play_button_bmp = load_bitmap("sprites\\start_button.bmp", NULL);
BITMAP *instructions_button_bmp = load_bitmap("sprites\\instructions_button.bmp", NULL);
BITMAP *highscores_button_bmp = load_bitmap("sprites\\highscores_button.bmp", NULL);
BITMAP *credits_button_bmp = load_bitmap("sprites\\credits_button.bmp", NULL);
BITMAP *exit_button_bmp = load_bitmap("sprites\\exit_button.bmp", NULL);
//other buttons
BITMAP *menu_button_bmp = load_bitmap("sprites\\menu_button.bmp", NULL);
//instructions
BITMAP *instructions_info = load_bitmap("placeholder_sprites\\instructions.bmp", NULL);
//credits
BITMAP *credits_bitmap = load_bitmap("placeholder_sprites\\credits.bmp", NULL);
//highscores
BITMAP *highscore_table = load_bitmap("placeholder_sprites\\highscore_table.bmp", NULL);

//declare variables
//variables from data structures
button play_button;
button instructions_button;
button highscores_button;
button credits_button;
button exit_button;
button menu_button;

//score
int highscore_return;
int score;
//rand seed from clock
srand( time(NULL) );

//buttons
play_button.x = 30, play_button.y = 170, play_button.w = 70, play_button.h = 28, play_button.select = false;
instructions_button.x = 30, instructions_button.y = 210, instructions_button.w = 164, instructions_button.h = 28, instructions_button.select = false;
highscores_button.x = 30, highscores_button.y = 250, highscores_button.w = 150, highscores_button.h = 28, highscores_button.select = false;
credits_button.x = 30, credits_button.y = 290, credits_button.w = 95, credits_button.h = 28, credits_button.select = false;
exit_button.x = 30, exit_button.y = 330, exit_button.w = 64, exit_button.h = 28, exit_button.select = false;
menu_button.x = 300, menu_button.y = 400, menu_button.w = 70, menu_button.h = 28, menu_button.select = false;
//call intro
evil_sock_puppet_intro(buffer);

//menu
while(!key[KEY_ESC]){

                     //blit background, title & version number
                     blit(menu_background, buffer, 0, 0, 0, 0, 640, 480);
                     masked_blit(title, buffer, 0, 0, 80, 30, 640, 480);
                     version_number(buffer);

                     //blit buttons to buffer
                     if(!play_button.select)
                     blit(play_button_bmp, buffer, 0, 0, play_button.x, play_button.y, play_button.w, play_button.h);
                     else
                     blit(play_button_bmp, buffer, play_button.w + 1, 0, play_button.x, play_button.y, play_button.w, play_button.h);

                     if(!instructions_button.select)
                     blit(instructions_button_bmp, buffer, 0, 0, instructions_button.x, instructions_button.y, instructions_button.w, instructions_button.h);
                     else
                     blit(instructions_button_bmp, buffer, instructions_button.w + 1, 0, instructions_button.x, instructions_button.y, instructions_button.w, instructions_button.h);

                     if(!highscores_button.select)
                     blit(highscores_button_bmp, buffer, 0, 0, highscores_button.x, highscores_button.y, highscores_button.w, highscores_button.h);
                     else
                     blit(highscores_button_bmp, buffer, highscores_button.w + 1, 0, highscores_button.x, highscores_button.y, highscores_button.w, highscores_button.h);

                     if(!credits_button.select)
                     blit(credits_button_bmp, buffer, 0, 0, credits_button.x, credits_button.y, credits_button.w, credits_button.h);
                     else
                     blit(credits_button_bmp, buffer, credits_button.w + 1, 0, credits_button.x, credits_button.y, credits_button.w, credits_button.h);

                     if(!exit_button.select)
                     blit(exit_button_bmp, buffer, 0, 0, exit_button.x, exit_button.y, exit_button.w, exit_button.h);
                     else
                     blit(exit_button_bmp, buffer, exit_button.w + 1, 0, exit_button.x, exit_button.y, exit_button.w, exit_button.h);

                     //check if buttons are hovered
                     play_button.select = check_button(play_button);
                     instructions_button.select = check_button(instructions_button);
                     highscores_button.select = check_button(highscores_button);
                     credits_button.select = check_button(credits_button);
                     exit_button.select = check_button(exit_button);

                     //call function if its respective button is pressed
                     //if(mouse_b & 1 && play_button.select){
                                while(loop){
                                //score = game(buffer);
                                //highscore_return = highscores(buffer, highscore_table, menu_button, play_button, score, highscore);
                                if(highscore_return != 1)
                                loop = false;
                                }loop = true;
                                //}

                     if(mouse_b & 1 && instructions_button.select)
                                instructions(buffer, instructions_info, menu_button_bmp, menu_button);

                     if(mouse_b & 1 && highscores_button.select)
                                highscores(buffer, highscore_table, menu_button_bmp, menu_button);

                     if(mouse_b & 1 && credits_button.select)
                                credits(buffer, credits_bitmap, menu_button_bmp, menu_button);

                     if(mouse_b & 1 && exit_button.select)
                                return 0;

                     //preview
                     blit(play_preview, buffer, 0, 0, 302, 138, 300, 300);

                     //blit mouse to buffer
                     show_mouse(buffer);

                     //blit buffer to screen and pause
                     blit(buffer, screen, 0, 0, 0, 0, 640, 480);
                     rest(25);
                     clear_bitmap(buffer);
                     }
return 0;
}
END_OF_MAIN()
这个很好用

#include<allegro.h>
#include"menu.h"

//external functions
extern bool check_button(button);

void instructions(BITMAP *buffer, BITMAP *instructions, BITMAP *menu_button_bmp, button menu_button){
                while(!key[KEY_ESC]){
                                     //check if buttons are hovered
                                     menu_button.select = check_button(menu_button);

                                      //go to the main menu if the menu button is pressed
                                     if(mouse_b & 1 && menu_button.select)
                                     return;

                                     //blit bitmaps to buffer
                                     blit(instructions, buffer, 0, 0, 0, 0, 640, 480);
                                     if(!menu_button.select)
                                     blit(menu_button_bmp, buffer, 0, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);
                                     else
                                     blit(menu_button_bmp, buffer, menu_button.w + 1, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);

                                     //blit mouse to buffer
                                     show_mouse(buffer);

                                     //blit buffer to screen and pause
                                     blit(buffer, screen, 0, 0, 0, 0, 640, 480);
                                     rest(25);
                                     clear_bitmap(buffer);
                                     }
}
高分。cpp:这一个没有

#include<allegro.h>
#include<fstream>
#include"menu.h"

using namespace std;

extern int save_game_highscore(int score[]);
extern int check_button(button);

//menu accessed
void highscores(BITMAP *buffer, BITMAP *highscore_table, BITMAP *menu_button_bmp, button menu_button){
                //declare file input output streams
                ifstream load_highscores;

                //declare variables
                int i;
                int save_return;
                bool change_highscore = true;
                bool close = false;

                //load highscores from a file
                int highscore [5] = {400, 200, 150, 100, 25};
                load_highscores.open("savefiles\\highscores.save", ios::in);
                for(i = 0; i < 6; i++)
                load_highscores >> highscore[i];
                load_highscores.close();

                while(!key[KEY_ESC] && close == false){

                              //blit highscore table to buffer
                              blit(highscore_table, buffer, 0, 0, 0, 0, 640, 480);
                              textprintf_ex(buffer, font, 119, 145, makecol(255, 255, 255), -1, "%d", highscore[0]);
                              textprintf_ex(buffer, font, 119, 192, makecol(255, 255, 255), -1, "%d", highscore[1]);
                              textprintf_ex(buffer, font, 119, 238, makecol(255, 255, 255), -1, "%d", highscore[2]);
                              textprintf_ex(buffer, font, 119, 280, makecol(255, 255, 255), -1, "%d", highscore[3]);
                              textprintf_ex(buffer, font, 119, 322, makecol(255, 255, 255), -1, "%d", highscore[4]);

                              //blit buttons to buffer
                              if(!menu_button.select)
                              blit(menu_button_bmp, buffer, 0, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);
                              else
                              blit(menu_button_bmp, buffer, menu_button.w + 1, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);

                              show_mouse(buffer);

                              //check if menu button is pressed & if so return to the menu
                              menu_button.select = check_button(menu_button);
                              if(mouse_b & 1 && menu_button.select)
                                        return;

    //blit buffer to screen
    blit(buffer, screen, 0, 0, 0, 0, 640, 480);
    rest(25);
}
}

我发现了问题!:D在highscores.cpp中,外部按钮检查函数被写成int而不是bool。现在都修好了O刚意识到我不知何故忘了在问题中包含按钮检查功能的代码。哦,这已经不重要了。

你不可能把问题缩小一点吗?对不起。我不确定是什么导致了这个问题,所以我把所有的代码都保存在那里/我发现了问题,我把外部函数写成int而不是bool。明天当那个愚蠢的限制消失时,我会发布一个正确的答案。
#include<allegro.h>
#include<fstream>
#include"menu.h"

using namespace std;

extern int save_game_highscore(int score[]);
extern int check_button(button);

//menu accessed
void highscores(BITMAP *buffer, BITMAP *highscore_table, BITMAP *menu_button_bmp, button menu_button){
                //declare file input output streams
                ifstream load_highscores;

                //declare variables
                int i;
                int save_return;
                bool change_highscore = true;
                bool close = false;

                //load highscores from a file
                int highscore [5] = {400, 200, 150, 100, 25};
                load_highscores.open("savefiles\\highscores.save", ios::in);
                for(i = 0; i < 6; i++)
                load_highscores >> highscore[i];
                load_highscores.close();

                while(!key[KEY_ESC] && close == false){

                              //blit highscore table to buffer
                              blit(highscore_table, buffer, 0, 0, 0, 0, 640, 480);
                              textprintf_ex(buffer, font, 119, 145, makecol(255, 255, 255), -1, "%d", highscore[0]);
                              textprintf_ex(buffer, font, 119, 192, makecol(255, 255, 255), -1, "%d", highscore[1]);
                              textprintf_ex(buffer, font, 119, 238, makecol(255, 255, 255), -1, "%d", highscore[2]);
                              textprintf_ex(buffer, font, 119, 280, makecol(255, 255, 255), -1, "%d", highscore[3]);
                              textprintf_ex(buffer, font, 119, 322, makecol(255, 255, 255), -1, "%d", highscore[4]);

                              //blit buttons to buffer
                              if(!menu_button.select)
                              blit(menu_button_bmp, buffer, 0, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);
                              else
                              blit(menu_button_bmp, buffer, menu_button.w + 1, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);

                              show_mouse(buffer);

                              //check if menu button is pressed & if so return to the menu
                              menu_button.select = check_button(menu_button);
                              if(mouse_b & 1 && menu_button.select)
                                        return;

    //blit buffer to screen
    blit(buffer, screen, 0, 0, 0, 0, 640, 480);
    rest(25);
}
}