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 will make
an obstacle move across the stage.
In the finished game, it will look like
a racer is moving to the right.
However, the racer stays in place
and only the obstacles move.
This creates the illusion that the racer is moving.
First, program the obstacle sprite
to start at a random point on the right of the screen.
Select the Obstacle sprite.
From the Motion menu, drag out a go to block.
Type in 240 for the x value in the go to block.
Click the block to see what happens.
The obstacle moves to the right of the stage,
but it always goes to the same spot.
To make it go to a random spot on the right side,
add a pick random block from the Operators menu
and place it in the y value.
Change the values to negative 180, and 180.
Click the block a few times to test it.
Great, now the obstacle sprite moves up and down
to a random spot on the right.
Next, make the obstacle move to the left.
From the Control menu, drag out a repeat until block
and add it to the bottom of the block stack.
Remember, the repeat until block
is a loop that repeats the blocks inside it
until something specific happens.
In this case, the obstacle should repeat moving to the left
until it reaches the left edge of the stage.
From the Motion menu, drag out a change x by block
and insert it into the repeat until loop.
Since the sprite needs to move left
along the stage's x-axis, change the 10 to negative 10.
The loop needs to be able to detect
when the obstacle is on the left side of the stage.
The obstacle is on the left when its X-position
is negative 240.
From the Operators menu, drag out a less than block
and add it to the repeat until loop.
Add an x position block from the Motion menu
to the first space.
In the second space, type negative 240.
The loop now reads, repeat moving negative 10 steps
until the sprite's x position is less than negative 240.
In other words, repeat moving to the left
until the sprite is on the left edge.
Test your code by clicking on these blocks.
Nice, the obstacle starts at a random position on the right
and moves towards the left.
Alright, here's the game plan.
First, make the obstacle sprite
go to a random spot on the right
using a go to block and a pick random block.
Next, make the obstacle sprite move towards the left
using a repeat until loop, a condition, and motion blocks.
In the next video, you'll program this obstacle
to copy itself forever, making more obstacles to avoid.