diff options
Diffstat (limited to 'main.rb')
-rw-r--r-- | main.rb | 131 |
1 files changed, 110 insertions, 21 deletions
@@ -44,33 +44,55 @@ def edge_keys(state) green = [50, 187, 50, 200] grey = [0, 0, 0, 100] - state.key_positions + always_grey = state.key_positions .reject { |_k, v| v[:game] } - .reject { |k, _v| k == :space } + .reject { |k, _v| [:backspace, :space, :tab].include? k } .map { |k, _v| colour_key(state, k, grey) } - .append(colour_key(state, :space, state.playing ? grey : green)) + + 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_challenge(state, outputs, reset: false, sound: true) +def new_fingerless_challenge(state) + key = state.keys + .reject { |key| state.past_challenges.any? { |ch| key == ch[:key] } } + .sample - if reset then - state.past_challenges = [] - state.score = 0 - state.game_count += 1 - (outputs.sounds << "sfx/game_start.wav") if sound - else - state.past_challenges << state.challenge - state.score += 1 - (outputs.sounds << "sfx/correct.wav") if sound - end + state.challenge = { + key: key + } - if state.highscore < state.score then - state.highscore = state.score - end + 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 @@ -148,6 +170,33 @@ def new_challenge(state, outputs, reset: false, sound: true) 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] } @@ -184,7 +233,9 @@ def game_over(state, outputs, ch) key = key_name(state, ch[:key]) state.challenge_string = case ch[:fail_reason] when :lift - "oh no! you lifted your #{ch[:hand]} #{ch[:finger]} off of #{key}!" + 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 @@ -348,9 +399,13 @@ def initialise(state, outputs, grid) state.highscore = 0 state.game_count = 0 + + state.use_fingers = true new_challenge(state, outputs, reset: true, sound: false) - outputs.sounds << "music/playing.ogg" + outputs.sounds << "music/playing2.ogg" + + puts "initialisation complete" end def tick(args) @@ -367,6 +422,7 @@ def tick(args) 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, @@ -390,6 +446,7 @@ def tick(args) outputs.primitives << colour_key(state, state.challenge[:key], green) state.past_held_keys = truthies[:held].dup + end end @@ -404,6 +461,21 @@ def tick(args) 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) @@ -414,10 +486,26 @@ def tick(args) outputs.primitives << make_score_text(state, gtk, grid) end - if state.game_count > 0 then + 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 @@ -427,5 +515,6 @@ def tick(args) outputs.debug << [30, 125, state.past_challenges.map { |ch| ch[:key] }.to_s].label outputs.debug << [30, 150, "score: #{state.score}; " + - "highscore: #{state.highscore}"].label + "highscore: #{state.highscore}; " + + "game count: #{state.game_count}"].label end |