class Pong {
public:
Pong(int speed) {
gSpeed = speed;
gPongBG = SDL_LoadBMP("pongBG.bmp");
gPongBGSurf = gPongBG;
gPongRect.w = 800;
gPongRect.h = 460;
gPongRect.x = 700;
gPongRect.y = 220;
gPongPlayer = SDL_LoadBMP("pongPlayer.bmp");
gPongPlayerRect.h = 50;
gPongPlayerRect.w = 10;
gPongPlayerRect.x = 50;
gPongPlayerRect.y = 0;
}
~Pong() {
}
void drawPong() {
gPongBGSurf = gPongBG;
SDL_BlitSurface(gPongBGSurf, NULL, gScreenSurface, &gPongRect);
SDL_BlitSurface(gPongPlayer, NULL, gPongBGSurf, &gPongPlayerRect);
}
void movePlayer() {
gPongPlayerRect.y++;
}
以下代码使得gPongPlayerRect自己制作了多个副本,而不是像我计划的那样移动它。稍后在代码中,我更新名为gWindow的主窗口,主窗口的表面是wScreenSurface。如果我将播放器直接滑到窗口表面上,它会移动,所以我猜问题是旧的gPongBGSurf表面保持更新。我怎么能最终解决这个问题?谢谢!
答案 0 :(得分:1)
我的猜测是你忘了擦除Pong Surface:
Uint32 black= SDL_MapRGBA(gPongBGSurf->format,0,0,0,255);
SDL_FillRect(gPongBGSurf, NULL, black);
SDL_BlitSurface(gPongPlayer, NULL, gPongBGSurf, &gPongPlayerRect);
SDL_BlitSurface(gPongBGSurf, NULL, gScreenSurface, &gPongRect);
有关SDL2游戏的完整示例,其中多个表面相互重叠,然后在屏幕表面上,您可以阅读Rock Dodger CE的小源代码,这只是一个文件。