Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;SDL2,移动太快了_C++ - Fatal编程技术网

C++ C++;SDL2,移动太快了

C++ C++;SDL2,移动太快了,c++,C++,我目前的乒乓球游戏有问题。我正在尝试平滑玩家的移动(这样当is移动时不会有太多的口吃,并且在第一次按键后不会延迟) 这是我到目前为止的代码,问题是当我的动作更流畅时,桨移动得太快了!我现在不知道如何让它移动得更慢 有没有办法让划桨移动得更慢,或者是我把代码写错了 maingame.h #pragma once #include <iostream> #include <SDL/SDL.h> #include <string> #include "maing

我目前的乒乓球游戏有问题。我正在尝试平滑玩家的移动(这样当is移动时不会有太多的口吃,并且在第一次按键后不会延迟)

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

有没有办法让划桨移动得更慢,或者是我把代码写错了

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;
};
#pragma一次
#包括
#包括
#包括
#包括“maingame.h”
类主游戏
{
公众:
主要游戏();
~main game();
//加载图片
bool loadMedia(标准::字符串路径);
//初始化系统
void init();
//运行游戏
无效运行();
//史诗般的游戏循环
void gameloop();
//画屏幕
无效抽取();
void UserInput();
私人:
//窗口
SDL_窗口*_窗口;
//重新定义
SDL_渲染器*_rend;
//屏幕浮出水面
SDL_表面*_屏幕表面;
//球员,ai和球
直桨;
SDL______ai;
SDL直球;
//检查您是否按下了W或S按钮
bool keydown_w=错误;
bool keydown_s=错误;
//鲍尔事件
SDL_事件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;
        }
    }
}
#包括“maingame.h”
/*
PONG V0.2
黑色背景检查
挡板出现在屏幕上-检查
屏幕上出现其他拨杆-检查
球出现在屏幕上-检查
移动球员球拍-检查
无法移动到地图之外-检查
运动平滑器-
使球旋转-
带桨的科里森-
记分-
记分-
让第二个玩家可以选择-
*/
//屏幕宽度和高度
屏幕宽度=1024;
屏幕高度=768;
maingame::maingame()
{
_窗口=空PTR;
_rend=nullptr;
}
maingame::~maingame()
{
}
void maingame::init()
{
SDL_Init(SDL_Init_EVERYTHING);
}
void maingame::run()
{
init();
//创建一个窗口
_window=SDL\u CreateWindow(“PONG”,SDL\u WINDOWPOS\u居中,SDL\u WINDOWPOS\u居中,屏幕宽度,屏幕高度,显示SDL\u窗口);
//创建渲染
_rend=SDL\u CreateRenderer(\u window,-1,SDL\u RENDERER\u加速);
//让运动员起跑
_桨叶x=100;
_桨叶y=(屏幕高度/2)-100;
_桨叶w=20;
_桨叶h=200;
//列出人工智能
_ai.x=屏幕宽度-100;
_ai.y=(屏幕高度/2)-100;
_ai.w=20;
_ai.h=200;
//起球
_ball.x=屏幕宽度/2-20;
_ball.y=(屏幕高度/2)-20;
_w=20;
_球h=20;
draw();
gameloop();
}
void maingame::draw()
{
//使渲染为黑色
SDL_SetRenderDrawColor(_rend,0,0,0);
//使用我们使用SDL_SetRenderDrawColor设置的颜色清除渲染,在本例中为黑色
SDL_RenderClear(_rend);
//做下一件事,我们会把它变成白色
SDL_SetRenderDrawColor(_rend,255,255,0);
//将桨、ai和球设置为SDL_SetRenderDrawColor的颜色,在本例中为白色
SDL_RenderFillRect(_rend,&u paile);
SDL_RenderFillRect(_rend,&u ai);
SDL_RenderFillRect(_rend,&u ball);
//SDL_RenderDrawRect(_rend,&u paile);
//呈现渲染,并将其绘制到屏幕上
SDL_RenderPresent(_rend);
}
boolmaingame::loadMedia(标准::字符串路径)
{
//加载成功标志
布尔成功=真;
SDL_表面*pic;
//加载飞溅图像
pic=SDL_LoadBMP(path.c_str());
如果(pic==NULL)
{
printf(“无法加载图像%s!SDL错误:%s\n”,“在屏幕上获取图像/hello\u world.bmp”,SDL\u GetError());
成功=错误;
}
回归成功;
}
void maingame::gameloop()
{
const Uint8*keys=SDL_GetKeyboardState(NULL);
bool keydown_w=错误;
bool keydown_s=错误;
while(true)
{
while(SDL_PollEvent(&e)!=0)
{
//按X键,退出程序
如果(e.type==SDL\u退出)
{
出口(1);
}
用户输入();
}
用户输入();
draw();
}
}
void maingame::UserInput()
{
浮动lol=0;
//按了一个键!
if(e.type==SDL_KEYDOWN)
{
//按W键,移动播放器
if(e.key.keysym.sym==SDLK\u w)
{
keydown_w=真;
}
//按下S键,移动播放器
else if(e.key.keysym.sym==SDLK_s)
{
keydown_s=真;
}
}
if(e.type==SDL\u KEYUP)
{

std::cout这样做可能是不好的编程实践,但我认为解决问题的唯一方法是添加SDL_延迟()在您的gameloop函数中。我建议您将其用作临时修复,直到找到替代解决方案。希望这能有所帮助。

用浮点数替换桨高度,并将其每
用户输入移动0.1 px
?@MooingDuck抱歉,我真的不明白您的意思。哦,我明白了问题,您正在移动
SDL\u Rect
。“好吧,你应该换一个。”MooingDuck这么说,我应该把桨换成不是SDL-Rect?