This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SgtElis's profile picture

Worm Select in my program? (A easy way?)

Started by SgtElis, 02 February 2013 - 04:38 AM
SgtElis #1
Posted 02 February 2013 - 05:38 AM
So I'm would like to have a selection thing like the one that the worm program has! Before I have used local bla = read() and then had a number to select but I want to get a little bit more advance! Is there a way to make something like this that dont take up tons of place with code? I have seen some examples but that havn't really worked…

Tnx!

//Elis
KaoS #2
Posted 02 February 2013 - 06:28 AM
take a look at my menu functions in my sig. you would want the basic menu example, no scrolling or anything
remiX #3
Posted 02 February 2013 - 07:05 AM
Well, it's basically a while true do with an os.pullEvent() (waiting for the key event) until enter is pressed.
Something like this? If you need me to explain anything, ask away :)/>/>/>

screenX, screenY = term.getSize()

t = {
	colSel = colours.yellow,
	colNotSel = colours.lime,
	difficulties = {
		{ text = "Easy", diffLevel = 1, diffSpeed = 1 },
		{ text = "Medium", diffLevel = 2, diffSpeed = 0.5 },
		{ text = "Hard", diffLevel = 3, diffSpeed = 0.1 }
	}
}

function getDifficulty()
	local sel = 1 -- define which option is selected by default
	while true do
		for i = 1, #t.difficulties do
			term.setCursorPos(math.floor((screenX-#t.difficulties[i].text)/2), math.floor(screenY/2) - 1 + i)
			term.setTextColour(i == sel and t.colSel or t.colNotSel)
			write(t.difficulties[i].text)
		end
		_, key = os.pullEvent("key")
		if key == keys.down and sel < #t.difficulties then
			sel = sel + 1
		elseif key == keys.up and sel > 1 then
			sel = sel - 1
		elseif key == keys.enter then
			return t.difficulties[sel].text, t.difficulties[sel].diffLevel, t.difficulties[sel].diffSpeed
		end
	end
end

term.clear()

difficulty, level, speed = getDifficulty()

term.clear()
term.setCursorPos(1, 1)
term.setTextColour(colours.yellow)

print( "You chose the difficulty '" .. difficulty .. "' which has the difficulty level of " .. level .. " and speed of " .. speed .. ".\n\nGoodluck!\n" )

term.setTextColour(colours.white)
SgtElis #4
Posted 02 February 2013 - 10:17 AM
Well, it's basically a while true do with an os.pullEvent() (waiting for the key event) until enter is pressed.
Something like this? If you need me to explain anything, ask away :)/>/>/>

screenX, screenY = term.getSize()

t = {
	colSel = colours.yellow,
	colNotSel = colours.lime,
	difficulties = {
		{ text = "Easy", diffLevel = 1, diffSpeed = 1 },
		{ text = "Medium", diffLevel = 2, diffSpeed = 0.5 },
		{ text = "Hard", diffLevel = 3, diffSpeed = 0.1 }
	}
}

function getDifficulty()
	local sel = 1 -- define which option is selected by default
	while true do
		for i = 1, #t.difficulties do
			term.setCursorPos(math.floor((screenX-#t.difficulties[i].text)/2), math.floor(screenY/2) - 1 + i)
			term.setTextColour(i == sel and t.colSel or t.colNotSel)
			write(t.difficulties[i].text)
		end
		_, key = os.pullEvent("key")
		if key == keys.down and sel < #t.difficulties then
			sel = sel + 1
		elseif key == keys.up and sel > 1 then
			sel = sel - 1
		elseif key == keys.enter then
			return t.difficulties[sel].text, t.difficulties[sel].diffLevel, t.difficulties[sel].diffSpeed
		end
	end
end

term.clear()

difficulty, level, speed = getDifficulty()

term.clear()
term.setCursorPos(1, 1)
term.setTextColour(colours.yellow)

print( "You chose the difficulty '" .. difficulty .. "' which has the difficulty level of " .. level .. " and speed of " .. speed .. ".\n\nGoodluck!\n" )

term.setTextColour(colours.white)
Yes, that is what I want! Now, how can I change this so it starts new functions or programs instead of printing what difficulty I choosed and can I add more?
I havn't looked much into the code yet but I will soon, just thought I could ask now to maybe save some time :P/>
remiX #5
Posted 02 February 2013 - 11:18 AM
Hmm, how do you mean? So it brings up the list of certain programs and clicking enter will run them?

Well, using fs.list("/") will give you a table with all programs and directories of the computer (excluding rom) in which you can print that list and then clicking enter will run it using shell.run("program")
KingMachine #6
Posted 02 February 2013 - 11:19 AM
It is my personal opinion that one should not be dabbling in code that they do not wish to learn for themselves in order to reap the benefits of gratification.

EDIT:
By this I mean that if you aren't taking the time to understand what you're working with, you shouldn't be working with it.
KaoS #7
Posted 03 February 2013 - 01:34 AM
It is my personal opinion that one should not be dabbling in code that they do not wish to learn for themselves in order to reap the benefits of gratification.

EDIT:
By this I mean that if you aren't taking the time to understand what you're working with, you shouldn't be working with it.

A very true statement… some people seek gratification though and I suppose if that is what they want I wouldn't stop them
SgtElis #8
Posted 04 February 2013 - 12:28 AM
It is my personal opinion that one should not be dabbling in code that they do not wish to learn for themselves in order to reap the benefits of gratification.

EDIT:
By this I mean that if you aren't taking the time to understand what you're working with, you shouldn't be working with it.
Well, I try to take my time to learn and understand but sometimes you need a little push to get going!