Page 1 of 1

[SOLVED] How to make a "fill in the blank" puzzle using user input?

Posted: Tue Dec 19, 2017 3:22 pm
by HungerBoatz
Hello! I am new on using text input. I looked at the love2d official website which is: https://love2d.org/wiki/love.textinput

But i have some problem, How do i make a lot of text input in my game? for example: I type letter 'e' in one place, then I type "l" in another place, and so on. like this puzzle :

Image

Thanks ;)

Re: How to make a "fill in the blank" puzzle using user input?

Posted: Tue Dec 19, 2017 4:02 pm
by mr_happy
Depends! If you wanted to allow the player to click on a blank space and then enter a letter you could do something like this:

Lets say your 'blanks' are represented by boxes 64x64 each at top left of the screen, When the user clicks the mouse (and mouse y co-ord is less than 64, you work out which 'box' they want to enter the letter in, by dividing the mouse x co-ordinate by 64 (I'm simplifying here, ignoring spaces between boxes, clicks outside the boxes etc etc etc). So if they clicked with the mouse at x = 70 it would be in the space occupied by the second box so they want to guess the second letter. You take input and display the letter in the second box - if it matches the second character in your answer.

You got my description spot on BTW, but my name doesn't start with an 'E'!

Re: How to make a "fill in the blank" puzzle using user input?

Posted: Tue Dec 19, 2017 4:45 pm
by zorg
Or, you could treat all those spaces like one string (or characters in a table, which should be better for editing purposes), just record the current position of the cursor, and type away; when textinput gets called for another typed character, it would need to put that character in the "current" slot.

How you visualise the word is completely up to you, whether you want it to show as one word, or as separate spaces like in your image. Those "places" are completely imaginary, they don't need to be separate, that's just the visualization of the underlying logic part.

Re: How to make a "fill in the blank" puzzle using user input?

Posted: Wed Dec 20, 2017 2:00 pm
by HungerBoatz
Now i fixed it already , i used the love.textinput(t) function for my input. I also used the string.upper() function to make all the letters uppercase when they get typed. In my code, i typed something like this:

Code: Select all

function puzzle1:init()
    question = {}
    question.font = love.graphics.getFont()
    question.text = "Living thing, big, fat, gray"
    question.w = question.font:getWidth(question.text)
    question.h = question.font:getHeight(question.text)
    question.x = love.graphics.getWidth() / 2 - question.w
    question.y = 100
    
    answerbox = {}
    answerbox.image = love.graphics.newImage("images/answerbox.png")
    answerbox.x = love.graphics.getWidth() / 2 - answerbox.image:getWidth() / 2
    answerbox.y = love.graphics.getHeight() / 2
    
    letter2 = ""
    letter3 = ""
    letter4 = ""
    letter5 = ""
    letter6 = ""
    letter7 = ""
    letter8 = ""
end

function puzzle1:update(dt)
  
end

function puzzle1:draw()
    love.graphics.setColor(255, 255, 255)
    love.graphics.draw(answerbox.image, answerbox.x, answerbox.y)
    love.graphics.setColor(0, 0, 0)
    love.graphics.print(question.text, question.x, question.y, 0, 2, 2)
    love.graphics.print(hint1, 270, 340, 0, 2, 2)
    
    love.graphics.print(letter2, 305, 340, 0, 2, 2)
    love.graphics.print(letter3, 340, 340, 0, 2, 2)
    love.graphics.print(letter4, 375, 340, 0, 2, 2)
    love.graphics.print(letter5, 410, 340, 0, 2, 2)
    love.graphics.print(letter6, 445, 340, 0, 2, 2)
    love.graphics.print(letter7, 480, 340, 0, 2, 2)
    love.graphics.print(letter8, 515, 340, 0, 2, 2)
end

function puzzle1:textinput(t)
    if letter2 == "" then
      letter2 = string.upper(t)
    elseif letter3 == "" then
      letter3 = string.upper(t)
    elseif letter4 == "" then
      letter4 = string.upper(t)
    elseif letter5 == "" then
      letter5 = string.upper(t)
    elseif letter6 == "" then
      letter6 = string.upper(t)
    elseif letter7 == "" then
      letter7 = string.upper(t)
    elseif letter8 == "" then
      letter8 = string.upper(t)
    end
end
Result: It will asks the user for an input to the second letter. Then if its done, it will asks the user for an input to the third letter, and so on.

BTW Thanks for all of you guys answers, it helped me a lot :)