简体   繁体   中英

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! 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?

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;
        }
    }
}

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. I would suggest you use it as a temporary fix until you find an alternative solution. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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