{
"id": "119",
"prompt": "Space-invaders-lite: my ship follows pointer_x along the bottom and a tap fires a single bullet (only one alive at a time - one x and one y state slot, y of 0 meaning idle). Three invaders descend, each respawning at the top in an LCG-hashed column when shot; the descent speeds up with kills. Any invader reaching the ground ends the game - show kills and restart on tap.",
"solution_rl": "fn hash(seed: i32) -> i32 {\n let mut h: i32 = seed * 747796405 + 2891336453;\n h = (h >> 16) ^ h;\n if h < 0 { h = 0 - h; }\n h\n}\n\nfn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n host::display::state_set(1, 0);\n host::display::state_set(2, 0);\n host::display::state_set(3, 1);\n host::display::state_set(4, 2);\n host::display::state_set(5, 3);\n host::display::state_set(6, 30);\n host::display::state_set(7, 80);\n host::display::state_set(8, 130);\n host::display::state_set(9, 0);\n host::display::state_set(10, 0);\n }\n let down: i32 = host::display::pointer_down();\n let tap: bool = down == 1 && host::display::state_get(11) == 0;\n host::display::state_set(11, down);\n let score: i32 = host::display::state_get(9);\n if host::display::state_get(10) == 1 {\n host::display::clear(0x140008);\n host::display::draw_string(130, 180, \"THEY LANDED\", 0xff4040, 4);\n host::display::draw_string(150, 250, \"KILLS\", 0x808080, 2);\n host::display::draw_number(260, 244, score, 0xffffff, 3);\n host::display::draw_string(130, 320, \"TAP TO DEFEND AGAIN\", 0x808080, 2);\n if tap { host::display::state_set(0, 0); }\n } else {\n let mut px: i32 = host::display::pointer_x() - 20;\n if px < 0 { px = 0; }\n if px > w - 40 { px = w - 40; }\n let mut blx: i32 = host::display::state_get(1);\n let mut bly: i32 = host::display::state_get(2);\n if tap && bly == 0 {\n blx = px + 18;\n bly = h - 40;\n }\n if bly > 0 {\n bly = bly - 9;\n if bly < 4 { bly = 0; }\n }\n let mut spd: i32 = 1 + score / 5;\n if spd > 4 { spd = 4; }\n host::display::clear(0x000010);\n let mut i: i32 = 0;\n while i < 3 {\n let rnd: i32 = host::display::state_get(3 + i);\n let ix: i32 = hash(rnd * 13 + i) % (w - 32);\n let mut iy: i32 = host::display::state_get(6 + i);\n iy = iy + spd;\n if bly > 0 && blx >= ix && blx < ix + 28 && bly >= iy && bly < iy + 20 {\n host::display::state_set(9, host::display::state_get(9) + 1);\n host::display::state_set(3 + i, rnd + 1);\n iy = 20;\n bly = 0;\n }\n if iy > h - 40 {\n host::display::state_set(10, 1);\n }\n host::display::state_set(6 + i, iy);\n host::display::fill_rect(ix, iy, 28, 20, 0xdd44dd);\n host::display::fill_rect(ix + 4, iy + 6, 6, 6, 0x000000);\n host::display::fill_rect(ix + 18, iy + 6, 6, 6, 0x000000);\n i = i + 1;\n }\n host::display::state_set(1, blx);\n host::display::state_set(2, bly);\n if bly > 0 {\n host::display::fill_rect(blx - 2, bly, 4, 10, 0xffffff);\n }\n host::display::fill_rect(px, h - 24, 40, 12, 0x00ddff);\n host::display::fill_triangle(px + 12, h - 24, px + 28, h - 24, px + 20, h - 36, 0x00ddff);\n host::display::draw_number(8, 6, score, 0xffffff, 2);\n }\n host::display::present();\n}\n",
"tags": [
"game",
"state",
"prng",
"pointer"
]
}