我正在尝试使用 CloudPebble 上的 C 在 Pebble 中创建一个简单的掷骰子应用程序。我有一系列不同的模具尺寸,您可以使用向上/向下滚动,然后使用中间按钮滚动(目前只是生成一个随机数,稍后会变得更漂亮)。顶部还有一个显示当前骰子的标签。
它大部分都在工作,但是有一个我一生都无法弄清楚的错误:
- 滚动骰子时,至少有一个骰子不会显示骰子编号
- 损坏的骰子仍会滚动,但 rand() 函数似乎没有上限(或者上限可能为 1000;我在损坏的骰子上滚动测试的最高值是 880)
刚开始的时候,D6坏了。我修补和调整并添加了一些代码以查看后台发生了什么,但突然 D6 开始工作(不知道为什么)并且 D20 坏了。我尝试将数组中的“20”项更改为“35”只是为了看看它会做什么,现在-D35 工作,但 D2 和 D4 坏了。我把它改回来,不同的骰子破了。
附件是我当前的代码,目前显示 D20 和 D4 已损坏。我已经删除了用于故障排除的所有垃圾代码、返工代码和辅助代码,因为它们都没有帮助。
任何人都可以帮助解释为什么骰子似乎随机破裂,以及如何解决它?谢谢!
#include <pebble.h>
static Window *s_main_window;
static TextLayer *s_label_layer;
static TextLayer *s_output_layer;
static int dice_select = 6;
static char *die_label = "D";
static char *dice_array[] = {"2", "4", "6", "8", "10", "12", "20", "100", NULL};
// === Separate Processes ===
static void update_label(char *die) {
die_label[1] = '\0';
strcat(die_label, die);
text_layer_set_text(s_label_layer, die_label);
}
// === Main Window Processes ===
static void main_window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect window_bounds = layer_get_bounds(window_layer);
// Create label TextLayer
s_label_layer = text_layer_create(GRect(5, 0, window_bounds.size.w - 10, 24));
text_layer_set_font(s_label_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
text_layer_set_text_alignment(s_label_layer, GTextAlignmentCenter);
text_layer_set_text(s_label_layer, "D20");
text_layer_set_overflow_mode(s_label_layer, GTextOverflowModeWordWrap);
layer_add_child(window_layer, text_layer_get_layer(s_label_layer));
// Create output TextLayer
s_output_layer = text_layer_create(GRect(5, 24, window_bounds.size.w - 10, window_bounds.size.h - 24));
text_layer_set_font(s_output_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28));
text_layer_set_text(s_output_layer, "No button pressed yet.");
text_layer_set_overflow_mode(s_output_layer, GTextOverflowModeWordWrap);
layer_add_child(window_layer, text_layer_get_layer(s_output_layer));
}
static void main_window_unload(Window *window) {
// Destroy output TextLayer
text_layer_destroy(s_label_layer);
text_layer_destroy(s_output_layer);
}
// === Button Handlers ===
static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
// Increment dice selection
if(dice_select < 7) {
dice_select += 1;
}
update_label(dice_array[dice_select]);
}
static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
// Establish die selection
char *die = dice_array[dice_select];
int die_sides = atoi(die);
// Roll die
int result = rand() % die_sides + 1;
char *result_str = "";
snprintf(result_str, sizeof(result_str), "%d", result);
// Print result
text_layer_set_text(s_output_layer, result_str);
}
static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
// Increment dice selection
if(dice_select > 0) {
dice_select -= 1;
}
update_label(dice_array[dice_select]);
}
static void click_config_provider(void *context) {
// Register the ClickHandlers
window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
}
// === Initialize/Deinitialize App ===
static void init() {
// Create main Window
s_main_window = window_create();
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload,
});
window_set_click_config_provider(s_main_window, click_config_provider);
window_stack_push(s_main_window, true);
}
static void deinit() {
// Destroy main Window
window_destroy(s_main_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
(注意:我是一个未经训练的自学初学者,所以像“你为什么那样做?”这样的所有问题的答案都是“因为我不知道更好。”)
编辑:根据评论添加的新代码。
这部分效果很好:
static char die_label[32] = "D";
static void update_label(char *die) {
snprintf(die_label, sizeof(die_label), "D%s", die);
text_layer_set_text(s_label_layer, die_label);
}
但这部分将不再打印滚动结果:
// Roll die
int result = rand() % die_sides + 1;
char result_str[32];
snprintf(result_str, sizeof(result_str), "%d", result);
// Print result
text_layer_set_text(s_output_layer, result_str);