我使用 SDL 创建了一个程序,其中一个矩形在程序的墙壁内不断碰撞,但碰撞检查无法正常工作。
这是代码:`
int main(int argc, char *argv[]){
//variable Initialization]
width = height = 45;
srcX = srcY = 0;
destY = destX = 0;
vlc = 1;
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Bouncing Balls","./ball.jpg");
backg = IMG_Load("./back.png");
ball = IMG_Load("./ball.jpg");
while (checkBounce){
//Increase velocity
destX += vlc;
destY += vlc;
//Collision Checking
if (destX < 0){
destX = 0;
vlc = -vlc;
destX += vlc;
}
if (destY < 0){
destY = 0;
vlc = -vlc;
destY += vlc;
}
if (destY + height > 480){
destY = 480 - height;
vlc = -vlc;
}
if (destX + width > 640){
destX = 640 - width;
vlc = -vlc;
}
if (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT)
checkBounce = false;
}
//Applying Surfaces
applySurface(0, 0, backg, screen);
applyBall(srcX, srcY, destX, destY, width, height, ball, screen);
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}
这是 gif 图片发生了什么:Bouncing Rectangle.gif