第一个程序:让屏幕说话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.
程序从上往下,一行一行执行。这是编程最基本的规则。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:
int main() {
Brain.Screen.print("Hello VEX!");
return 0;
}
int main() {
Brain.Screen.print("Hello VEX!");
return 0;
}
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:
int main()— 程序的入口,所有代码从这里开始int main()— The entry point of the program, all code starts hereBrain.Screen.print("Hello VEX!")— 在屏幕上打印引号里的文字Brain.Screen.print("Hello VEX!")— Prints the text inside the quotes on the screenreturn 0;— 告诉电脑"程序正常结束了"return 0;— Tells the computer "the program finished normally"
代码解释:Code explanation:
int main()— 程序的入口,所有代码从这里开始int main()— The entry point of the program, all code starts hereBrain.Screen.print("Hello VEX!")— 在屏幕上打印引号里的文字Brain.Screen.print("Hello VEX!")— Prints the text inside the quotes on the screenreturn 0;— 告诉电脑"程序正常结束了"return 0;— Tells the computer "the program finished normally"
代码解释:Code explanation:
void initialize()— PROS 的初始化函数,程序启动时自动执行void initialize()— PROS initialization function, runs automatically when the program startspros::lcd::initialize()— 初始化屏幕(PROS 需要先初始化才能用)pros::lcd::initialize()— Initialize the screen (PROS requires this before use)pros::lcd::set_text(1, "Hello VEX!")— 在屏幕第 1 行显示文字pros::lcd::set_text(1, "Hello VEX!")— Display text on line 1 of the screen
2.3 动手练习:显示自己的名字2.3 Hands-on: Display Your Name
把 "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?
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).
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).
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.
Brain.Screen.print("Apple");
Brain.Screen.setCursor(2, 1);
Brain.Screen.print("Banana");
Brain.Screen.setCursor(3, 1);
Brain.Screen.print("Cherry");
Brain.Screen.print("Apple");
Brain.Screen.setCursor(2, 1);
Brain.Screen.print("Banana");
Brain.Screen.setCursor(3, 1);
Brain.Screen.print("Cherry");
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
写一段代码,让 Brain 屏幕显示三行内容:Write code to display three lines on the Brain screen:
- 第 1 行:你的队伍名字Line 1: Your team name
- 第 2 行:你的队伍号码Line 2: Your team number
- 第 3 行:
Ready to compete!Line 3:Ready to compete!