Home > Blockchain >  How to Check if a User :key_down is Present in an Array/String
How to Check if a User :key_down is Present in an Array/String

Time:04-07

I am building a Hangman game in Ruby2d. I want to be able to tell if the key a user presses is present within an array of my chosen word.

I tried this, but nothing happens at all:

on :key_down do |event|
    if chosen_array.include? event.key  
        print "Correct!"
  end
end

CodePudding user response:

This should show result in ruby2d window

require "ruby2d"

chosen_array = %w[a b c]

on :key_down do |event|
  clear

  result =
    if chosen_array.include?(event.key.downcase)
      "Correct!"
    else
      "Wrong"
    end

  Text.new(result)
end

show

If you press a, b or c, result in window will be "Correct!", otherwise "Wrong!"

  • Related