save code
This commit is contained in:
+25
-27
@@ -1,54 +1,52 @@
|
||||
// =====================================================================
|
||||
// 第 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>
|
||||
#include <cstdlib> // rand, srand
|
||||
#include <ctime> // time
|
||||
// TODO(步骤 1):加上随机数和时间需要的头文件
|
||||
// 提示:#include <cstdlib> 和 <ctime>
|
||||
// TODO(步骤 3):加 #include <cstdio> 才能用 sprintf
|
||||
|
||||
// 全局变量
|
||||
Fl_Box *result_box;
|
||||
Fl_Box *info_box; // 显示本次点数的辅助信息
|
||||
// TODO(步骤 3):再声明一个全局 Fl_Box 指针 info_box,用来显示本次点数
|
||||
|
||||
// 回调函数——每次点击按钮执行
|
||||
void draw_callback(Fl_Widget *w, void *data) {
|
||||
int r = std::rand() % 100;
|
||||
// TODO(步骤 2):在这里生成一个 0~99 的随机数 r
|
||||
// 提示:int r = std::rand() % 100;
|
||||
|
||||
// 显示点数(调试用,熟悉后可以删掉这行)
|
||||
char info[50];
|
||||
std::sprintf(info, "本次点数:%d", r);
|
||||
info_box->label(info);
|
||||
// TODO(步骤 3):用 sprintf 把点数塞进字符串,显示在 info_box 上
|
||||
// char info[50];
|
||||
// std::sprintf(info, "本次点数:%d", r);
|
||||
// info_box->label(info);
|
||||
|
||||
// 判断稀有度
|
||||
if (r < 70) {
|
||||
result_box->label("☆ 普通卡 ☆");
|
||||
result_box->labelcolor(fl_rgb_color(128, 128, 128)); // 灰色
|
||||
} else if (r < 95) {
|
||||
result_box->label("★ 稀有卡 ★");
|
||||
result_box->labelcolor(FL_BLUE); // 蓝色
|
||||
} else {
|
||||
result_box->label("★★★ 超稀有卡!! ★★★");
|
||||
result_box->labelcolor(FL_RED); // 红色
|
||||
}
|
||||
// TODO(步骤 4):根据 r 的范围设置稀有度文字和颜色
|
||||
// < 70 普通卡,灰色 fl_rgb_color(128, 128, 128)
|
||||
// < 95 稀有卡,FL_BLUE
|
||||
// 其他 超稀有卡,FL_RED
|
||||
result_box->label("(写好步骤 4 后这里会变)");
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::srand(std::time(nullptr)); // 种子设一次
|
||||
// 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);
|
||||
|
||||
// 信息标签(小字号,显示点数)
|
||||
info_box = new Fl_Box(50, 200, 300, 50, "");
|
||||
info_box->labelsize(14);
|
||||
// TODO(步骤 3):在这里创建 info_box(位置 (50,200)、宽 300、高 50、字号 14)
|
||||
|
||||
window->end();
|
||||
window->show();
|
||||
|
||||
Reference in New Issue
Block a user