{
"id": "141",
"prompt": "Build a 2x2 drum pad machine: each screen quadrant is a pad - kick (60 Hz sine) top-left, snare (host::audio::noise(120)) top-right, hat (a 30 ms noise tick) bottom-left, cowbell (800 Hz square) bottom-right. Edge-detect the tap (previous pointer_down in a state slot), pick the pad from which half of the screen the tap landed in, flash the struck pad white for 5 frames, and label all four.",
"solution_rl": "fn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let down: i32 = host::display::pointer_down();\n let prev: i32 = host::display::state_get(0);\n let mut flash: i32 = host::display::state_get(1);\n let mut which: i32 = host::display::state_get(2);\n if down == 1 && prev == 0 {\n let px: i32 = host::display::pointer_x();\n let py: i32 = host::display::pointer_y();\n let mut pad: i32 = 0;\n if px >= w / 2 { pad = pad + 1; }\n if py >= h / 2 { pad = pad + 2; }\n if pad == 0 { host::audio::tone(60, 180, 0); }\n if pad == 1 { host::audio::noise(120); }\n if pad == 2 { host::audio::noise(30); }\n if pad == 3 { host::audio::tone(800, 90, 1); }\n flash = 5;\n which = pad;\n }\n if flash > 0 { flash = flash - 1; }\n host::display::state_set(0, down);\n host::display::state_set(1, flash);\n host::display::state_set(2, which);\n host::display::clear(0x000000);\n let mut i: i32 = 0;\n while i < 4 {\n let x: i32 = (i % 2) * (w / 2);\n let y: i32 = (i / 2) * (h / 2);\n let mut c: i32 = 0x1c1c24;\n if flash > 0 && i == which { c = 0xffffff; }\n host::display::fill_rect(x + 6, y + 6, w / 2 - 12, h / 2 - 12, c);\n i = i + 1;\n }\n host::display::draw_string(24, 24, \"KICK\", 0x00ff88, 2);\n host::display::draw_string(w / 2 + 24, 24, \"SNARE\", 0x00ff88, 2);\n host::display::draw_string(24, h / 2 + 24, \"HAT\", 0x00ff88, 2);\n host::display::draw_string(w / 2 + 24, h / 2 + 24, \"COWBELL\", 0x00ff88, 2);\n host::display::present();\n}\n",
"tags": [
"audio",
"pointer",
"state"
]
}