summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Harley2020-07-12 15:53:55 +0100
committerTom Harley2020-07-12 15:53:55 +0100
commitc9d9096f14f436bd1ea5d3fb9f5e9e852b4d08e1 (patch)
tree953ddd1b359832cb5876f9b2cd4aea224739e084
parentLots of changes lol (diff)
downloadtyping-twister-c9d9096f14f436bd1ea5d3fb9f5e9e852b4d08e1.tar.gz
typing-twister-c9d9096f14f436bd1ea5d3fb9f5e9e852b4d08e1.zip

potential first submission

-rw-r--r--main.rb112
1 files changed, 82 insertions, 30 deletions
diff --git a/main.rb b/main.rb
index abfbfb3..a1514ea 100644
--- a/main.rb
+++ b/main.rb
@@ -1,5 +1,10 @@
# 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
@@ -55,6 +60,7 @@ def new_challenge(state, outputs, reset: false, sound: true)
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
@@ -62,6 +68,10 @@ def new_challenge(state, outputs, reset: false, sound: true)
(outputs.sounds << "sfx/correct.wav") if sound
end
+ if state.highscore < state.score then
+ state.highscore = state.score
+ end
+
hand = state.hands.sample
puts hand
@@ -85,8 +95,8 @@ def new_challenge(state, outputs, reset: false, sound: true)
finger = finger_choice.sample
- finger = (state.fingers - [state.challenge[:finger]]).sample
puts finger
+
adj_fingers = case finger
when "little finger"
["ring finger"]
@@ -124,10 +134,6 @@ def new_challenge(state, outputs, reset: false, sound: true)
puts "chosen key: #{key or "nil"}"
- # key = state.keys
- # .reject { |key| state.past_challenges.any? { |ch| key == ch[:key] } }
- # .sample
-
# remove old challenge that used the same finger
state.past_challenges
.reject! { |ch| ch[:finger] == finger && ch[:hand] == hand }
@@ -139,7 +145,7 @@ def new_challenge(state, outputs, reset: false, sound: true)
}
puts "state.challenge: #{state.challenge}"
- state.challenge_string = "#{hand} #{finger} on #{key}!"
+ state.challenge_string = "#{hand} #{finger} on #{key_name(state, key)}!"
end
def lifted_key?(state, keys_lifted)
@@ -174,11 +180,13 @@ def game_over(state, outputs, 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
- "oh no! you lifted your #{ch[:hand]} #{ch[:finger]} off of #{ch[:key]}!"
+ "oh no! you lifted your #{ch[:hand]} #{ch[:finger]} off of #{key}!"
when :wrong_key
- "oops! you shouldn't have pressed #{ch[:key]}!"
+ "oops! you shouldn't have pressed #{key}!"
else
"unknown error, my bad :("
end
@@ -195,15 +203,45 @@ def make_challenge_text(state, grid)
}.label
end
-def make_score_text(state, grid)
- {
- x: grid.center.x,
- y: 555,
- text: state.score,
+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)
@@ -227,7 +265,7 @@ def initialise(state, outputs, grid)
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 },
+ 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 },
@@ -241,11 +279,13 @@ def initialise(state, outputs, grid)
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 },
+ 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 },
+ 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 },
@@ -256,10 +296,11 @@ def initialise(state, outputs, grid)
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 },
+ 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 },
+ 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 },
@@ -269,17 +310,19 @@ def initialise(state, outputs, grid)
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, 148, 54], game: false },
+ 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 },
- left_meta: { pos: [ 68, 0, 67, 54], game: false },
- left_alt: { pos: [135, 0, 68, 54], game: false },
+ 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 },
- right_meta: { pos: [608, 0, 67, 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 }
+ right_control: { pos: [743, 0, 67, 54], game: false,
+ name: "right control" }
}
state.keys = state.key_positions
@@ -303,6 +346,8 @@ def initialise(state, outputs, grid)
"right hand"
]
+ state.highscore = 0
+ state.game_count = 0
new_challenge(state, outputs, reset: true, sound: false)
outputs.sounds << "music/playing.ogg"
@@ -346,8 +391,9 @@ def tick(args)
state.past_held_keys = truthies[:held].dup
end
+ end
- else
+ if not state.playing then
outputs.primitives << pressed_keys(state, state.past_held_keys)
@@ -365,7 +411,11 @@ def tick(args)
outputs.primitives << make_challenge_text(state, grid)
if state.score > 0 then
- outputs.primitives << make_score_text(state, grid)
+ outputs.primitives << make_score_text(state, gtk, grid)
+ end
+
+ if state.game_count > 0 then
+ outputs.primitives << make_highscore_text(state, gtk, grid)
end
outputs.primitives <<
@@ -376,4 +426,6 @@ def tick(args)
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}"].label
end