Random map generation tutorial for Scratch games

        In many survival/sandbox games, maps are often randomly generated, such as: Don't Starve , Minecraft, etc. So how can we achieve this in scratch?

        There are two ways to do this in scratch - brushes and clones. Let's talk about clones first.

        We can set the clones to be square first, and then connect each other to form a large square map. These clones can change colors to simulate different terrains (deserts, forests, plains...), Think about it carefully, are these squares similar to the blocks in my world? That's right, this is similar to the principle of minecraft, but ours is 2D.

        Tip: To write this program, infinite copy must be turned on

Let's move on to the practical part:


1. Draw clones

        Let's first draw a square without a border, with a side length of 15 grids (a grid has 4 coordinates, and the side length is 60 coordinates). Of course, you can also set it to other sizes, but this is an integer, which is convenient for calculation. (The square can be slightly larger, otherwise there will be a slight gap when zooming in)

as the picture shows

as the picture shows

2. Write a program

        Let's set a few variables first: number of rows, number of columns, number of clones, number of private clones (private variables).

        The number of rows and columns refers to the size of the map, and a map with ten rows and ten columns has 100 grids. The number of clones and private clones is to help the clone figure out its own number and which number it is, so as to know which column and row it should be in, and where its coordinates are.

        The first part of the program is to set the initial variable (here the size is set to 10*10), then hide the body, and then increase the number of clones by one to clone itself once. It's easy to understand, the hard part is the clone procedure.

Ontology program

Ontology program

        Next, let’s look at the clone program, first display, and then set the number of private clones to the current number of clones (needless to say this step, private variables can be owned by the clone itself, which is equivalent to numbering yourself ).

        Since it is too slow to make the main body copy the clones one by one, it is better to let the clones copy the clones, and then let the cloned clones make a new clone... In this way, the number of clones will increase exponentially, The speed has increased dozens of times, and even copying 10,000 copies is a blink of an eye. Just let the clones repeat the duplication until the total number equals the number of rows and columns.

        After running the above program, if you find that the number of clones reaches 100 in an instant, it is a success. The next step is to put the clones in their proper place and arrange them into a square. (For the convenience of observing the effect, we first change the number of rows and columns to 3*3. In addition, if you don’t want the computer to freeze, it is best not to exceed 100*100)

        We first set up two private variables for the clone, namely x and y. These two variables are what give the clone its own coordinates.

        So how can the clones return to their places? The principle is very simple. It is similar to the questions of queuing up for morning exercises or arranging a square matrix in elementary school. It is nothing more than changing the number of rows and columns based on the total number of people to counting the number of columns and rows.

        The specific formula is as follows:

The top is the x coordinate and the bottom is the y coordinate

The top is the x coordinate and the bottom is the y coordinate

        The front is quite simple, but what does multiplying by 60 mean? As we mentioned earlier, the side length of the clones is 60 coordinates, and multiplying by 60 is to keep a certain distance between the clones.

        Let's put this formula into the program again:

        At this time, the two variables come in handy, "move to x:xy:y" and then move the clone to the corresponding position, because the map needs to be moved in real time, and another repeat execution is required.

        Try running it:

        That's right, something went wrong with the program. The column on the right jumps up one space, but where is the problem?

        This is the problem of the y coordinate, so let's check the formula just now:

        Let the clone number be 1: 1/3=0...1 (1-1)/3*60=0

        Let the clone number be 2: 2/3=0...2 (2-2)/3*60=0

        Let the clone number be 3: 3/3=1...0 (3-0)/3*60=60

        Let the clone number be 6: 6/3=2...0 (6-0)/3*60=120

        In the end we found that if the number of the clone is a multiple of the number of columns, it will have 60 more coordinates.

        So we need to judge whether the number of the clone is a multiple of the number of columns, and if so, subtract 60 coordinates.

Guess you like

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