Sprite Continuing Gliding Change Direction on Arrow Key
Tell Me Where To Go - Coordinates And Direction¶
After getting to know the basics of the environment, it is time to learn about how you can send your sprites to the desired location on the stage. To do that, you need to know how the stage is organized and which commands enable movement.
The Stage
When you open Scratch, the stage is automatically created: a white rectangle 480 dots wide and 360 dots high. The smallest dot that can be displayed on a computer graphics screen is called Pixel.
The Stage is where your stories, games and animations will run. It is stationary, like an aquarium, but its inhabitants - Sprites are always moving and interacting with each other. In order to control the sprites' movements easily, each spot on the stage is assigned an address - coordinates x and y, these coordinates represent the distance of that particular spot from the center of the stage. The spot, which is located at the center of the stage has the coordinates х=0 and у=0, or (0,0).
The coordinates allow us to accurately move our sprites around the stage and position them anywhere we want (x, y). The current position of the sprite can be seen in the active sprite's information.
Functions of the Motion Blocks
All commands, which enable positioning of the sprite in the desired location and control its direction and movement, are located in the group of blocks called Motion. In this lesson, you are going to learn more about motion blocks and how to use reporter blocks, by looking at examples and doing exercises. Reporter blocks do not correspond to language commands, and they cannot stand independently in a script. The function of a reporter block from the Motion group is to store the current coordinates and directions of Sprites.
Motion Reporters
In this group there are
and
blocks, which contain the current information on the position of the sprite (its x and y coordinates), and the
block, which represents the direction of the sprite.
If you want to see the current coordinates and the direction of the sprite, you need to click on the checkbox next to the desired block. If you click on the checkboxes next to motion reporters, displays with which you can monitor the current coordinates and direction of the sprite will appear on the stage.
Absolute and Relative Motion
You can send your sprite to a specific spot on the stage in two different ways: with absolute or relative motion.
Absolute Motion is moving to a specific spot - destination, regardless of the current position of the sprite.
In Scratch you can send your sprite to a given position (х,у) on the stage, that is to say, you can perform absolute motion by using the following blocks:
-
- go to position (х,у), -
- glide to the position (х,у), -
- set coordinate x to the position, -
- set coordinate y to the position.
By using the
block the sprite will move instantly to the given position (х,у).
Similarly, the target will be reached with the block
, but the move would not be instant; it would last a given number of seconds. The higher the number of given seconds, the longer it will take the sprite to reach its destination.
Another way to set a destination in absolute motion is to independently set the coordinates x and y, using the blocks
and
.
Relative Motion is moving to a location defined by the number of steps the sprite will make from the current position. Of course, you also need to set the direction in which the sprite should move (right, up, etc.).
Another way to set a destination in relative motion is to independently set the coordinates x and y, by using the
and
blocks.
In Scratch, you can send the sprite to a location defined by the number of steps from its current position, that is to say, you can perform relative motion by using the following blocks:
-
- move a certain amount of pixels horizontally in relation to the current position, -
- move a certain amount of pixels vertically in relation to the current position (х,у), -
- turn right a certain amount of degrees in relation to the current direction, -
- turn left a certain amount of degrees in relation to the current direction, -
- move a certain amount of steps in the given direction in relation to the current position.
Direction and Rotation
In addition to the rotation blocks
and
, which allow you to change the direction in relation to the current direction of the sprite, in Scratch, there is an option to use blocks that set the direction regardless of the current position of the Sprite.
These are
and
blocks.
On the following Figure you can find the main direction values which you can set in the input field of the first block: up (North), right (East), down (South) and left (West).
You can also set other values, for example, value 45 will point the sprite in the direction of the northeast, and 135 to southeast. To point it west, you do not need to use negative numbers. You can type in numbers from 180 to 360 instead.
The second block points the sprite towards a mouse-pointer or towards some other sprite in the project. By clicking on the white triangle in the value field, you can open the drop-down list and select towards which object you want to point your sprite. For example, in the project "Dinosaur Walk", which will be analyzed later in this lesson, the cat can be pointed towards the mouse-pointer or towards one of the three dinosaurs, which represent sprites in this project.
Study the following examples¶
As we have shown the easy way to make our sprite talk in our "Hello World" project, and then by doing exercises upgraded our project so that the sprite really pronounces the text, we will now show the basic commands which will enable us to move our sprite, and then we will upgrade them through different exercises.
Example 1 - Project "The Walk"¶
Click on the Motion blocks, then drag the
block to the Script Area and click on it. The cat will move 10 steps to the right.
Click several times on this block and take the cat to the right edge of the screen.
Clicking repeatedly on the move block has allowed the action assigned by this block, to be repeated several times. Repeating of a particular action can also be achieved by coding.
Return the cat to the middle of the screen and click the Control blocks. Differently shaped blocks from the ones you were using will appear in the Blocks Palette - C shaped blocks with a "mouth" in which you can insert other blocks.
Select the block
and drag it to the Scripts Area. Clicking on this block enables all the blocks which are inserted into it to run forever (until you stop the running of the program by clicking on the sign stop).
Insert a move block into the "mouth" of the repeat forever block and click on them. The cat will go off-screen again.
There is a way to keep the movement of the sprite inside the borders of the screen. That would be to use the block with the command if on edge, bounce. This block is located in Motion blocks.
Stop the running of the blocks by clicking the stop sign, and then drag the block
to the Scripts Area and insert it into the "mouth" of the repeat forever block, below the move block.
By running this changed script, the cat will constantly move from one edge to the other, but when it moves to the left, it will be facing the wrong way. Of course, there is a way to fix this. One is to change the way the sprite is moving in the Sprite Information, and the other is to use one of the rotation blocks.
Drag the block
from the Motion blocks and place it above the repeat forever block. Make sure that the left-right rotation mode is selected from the drop-down list of this block.
Put the
block at the top of the Script, and by doing this you have completed the "Walk" project.
You can now run the project by clicking on the green flag and stop it by clicking on the stop sign. Save the project and continue exploring.
In our next project, we will show how you can introduce new sprites and backdrops, and how to guide a sprite by using the mouse-pointer. Therefore, before you move on to this example, watch the tutorials Add a Sprite and Add a Backdrop.
Example 2 – "The Dinosaur Walk"¶
In the previous example, we used the repeat forever block to make the cat sprite move continuously between the edges of the screen until we stop the running of the project by clicking the stop sign. In this project, we will have four sprites, and each of them will have their own scrip, which will determine their behavior. The cat will follow the mouse-pointer forever, and the remaining three sprites - the dinosaurs, will forever be directed towards the cat. The look of the stage at the beginning of the running of the project is presented in the following Figure.
Creation of the Project
Click on the icon for choosing backdrops and choose the Jurassic backdrop from the backdrop library.
Choose the sprites Dinosaur1, Dinosaur2 and Dinosaur3 from the Sprites library.
Place your sprites like they are on the Figure above. Yo need to change the direction of the Dinosaur2. The default direction value for all sprites is 90 о (they are looking to the right) and their rotation style is All Around. All of these settings can be changed in the Sprite Information part or by using appropriate blocks to form scripts added to a particular sprite. In this project, we will use the first option.
In the Sprite information window set the rotation style as follows: Dinosaur1 - don't rotate, Dinosaur2 - left/right, Dinosaur3 - all around.
To all dinosaurs add the same script, which will command them to point towards the cat during the running of the entire project. However, they will behave differently because they don't have the same rotation style in their Sprite information settings.
For the cat sprite you need to set the commands, which will enable it to point towards the coordinates of the mouse-pointer, that is, to move across the stage the same way the user moves the mouse.
The Scrips describing the behavior of the dinosaurs and the cat are presented in the following Figure.
Run the project and move the mouse across the stage. Note that the dinosaurs are tracking its movements in different ways.
Example 3 – "Linear Motion"¶
Study the Use the Arrow Keys tutorial, and create a project where the cat is led around the stage with the keyboard.
Add the
block to the cat.
Choose the key Right Arrow.
Choose the
block from the Motion blocks and connect it to the previous block.
Choose the
block from the Motion blocks and connect it to the previous block.
Press the right arrow key on your keyboard a few times. What happens?
Duplicate this script (right-click on the first block, then select Duplicate).
In the new script, replace the right arrow with the left arrow, in the block point in direction instead of 90 choose -90.
Press the left arrow key on your keyboard a few times. What happens?
Similarly, make two more scripts: to guide the cat 10 steps up by pressing the up arrow key, or 10 steps down by pressing the down arrow key.
Example 4 – "Motion with a Turn"¶
We are going to create one more project for moving sprites by using a keyboard, but with modified functions of the arrow keys. We will remove the blocks for direction, and we will join the left arrow key and right arrow key with the commands which rotate the sprite 15 degrees to the left or to the right. Also, we will join the up arrow key and the down arrow key with the block move 10 steps , i.e. go -10 steps . The corresponding scripts will have the following layout.
Run the project and test how you can manage sprite's movement.
Source: https://petlja.org/biblioteka/r/lekcije/BlockBasedProgScratchPracticeEng/scratch3-coordinates
Post a Comment for "Sprite Continuing Gliding Change Direction on Arrow Key"