Are you interested in creating your own games, launching your own apps, or even building your own robot? You can do that with coding! It’s actually much easier than you might think. During our Code From Scratch workshop, we’ll be releasing weekly coding tutorials for you to try out at home. Each tutorial will explain the project in simple terms and provide step-by-step instructions on how to finish the project. During our first few projects, we’ll be coding with apps that can be found on Windows, Mac, and Linux systems like the Raspberry Pi, but eventually we’ll be creating real world projects with breadboards, wiring, LEDs, motors, and more!
For this weeks Code From Scratch tutorial, we’re going to be creating a Halloween themed game called Candy Catch!
To follow along with this tutorial, open up a new project using the button below…
We’re going to break down this project into a few simple steps…
Goal #1: Create Sprites & A Background
Goal #2: Move The Candy Bowl Sprite
Goal #3: Make The Candy Appear
Goal #4: Make The Candy Fall
Goal #5: Make The Bowl Catch The Candy
Goal #6: Score Points For The Candy
For Goal 1 of the project, we need to create the graphics for our game. To do this, we’re going to delete the cat that Scratch adds to every project. Once the cat is gone, we can select new sprites to represent our Candy & Candy Bowl. We will also pick a background.
Select Delete Sprite: Click on the Trash Can icon to delete the old sprite (the cat).
Select Choose A Sprite: By hovering your mouse over top of the “Choose A Sprite” button (which looks like a blue circle with a cat icon on it), you can either choose a pre-made sprite (by selecting the magnifying glass icon) or draw your own sprite (by selecting the paint brush). For this tutorial, you need to select two sprites: The Bowl and The Candy. You can change the size of these sprites by adjusting the “Size” number in the Sprite Selection area.
Select Choose A Backdrop: By hovering your mouse over top of the “Choose A Backdrop” button (which looks like a blue circle with a picture frame icon on it), you can either choose a pre-made backdrop (by selecting the magnifying glass icon) or draw your own backdrop (by selecting the paint brush).
In this section we show you how to code your bowl. We want the player to be able to move the bowl left and right by clicking the arrow keys on their keyboard…
Select the Bowl sprite: By clicking on the Bowl sprite, you will be able to add code to it.
Select When Green Flag Clicked: Drag over the “When [Green Flag] Clicked” block (Events)
Select Forever: Drag & connect the “forever” block (Control).
Select If () Then (): Drag and connect the “if () then ()” block (Control) in between the two orange bars of the “Forever” loop that we added in the previous step.
Select Key (Space) Pressed: Drag and drop the “key (space) pressed” block (Sensing) into the dark orange space of the “If () Then ()” code block that we added during the previous step. Use the dropdown menu to select right arrow. The code block should now say “Key (Right Arrow) Pressed”. The two code blocks should now read “If Key (Right Arrow) Pressed, Then…”.
Select Change X By (): Drag & connect the “change x by ()” block (Motion) in between the two orange bars of the “If () Then ()” block.
Select If () Then (): Drag and connect the “if () then ()” block (Control) in between the two orange bars of the “Forever” loop, but underneath the “If () Then ()” code block that we added in the previous step (which contains our right arrow code).
Select Key (Space) Pressed: Drag and drop the “key (space) pressed” block (Sensing) into the dark orange space of the “If () Then ()” code block that we added during the previous step. Use the dropdown menu to select left arrow. The code block should now say “Key (Left Arrow) Pressed”. The two code blocks should now read “If Key (Left Arrow) Pressed, Then…”.
Select Change X By (): Drag & connect the “change x by ()” block (Motion) in between the two orange bars of the “If () Then ()” block. Change the (10) to (-10).
In this section we show you how to make candy appear at the top of the screen…
Select the Candy sprite: By clicking on the Candy sprite, you will be able to add code to it.
Select When Green Flag Clicked: Drag over the “When [Green Flag] Clicked” block (Events)
Select Go To Random Position: Drag over the “Go To (Random Position)” block (Motion)
Select Set Y To 0: Drag over the “Set Y To (0)” block (Motion). Change the number to 180.
In this section we show you how to make your candy fall from the top of the screen. We also have code to teleport candy back to the top of the screen after it either hits the ground or gets caught by the bowl. By teleporting candy back to the top of the screen after it reaches the bottom, we only need to code one sprite…
Select the Candy sprite: By clicking on the Candy sprite, you will be able to add code to it.
Select When Green Flag Clicked: Drag over the “When [Green Flag] Clicked” block (Events)
Select Forever: Drag & connect the “forever” block (Control).
Select Change Y By (): Drag & connect the “change y by ()” block (Motion) in between the two orange bars of the Forever block. Change the (10) to (-5).
Select If () Then (): Drag and connect the “if () then ()” block (Control) in between the two orange bars of the “Forever” loop, but underneath the “Change Y By -5” block that we added in the previous step.
Select () < (): Drag & drop the “() < ()” block (Operators) into the dark space inside the “If () Then ()” block. Change the number in the white space on the right side of the block to “-170”.
Select Y Position: Drag & drop the “Y Position” block (Motion) into the white space on the left side of the green “() < ()” operator block we added in the previous step.
Select Go To Random Position: Drag over the “Go To (Random Position)” block (Motion)
Select Set Y To 0: Drag over the “Set Y To (0)” block (Motion). Change the number to 180.
In this section we add code to let the bowl actually catch the candy…
Select the Candy sprite: By clicking on the Candy sprite, you will be able to add code to it.
Select When Green Flag Clicked: Drag over the “When [Green Flag] Clicked” block (Events)
Select Forever: Drag & connect the “forever” block (Control).
Select If () Then (): Drag and connect the “if () then ()” block (Control) in between the two orange bars of the “Forever” loop.
Select Touching (): Drag and drop the “Touching ()” block (Sensing) into the dark orange space of the “If () Then ()” block. Use the drop down menu to select the name of your bowl sprite.
Select Start Sound (): Drag and connect the “Start Sound ()” block (Sound) in between the orange bars of the “If () Then ()” block. Use the drop down menu to select the name of your sound.
Select Go To Random Position: Drag over the “Go To (Random Position)” block (Motion)
Select Set Y To 0: Drag over the “Set Y To (0)” block (Motion). Change the number to 180.
In this section we add code that will keep track of how many candies we caught and add that number to the scoreboard…
Select the Candy sprite: By clicking on the Candy sprite, you will be able to add code to it.
Make A Variable called Score: In the panel on the left, go to the Variables section and click “Make A Variable” – call the new variable “Score” (For All Sprites).
Select Change My Variable By 1: Drag & connect the “Change my variable by (1)” block (Variables) in between the orange bars of the “If () Then ()” that we created in our last step (add it right above the “Start Sound” block).
Select Set My Variable To 0: Drag & connect the “Set My Variable To 0” block (Variables) underneath the “When (Green Flag) Clicked” code block at the top of the script we created in the previous step.
Congratulations! If you made it this far, you’ve successfully created your own Candy Catch game!
Visit us every other week for a new beginner-friendly coding tutorial!
Part 3 of our "Unity Game From Scratch" tutorial. This week we'll look at a few commands you can experiment with to add new mechanics to your game!
This is Part 2 of our simple guide to teach you the basics of the Unity game engine. This week we'll be looking at some code that you can add to your game!
Follow this easy guide to learn the basics about the Unity game engine. Over this two part tutorial, we'll be creating a simple game from scratch!
Today we're going to be learning about the Unity game engine. By loading up the Lego Microgame that comes with Unity, we'll learn the basics about this game engine!