summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--main.rb271
2 files changed, 273 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..db500b9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*~
+*.sw*
diff --git a/main.rb b/main.rb
new file mode 100644
index 0000000..0a8cf78
--- /dev/null
+++ b/main.rb
@@ -0,0 +1,271 @@
+# vim: ft=ruby ts=2 sw=2 et tw=72
+
+def offset(pos, off)
+ x, y, *rest = pos
+ [x + off.x, y + off.y, *rest]
+end
+
+def colour_key(state, key, colour)
+ puts "colour_key: #{state.key_positions[key]}"
+ pos = offset(state.key_positions[key][:pos], state.keyboard.pos)
+ [*pos, *colour].solid
+end
+
+def make_background(grid)
+ [*grid.rect, 80, 80, 150].solid
+end
+
+def new_challenge(state, reset: false)
+
+ if reset then
+ state.past_challenges = []
+ else
+ state.past_challenges << state.challenge
+ end
+
+ hand = state.hands.sample
+ finger = state.fingers.sample
+
+ # remove old challenge that used the same finger
+ state.past_challenges
+ .reject! { |ch| ch[:finger] == finger && ch[:hand] == hand }
+
+ key = state.keys
+ .reject { |key| state.past_challenges.any? { |ch| key == ch[:key] } }
+ .sample
+
+ state.challenge = {
+ hand: hand,
+ finger: finger,
+ key: key
+ }
+
+ puts "state.challenge: #{state.challenge}"
+ state.challenge_string = "#{hand} #{finger} on #{key}!"
+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])
+ .detect { |key| key != state.challenge[:key] }
+
+ return nil unless err
+
+ {
+ key: err,
+ fail_reason: :wrong_key
+ }
+end
+
+def game_over(state, ch)
+ state.playing = false
+ state.failed_challenge = ch
+
+ puts "game_over: #{ch.to_s}"
+ state.challenge_string = case ch[:fail_reason]
+ when :lift
+ "oh no! you lifted your #{ch[:hand]} #{ch[:finger]} off of #{ch[:key]}!"
+ when :wrong_key
+ "oops! you shouldn't have pressed #{ch[: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: 1,
+ alignment_enum: 1,
+ font: "fonts/kenney_bold.ttf"
+ }.label
+end
+
+def initialise(state, 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 },
+ 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 },
+ close_square_brace: { pos: [675, 162, *normal_size], game: true },
+ back_slash: { pos: [729, 162, 81, 54], game: false },
+
+ caps_lock: { pos: [ 0, 108, 95, 54], game: false },
+ 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_quote: { pos: [635, 108, *normal_size], game: true },
+ enter: { pos: [689, 108, 121, 54], game: false },
+
+ left_shift: { pos: [0, 54, 122, 54], game: false },
+ 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 },
+ right_shift: { pos: [662, 54, *normal_size], game: false },
+
+ left_control: { pos: [ 0, 0, 68, 54], game: false },
+ left_meta: { pos: [ 68, 0, 67, 54], game: false },
+ left_alt: { pos: [135, 0, 68, 54], game: false },
+ space: { pos: [203, 0, 337, 54], game: false },
+ right_alt: { pos: [560, 0, 68, 54], game: false },
+ right_meta: { pos: [614, 0, 67, 0], game: false },
+ menu: { pos: [681, 0, 68, 54], game: false },
+ right_control: { pos: [735, 0, 67, 54], game: false }
+ }
+
+ puts "huh"
+
+ state.keys = state.key_positions
+ .select { |key| puts key ; key[1][:game] }
+ .keys
+
+ puts "huh"
+ puts "state.keys: #{state.keys.to_s}"
+
+ 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"
+ ]
+
+ new_challenge(state, reset: true)
+end
+
+def tick(args)
+ grid, inputs, state, outputs, runtime, passes = args.destructure
+ gtk = args.gtk
+
+ initialise(state, 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]
+
+ 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, failed_challenge)
+ else
+
+ new_challenge(state) if truthies[:down]
+ .any? { |key| key == state.challenge[:key] }
+
+ outputs.primitives <<
+ truthies[:held]
+ .select { |key| state.key_positions[key][:game] }
+ .map { |key| colour_key(state, key, blue) }
+
+ outputs.primitives << colour_key(state, state.challenge[:key], green)
+
+ state.past_held_keys = truthies[:held].dup
+ end
+
+ else
+
+ puts state.past_held_keys
+ puts state.past_held_keys
+ .select { |key| state.key_positions.has_key? key }
+
+ outputs.primitives << state.past_held_keys
+ .select { |key| state.key_positions.has_key? key }
+ .map { |key| puts key.to_s ; colour_key(state, key, blue) }
+
+ outputs.primitives << colour_key(state, state.failed_challenge[:key], red)
+
+ if inputs.keyboard.key_down.space then
+ new_challenge(state, reset: true)
+ state.playing = true
+ end
+
+ end
+
+ outputs.primitives << make_challenge_text(state, grid)
+
+ 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
+end