📜 ⬆️ ⬇️

World expansion in Scratch

In the Scratch programming environment, the scope is limited to the scene. The scene gives you the opportunity to look at the world within x from -240 to 240. And from y from -180 to 180. But what to do when this space is not enough? How to remove this restriction?




Let's start by exploring the possibilities. It seems everything seems simple. Our ideal end result is to put our sprite in the position we need and then move towards it, or it should move towards us.


Let's explore the possibilities of our programming language. To set our sprite to the point 1000; 1000 we need to use the command “ go to x: 1000 y: 1000 ”. let's try. Run the command (by clicking once on it) and get the following result.



Kitty can stand at x = 271 and y = 215 . Wherever we put our sprite, it cannot escape further into the scene. Wow restriction! What to do?


Now we use an interesting trick. The world that falls into the area of ​​the scene we call the physical world. It is real, and we fully see it. Everything that should happen in the game takes place in sight and there is nothing more behind the edge of the stage. All sprites here and now. Nothing exists beyond the borders.


The physical world - a world limited scene Scratch

The physical world - all objects on the Scratch scene

But in order to expand it, we need another world, let's call it “virtual”. It is invisible, more precisely, we see the part that falls into the area of ​​the scene.


How do we imagine this world? The virtual world is represented as a large piece of paper. And the scene is a window to the virtual world.


Expanded World to Scratch

A bit of theory and history . It turns out that the technology of "virtual" worlds is used in all computers when it comes to memory. The operating system and all its programs require many times more RAM than they are physically, and in order to expand it, people came up with an algorithm that expands the RAM with the hard disk memory. And the resulting memory was called "virtual" memory. Thus, programmers now do not need to think about the expenditure of RAM, as it was at the beginning of the computer industry, when each piece was planned out. In those “dark times”, a computer could include only one program, all the others did not turn on until the included program was completed. Not like now, we can surf the Internet, listen to music, draw, watch a movie.


Having tried the standard tool, we realized that we could not directly use the " go to x: y: " command. We need to move away from the standard tools and create our x and y coordinates, using variables with the “ only for this sprite ” scope. And we insert our coordinates " go to x: x y: y".


We create private variables and use them to position our object.

Now we need to understand how our hero will move through the virtual world. We can not move the stage, so the whole virtual world must move.


In the virtual world, the world moves, not the object.

If you watch, you will see, when the hero moves to the left, the virtual world moves to the right. In other words, when you press the arrow to the left, the world moves towards it, to the right. Sprite moves in one direction, the world - in the opposite. And thus, it turns out such a movement in which the sprite does not move at all, and all control from the keyboard is in the auxiliary objects.


The movement of the virtual world around the hero

Note : all trees have the same code.


Note that each time you start the game, all trees will change their location on the field in x (-450; 450) and y (-450; 450). It turns out a map of the world - a square. In theory, the hero must move within this world.


Let's make it so that our hero could not go beyond the virtual world. As soon as the hero reaches the border of the virtual world, he must stop, or rather the entire virtual world must stop. In other words, every object in the world should know the position of the protagonist. Create "x hero" and "y hero" with the scope "For all sprites." And we write the code for the main character with location tracking in the virtual world.


Tracking the virtual location of the main character

Note the “and” operator. This is an interesting logical operation. If you read the phrase “if the up arrow key is pressed and the hero has less than 450, then we change the variable of the hero to 10”. The change in the hero's variable will occur only when both conditions are met, in other words, it will happen when the up arrow is pressed and the coordinate of the hero is less than 450. Both this and that is done.


It remains to refine the code for the virtual world. We use the operator "and" and comparison. Be careful when you work on this script, because the world moves to the left, while the hero moves to the right.


By the way, while writing the code, I found an error in Scratch, so that it does not happen again, please pull out new operators from the command area, do not change the comparison operators with the right mouse button.


Script for objects.


Code to restrict the movement of the virtual world depending on the position of the hero

That's all. Make up the rest yourself. For example, make the game "Santa Claus in search of the Christmas tree." Father Frost needs to quickly collect all the Christmas trees so that each child has a New Year tree at home.


Link to the project.


Information for parents. This month, our club opens new courses on computer literacy and programming for children ages 7-8 and 9-12. On them we will study not only programming, but also how to properly use a computer. Together with the children, we will create many projects that affect not only programming, but also physics, mathematics, and astronomy. A unique program, rich in exciting projects, is waiting for your child in the classroom. Our club and activities.


By the way, in our club a child can plunge into the world of robotics and inventions. By the way, for 5-6 years we also have mugs. See you in class.


')

Source: https://habr.com/ru/post/400909/


All Articles