3 Bat-Ball Interaction
We need the ball to bounce back up the screen when it hits the bat. When the ball hits the walls or other objects, we want it to bounce back at the same angle it hit the object, like a snooker ball against a cushion. That isn't what we want to happen when the ball hits the bat.
In order to make the game less predictable and to give the player a greater ability to direct the ball, we can make it so that the ball changes direction depending on which part of the bat it hits. In the following method, it doesn't matter which angle the ball hits the bat. What matters is where it hits the bat.
The diagram shows a bat with a ball heading towards it. When the ball makes contact with the bat, it will start moving towards the point where the dotted liines meets. Then we will send it back in exactly the opposite direction. The dotted lines show the possible directions in which the ball can be sent back towards the top of the screen.
If the dotted lines meet nearer to the bat, the ball can be sent back in more directions and the game is harder to play.
That's the theory behind the method described below. There are other ways to achieve this effect. Feel free to experiment and come up with an easier/better method.
Go to the Ball object's window and add a Collision event with obj_bat. Drag in the Move Towards icon (on the Move tab) and enter the settings as below.
The value for x points to the centre of the bat (our sprite is 64 pixels wide)
The value for y is based on finding a point below the bat. In this case we add 40 pixels to the point where the bat is. Increasing the number to say 50, should make the game easier. Decreasing the number would make it harder.
Drag in a Reverse Horizontal and a Reverse Vertical icon so that you have the following actions listed.
Save and test your work so far. Play around with the code a little if you want to. You have come a long way down the road to making a playable game.
You might want to add a wee sound event when the ball hits the bat. Go to the Sounds folder on the left of the screen and create a sound. Import a suitable noise (remember you will hear this a lot when you play, don't make it too annoying). Return to the event that you made and drag in the Play Sound icon from the Main1 tab.