Skip to main content

Lesson 2: Repeating Actions

Now that the VR Robot has drawn a square on the Art Canvas Playground using the Pen, you will learn to repeat blocks and simplify your project. This Lesson will walk you through the steps to draw a square on the Art Canvas Playground using the [Repeat] block.

Art Canvas Playground with a VR Robot at the end of the project. One square with dark black lines have been drawn on the Playground to the upper right of the VR Robot.

Learning Outcomes

  • Identify how to use the [Repeat] block to repeat the blocks inside of it for a set number of times.
  • Identify that the [Repeat] block can accept integers as parameters.
  • Identify that conditions are used to end a [Repeat] block.
  • Describe why a [Repeat] block would be used in a project.
  • Describe the flow of a project that contains a [Repeat] block.

Rename the Project

  • If the previous Lesson’s project is not already loaded, load the Unit3Lesson1 project.
  • Rename the project by selecting the project name box.

    VEXcode VR coding environment with a red box at the top middle calling out the project name box. The project name box reads Unit 3 Lesson 1.
  • Enter the new project name Unit3Lesson2, and select “Save.”

    Project name popup open with the new title, Unit 3 Lesson 2 typed. A red box calls out the Save option at the bottom of the project name popup.

Repeating Actions

The [Repeat] block is used to repeat the blocks within it a set number of times. The [Repeat] block saves time and effort while creating projects where blocks repeat. Rather than taking the time to drag in additional blocks or duplicate existing blocks in the workspace, the [Repeat] block can be used to save space and time.

  • Begin by modifying the previous project or create a new project to match this base project. You may use traditional blocks, Switch blocks, and a combination of both to create your project. 

    A VEXcode VR project with a when started block and nine blocks attached. The blocks read as follows from top to bottom: move robot pen down, drive forward for 600 millimeters, turn right for 90 degrees, drive forward for 600 millimeters, turn right for 90 degrees, drive forward for 600 millimeters, turn right for 90 degrees, drive forward for 600 millimeters, and turn right for 90 degrees.
  • The repeated blocks in this project are the [Drive for] and the [Turn for] blocks. Remove the bottom six blocks in the project so only one [Drive for] and one [Turn for] block is left beneath the [move robot pen] block.

    The project from above with a red box around the bottom 6 blocks. A red arrow points to the right to show what the project will look like when those 6 blocks are deleted. The new project has a when started block with three blocks attached below. They read move robot pen down, drive forward for 600 millimeters, and turn right for 90 degrees.
  • Drag in a [Repeat] block. Notice that the [Repeat] block will create a gray shadow around the blocks that will be inside when placing the block in the workspace.

    The project from before with three blocks below the when started. A repeat loop is shown in the process of being added around the drive forward and turn right blocks, this is indicated by a shadow previewing where the block will go. A red arrow points to the right to show the project after the repeat loop is added. The project reads when started, move robot pen down, repeat 10. Inside of the repeat 10 are two blocks: drive forward for 600 millimeters and turn right for 90 degrees.
  • Set the parameter of the [Repeat] block to “4” so a VR Robot draws all four sides of a square.

    The same project as before with a red box around the numeric parameter of the repeat loop. The number has changed from 10 to 4.

    Using Switch Blocks

    The [Repeat] block can accept integers. Decimals will not work as a parameter for a [Repeat] block.

    Repeat loop in VEXcode VR.

    This is the Switch [Repeat] block. In this example, the [Repeat] block is set to repeat behaviors 10 times. The command, for repeat_count in range(10), is the code that starts a loop that repeats 10 times. 

    The Switch block version of the repeat loop. This block is shaped as a square with the right side open. The text inside the block reads for repeat underscore count in range with the number 10 in parentheses and a colon at the end.

    The  image below shows the Switch [Drive for] and [Turn for] commands that are nested inside of the Switch [Repeat] C-block. The Switch [Repeat] block's parameter is set to "4," meaning that the [Drive for] and [Turn for] commands nested within the C-block will be repeated four times.

    The same switch repeat loop as before with the number in the parentheses set to 4. Inside the open area of the block are two other switch blocks. The first block reads drivetrain dot drive underscore for with forward comma 600 comma MM in parentheses. The second block reads drivetrain dot turn underscore for with right 90 degrees in parentheses.

    The image below shows the same stack of Switch blocks converted into one Switch block.

     for repeat_count in range(4): is the Python command that creates the repeat loop that tells the robot to repeat the indented lines of code beneath this command 4 times.

    The next lines of code are indented underneath the Repeat command because these are the behaviors that will be repeated. The indentation is set to a default of 4 spaces. When typing in Python commands, be sure to always use the same number of spaces when indenting commands, as projects will not run properly if indentations are inconsistent. 

    The command, drivetrain.drive_for(FORWARD, 600, MM) tells the robot to drive forward 600 mm and the command, drivetrain.turn_for(RIGHT, 90, DEGREES) tells the robot to turn right for 90 degrees. Because these commands are indented under the Repeat command, these behaviors are part of the repeat loop and will be repeated four times.  

    A multiline Switch block showing that the two drivetrain commands from before can be indented underneath the for command and accomplish the same behaviors with the robot.

  • Select the “Open Playground” button to open the Art Canvas Playground if it is not already open.

    VEXcode VR coding environment with a red box calling out the Open Playground icon and text to the top right of the toolbar.
  • Select the “Start” button to test the project.

    VEXcode VR coding environment with a red box calling out the Start icon and text to the top right of the toolbar.
  • Watch the VR Robot drive forward and turn right four times on the Art Canvas Playground to draw a square with the Pen.

    Art Canvas Playground with a VR Robot at the end of the project. One square with dark black lines have been drawn on the Playground to the upper right of the VR Robot.

Select the Next button to continue with the rest of this Lesson.