views:

289

answers:

3

Hi there everyone!
I'm making a Pygame of, basically, "Breakout".
I'm using collisions, and want a simple way to have different bounce effects of the different sides of one rectangle.
What I currently have for the ball-to-bat collision is this:
"dot" = ball; "bat" = bat; so you all understand.

if dot.rect.colliderect(bat.rect):<br>
 dot.dy *= -1

I'd like something that interacts with each side, so could reverse the self.dx value of the ball when it hits the side of the bat, and only reversing the self.dy value when it strikes the top.

Thanks!!! :D

A: 

What you want is a second check (within the if) that checks if the dot's Y coordinate is the same as the bat's Y coordinate (that is, the ball hit the bat while the ball was at the same height as the bat, which means it must have hit from the side), then reverse the X velocity; otherwise, you have hit the bat on the top so reverse the Y velocity.

It's not exactly right, but that'll get you going.

McWafflestix
A: 

THANKS! It didn't take much time at all. For anyone else, here's the code I used as a guideline:

if dot.rect.colliderect(bat.rect): if 570 < dot.rect.centery < 610 and (bat.rect.centerx - 75) < dot.rect.centerx < (bat.rect.centerx + 75): dot.dx *= -1 else: dot.dy *= -1

Works great. Only one problem: With Python collisions, the objects go into other objects a tiny bit before reacting to the collision. So you can't set the limits on the if statement to the exact measurement, because if it hits the top with those EXACT measurements, it will set off the first if statement and royally screw up. But this works great for being simple.

Thanks again McWafflestix! :D

A: 

Thankyou both! You rock! :D