- Let’s remove our stars, and build a fully complete game with Crystal Catch.
- We’ll start by using our cat, and try to build a game where we’re catching the “Crystal” sprite. We’ll ultimately want crystals to fall from the sky, and for the cat to catch them before they reach the ground.
- First, we’ll add blocks for our cat to move left and right with the arrow keys:
when [left arrow v] key pressed
change x by (-10)
when [right arrow v] key pressed
change x by (10)
- Then, we’ll want our cat to start in the middle of the stage when the green flag is clicked:
when green flag clicked
go to x: (0) y: (-125)
say (Catch the crystals without letting them hit the ground!) for (4) seconds
broadcast (begin v)
- We’ll also say some instructions for our user.
- We should hide the crystal before the game starts, and then make clones of itself since we’ll want many crystals to appear. Before that, we’ll need our cat to broadcast a message, “begin”.
- Now we can tell our crystal to hide itself at the beginning, and make clones of itself when the game has started:
when green flag clicked
hide
when I receive [begin v]
create clone of (myself v)
- Then, when the crystal starts as a clone, it should show itself:
when I start as a clone
show
go to x: (0) y: (160)
forever
change y by (-2)
- After our crystal appears, it will start at the top center of the stage, and move down over and over in a forever loop.
- Notice that we’re building our game one component at a time, and we can always start our program to make sure that everything we’ve made is working so far.
- Next, our crystal will need to check if it is touching the cat:
when I start as a clone
show
go to x: (0) y: (160)
forever
change y by (-2)
if <touching (Cat v) ?> then
- We’ll make a new variable called “catches” to represent the score, and we’ll reset it in our cat’s script, since we’re doing other resetting there as well:
when green flag clicked
go to x: (0) y: (-125)
set [catches v] to (0)
say (Catch the crystals without letting them hit the ground!) for (4) seconds
broadcast (begin v)
- Now we can go back to our crystal’s script, and add blocks for what we need to do when it touches our cat:
when I start as a clone
show
go to x: (0) y: (160)
forever
change y by (-2)
if <touching (Cat v) ?> then
change [catches v] by (1)
create clone of (myself v)
delete this clone
- We’ll need to increase our “catches” variable by 1, to keep track of our score.
- Then, we need to create a new crystal, and make our original crystal delete itself.
- The new crystal will start at the top of the screen, since it’s a new clone.
- Let’s change the x value of the crystal’s position to something random, so our game has some unpredictability. The far left and far right of the stage will be some big numbers, so we’ll use negative 200 and 200:
when I start as a clone
show
go to x: (pick random (-200) to (200)) y: (160)
- Now, our crystals will appear in different spots every time.
- If we don’t catch the crystal, we might want to count the number of misses there has been so far.
- We’ll create a new variable called “misses”, which will keep track of the number of times that our cat has not caught the crystal. We’ll reset this to 0 every time the game is started, as well:
when green flag clicked
go to x: (0) y: (-125)
set [catches v] to (0)
set [misses v] to (0)
say (Catch the crystals without letting them hit the ground!) for (4) seconds
broadcast (begin v)
- Now, we can tell our crystal to check if it’s touching the ground (or edge):
when I start as a clone
show
go to x: (0) y: (160)
forever
change y by (-2)
if <touching (Cat v) ?> then
change [catches v] by (1)
create clone of (myself v)
delete this clone
end
if <touching (edge v) ?> then
change [misses v] by (1)
create clone of (myself v)
delete this clone
end
- If our crystal reaches the edge, we’ll increase the value of “misses”, and then create a new clone, and delete this clone.
- We might want a limit to the number of misses we can have, so we can have a condition to check for that:
if <touching (edge v) ?> then
change [misses v] by (1)
if <(misses) = (3)> then
broadcast (game over v)
delete this clone
end
create clone of (myself v)
delete this clone
end
- After we miss, we’ll check if the number is three. If so, we’ll broadcast a message for our cat, and then delete this clone.
- Finally, in our cat’s script, we can say our score when we receive that message:
when I receive [game over v]
say (join (Your score is) (catches)) for (5) seconds
- We can try this, and see that our program is working as we intended. We can create more crystals at a time, by having our crystal clone itself over and over:
when I receive [begin v]
forever
create clone of (myself v)
wait (15) seconds
- Now, every 15 seconds, a new crystal will be created.
- But we see that when the game ends, new crystals will still be created. So, we’ll need our cat to stop everything in our program:
when I receive [game over v]
say (join (Your score is) (catches)) for (5) seconds
stop [all v]
- With these examples, hopefully you can see how all of these components, tools, and concepts can be used to build interesting and exciting projects in Scratch.
- We encourage you to create something of your own, and share it with your friends, family and us. Thanks for joining us in Introduction to Programming with Scratch!