Welcome to Stratos!
////////////////////////////////////////////////////////////////////////////////// Receives: RECT describing current position of ball/// RECT describing brick or paddle being tested for interesection/// Returns: EIntersect describing how ball rect intersected rectangle///////////////////////////////////////////////////////////////////////////////EIntersect CSuperBrickBreaker::BallRectIntersect(RECT p_ball_rect, RECT p_rect){ bool l_contains_top_left = false; bool l_contains_top_right = false; bool l_contains_bot_left = false; bool l_contains_bot_right = false; // check if top-left corner of the ball is in the rectangle l_contains_top_left = ( (p_ball_rect.top >= p_rect.top) && (p_ball_rect.top <= p_rect.bottom) && (p_ball_rect.left >= p_rect.left) && (p_ball_rect.left <= p_rect.right) ); // check if top-right corner of the ball is in the rectangle l_contains_top_right = ( (p_ball_rect.top >= p_rect.top) && (p_ball_rect.top <= p_rect.bottom) && (p_ball_rect.right >= p_rect.left) && (p_ball_rect.right <= p_rect.right) ); // check if bottom-left corner of the ball is in the rectangle l_contains_bot_left = ( (p_ball_rect.bottom >= p_rect.top) && (p_ball_rect.bottom <= p_rect.bottom) && (p_ball_rect.left >= p_rect.left) && (p_ball_rect.left <= p_rect.right) ); // check if bottom-right corner of the ball is in the rectangle l_contains_bot_right = ( (p_ball_rect.bottom >= p_rect.top) && (p_ball_rect.bottom <= p_rect.bottom) && (p_ball_rect.right >= p_rect.left) && (p_ball_rect.right <= p_rect.right) ); if ( (l_contains_top_left) && (l_contains_top_right) ) { return(IS_BOTTOM); } else if ( (l_contains_top_right) && (l_contains_bot_right) ) { return(IS_LEFT); } else if ( (l_contains_bot_right) && (l_contains_bot_left) ) { return(IS_TOP); } else if ( (l_contains_bot_left) && (l_contains_top_left) ) { return(IS_RIGHT); } else if (l_contains_top_left) { // only the top left corner 'penetrated' the rectangle // if the top-left corner is farther from the bottom of the // rectangle than the right side, assume the ball interected // the right if ( (p_rect.bottom - p_ball_rect.top) >= (p_rect.right - p_ball_rect.left) ) { return(IS_RIGHT); } else { return(IS_BOTTOM); } } else if (l_contains_top_right) { // only the top right corner 'penetrated' the rectangle // if the top-right corner is farther from the bottom of the // rectangle than the left side, assume the ball interected // the left if ( (p_rect.bottom - p_ball_rect.top) >= (p_ball_rect.right - p_rect.left) ) { return(IS_LEFT); } else { return(IS_BOTTOM); } } else if (l_contains_bot_right) { // only the bottom right corner 'penetrated' the rectangle // if the bottom-right corner is farther from the top of the // rectangle than the left side, assume the ball interected // the left if ( (p_ball_rect.bottom - p_rect.top) >= (p_ball_rect.right - p_rect.left) ) { return(IS_LEFT); } else { return(IS_BOTTOM); } } else if (l_contains_bot_left) { // only the bottom left corner 'penetrated' the rectangle // if the bottom-left corner is farther from the top of the // rectangle than the right side, assume the ball interected // the right if ( (p_ball_rect.bottom - p_rect.top) >= (p_rect.right - p_ball_rect.left) ) { return(IS_RIGHT); } else { return(IS_BOTTOM); } } return(IS_NONE);}
for blx in pygame.sprite.groupcollide(ballsprite,blocksprites,0,1): if blx.rect.collidepoint(bola.rect.topleft) and blx.rect.collidepoint(bola.rect.topright): bola.rebound1() elif blx.rect.collidepoint(bola.rect.bottomleft) and blx.rect.collidepoint(bola.rect.bottomright): bola.rebound1() elif blx.rect.collidepoint(bola.rect.topleft) and blx.rect.collidepoint(bola.rect.bottomleft): bola.rebound2() elif blx.rect.collidepoint(bola.rect.bottomright) and blx.rect.collidepoint(bola.rect.topright): bola.rebound2() elif blx.rect.collidepoint(bola.rect.bottomright): if (bola.rect.bottom - blx.rect.top) >= (bola.rect.right - blx.rect.left): bola.rebound1() else: bola.rebound2() elif blx.rect.collidepoint(bola.rect.bottomleft): if (bola.rect.bottom - blx.rect.top) >= (blx.rect.right - bola.rect.left): bola.rebound1() else: bola.rebound2() elif blx.rect.collidepoint(bola.rect.topleft): if (blx.rect.bottom - bola.rect.top) >= (blx.rect.right - bola.rect.left): bola.rebound1() else: bola.rebound2() elif blx.rect.collidepoint(bola.rect.topright): if (blx.rect.bottom - bola.rect.top) >= (bola.rect.right - blx.rect.left): bola.rebound1() else: bola.rebound2()