Paddle!
{Introduction }
Paddle is a 2 player variation of the famous pong game!
{Step 1}
Let’s start by making the ball. Use set mySprite to sprite to create a ball sprite. Change the variable name of mySprite to ball.
{Step 2}
Put in code to set ball velocity to vx of 100
and vy of 100
. Also, set ball bounce on wall.
{Step 3}
Use some more code to set the y of ball with pick random to get a some value between 0
and 120
.
{Step 4}
Create a function called create_ball
and move the code you added in the previous
steps from on start into this function. Now, call create_ball in on start.
{Step 5}
Let’s work on the left paddle. Add a new set mySprite to sprite to on start and rename the variable to left_paddle. Change the kind to LeftPaddles.
{Step 6}
Make the left_paddle move up and down using
move left_paddle with buttons. Use the velocity of 0
for vx and 150
for vy.
{Step 7}
Add code to set left_paddle left to a position of 0
.
Also, you want to set left_paddle stay in screen to keep the paddle from disappearing.
{Step 8}
Create a function called create_left_paddle
and move the code you added to the on start into this function. Now, add a call create_left_paddle in on start.
{Step 9}
Create another function called create_right_paddle
. This will have the code for the right_paddle.
Copy all of the code from the create_left_paddle
function and put it in this new
function. Change all of the left_paddle variables in the function to
right_paddle. Now, add a call create_right_paddle in on start.
{Step 10}
Let’s make some more changes inside of create_right_paddle
. Set the kind of the paddle sprite to RightPaddles. Change the position setting of the paddle from left to
right and set the value to 160
. To make this a 2 player game, replace the
move right_paddle code with a multiplayer player 2 move for
right_paddle. Use the same values for vx and vy
{Step 11}
Make sure that you call 3 functions, call create_ball, call create_left_paddle, and call create_right_paddle in the on start.
{Step 12}
Add an event that runs code for when ball overlaps with left_paddle. This happens for the case when on sprite of kind Player overlaps otherSprite of kind LeftPaddles.
{Step 13}
Inside the event, set the inverse of the horizontal speed for sprite to simulate the bounce on the paddle. This happens if you set sprite vx (velocity x) to the negative value of the horizontal speed, which is sprite vx (velocity x) multiplied by -1
. Also, change score by 1
for the player using the left_paddle.
{Step 14}
Copy the event and all its code from the previous the step. In this event, change the otherSprite kind from LeftPaddles to RightPaddles. Replace the change score by with the multiplayer change player 2 score by for the player using the right_paddle. Keep the same score increment of 1
.