1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
# 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
|