I've been looking at Pong and Breakout games made with Scratch and I've noticed that sometimes when the paddle connects with the ball, the ball angles to the left and sometimes the ball angles to the right. What makes that happen and how do you control it with the paddle? Can you assign parts of the paddle to affect the trajectory of the ball one way and other parts of the paddle to affect the trajectory of the ball another way?
Gene
Offline
There are many ways to control the paddle-ball interaction, but a lot of people do the simplest thing, and have the bounce direction be random.
If you want to control based on position on the paddle, the best thing to do is probably to do tests based on the difference in the x-coordinates of the paddle and ball. To do this you probably should have a global variable that the paddle sets to its x-position, so that the ball can test it. You may need to broadcast a message to synchronize the ball and paddle scripts (update the global variable when the paddle sees itself touching the ball, then broadcast a message for the ball to bounce).
Offline
Another way to do it would be to have it detect touching the paddle with "if touching" and the color of the paddle, and then bounce differently depending on "distance to paddle," which is more on the ends of the paddle & smaller in the middle (or whereever you put the center).
I think it would probably work also to say, "if touching ( color of paddle ) : flip direction" um how would you say to flip the direction? I'm sure there's a simple way. If direction > 0 then direction -= 90, else += 90. That seems to work pretty well. I just uploaded a demonstration version.
Since my code detects the color of the paddle, you could have the paddle change colors (like from snagging powerups that come by) and have the ball react differently. Or you could even color parts of the paddle differently to make it have a range of properties.
I like how much of Scratch is based on color. It's very programmable in practice, but it has this physicality to it that makes the physics of the system both comprehensible & interesting.
<3
Offline
Thanks for all the answers. I am a right brained animator new to programming, but the "distance to center" idea sounds interesting and having the paddle multicolored sounds like a lot of fun.
Once I figure out how to have a paddle moving back and forth, I might try seeing if I can pivot it at one end so it swings like a bat.
Gene
Offline