C ++ SDL2,移动速度太快

时间:2015-03-05 22:34:46

标签: c++

我目前的乒乓球比赛有问题。我正试图平滑玩家的动作(所以当它移动时它不会断断续续,并且在第一次按键后没有延迟)

这是我的代码到目前为止,问题是我的运动更顺畅_paddle移动得太快了!我现在不知道如何让它移动得更慢!

有没有办法可以让_paddle移动得更慢或者我写错了代码?

maingame.h

  #pragma once
#include <iostream>
#include <SDL/SDL.h>
#include <string>
#include "maingame.h"

class maingame
{
public:
    maingame();
    ~maingame();

    //loads pictures
    bool loadMedia(std::string path);

    //init the system
    void init();

    //runs the game
    void run();

    //THE EPIC GAMELOOP
    void gameloop();

    //draw the screen
    void draw();

    void UserInput();

private:
    //window
    SDL_Window* _window;

    //redenderer
    SDL_Renderer* _rend;

    //the screens surface
    SDL_Surface* _screensurface;

    //player, ai and the ball
    SDL_Rect _paddle;
    SDL_Rect _ai;
    SDL_Rect _ball;

    //checks if you pressed down the W or S button
    bool keydown_w = false;
    bool keydown_s = false;

    //Event for the pall stuff
    SDL_Event e;
};

maingame.c

#include "maingame.h"

/*
PONG V0.2
 Black background - CHECK
 paddle appear on screen - CHECK
 other paddle appear on screen - CHECK
 ball appear on screen - CHECK
 move player paddle - CHECK
 impossible to move outside of map - CHECK
 movement smoother -
 make ball go around -
 collison with paddles -
 keep scores -
 show scores -
 make a 2nd player chooseable -

*/
//screen width and height
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;

maingame::maingame()
{
    _window = nullptr;
    _rend = nullptr;
}


maingame::~maingame()
{
}

void maingame::init()
{
    SDL_Init(SDL_INIT_EVERYTHING);
}

void maingame::run()
{

    init();
    //creating a windows
    _window = SDL_CreateWindow("PONG", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

    //create the render
    _rend = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);

    //set out the player
    _paddle.x = 100;
    _paddle.y = (SCREEN_HEIGHT / 2) - 100;
    _paddle.w = 20;
    _paddle.h = 200;

    //set out the ai
    _ai.x = SCREEN_WIDTH - 100;
    _ai.y = (SCREEN_HEIGHT / 2) - 100;
    _ai.w = 20;
    _ai.h = 200;

    //set out the ball
    _ball.x = SCREEN_WIDTH / 2 - 20;
    _ball.y = (SCREEN_HEIGHT / 2) - 20;
    _ball.w = 20;
    _ball.h = 20;

    draw();
    gameloop();
}

void maingame::draw()
{
    //make the render be black
    SDL_SetRenderDrawColor(_rend, 0, 0, 0, 0);

    //Clear the render with the color we set with SDL_SetRenderDrawColor, in this case black
    SDL_RenderClear(_rend);

    //make the next things we will render white
    SDL_SetRenderDrawColor(_rend, 255, 255, 255, 0);

    //make the paddle, ai and ball the color of SDL_SetRenderDrawColor, which is whites in this case
    SDL_RenderFillRect(_rend, &_paddle);
    SDL_RenderFillRect(_rend, &_ai);
    SDL_RenderFillRect(_rend, &_ball);

    //SDL_RenderDrawRect(_rend, &_paddle);

    //Present the render, draw it to the screen
    SDL_RenderPresent(_rend);
}

bool maingame::loadMedia(std::string path)
{
    //Loading success flag
    bool success = true;
    SDL_Surface* pic;

    //Load splash image
    pic = SDL_LoadBMP(path.c_str());

    if (pic == NULL)
    {
        printf("Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError());
        success = false;
    }

    return success;
}

void maingame::gameloop()
{
    const Uint8 *keys = SDL_GetKeyboardState(NULL);
    bool keydown_w = false;
    bool keydown_s = false;

    while (true)
    {
        while (SDL_PollEvent(&e) != 0)
        {
            //pressed the X, quit the program
            if (e.type == SDL_QUIT)
            {
                exit(1);
            }
            UserInput();
        }

        UserInput();
        draw();
    }
}

void maingame::UserInput()
{
    float lol = 0;
    //Pressed a key!
    if (e.type == SDL_KEYDOWN)
    {
        //pressed W, move the player
        if (e.key.keysym.sym == SDLK_w)
        {
            keydown_w = true;
        }
        //pressed S, move the player
        else if (e.key.keysym.sym == SDLK_s)
        {
            keydown_s = true;
        }
    }
    if (e.type == SDL_KEYUP)
    {
        std::cout << keydown_w << std::endl;
        if (e.key.keysym.sym == SDLK_w)
            keydown_w = false;
        if (e.key.keysym.sym == SDLK_s)
            keydown_s = false;
    }

    if (keydown_w)
    {
        if (_paddle.y > 1)
        {
            _paddle.y -= 1;
        }
        std::cout << keydown_w << std::endl;
    }
    if (keydown_s)
    {
        if (_paddle.y < SCREEN_HEIGHT - _paddle.h)
        {
            _paddle.y += 1;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这可能是糟糕的编程习惯,但我唯一能看到解决问题的方法是在你的游戏循环函数中添加一个SDL_Delay()。我建议您使用它作为临时修复,直到找到替代解决方案。希望这可以帮助。