Paddle!

{Introduction }

Paddle is a 2 player variation of the famous pong game!

A ball bouncing on paddles

{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.

Playerspriteof kindsetballtoon start

{Step 2}

Put in code to set ball velocity to vx of 100 and vy of 100. Also, set ball bounce on wall.

Playerspriteof kindball100100ballONsetbounce on wallsetvelocity to vxvysetballtoon start

{Step 3}

Use some more code to set the y of ball with pick random to get a some value between 0 and 120.

Playerspriteof kindball100100ballONball0120pick randomtosetytosetbounce on wallsetvelocity to vxvysetballtoon start

{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.

functioncreate_ballPlayerspriteof kindball100100ballONball0120pick randomtosetytosetbounce on wallsetvelocity to vxvysetballtocallcreate_ballon 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.

LeftPaddlesspriteof kindsetleft_paddletoon start

{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.

LeftPaddlesspriteof kindleft_paddle0150movewith buttonsvxvysetleft_paddletoon start

{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.

LeftPaddlesspriteof kindleft_paddle0150left_paddle0left_paddleONsetstay in screensetlefttomovewith buttonsvxvysetleft_paddletoon start

{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.

functioncreate_left_paddleLeftPaddlesspriteof kindleft_paddle0150left_paddle0left_paddleONsetstay in screensetlefttomovewith buttonsvxvysetleft_paddletocallcreate_left_paddleon 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.

functioncreate_right_paddleLeftPaddlesspriteof kindright_paddle0150right_paddle0right_paddleONsetstay in screensetlefttomovewith buttonsvxvysetright_paddletocallcreate_right_paddleon 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

functioncreate_right_paddleRightPaddlesspriteof kindright_paddle0150right_paddle160right_paddleONsetstay in screensetrighttoplayer2movewith buttonsvxvysetright_paddletocallcreate_right_paddleon start

{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.

functioncreate_ballPlayerspriteof kindball100100ballONball0120pick randomtosetytosetbounce on wallsetvelocity to vxvysetballtofunctioncreate_left_paddleLeftPaddlesspriteof kindleft_paddle0150left_paddle0left_paddleONsetstay in screensetlefttomovewith buttonsvxvysetleft_paddletofunctioncreate_right_paddleRightPaddlesspriteof kindright_paddle0150right_paddle160right_paddleONsetstay in screensetrighttoplayer2movewith buttonsvxvysetright_paddletocallcreate_ballcallcreate_left_paddlecallcreate_right_paddleon 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.

PlayerLeftPaddlesspriteotherSpriteonof kindoverlapsof kind

{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.

PlayerLeftPaddlesspriteotherSpritespritespritevx (velocity x)-1×1change score bysetvx (velocity x)toonof kindoverlapsof kind

{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.

PlayerRightPaddlesspriteotherSpritespritespritevx (velocity x)-1×1changeplayer2score bysetvx (velocity x)toonof kindoverlapsof kind