Scratch
Easy
60 mins
365 points
What you need:
  • Computer/laptop

Sound effects

Sound effects are a great way to improve a game or story. In this baseball game we'll add in sound effects for hitting the ball and the crowd.

This message will be visible to everyone. Send

    This code along has finished! ⌚

    You can watch it back by playing the video!

    The code along is open for everyone to join in but if you would like to earn the points and badges please join our club!

    1 - Create a new Scratch project

    Create a new Scratch project and delete the cat sprite.

    https://scratch.mit.edu

    2 - Add the Baseball 2 backdrop

    Ass the Baseball 2 backdrop from the backdrop library.


    3 - Add and code the Batter sprite

    Add the Batter sprite from the sprite library and then add the following code to make her swing bat when you press the space bar.

    when green flag clicked go to x (169) y (37) switch costume to [batter-a v] // start with this costume when [space v] key pressed switch costume to [batter-b v] wait (.1) seconds switch costume to [batter-c v] wait (.1) seconds switch costume to [batter-d v] wait (.1) seconds



    4 - Add and code the Baseball sprite

    Add the Baseball sprite from the sprite library and give it the following code to set it up for the start and make it move towards the batter.

    when green flag clicked show set size to (25) % go to x (-210) y (45) // the middle of the left hand side forever change x by (10) // make it move to the right end



    5 - Detect a hit

    When the ball touches the bat we will play a hit sound. To detect if the ball touches the bat we will use a touching color   ? block and use the colour of the bat.

    Add the following code to the Baseball sprite, underneath the change x by 10  block. 

    when green flag clicked show set size to (25) % go to x (-210) y (45) forever change x by (10) // add new code under here if < touching color (#ffc800) ? > then end end


    6 - Add some sound effects

    Now let's add our first sound effects! When the ball hits the bat we will play a "hit" sound, make the ball shoot off and then play a "crowd cheer" sound.

    Add the following code to the Baseball sprite, inside the if then block.

    when green flag clicked show set size to (25) % go to x (-210) y (45) forever change x by (10) if < touching color (#ffc800) ? > then start sound (Tennis Hit v) // add this sound from the sound library glide (1) secs to x (-250) y (170) // shoot away hide start sound (Cheer v) // add this sound from the sound library end end

    Once you've added your code, test that it works when you hit the ball (press the space bar to swing).


    7 - Add a sound effect if you miss

    Finally let's add a "ohhh" sound from the crowd if you miss the ball. We'll know that you missed if the x position of the ball is greater than 220 (see the picture with the red line for where x 220 is).

    Add another if then block under the first one with the following code.

    when green flag clicked show set size to (25) % go to x (-210) y (45) forever change x by (10) if < touching color (#ffc800) ? > then start sound (Tennis Hit v) glide (1) secs to x (-250) y (170) hide start sound (Cheer v) end if < (x position) > (220) > then play sound (Crowd Gasp v) until done // add this sound from the sound library hide stop [all v] end end

    Again, once you've added your code test that it works when you miss the ball.



    9 - Challenge!

    Well done you've added all the code for this Sound Effects project. Now as an challenge extra challenge can you add another sound into the project?

    Have a think about when the sound should play and where in the code you'll need to add it.

    8 - What's the difference?

    What is the difference between the start sound [pop] block and the play sound [pop] until done block?

    see answer

    The start sound [pop] block starts playing the sound and will immediately run any code blocks underneath it.

    The play sound [pop] until done block will play the sound until it is finished and then it will run any code blocks underneath it.

    Join our club 😃

    Create your profile, earn points & badges and learn how to code!

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

    Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more