Pyramid block programming

    A pyramid is a tetrahedron with an upper tip and a lower part. Its cross-section is a triangle. Rectangular bricks can be stacked into a large triangle to represent the pyramid.
    The following introduces the use of Scratch to draw pyramids.

1. Define "bricks" building blocks

Figure (1) Define the "brick" building block

Figure (2) "Brick" building block code

2. Define 5 variables

Click [Variables] on the left side of Scratch --> Create the following 5 variables

  • The lower left corner of the current brick X
  • The lower left corner of the current brick Y
  • Pyramid layers
  • Brick length
  • The brick width
        is shown in figure (3):
Figure (3) Define 5 variables

3. Define the building block "Adjust the length and width of the bricks"

    Since the size of the entire canvas is 480x360, the total length of the bricks on each layer cannot exceed 480, and the height of the pyramid (the total thickness of the bricks) cannot exceed 360.

Figure (4) Define the "adjust the length and width of the bricks" blocks

Figure (5) "Adjust the length and width of the bricks" brick code

4. Define the "Pyramid" building blocks


Figure (6) Define the "Pyramid" building block

Figure (7) "Pyramid" building block code

5. Main function

5.1 Draw a 10-layer pyramid

Figure (8) Main function (10-layer pyramid)

    The effect is as follows:

Figure (9) The rendering of the 10-layer "pyramid"

5.2 Draw a 20-layer pyramid

Figure (13) Main function (20-layer pyramid)

Figure (14) The effect of the 20-layer "pyramid"

Guess you like

Origin blog.csdn.net/sanqima/article/details/113808404