Posted 05 November 2012 - 05:02 AM
Hello and welcome!
I think it is meant for noobs but can be used by anyone. Please examine the code and if anything's unclear, don't ask me since I'm a noob myself XD.
Tips and improvements (on either the code or the comments) are greatly appreciated.
Download:
or
http://pastebin.com/UiMJPdGT
or copy the following code:
In case the indentation got fudged up, I can't help that, sorry.
Hope I can help anyone with this and thanks for reading.
~Username
Today I made a really simple and quick example:
A mouse controlled menu.
I think it is meant for noobs but can be used by anyone. Please examine the code and if anything's unclear, don't ask me since I'm a noob myself XD.
Spoiler
If you really want to, you may ask ;-)Tips and improvements (on either the code or the comments) are greatly appreciated.
Download:
pastebin get UiMJPdGT
or
http://pastebin.com/UiMJPdGT
or copy the following code:
Spoiler
--[[ MOUSE MENU TUTORIAL ]]--
--[[ Copyright © 2012 InputUsername ]]--
--[[
State is the variable representing the current menu.
It can be anything as long as it's a string.
It is easier if you keep the string 'state' as short as possible.
]]--
local state = "main"
--[[
Here we define a table containing the options for
the main menu and the menu on the 'info' screen.
]]--
local tMainOptions = { "[ Play ]", "[ Info ]", "[ Quit ]" }
local tInfoOptions = { "[ Credits ]", "[ Back ]" }
--[[
The following lines of code are function definitions for the
draw functions of the menu.
]]--
local function drawMain() -- defines the drawMain function, which obviously draws the main menu
term.clear()
term.setCursorPos(2,1)
term.write("Mouse menu tutorial - by InputUsername")
for i = 1, #tMainOptions do -- starts a for loop to print all the options in the table 'tMainOptions'
term.setCursorPos(2,1 + (i * 2)) -- makes the cursor skip a line every time it prints an option
term.write(tMainOptions[i]) -- prints the menu options as a 'button'
end
end
local function drawGame() -- defines the drawGame function, which draws the contents of the 'game'
term.clear()
term.setCursorPos(2,1)
term.write("Game!")
term.setCursorPos(2,3)
term.write("[ Back ]")
end
local function drawInfo() -- defines the drawInfo function, which is similar to the function 'drawMain()'
term.clear()
term.setCursorPos(2,1)
term.write("Info")
for i = 1, #tInfoOptions do
term.setCursorPos(2,1 + (i * 2))
term.write(tInfoOptions[i])
end
end
local function drawCredits() -- defines the drawCredits function
term.clear()
term.setCursorPos(2,1)
term.write("Credits")
term.setCursorPos(2,2)
term.write("Mouse menu tutorial, [c]2012 InputUsername")
end
--[[
Here we start the menu's functionality
]]--
while true do -- start an infinite loop
if state == "main" then -- if the current menu is the main menu
drawMain() -- calls the drawMain() function, which draws the main menu
local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
if button == 1 then -- if the user clicks with the left button (1 is left, 2 is right)
for i = 1, #tMainOptions do -- some 'complicated' code to check where the mouse click was
if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
if i == 1 then
state = "play"
elseif i == 2 then
state = "info"
elseif i == 3 then
state = "exit"
end
end
end
end
elseif state == "play" then
drawGame() -- draws the contents of the 'game'
local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
if button == 1 then
if X >= 2 and X <= 6 and Y == 3 then
state = "main"
end
end
elseif state == "info" then
drawInfo()
local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
if button == 1 then
for i = 1, #tInfoOptions do
if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
if i == 1 then
state = "credits"
elseif i == 2 then
state = "main"
end
end
end
end
elseif state == "credits" then
drawCredits() -- draw the credits
sleep(2) -- wait 2 seconds
state = "main" -- return to the main menu
elseif state == "exit" then
break
end
end
term.clear()
term.setCursorPos(1,1)
In case the indentation got fudged up, I can't help that, sorry.
Hope I can help anyone with this and thanks for reading.
~Username