0 / 8 页已完成

第一个程序:让屏幕说话Your First Program: Make the Screen Talk

2.1 程序是什么?2.1 What Is a Program?

程序就是一组指令,告诉机器人该做什么。就像菜谱一步步教你做菜一样,程序也是一行行代码,电脑从上往下按顺序执行。A program is a set of instructions that tells the robot what to do. Just like a recipe guides you step by step, a program is lines of code that the computer runs from top to bottom in order.

第一行执行完,执行第二行,再执行第三行......一直到最后一行结束。After the first line finishes, it runs the second line, then the third... all the way to the last line.

记住这个规则Remember This Rule

程序从上往下,一行一行执行。这是编程最基本的规则。Programs run from top to bottom, one line at a time. This is the most basic rule of programming.

2.2 在屏幕上显示文字2.2 Display Text on the Screen

VEX Brain(机器人的大脑)有一块屏幕。我们的第一个任务很简单:让屏幕显示一句话。The VEX Brain (the robot's brain) has a screen. Our first task is simple: display a message on the screen.

不同的开发环境写法略有不同,点击上方的标签切换查看:The code looks slightly different depending on the development environment. Click the tabs above to switch:

C++ (VEXcode)
int main() {
    Brain.Screen.print("Hello VEX!");
    return 0;
}
C++ (VS Code)
int main() {
    Brain.Screen.print("Hello VEX!");
    return 0;
}
C++ (PROS)
void initialize() {
    pros::lcd::initialize();
    pros::lcd::set_text(1, "Hello VEX!");
}

运行这段代码,Brain 屏幕上就会显示 Hello VEX! 这几个字。Run this code and the Brain screen will display Hello VEX!

代码解释:Code explanation:

代码解释:Code explanation:

代码解释:Code explanation:

2.3 动手练习:显示自己的名字2.3 Hands-on: Display Your Name

动手试试Try It Yourself

"Hello VEX!" 改成你自己的名字,比如 "I am Edison!",然后运行看看效果。Change "Hello VEX!" to your own name, like "I am Edison!", then run it and see what happens.

注意:引号里面的内容可以随便改,但引号本身不能少Note: You can change anything inside the quotes, but don't forget the quotes themselves.

2.4 显示多行文字2.4 Display Multiple Lines

如果你想显示不止一行呢?What if you want to display more than one line?

C++ (VEXcode)
int main() {
    Brain.Screen.print("Line 1: Hello!");
    Brain.Screen.setCursor(2, 1);
    Brain.Screen.print("Line 2: I am a robot.");
    Brain.Screen.setCursor(3, 1);
    Brain.Screen.print("Line 3: Nice to meet you!");
    return 0;
}

setCursor(行, 列) 把光标移到指定位置。第一个数字是行号,第二个是列号(通常写 1 就行)。setCursor(row, column) moves the cursor to a specific position. The first number is the row, the second is the column (usually just use 1).

C++ (VS Code)
int main() {
    Brain.Screen.print("Line 1: Hello!");
    Brain.Screen.setCursor(2, 1);
    Brain.Screen.print("Line 2: I am a robot.");
    Brain.Screen.setCursor(3, 1);
    Brain.Screen.print("Line 3: Nice to meet you!");
    return 0;
}

setCursor(行, 列) 把光标移到指定位置。第一个数字是行号,第二个是列号(通常写 1 就行)。setCursor(row, column) moves the cursor to a specific position. The first number is the row, the second is the column (usually just use 1).

C++ (PROS)
void initialize() {
    pros::lcd::initialize();
    pros::lcd::set_text(1, "Line 1: Hello!");
    pros::lcd::set_text(2, "Line 2: I am a robot.");
    pros::lcd::set_text(3, "Line 3: Nice to meet you!");
}

PROS 更简单:set_text(行号, 文字),直接指定在第几行显示。PROS is simpler: set_text(line_number, text) directly specifies which line to display on.

检查点:预测输出Checkpoint: Predict the Output
下面这段代码运行后,屏幕第 2 行显示什么?After running the code below, what does line 2 of the screen show?
C++ (VEXcode)
Brain.Screen.print("Apple");
Brain.Screen.setCursor(2, 1);
Brain.Screen.print("Banana");
Brain.Screen.setCursor(3, 1);
Brain.Screen.print("Cherry");
C++ (VS Code)
Brain.Screen.print("Apple");
Brain.Screen.setCursor(2, 1);
Brain.Screen.print("Banana");
Brain.Screen.setCursor(3, 1);
Brain.Screen.print("Cherry");
C++ (PROS)
pros::lcd::initialize();
pros::lcd::set_text(1, "Apple");
pros::lcd::set_text(2, "Banana");
pros::lcd::set_text(3, "Cherry");

2.5 写代码练习2.5 Coding Exercise

挑战Challenge

写一段代码,让 Brain 屏幕显示三行内容:Write code to display three lines on the Brain screen: