简体   繁体   English

C ++ SDL2,移动速度太快

[英]C++ SDL2, moving too fast

I'm currently having a problem with my pong game. 我目前的乒乓球比赛有问题。 I'm trying to smooth out the player movement (so it wont stutter so much when is moves, and no delay after first keypress) 我正试图平滑玩家的动作(所以当移动时它不会断断续续,并且在第一次按键后没有延迟)

This is my code so far, problem is while I've got the movement smoother the _paddle move too fast! 这是我的代码到目前为止,问题是我的运动更顺畅_paddle移动得太快了! and I don't now how to make it move slower! 我现在不知道如何让它移动得更慢!

Is there a way i can make the _paddle move slower or did i write the code wrong? 有没有办法可以让_paddle移动得更慢或者我写错了代码?

maingame.h 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 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;
        }
    }
}

It maybe bad programming practice to do so but the only way I would see getting around your problem is to add an SDL_Delay() in your gameloop function. 这可能是糟糕的编程习惯,但我唯一能看到解决问题的方法是在你的游戏循环函数中添加一个SDL_Delay()。 I would suggest you use it as a temporary fix until you find an alternative solution. 我建议您使用它作为临时修复,直到找到替代解决方案。 Hope this helps. 希望这可以帮助。

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

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