【问题标题】:C++ SDL Reaction Time GameC++ SDL 反应时间游戏
【发布时间】:2013-05-03 03:06:20
【问题描述】:

我和我的朋友需要创建一个反应时间游戏。 某事like this

现在我们只是设法显示了红色按钮的图像,但我们需要帮助如何制作一个点击框,如果你点击红色按钮,它就会变成绿色。

谁能告诉我们怎么做?

我们正在使用 SDL,我想这很重要。

到目前为止,这是我们的代码:

#include <SDL/SDL.h>

void Plot(SDL_Surface *sur, int x, int y, SDL_Surface *dest)
{
    SDL_Rect rect = {x, y};
    SDL_BlitSurface(sur, NULL, dest, &rect);
}

SDL_Surface *LoadImage(const char *filename)
{
    SDL_Surface *sur = NULL;
    sur = SDL_LoadBMP(filename);

    if(sur == NULL)
    {
        printf("Img not found");
    }

    SDL_Surface *opsur = NULL;

    if(sur != NULL)
    {
        opsur = SDL_DisplayFormat(sur);
        SDL_SetColorKey(opsur, SDL_SRCCOLORKEY, 0xFFFFFF);
        if(opsur != NULL)
            SDL_FreeSurface(sur);
    }

    return opsur;
}

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    SDL_WM_SetCaption("Eksamensprojekt", NULL);
    SDL_Event Event;
    bool Running = true;

    SDL_Surface *sur = LoadImage("Red.bmp");

    while(Running)
    {
        while(SDL_PollEvent(&Event))
        {
            if(Event.type == SDL_QUIT)
                Running = false;
        }
        SDL_FillRect(screen, &screen->clip_rect, 0x000000);

        Plot(sur, 215, 140, screen);

        SDL_Flip(screen);
    }

}

【问题讨论】:

    标签: c++ time sdl


    【解决方案1】:

    您可以将 SDL_Rect 用作​​命中框。您可以使用 SDL 自己的事件处理系统来检查鼠标按钮何时被单击以及它的位置。然后你只需要检查鼠标位置是否在 SDL_Rect 内。

    您可以阅读有关 SDL 的更多信息here. 所以......路上有点帮助。您有一个主循环并拉动事件。

    if ( event.type == SDL_MOUSEBUTTONDOWN ){
    
        //Get mouse coordinates
        int x = event.motion.x;
        int y = event.motion.y;
    
        //If the mouse is over the button
        if( checkSpriteCollision( x, y ) ){
            // Yay, you hit the button
            doThings();
        }
        else
        {
            // D'oh I missed
        }
    
    }
    

    将其添加到 while 中,这至少可以帮助您入门。

    【讨论】:

    • 我们已经看过并尝试过,但仍然失败:(如果您或其他任何人可以为我们写出来,那就太好了:)
    • 感谢您的回答,但我们仍然无法使其正常工作。有 2 个 while 函数,我应该将它插入哪一个?当你点击时,如何让它显示绿色按钮?当你点击它时,我希望它从红色变为绿色。
    • 把它放在内循环中(在If ( event.type == SDL_Quit ) 下)要切换颜色,你应该和在绘图函数中做的基本相同,只是用绿色表面而不是红色表面。您可以使用 bool 值来确定您应该对两个表面中的哪一个进行 blit。
    【解决方案2】:

    像这样?

        while(Running)
        {
            while(SDL_PollEvent(&Event))
            {
                if(Event.type == SDL_QUIT)
                    Running = false;
    
                if ( event.type == SDL_MOUSEBUTTONDOWN ){
    
                    //Get mouse coordinates
                    int x = event.motion.x;
                    int y = event.motion.y;
    
                    //If the mouse is over the button
                    if( checkSpriteCollision( x, y ) ){
                        // Yay, you hit the button
                        doThings();
                    }
                    else {
                        // D'oh I missed
                    }
    
                }
            }
            SDL_FillRect(screen, &screen->clip_rect, 0x000000);
    
            Plot(sur, 215, 140, screen);
    
            SDL_Flip(screen);
        }
    
    }
    

    【讨论】:

    • 请在编程方面很棒的人帮助我们:D
    • 是的,现在您有了检查是否有人点击按钮的代码(假设您实现了CheckSpriteCollision(x,y) 函数。但是您需要移动Plot(...) 以便当有人点击时它会闪烁绿色表面按钮,否则为红色按钮。
    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多