Edit: Ohh, sorry for double-post. I hope it is ok, I wrote a lot of new stuff.
So, please let me know if I have understood your code.
Code: Select all
explode = function(str)
local t = {}
for i = 1, str:len() do t[i] = str:sub(i, i) end
return t
end
This function gets a string by a parameter. Then it creates a table named "t" and calls a for-loop with the length of the string as "exp2".
In this for-loop it replaces the current index of the table with the current letter .
Finally it returns the table.
Code: Select all
implode = function(t)
local str = ''
for i, v in ipairs(t) do str = str .. v end
return str
end
This function gets a table as parameter. Then it creates a new string named "str" and appends the current value (v?) of the table to the string.
Finally it does the same as explode(), it returns the table.
Code: Select all
newword = function()
local word = explode(words[math.ceil(math.random() * #words)])
local guess = {}
for i = 1, #word do guess[i] = ' _' end
return word, guess
end
I don't understand the first line, it would be nice if you explain.
I understand that it creates a table and calls a for-loop, that sets a " _" for every index of the guess table.
Then it returns the word and guess-table, so they can be replaced with the empty ones.
Code: Select all
love.load = function()
words = {'douse', 'house', 'louse', 'mouse', 'rouse', 'souse',}
word, guess = newword()
end
Sets the wordlist and calls the function newword() once.
Code: Select all
love.keypressed = function(key, code)
if key == 'escape' then word, guess = newword()
elseif code > 96 and code < 123 then -- could be key:find('%l')
for i, v in ipairs(word) do
if v == key then guess[i] = ' ' .. v end
end
end
end
Checks the user keyboard-inputs. If the key is "escape" it calls newword() and so it resets the game.
If the key is letter with a code between 96 and 123 it call a for-loop, that checks if the value of the table "word" is equal to the pressed key.
If it is true, it replace the current index of guess with the key, so the draw function displays the letter.
Code: Select all
love.draw = function()
love.graphics.print(implode(guess), 0, 0)
end
Calls the function implode() with the table "guess" and displays the returned string.
Also, why "love.draw = function()" instead of "function love.draw()"?
It is very important for me to understand your code. We don't learn with copy and paste.
I hope, I wrote it understandable. Please correct my wrong assumptions. Thank you a lot.