Today, we're going on an underwater adventure with a friendly crab. We're going to create a fun game where a crab will chase your mouse cursor around the screen.
You'll learn how to add a new sprite, animate it, and get it to chase your mouse cursor. So, let's dive in and start coding!
Open the Scratch website in a new tab and create a new project. Delete the cat sprite from the project.
To create a new Python project for a Microbit, open the website python.microbit.org.
This will open the code editor with a new project. It might have some example code already added such as:
# Imports go at the top
from microbit import *
# Code in a 'while True:' loop repeats forever
while True:
display.show(Image.HEART)
sleep(1000)
display.scroll('Hello')
You should delete this code except for the import
line that you will need. This imports the necessary libraries you will need to code a microbit.
# Imports go at the top
from microbit import *
In this project we are going to make a crab chase after our mouse cursor.
Let's add the Crab sprite to our project from the sprite library.
To add a sprite from the sprite library follow these steps:
You can use search box or the filter links (Animals, People, Fantasy etc) to locate your sprite.
In this step, we will make the crab point towards your mouse cursor. The mouse cursor is the arrow or pointer that moves around when you move your mouse. Let's get started!
Add the following Scratch code to your crab sprite: when green flag clicked
forever
point towards [mouse-pointer v]
end
Now, we are going to make the crab walk towards your mouse cursor! Just like how you keep walking until you reach your destination, the crab will keep walking because it is inside the forever
block.
Add the move (10) steps
block inside the forever
block:: when green flag clicked
forever
point towards [mouse-pointer v]
move (10) steps //add this block
end