Files
chouka/lesson_gui_3.cpp
T
2026-05-16 12:05:04 +08:00

55 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// =====================================================================
// 第 3 课:随机抽卡 —— 起步骨架
// 教程文件:第03课-随机抽卡.md
// 完整答案:solutions/lesson_gui_3.cpp
// 起步状态:复用第 2 课的"点按钮显示固定文字",逐步加随机和稀有度判断
// =====================================================================
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Box.H>
// TODO(步骤 1):加上随机数和时间需要的头文件
// 提示:#include <cstdlib> 和 <ctime>
// TODO(步骤 3):加 #include <cstdio> 才能用 sprintf
Fl_Box *result_box;
// TODO(步骤 3):再声明一个全局 Fl_Box 指针 info_box,用来显示本次点数
void draw_callback(Fl_Widget *w, void *data) {
// TODO(步骤 2):在这里生成一个 0~99 的随机数 r
// 提示:int r = std::rand() % 100;
// TODO(步骤 3):用 sprintf 把点数塞进字符串,显示在 info_box 上
// char info[50];
// std::sprintf(info, "本次点数:%d", r);
// info_box->label(info);
// TODO(步骤 4):根据 r 的范围设置稀有度文字和颜色
// < 70 普通卡,灰色 fl_rgb_color(128, 128, 128)
// < 95 稀有卡,FL_BLUE
// 其他 超稀有卡,FL_RED
result_box->label("(写好步骤 4 后这里会变)");
}
int main() {
// TODO(步骤 1):在这里设一次随机种子
// 提示:std::srand(std::time(nullptr));
Fl_Window *window = new Fl_Window(400, 350, "抽卡世界 - 第3课");
Fl_Button *btn = new Fl_Button(130, 30, 140, 50, "抽一张!");
btn->labelsize(18);
btn->callback(draw_callback);
result_box = new Fl_Box(50, 110, 300, 80, "点击按钮抽卡吧!");
result_box->labelsize(28);
result_box->labelfont(FL_BOLD);
// TODO(步骤 3):在这里创建 info_box(位置 (50,200)、宽 300、高 50、字号 14
window->end();
window->show();
return Fl::run();
}