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

[HELP] Coroutine Timer

Started by TheOddByte, 16 March 2013 - 11:39 PM
TheOddByte #1
Posted 17 March 2013 - 12:39 AM
Well as the title says I need help with creating a coroutine timer since I haven't quite figured out how I should do one..
I need it for returning how long time it has passed after I've completed this game.
Spoiler


os.pullEvent = os.pullEventRaw



function cleanScreen()
term.clear()
term.setCursorPos(1,1)
end

function drawAt(x,y,text)
term.setCursorPos(x,y)
write(text)
end

function randomWord()

t_words = {
"Ain't", "Bodywax", "Order", "Bunny", "Razor", "Nobody", "Calculator", "Malicious", "Eraser", "Banana",
"Remote Control", "25000$", "Alphabetical", "XXmyNameIsAwesome@hotmail.comXX", "Nuclear", "Atomic",
"Listen", "Rednet", "textutils.slowPrint[[Hello World]]", "This is Sparta!!", "[SPOILER] This is a spoiler [/SPOILER]",
"Programmer", "Boxer Tournament", "Chuck Testa", "term.clear() term.setCursorPos(1,1)", "Alphabetical", "Einstein",
"Clock", "Minecraft", "Moddable", "Mods", "Moderators", "T-rex", "Dinosaur", "Deforestation", "Landslide", "Heat wave",
"Hacker", "Invincible", "MMORPG", "Doctor", "Teacher", "Professor", "Fireman", "Police Officer", "Pilot", "Creativity",
"Valentines Day", "Hipster", "Fear", "Sadness", "Bravery", "Genius", "Dumb", "Slow", "Preservation", "Public", "Throught",
"With", "Itch", "Flying", "Width", "Height", "Lenght", "Isotremic", "Pixel", "Buffer", "Thunder", "Storm", "Volcano",
"Overpowered", "League Of Legends", "Laughing out loud", "Rolling on the floor laughing", "Shake", "Pain", "Side", "Ice",
"Ice age", "Ears", "Stomache", "Headache", "Electric", "Figure", "Laundry", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday", "Harlem", "Poison", "Headphones", "Robots", "R2D2", "C3PO", "Bobba Fett", "Luke Skywalker",
"Yoda", "Chewbacca", "Darth Vader", "Coroutines", "Drinking", "Alive", "Die", "Dead", "Beers", "Bears", "Beer", "Bear",
"Baby", "Babies", "Rockstar", "Red Bull", "Ronin", "Monster", "Shark", "Burn", "High", "Dressed", "Boys", "Girls", "Sleep",
"Awakened", "Changed", "My", "Mind", "Intentions", "Feelings", "Charity", "Office", "Five finger fillet", "Person",
"Went", "Live", "Computer", "ComputerCraft", "CraftOS", "Scratch", "Floor", "Lamp", "Wall", "Boat", "Ceiling", "Vain",
"Unlisted", "Smart", "Fast", "Double brackets", "String", "Table", "end", "%appdata%", "Yeah", "Choices", "Darling",
"Guitar", "Hero", "Drums", "Singer", "Piano", "Keyboard", "Filth", "City", "Town", "River", "Last", "Standing",

}

word = t_words[math.random(1, #t_words)]

end



function game()
finished = false
ran = 0
words = 0
newWord = true
while true do
if finished == true then
term.clear()
term.setCursorPos(1,1)
print("You Have Completed The Task")
print("TIME:"..time)
sleep(2)
break
end
time = os.clock()
if newWord == true then
randomWord()
end
if words == num then
term.clear()
term.setCursorPos(1,1)
print[[

	   .-=========-.
	   \'-=======-'/
	   _|   .=.   |_
	  ((|  {{1}}  |))
	   \|   /|\   |/
		\__ ''' __/
		  _') ('_
		_/_______\_
	   /___________\
	 You Have Won

Press any key to continue	
]]
sleep(1)
os.pullEvent("key")
term.clear()
term.setCursorPos(1,1)
if term.isColor() then
term.setTextColor(colors.yellow)
end
print("STATS")
print("")
print("WORDS: "..num)
if term.isColor() then
term.setTextColor(colors.red)
end
print("FAILS: "..fails)
print("")
if term.isColor() then
term.setTextColor(colors.white)
end
print("Press any key to continue")
  sleep(1)
os.pullEvent("key")
term.clear()
term.setCursorPos(1,1)
break
end
term.clear()
term.setCursorPos(1,1)
print("Word: "..word)
input = read()
if input == word then
newWord = true
words = words + 1
elseif input ~= word then
fails = fails + 1
newWord = false
end
end
  end



function events()
event, p1 = os.pullEvent()
if event == "key" and
  p1 == 208 then
  num = num - 1
  if num < 1 then
num = 1
end
elseif  p1 == 200 then
  num = num + 1
  if num > 2000 then
num = 2000
end
elseif p1 == 28 then
inPre = false
game()

end
  end


function  menuEvents()
   event, p1 = os.pullEvent()
if event == "key" and
  p1 == 208 then
  menuStat = menuStat + 1
  if menuStat > 4 then
menuStat = 4
end
elseif  p1 == 200 then
  menuStat = menuStat - 1
  if menuStat < 1 then
menuStat = 1
end
elseif p1 == 28 then
inMenu = false
if menuStat == 1 then
--Single Player
preGame()

elseif menuStat == 2 then
--Multiplayer
preGame()

elseif menuStat == 3 then
credits()

elseif menuStat == 4 then
--Exit
cleanScreen()

end
end
end




  function drawMenu()
  term.clear()
  term.setCursorPos(1,1)
  term.setCursorPos(1,3)
  print("Words ["..num.."]")
print("")
print("Press [Enter] to continue")
  end


function drawMenuArt()
local mArt = {
"+------------------------------------------------+",
"|:::::::::::::::+----Typer----+::::::::::::::::::|",
"+------------------------------------------------+",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"|												|",
"+------------------------------------------------+",
}
local mOptions = {
"Single Player", "Multiplayer", "Credits", "Exit",
}
for i = 1, #mArt do
drawAt(1,i,mArt[i])
end

for i = 1,#mOptions do
drawAt(4,i + 4,mOptions[i])
end
drawAt(2,4 + menuStat,">")
  end


  function credits()

inCredits = true
yCrdPos = 10
scrollBar = 4

while inCredits do
cleanScreen()
drawCredits()
drawExtra()
cEvents()
end
end


--CREDITS


function cEvents()
event, p1 = os.pullEvent()

		if event == "key" and
		  p1 == 28 then
inCredits = false
cleanScreen()


elseif p1 == 200 then
yCrdPos = yCrdPos + 1
scrollBar = scrollBar - 0.5  
if scrollBar < 4 then
scrollBar = 4
yCrdPos = 10
end

elseif p1 == 208 then
		 yCrdPos = yCrdPos - 1
scrollBar = scrollBar + 0.5
if scrollBar > 17 then
scrollBar = 17
yCrdPos = yCrdPos + 1
end
		  end
			end  


function drawExtra()
drawAt(1,1,"+-------------------------------------------------+")
drawAt(1,2,"|				   +--CREDITS--+				 |")
drawAt(1,3,"|-------------------------------------------------|")

drawAt(1,18,"+-------------------------------------------------+")
drawAt(1,19,"			  --PRESS [ENTER] TO EXIT--			  ")


end



function drawCredits()



creditsMenuArt = {  
"+-------------------------------------------------+",
"|				   +--CREDITS--+				 |",
"|-------------------------------------------------|",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"|											   | |",
"+-------------------------------------------------+",
"			   --PRESS [ENTER] TO EXIT--			",
}

creditsMenuArtCol = {  
"												   ",
"					+--CREDITS--+				  ",
"												   ",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"												| |",
"+-------------------------------------------------+",
"			   --PRESS [ENTER] TO EXIT--			",
}



for i = 1, #creditsMenuArt do
   drawAt(1,i,creditsMenuArt[i])
   drawAt(50,scrollBar,"=")
	  drawAt(20,yCrdPos - 3,"Typer 2013(c)")
   drawAt(18,yCrdPos,"Author: Hellkid98")
   drawAt(19,yCrdPos + 3,"+-Programmers-+")
   drawAt(22,yCrdPos + 4,"Hellkid98")
   end
  end




function preGame()
fails = 0
num = 1
inPre = true
while inPre do
drawMenu()
events()
  end
   end


  function main()
menuStat = 1
inMenu = true
while inMenu do
cleanScreen()
drawMenuArt()
menuEvents()
end
end  

  main()


SpoilerOr do this
————-
pastebin get B2A21J5z typer
Kingdaro #2
Posted 17 March 2013 - 07:31 AM
If you just subtract the ending os.clock() with the os.clock() when the game started, you should get the sufficient time without having to create any coroutines or parallels.


local startTime = os.clock()

doGameStuff()

local endTime = os.clock()

local totalTime = endTime - startTime
print(totalTime)

If you want to find the current game running time, keep startTime recorded, and just subtract the current os.clock() from the startTime. A small example demonstrating this concept:


local startTime = os.clock()
local endTime

local updateTimer = os.startTimer(0.05)
while true do
  local ev, p1 = os.pullEvent()
  if ev == 'timer' and p1 == updateTimer then
    term.clear()
    term.setCursorPos(1,1)
    print('Current Time: ' .. (os.clock() - startTime))
    updateTimer = os.startTimer(0.05)
  elseif ev == 'key' then
    endTime = os.clock()
    break
  end
end

term.clear()
term.setCursorPos(1,1)
print('Your time is ' .. (endTime - startTime))
TheOddByte #3
Posted 17 March 2013 - 10:16 AM
If you just subtract the ending os.clock() with the os.clock() when the game started, you should get the sufficient time without having to create any coroutines or parallels.


local startTime = os.clock()

doGameStuff()

local endTime = os.clock()

local totalTime = endTime - startTime
print(totalTime)

If you want to find the current game running time, keep startTime recorded, and just subtract the current os.clock() from the startTime. A small example demonstrating this concept:


local startTime = os.clock()
local endTime

local updateTimer = os.startTimer(0.05)
while true do
  local ev, p1 = os.pullEvent()
  if ev == 'timer' and p1 == updateTimer then
	term.clear()
	term.setCursorPos(1,1)
	print('Current Time: ' .. (os.clock() - startTime))
	updateTimer = os.startTimer(0.05)
  elseif ev == 'key' then
	endTime = os.clock()
	break
  end
end

term.clear()
term.setCursorPos(1,1)
print('Your time is ' .. (endTime - startTime))
Thx so much!
I thought no one would respond.
I used the top one of those two..
I did'nt think it was that simple with that one, About the other one.. Can
that one run while it is using read() or what?
TheOddByte #4
Posted 17 March 2013 - 12:06 PM
And now I'm wondering how I should make it print words per min in the end
I think i need to do something like this…

wMin = totalTime / <something>
remiX #5
Posted 17 March 2013 - 12:42 PM
Well, have a variable increment everything you type the correct word and then
wordsPerMin = correctWords / 60

I see this is the typer program you made a video about :P/> Nice
TheOddByte #6
Posted 18 March 2013 - 12:49 AM
Yep it is.. Why don't you test it and see how fast you are at typing..
The most updated link is always here.
Will be much more fun when multiplayer is done.