zoukankan      html  css  js  c++  java
  • 事件驱动与状态驱动

    假设有这么一个场景,是用键盘的方向键去控制一个物体前进的方向,其中有down、up、right和left,大家很容易想到的是事件驱动,大概模型如下:

    /* Alien screen coordinates */
        int alien_x=0, alien_y=0;
     
        /* Main game loop */
        /* Check for events */
        while( SDL_PollEvent( &event ) ){
            switch( event.type ){
                /* Look for a keypress */
                case SDL_KEYDOWN:
                    /* Check the SDLKey values and move change the coords */
                    switch( event.key.keysym.sym ){
                        case SDLK_LEFT:
                            alien_x -= 1;
                            break;
                        case SDLK_RIGHT:
                            alien_x += 1;
                            break;
                        case SDLK_UP:
                            alien_y -= 1;
                            break;
                        case SDLK_DOWN:
                            alien_y += 1;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

    但是,键盘事件只有在一个按键发生状态改变时才会发生,所以,上述的模型中,要移动100个距离,就要按100次键盘。为了避免这个问题,几乎所有游戏都是采用状态驱动的模型,如下:

    /* Alien screen coordinates */
        int alien_x=0, alien_y=0;
        int alien_xvel=0, alien_yvel=0;
        
        /* Main game loop */
        /* Check for events */
        while( SDL_PollEvent( &event ) ){
            switch( event.type ){
                /* Look for a keypress */
                case SDL_KEYDOWN:
                    /* Check the SDLKey values and move change the coords */
                    switch( event.key.keysym.sym ){
                        case SDLK_LEFT:
                            alien_xvel = -1;
                            break;
                        case SDLK_RIGHT:
                            alien_xvel =  1;
                            break;
                        case SDLK_UP:
                            alien_yvel = -1;
                            break;
                        case SDLK_DOWN:
                            alien_yvel =  1;
                            break;
                        default:
                            break;
                    }
                    break;
                /* We must also use the SDL_KEYUP events to zero the x */
                /* and y velocity variables. But we must also be       */
                /* careful not to zero the velocities when we shouldn't*/
                case SDL_KEYUP:
                    switch( event.key.keysym.sym ){
                        case SDLK_LEFT:
                            /* We check to make sure the alien is moving */
                            /* to the left. If it is then we zero the    */
                            /* velocity. If the alien is moving to the   */
                            /* right then the right key is still press   */
                            /* so we don't tocuh the velocity            */
                            if( alien_xvel < 0 )
                                alien_xvel = 0;
                            break;
                        case SDLK_RIGHT:
                            if( alien_xvel > 0 )
                                alien_xvel = 0;
                            break;
                        case SDLK_UP:
                            if( alien_yvel < 0 )
                                alien_yvel = 0;
                            break;
                        case SDLK_DOWN:
                            if( alien_yvel > 0 )
                                alien_yvel = 0;
                            break;
                        default:
                            break;
                    }
                    break;
                
                default:
                    break;
            }
        }
    
        /* Update the alien position */
        alien_x += alien_xvel;
        alien_y += alien_yvel;
  • 相关阅读:
    AppCan打包问题
    Layui数据表格模型
    Layui关闭弹出层并刷新父窗口
    spring boot重启后图片不见
    MySQL数据库创建时间和更新时间错乱问题
    Spring Boot解决无法访问图片的问题
    Spring Boot解决无法访问图片的问题
    reload() 方法用于重新加载当前文档。配合Ajax异步请求。
    Layui数据表格的接口数据请求方式为Get
    时间格式注解
  • 原文地址:https://www.cnblogs.com/littlethank/p/2575877.html
Copyright © 2011-2022 走看看