简体   繁体   English

SDL Snake放慢速度?

[英]SDL Snake slows down?

I've been working on a clone of Snake using SDL but I've found that as I run the game it slows down. 我一直在使用SDL制作Snake的克隆,但发现在运行游戏时它会变慢。 The snake starts moving quite fast but after a few turn it slows down quite a bit. 蛇开始移动得非常快,但转了几下后,它的速度却慢了很多。 I've been trying to figure out why but can't. 我一直在试图找出原因,但不能。 :( I assume it's got something to do with either the way I've tried to implement FPS or the update game function (where key handling takes place). :(我认为这与我尝试实现FPS的方式或更新游戏功能(在其中进行键处理)有关。

Here's where I'm trying to implement FPS: 这是我尝试实施FPS的地方:

void run(){
    int SKIP_TICKS;;
    long next_game_tick = time(0);
    long sleep_time = 0;
    std::cout<<next_game_tick<<std::endl;

    while (!quit){
        SKIP_TICKS = 1000 / fps;

        updateGame();
        render();

        next_game_tick += SKIP_TICKS;
        sleep_time = next_game_tick - time(0);
        sleep_time *= 10;
        usleep(sleep_time);
    }
}

And here is my update game function: 这是我的更新游戏功能:

void updateGame(){
    SDL_Event event;

    if (!isFrutActive){
        doNewFruit();
    }

    while (SDL_PollEvent(&event)){
        if (event.type == SDL_QUIT){
            quit = true;
        }
        if (event.type == SDL_KEYDOWN){//Get keyboard input
            switch (event.key.keysym.sym){
                case SDLK_ESCAPE:   quit = true; break;

                case SDLK_UP:
                case SDLK_w:
                    ydir = -SPEED;
                    xdir = 0;
                    break;

                case SDLK_DOWN:
                case SDLK_s:
                    ydir = SPEED;
                    xdir = 0;
                    break;

                case SDLK_LEFT:
                case SDLK_a:
                    xdir = -SPEED;
                    ydir = 0;
                    break;

                case SDLK_RIGHT:
                case SDLK_d:
                    xdir = SPEED;
                    ydir = 0;
                    break;
            }
        }
    }

Any help would be much appreciated. 任何帮助将非常感激。 Thanks. 谢谢。

I do believe that I've found a solution to the problem with sleep_time being set to a negative when I press the X. Here's my new code: 我确实相信我已经找到了解决问题的方法,当我按下X时,sleep_time设置为负数。这是我的新代码:

void run(){
    int SKIP_TICKS;
    long next_game_tick = SDL_GetTicks();
    long sleep_time = 0;

    while (!quit){
        SKIP_TICKS = 1000 / fps;

        updateGame();
        render();
        next_game_tick += SKIP_TICKS;
        sleep_time = next_game_tick - SDL_GetTicks();
        std::cout<<sleep_time<<std::endl;
        sleep_time *= 1000;
        if (sleep_time >= 0){
            usleep(sleep_time);
        }
        else{
            next_game_tick += SKIP_TICKS;
        }
    }
}

By using the if statement I avoid using usleep(sleep_time) if sleep_time is below zero. 通过使用if语句,如果sleep_time小于零,则避免使用usleep(sleep_time)。 Thanks a lot to the people who helped me! 非常感谢帮助我的人们! :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM