How to do Scratch's big map engine?

Primer

        Briefly, some game engines have an invisible minimap, which is usually used to detect collisions and movements. Then, the background of a large map engine must be very large (generally speaking, the size exceeds 200). On-target, that is, the bullet is limited by the stage area. Some creators may think that it is enough to just add a transparent border to the bullet, or it is not enough to switch back and forth between a large shape and a real shape? Well, it can be said that these ideas are very good, but there will be a lot of bugs in this way, and the interpretation is poor, and it is not easy to modify, which will have a profound impact on the later creation. At this time, someone came up with a solution: as long as the collision is detected in the stage area, isn't it all right? So I made the invisible mini-map just mentioned, but a new problem arises, because the size of the invisible mini-map and the big map are different, so even if the collision can be detected, the location of the collision cannot be detected on the big map .

       At this time, an important variable is used: the reduction factor. Briefly introduce the reduction factor. Since the sizes of the invisible small map and the large map are different, the sizes of the invisible small map and the large map are proportional to each other, which is a fixed value. This ratio is the reduction factor.

       The reduction factor is self-determined. Generally, when we do it, the size of the invisible small map will change with the size of the big map. Here are a few formulas:

About size: large map size * reduction factor = small map size, small map size / reduction factor = large map size, small map size / large map size = reduction factor.

About coordinates: large map coordinates * reduction factor = small map coordinates, small map coordinates / reduction factor = large map coordinates, small map coordinates / large map coordinates = reduction factor.

      With these formulas, it is obvious how to achieve the functions we want.

      We need several characters: one is the background of the big map, the other is the invisible small map, the third is the character displayed by the player, and the fourth is the invisible small collision box of the player.

Then

Let's create these roles:

The player appearance and map appearance are for the player to see; while the player collision and map detection are invisible small maps, responsible for collision

then

Let's set a reduction factor. If your map is small, the reduction factor can be larger. If your map is large, it is recommended that the reduction factor be smaller, as long as the collision can be carried out in the stage area.

Here I set the reduction factor to 0.2, how to set it depends on your creation

Usually I do this in the character "Player Collision", but you can do it in other characters as well.

Since the reduction factor is a constant, it is generally not adjusted, so I added "#" before the variable name to indicate a constant. Tips: Variable names usually do not affect the effect, but they will affect the interpretability of the code.

Ok, the reduction factor has been set, let's paint our scene next.

Painting in a map-like character

I just drew it like this. Of course, a map also needs a collision box, which is an invisible mini-map.

What? You can't draw an invisible minimap? It doesn't matter, I'll teach you by hand.

copy the map

Come to the map to detect the character

Paste the copied map into the role of map detection

make it hollow

bold outline

Well, we have finished drawing like this. Of course, how to draw this invisible small map depends on your actual situation. In short, the places that do not collide are hollow, and the places that do collide must be kept.

The same is true for player collision, so let's draw it next:

Come to the player character and copy the shape

Paste the shape into the player collision

Draw a rectangle smaller than the player, but not too small

Why is it smaller than the player and rectangular?

The answer is: easy creation.

Being smaller than the player can ensure that there will be no problems when colliding, while the rectangle is because of complex shape collisions will freeze. Of course, you can also draw a circle.

Delete the following player appearance

In this way, the player detection is drawn

Don't ask me why the drawing is so ugly, because the invisible mini-map doesn't need to be shown to players. If your work needs it, you can also beautify it, and draw it according to the actual situation.

next

We come to the map character

Here I set the size to 300 (the size depends on your actual situation), but you can see that even if I set the size = 300, the size is 273. This is because the Scratch editor has a stage area limit. You can use Fermi extensions, but I'm used to another way.

Create an empty shape, this shape does not need to draw anything, just leave it empty.

This method is to change to an empty shape first, then set the size, and then cut it, so that you can break through the size limit of Scratch (as shown in the picture).

Ok, let's go to the map to detect the character

Come to the map to detect the character

On the code!

Then, the code for map detection ends here, um, yes, you heard it right.

then

We come to the player detection character

Come to the player detect character

On the code!

Tips: If you don’t understand something or why you do it, you can go to the first paragraph of this article and read it carefully. If it doesn’t work, you can choose to ask in the comment area.

Ok, we set the coordinates of the birth point, and then we move.

On the code!

tips: Why do you want to make it this way? First of all, I put: ("Is the button d pressed"-"Is the button a pressed")*Movement speed, you must not know what this means, let me explain to those who don't understand, Scratch has a feature, Boolean value Put the building block in some return value building blocks, it will return 1 and 0, 1 is true, 0 is false, if we press the d key, a key is not pressed, then: (1-0)*3, this result = 3 , so our x will be +3, if the d key is not pressed, and the a key is pressed, then: (0-1)*3, this result = -3, so our x coordinate will be -3, I really don’t understand It doesn't matter, you can use the method you are familiar with to achieve movement.

OK, let's do the collision next:

Create a generic homemade building block called "collision"

On the code!

Of course, this is just one method of collision detection, you can also use your own method. The principle of the method used here is: return to the original position after encountering.

On the code!

Tips: Remember to create the four public variables that appear in the diagram!

Sorry, I forgot to add the code circled in the picture when I was doing the tutorial, remember to add it!

Sorry, I forgot to add the code circled in the picture when I was doing the tutorial, remember to add it!

OK

Next we come to the player appearance character

come to player character

On the code!

How to make the camera follow the effect? First of all, the camera must have a delay, so the player x-camera x can get how much the camera is delayed, and let the player move to this position to create the effect of the camera following.

OK

Let's go back to the map character

Come to the map like character

On the code!

very good

In this case, there should be no bugs, unless your code is misspelled, or there are some minor problems in the editor, but I believe that you can make a large map work through continuous efforts.

Guess you like

Origin blog.csdn.net/leyang0910/article/details/132032513