绘制同一个矩形的多个实例而不是移动它
Drawing multiple instances of the same rect instead of moving it
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 的 main window,而 main window 的表面是 wScreenSurface。如果我直接将播放器 blit 到 Window 表面,它会移动,所以我猜问题是旧的 gPongBGSurf 表面即使更新了也会保持不变。我怎么能最终解决这个问题?谢谢!
我的猜测是你忘记擦除 Pong 表面:
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 的小源代码,它只有一个文件。
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 的 main window,而 main window 的表面是 wScreenSurface。如果我直接将播放器 blit 到 Window 表面,它会移动,所以我猜问题是旧的 gPongBGSurf 表面即使更新了也会保持不变。我怎么能最终解决这个问题?谢谢!
我的猜测是你忘记擦除 Pong 表面:
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 的小源代码,它只有一个文件。