Jump to content




Implementing High Score?

lua

  • You cannot reply to this topic
2 replies to this topic

#1 moneymaster2012

  • Members
  • 38 posts

Posted 03 July 2016 - 07:53 PM

Hey!

I made a custom game from scratch, completely mine. I am pretty proud of it, but I want to add more. I want to make it more fun, so how would I implement high-score code so each time the player guesses with the least amount of guesses overrides the current high-score?

Here is my code:
tries = 1
term.clear()
term.setCursorPos(5,10)
print("Guess The NUMBER!")
sleep(0.25)
term.clear()
sleep(0.25)
term.setCursorPos(5,10)
print("Guess The NUMBER!")
sleep(0.5)
term.clear()
sleep(0.25)
term.setCursorPos(5,10)
print("Guess The NUMBER!")
sleep(2)
term.clear()
numb = math.random(1, 100)
term.setCursorPos(1,1)
print("A number has been chosen by the computer. Guess the number between 1 to 100. You will be given clues.")
term.setCursorPos(1,5)
while true do
  write"Guess: "
  guess = io.read()
  guess = tonumber(guess)
  if guess == numb then
	term.clear()
	term.setCursorPos(1,1)
	print("You won with "..tries.." tries! The number was "..numb)
	sleep(7)
	os.shutdown()
  else
	if guess > numb then
	  print("Your guess was too high!")
	elseif guess < numb then
	  print("Your guess was too low!")
	else
	  print("Guess numbers, not letters!")
	end
	tries = tries + 1
  end
end

Thanks!

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 03 July 2016 - 08:11 PM

If you want to keep the high scores only as long as the program runs, you could wrap the entire program in a loop and save the high score in a variable outside the loop. If you want high scores to persist, though, you're in for a tougher ride. You'll want to write the high score to a file and read it back on program startup. The FS API will help with that.

#3 moneymaster2012

  • Members
  • 38 posts

Posted 03 July 2016 - 08:16 PM

View PostLBPHacker, on 03 July 2016 - 08:11 PM, said:

If you want to keep the high scores only as long as the program runs, you could wrap the entire program in a loop and save the high score in a variable outside the loop. If you want high scores to persist, though, you're in for a tougher ride. You'll want to write the high score to a file and read it back on program startup. The FS API will help with that.

Ugh haha. I've done this before with floppy disk writing and locks. I guess I have to get back to it





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users