Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

2. Move the Sprite

Transcript

In this video, you will use events to move the player one sprite when the arrow keys are pressed.

To start, click on the player one sprite.

In this game, the user needs to be able to move the sprite forward and turn it left or right.

To find blocks that move the sprite, click on motion.

If you're not sure which blocks to choose, click on a few to see what they do.

For example, if you click on move ten steps, you see that it moves the sprite forward, so you can choose to add it to the program.

Clicking on the turn blocks, turns the sprite.

So drag both of those out as well.

You now have three blocks in your program.

But the only way to run them is to click on them.

That doesn't make for a very exciting game.

To fix this, use event blocks to run this code when the user presses the arrow keys.

Click on the events menu.

Event blocks tell the computer when to run code.

You can use an event block that would run code when the green flag is clicked or when this sprite is clicked.

But for this game, you will use a one key pressed block.

This block has a drop down menu that shows every key press option.

This sprite should move forward when you press the up arrow.

So select up arrow and attach this block to the move ten steps block.

Test your code by pressing the up arrow.

Great.

Now this sprite moves forward when the up arrow is pressed.

You can add more key press events to turn the sprite clock-wise with the right arrow, and counter clock-wise with the left arrow.

If your sprite ends up moving the wrong way when you test your code, don't worry.

Just go back to the code and try adding different events until it moves the way you want it.

Remember, an important part of computer science is sticking with the problems and trying different solutions until you find the right one.

Perfect.

Now it's your turn.

Click on the player one sprite using a key press event for up, left, and right arrow keys.

Then, add a movement block for each event.

arrow_backward Back
Next arrow_forward
Instructions
  1. Create an event for the three arrow presses.
  2. Add a "move" block to each arrow-press event.