我正在使用 SDL 在 C++ 中创建一个 pong 克隆。桨已准备好并根据用户输入移动。让我担心的是输入很奇怪,我预计两个桨叶都能平稳移动,但是当另一个桨叶移动时一个桨叶会滞后,而当另一个桨叶不动时会加速。我怀疑这是由于没有限制 FPS 还是由于我设法获得多个输入或两者兼而有之的奇怪方式。谁能告诉我我的代码有什么问题以及如何改进它?
我的代码:
#include <SDL/SDL.h>
#include <string>
#include "SDL/SDL_image.h"
#include <SDL/SDL_ttf.h>
bool quit = false;
bool keyheld[323]={false};
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
int paddle1x = 0;
int paddle1y = 50;
int paddle2x = 600;
int paddle2y = 500;
SDL_Color color = {255,255,255};
SDL_Event event;
SDL_Surface* screen = NULL;
SDL_Surface* timer = NULL;
SDL_Surface* background = NULL;
SDL_Surface* paddle1 =NULL;
SDL_Surface* paddle2 = NULL;
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL,destination, &offset);
}
SDL_Surface *load_image(std::string filename)
{
SDL_Surface* loadedimage = NULL;
SDL_Surface* optimisedimage = NULL;
loadedimage= IMG_Load(filename.c_str());
optimisedimage= SDL_DisplayFormatAlpha(loadedimage);
SDL_FreeSurface(loadedimage);
return optimisedimage;
}
int main(int argc, char *args[])
{
bool quit = false;
bool keyheld[323]={false};
int p1score=0;
int p2score=0;
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
TTF_Font *font=NULL;
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
background = load_image("background.png");
apply_surface(0,0,background,screen);
paddle1 = load_image("paddle1.png");
apply_surface(paddle1x,paddle1y,paddle1,screen);
paddle2=load_image("paddle2.png");
apply_surface(paddle2x,paddle2y,paddle2,screen);
font = TTF_OpenFont("kremlin.ttf", 22);
timer = TTF_RenderText_Solid(font,"Test!",color);
apply_surface(0,0,timer,screen);
SDL_WM_SetCaption("PONG!",NULL);
SDL_Flip(screen);
while(quit==false)
{
if(SDL_PollEvent(&event))
{
SDLKey keyPressed = event.key.keysym.sym;
if(event.type==SDL_QUIT)
{
return 0;
}
else if(event.type==SDL_KEYDOWN)
{
keyheld[keyPressed] = true;
}
else if(event.type==SDL_KEYUP)
{
keyheld[keyPressed] = false;
}
}
if(keyheld[SDLK_LEFT])
{
paddle1x--;
apply_surface(0,0,background,screen);
apply_surface(paddle1x,paddle1y,paddle1,screen);
apply_surface(paddle2x,paddle2y,paddle2,screen);
}
if(keyheld[SDLK_RIGHT])
{
paddle1x++;
apply_surface(0,0,background,screen);
apply_surface(paddle1x,paddle1y,paddle1,screen);
apply_surface(paddle2x,paddle2y,paddle2,screen);
}
if(keyheld[SDLK_a])
{
paddle2x--;
apply_surface(0,0,background,screen);
apply_surface(paddle1x,paddle1y,paddle1,screen);
apply_surface(paddle2x,paddle2y,paddle2,screen);
}
if(keyheld[SDLK_d])
{
paddle2x++;
apply_surface(0,0,background,screen);
apply_surface(paddle1x,paddle1y,paddle1,screen);
apply_surface(paddle2x,paddle2y,paddle2,screen);
}
apply_surface(0,0,timer,screen);
SDL_Flip(screen);
SDL_Delay(2);
}
return 0;
}