Please update your browser
Your browser version is not supported by this site. CS First may not work properly. Learn more.
In this video you'll create
a variable to hold the player's score,
and program the enemies to clone
each time a point is scored.
Variables store information like numbers and letters.
Once a value is stored in a variable
you can add to it, subtract from it,
and use it in many different places
while only needing to update the variable in one spot.
To start, click on your player sprite,
click make a variable.
Name the variable something that
makes sense for your program.
This example names it score.
Then, select the option to make
the variable for all sprites.
Now that you've created a place to store the score,
you need a way to add it.
The player will get a point if
the sprite touches the finish line.
To make that happen, go to the control menu
and select an if statement.
Then, click the sensing menu,
adding touching condition
and change the value to finish line.
Now this reads, "If touching finish line, then--"
If the sprite touches the finish line
the score should go up by one point.
Add a change score by one block to the if statement.
To make the sprite check if it's touching
the finish line for the entire game,
adding one flat clicked block to the top of the code,
then, from control, add a forever loop.
Test your code.
Cool, the score variable goes up
by one when the player touches the finish line.
To reset the score at the start of the game,
add a set score to zero block
to the beginning of the stack.
Try the game now.
There's a bug in the code.
There are enemies on the stage,
but they don't stop the player
from getting to the finish line.
To fix this, program the sprite
to stop moving if it touches the finish line
or if it's touching the enemy.
To do that, add an or operator
to to the repeat until block,
and add a touching block.
Change the drop down to your enemy in the touching block.
This statement now reads, "When space key pressed,
"repeat moving forward until touching
"the finish line or touching the enemy,
"then go back to the start."
Cool, a lot of games start out easy to paly
and get more difficult as the game goes on.
To make this game get harder as it's played,
start the game with one enemy
and clone the enemy each time the player scores a point.
Click control and place a create clone block
after the change score by one block.
In the drop down menu select the name of the enemy.
Then, select the enemy sprite
and remove a repeat block
so that only one clone is created
at the beginning of the game.
Test your code now.
Great, a new enemy clone appears
each time you score a point.
Ask your neighbor to try your game
and see how many points they can score.
Now it's your turn.
Create a score variable,
then program the sprite to earn a point
then stop moving when it touches the finish line.
Program the sprite to stop moving if it touches an enemy,
and make your game harder by creating
an enemy clone each time a point is scored.