# vim: ft=ruby ts=2 sw=2 et tw=72 def key_name(state, key_id) res = state.key_positions[key_id][:name] res or key_id end def dist(a, b) dx = b.x - a.x dy = b.y - a.y Math.sqrt(dx * dx + dy * dy) end def key_dist(state, a, b) dist(state.key_positions[a][:pos], state.key_positions[b][:pos]) end def offset(pos, off) x, y, *rest = pos [x + off.x, y + off.y, *rest] end def colour_key(state, key, colour) x = 3 pos = offset(state.key_positions[key][:pos], state.keyboard.pos) [pos.x + x, pos.y + x, pos.w - x * 2, pos.h - x * 2, *colour].solid end def pressed_keys(state, keys) blue = [50, 50, 187, 200] cyan = [50, 187, 187, 200] valid_keys = keys .select { |key| state.keys.include? key } .reject { |key| key == state.failed_challenge[:key] } valid_keys.map { |key| colour = state.past_challenges.any? { |ch| ch[:key] == key } ? blue : cyan colour_key(state, key, colour) } end def edge_keys(state) green = [50, 187, 50, 200] grey = [0, 0, 0, 100] always_grey = state.key_positions .reject { |_k, v| v[:game] } .reject { |k, _v| [:backspace, :space, :tab].include? k } .map { |k, _v| colour_key(state, k, grey) } maybe_green = [:space, :tab] .map { |key| colour_key(state, key, state.playing ? grey : green) } always_grey.append(maybe_green) end def make_background(grid) [*grid.rect, 80, 80, 150].solid end def new_fingerless_challenge(state) key = state.keys .reject { |key| state.past_challenges.any? { |ch| key == ch[:key] } } .sample state.challenge = { key: key } state.challenge_string = key_name(state, key) end def text_in_key(state, gtk, key_id, text, size: 1, font: "fonts/kenney_bold.ttf", **extra) key_pos = state.key_positions[key_id][:pos] key_center = [key_pos.x + key_pos[2].half, key_pos.y + key_pos[3].half] key_center = offset(key_center, state.keyboard.pos) box = gtk.calcstringbox(text, size, font) key_center = offset(key_center, [3, box.y.half]) { x: key_center.x, y: key_center.y, text: text, size_enum: size, alignment_enum: 1, font: font, **extra }.label end def new_fingered_challenge(state) hand = state.hands.sample puts hand unused_fingers = state.fingers .reject { |f| state.past_challenges .any? { |ch| f == ch[:finger] && hand == ch[:hand] } } fingers_but_most_recent = state.fingers - [state.challenge[:finger]] puts "unused fingers: #{unused_fingers}" finger_choice = case [0, unused_fingers.length].sample when 0 fingers_but_most_recent else unused_fingers end puts "finger choice: #{finger_choice}" finger = finger_choice.sample puts finger adj_fingers = case finger when "little finger" ["ring finger"] when "ring finger" ["little finger", "middle finger"] when "middle finger" ["ring finger", "index finger"] when "index finger" ["middle finger", "thumb"] when "thumb" ["index finger"] end puts adj_fingers.to_s adj_finger_keys = state.past_challenges .select { |ch| adj_fingers.include? ch[:finger] } .select { |ch| hand == ch[:hand] } .map { |ch| ch[:key] } puts adj_finger_keys.to_s available_keys = state.keys .reject { |key| state.past_challenges.any? { |ch| key == ch[:key] } } puts "available keys: #{available_keys.to_s}" nearby_keys = available_keys .select { |key| adj_finger_keys .all? { |k2| key_dist(state, key, k2) < 250 } } puts "nearby_keys: #{nearby_keys.to_s}" key = (nearby_keys or available_keys).sample puts "chosen key: #{key or "nil"}" # remove old challenge that used the same finger state.past_challenges .reject! { |ch| ch[:finger] == finger && ch[:hand] == hand } state.challenge = { hand: hand, finger: finger, key: key } puts "state.challenge: #{state.challenge}" state.challenge_string = "#{hand} #{finger} on #{key_name(state, key)}!" end def new_challenge(state, outputs, sound: true, reset: false) puts "jfiweopbnf" if reset then puts "reset" state.past_challenges = [] state.score = 0 state.game_count += 1 (outputs.sounds << "sfx/game_start.wav") if sound else puts "no reset" state.past_challenges << state.challenge state.score += 1 (outputs.sounds << "sfx/correct.wav") if sound end puts "fweiolfn" if state.highscore < state.score then state.highscore = state.score end state.use_fingers ? new_fingered_challenge(state) : new_fingerless_challenge(state) end def lifted_key?(state, keys_lifted) res = state.past_challenges .detect { |ch| keys_lifted.include? ch[:key] } return nil unless res res[:fail_reason] = :lift puts "lifted_key?: #{res}" res end def erroneous_press?(state, keys_pressed) err = (keys_pressed - [:char, :raw_key]) .select { |key| state.keys.include? key } .detect { |key| key != state.challenge[:key] } return nil unless err { key: err, fail_reason: :wrong_key } end def game_over(state, outputs, ch) state.playing = false state.failed_challenge = ch outputs.sounds << "sfx/incorrect.wav" puts "game_over: #{ch.to_s}" key = key_name(state, ch[:key]) state.challenge_string = case ch[:fail_reason] when :lift ch[:finger] ? "oh no! you lifted your #{ch[:hand]} #{ch[:finger]} off of #{key}!" : "oh no! you lifted your finger off of #{key}!" when :wrong_key "oops! you shouldn't have pressed #{key}!" else "unknown error, my bad :(" end end def make_challenge_text(state, grid) { x: grid.center.x, y: 600, text: state.challenge_string, size_enum: 3, alignment_enum: 1, font: "fonts/kenney_bold.ttf" }.label end def make_labelled_number(gtk, pos, label, value) score_text = { # x: grid.center.x, # y: 555, x: pos.x, y: pos.y, text: value.to_s, size_enum: 10, alignment_enum: 1, font: "fonts/kenney_bold.ttf" }.label score_size = gtk.calcstringbox(score_text.text, score_text.size_enum, score_text.font) score_label = { x: score_text.x - score_size.x.half, text: label, size_enum: -3, alignment_enum: 2, font: "fonts/kenney_bold.ttf" }.label score_label_size = gtk.calcstringbox(score_label.text, score_label.size_enum, score_label.font) score_label[:y] = score_text.y - score_size.y.half [score_text, score_label] end def make_score_text(state, gtk, grid) make_labelled_number(gtk, [grid.center.x, 555], "score:", state.score) end def make_highscore_text(state, gtk, grid) make_labelled_number(gtk, [grid.center.x + 350, 555], "highscore:", state.highscore) end def initialise(state, outputs, grid) return if state.initialised state.initialised = true state.playing = true normal_size = [54, 54] state.key_positions = { backtick: { pos: [ 0, 216, *normal_size], game: false }, one: { pos: [ 54, 216, *normal_size], game: true }, two: { pos: [108, 216, *normal_size], game: true }, three: { pos: [162, 216, *normal_size], game: true }, four: { pos: [216, 216, *normal_size], game: true }, five: { pos: [270, 216, *normal_size], game: true }, six: { pos: [324, 216, *normal_size], game: true }, seven: { pos: [378, 216, *normal_size], game: true }, eight: { pos: [432, 216, *normal_size], game: true }, nine: { pos: [486, 216, *normal_size], game: true }, zero: { pos: [540, 216, *normal_size], game: true }, hyphen: { pos: [594, 216, *normal_size], game: true }, equal_sign: { pos: [648, 216, *normal_size], game: true, name: "equals" }, backspace: { pos: [702, 216, 108, 54], game: false }, tab: { pos: [ 0, 162, 81, 54], game: false }, q: { pos: [ 81, 162, *normal_size], game: true }, w: { pos: [135, 162, *normal_size], game: true }, e: { pos: [189, 162, *normal_size], game: true }, r: { pos: [243, 162, *normal_size], game: true }, t: { pos: [297, 162, *normal_size], game: true }, y: { pos: [351, 162, *normal_size], game: true }, u: { pos: [405, 162, *normal_size], game: true }, i: { pos: [459, 162, *normal_size], game: true }, o: { pos: [513, 162, *normal_size], game: true }, p: { pos: [567, 162, *normal_size], game: true }, open_square_brace: { pos: [621, 162, *normal_size], game: true, name: "open square brace" }, close_square_brace: { pos: [675, 162, *normal_size], game: true, name: "closed square brace" }, back_slash: { pos: [729, 162, 81, 54], game: false, name: "backslash" }, caps_lock: { pos: [ 0, 108, 95, 54], game: false, name: "caps lock" }, a: { pos: [ 95, 108, *normal_size], game: true }, s: { pos: [149, 108, *normal_size], game: true }, d: { pos: [203, 108, *normal_size], game: true }, f: { pos: [257, 108, *normal_size], game: true }, g: { pos: [311, 108, *normal_size], game: true }, h: { pos: [365, 108, *normal_size], game: true }, j: { pos: [419, 108, *normal_size], game: true }, k: { pos: [473, 108, *normal_size], game: true }, l: { pos: [527, 108, *normal_size], game: true }, semicolon: { pos: [581, 108, *normal_size], game: true }, single_quotation_mark: { pos: [635, 108, *normal_size], game: true, name: "apostrophe" }, enter: { pos: [689, 108, 121, 54], game: false }, left_shift: { pos: [0, 54, 122, 54], game: false, name: "left shift" }, z: { pos: [122, 54, *normal_size], game: true }, x: { pos: [176, 54, *normal_size], game: true }, c: { pos: [230, 54, *normal_size], game: true }, v: { pos: [284, 54, *normal_size], game: true }, b: { pos: [338, 54, *normal_size], game: true }, n: { pos: [392, 54, *normal_size], game: true }, m: { pos: [446, 54, *normal_size], game: true }, comma: { pos: [500, 54, *normal_size], game: true }, period: { pos: [554, 54, *normal_size], game: true }, forward_slash: { pos: [608, 54, *normal_size], game: true, name: "forward slash" }, right_shift: { pos: [662, 54, 148, 54], game: false, name: "right shift" }, left_control: { pos: [0, 0, 68, 54], game: false, name: "left control" }, left_meta: { pos: [ 68, 0, 67, 54], game: false, name: "left meta" }, left_alt: { pos: [135, 0, 68, 54], game: false, name: "left alt" }, space: { pos: [203, 0, 337, 54], game: false }, right_alt: { pos: [540, 0, 68, 54], game: false, name: "right alt" }, right_meta: { pos: [608, 0, 67, 54], game: false, name: "right meta" }, menu: { pos: [675, 0, 68, 54], game: false }, right_control: { pos: [743, 0, 67, 54], game: false, name: "right control" } } state.keys = state.key_positions .select { |_k, v| v[:game] } .keys state.keyboard.pos = offset(grid.center, [-405, -135]) state.keyboard.img = "sprites/keyboard-us.png" state.keyboard.key_img = "sprites/keys-us.png" state.fingers = [ "little finger", "ring finger", "middle finger", "index finger", "thumb" ] state.hands = [ "left hand", "right hand" ] state.highscore = 0 state.game_count = 0 state.use_fingers = true new_challenge(state, outputs, reset: true, sound: false) outputs.sounds << "music/playing2.ogg" puts "initialisation complete" end def tick(args) grid, inputs, state, outputs, runtime, passes = args.destructure gtk = args.gtk initialise(state, outputs, grid) outputs.primitives << make_background(grid) outputs.primitives << [*state.keyboard.pos, 810, 270, state.keyboard.img].sprite red = [187, 50, 50, 200] green = [50, 187, 50, 200] blue = [50, 50, 187, 200] grey_text = { r: 64, g: 64, b: 64, a: 255 } truthies = { up: inputs.keyboard.key_up.truthy_keys, down: inputs.keyboard.key_down.truthy_keys, held: inputs.keyboard.key_held.truthy_keys } if state.playing then failed_challenge = lifted_key?(state, truthies[:up]) || erroneous_press?(state, truthies[:down]) if failed_challenge then game_over(state, outputs, failed_challenge) else new_challenge(state, outputs) if truthies[:down] .any? { |key| key == state.challenge[:key] } outputs.primitives << pressed_keys(state, truthies[:held]) outputs.primitives << colour_key(state, state.challenge[:key], green) state.past_held_keys = truthies[:held].dup end end if not state.playing then outputs.primitives << pressed_keys(state, state.past_held_keys) outputs.primitives << colour_key(state, state.failed_challenge[:key], red) if inputs.keyboard.key_down.space then new_challenge(state, outputs, reset: true) state.playing = true end if inputs.keyboard.key_down.tab then state.use_fingers = !state.use_fingers outputs.sounds << "sfx/correct.wav" end end if inputs.keyboard.key_down.backspace then if state.silent then outputs.sounds << "music/playing2.ogg" state.silent = false else $gtk.stop_music state.silent = true end end outputs.primitives << edge_keys(state) outputs.primitives << make_challenge_text(state, grid) if state.score > 0 then outputs.primitives << make_score_text(state, gtk, grid) end if state.game_count > 1 then outputs.primitives << make_highscore_text(state, gtk, grid) end bs_text = state.silent ? "unmute" : "mute" outputs.primitives << text_in_key(state, gtk, :backspace, bs_text, **grey_text) tab_text = state.playing ? "tab" : "mode" tab_size = state.playing ? 5 : 2 outputs.primitives << text_in_key(state, gtk, :tab, tab_text, size: tab_size, **grey_text) space_text = state.playing ? "space" : "try again" outputs.primitives << text_in_key(state, gtk, :space, space_text, **grey_text) outputs.primitives << [*state.keyboard.pos, 810, 270, state.keyboard.key_img].sprite outputs.debug << [30, 50, inputs.keyboard.key_held.truthy_keys.to_s].label outputs.debug << [30, 75, state.past_held_keys.to_s].label outputs.debug << [30, 100, state.challenge.to_s].label outputs.debug << [30, 125, state.past_challenges.map { |ch| ch[:key] }.to_s].label outputs.debug << [30, 150, "score: #{state.score}; " + "highscore: #{state.highscore}; " + "game count: #{state.game_count}"].label end