Lesson 3: Loops and the pen
Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
English
Draw with the pen
- The robot has a pen under it.
pen.move(DOWN)puts it on the floor; the robot then draws a line where it drives. pen.move(UP)lifts it again.pen.set_pen_color(RED)changes the colour (BLACK, RED, GREEN or BLUE).
Words for this lesson
| Word | Meaning |
|---|---|
| pen | draws a line on the floor under the robot |
| loop | code that runs again and again |
| range(4) | count 4 times: 0, 1, 2, 3 |
| indent | the spaces before a line inside the loop |
English
A square with a loop
from vexcode_vr import *
def main():
pen.move(DOWN)
for i in range(4):
drivetrain.drive_for(FORWARD, 400, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
vr_thread(main)
- The two indented lines repeat 4 times: drive one side, turn one corner.
- Four corners of 90 degrees are 360 degrees, so the robot ends where it started.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Draw a square with sides of 500 mm. Put the pen down and use a for loop.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Draw a triangle with 3 sides of 600 mm. Use a loop and turn LEFT. Hint: at each corner the robot turns 120 degrees.
Nhấn Chạy để xem kết quả ở đây.