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

Random number?

Started by ComputerCraftFan11, 05 March 2012 - 01:45 AM
ComputerCraftFan11 #1
Posted 05 March 2012 - 02:45 AM
Im making a program and when i try to run it, i get a random number (17.)

Heres my code:

selection = 1;
maxhealth = 20;
health = 20;
x = 0;
z = 0;
function drawMenu()
term.clear()
term.setCursorPos(1,1)
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
cPrint("Minecraft")
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
cPrint("")
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
if selection == 1 then
  cPrint("[ Singleplayer ]")
else
  cPrint("Singleplayer")
end
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
if selection == 2 then
  cPrint("[ Quit  Game ]")
else
  cPrint("Quit  Game")
end
end
while true do
local e,key = os.pullEvent( "key" )
if key == 17 or key == 200 then
  if selection > 1 then
   selection = selection - 1
   drawMenu()
  end
elseif key == 31 or key == 208 then
  if selection < 2 then
   selection = selection + 1
   drawMenu()
  end
elseif key == 28 then
  if selection == 2 then -- quit
   os.shutdown()
  end
  break
end
end
function game()
term.clear()
term.setCursorPos(1,1)
print("HP: " ..health.. "/" ..maxhealth.. " X: " ..x.. " Z: " ..z)
end
game()
while true do
local e,key = os.pullEvent( "key" )
if key == 17
  x = x - 1
  game()
elseif key == 31
  x = x + 1
  game()
elseif key == 57 then
  z = z + 1
  wait(0.1)
  z = z + 1
  wait(0.1)
  z = z - 1
  wait(0.1)
  z = z - 1
  game()
end
end
Casper7526 #2
Posted 05 March 2012 - 04:04 AM
#1 you dont need to have multiple functions of the same thing, you only need one center text function
#2 wait() should be sleep()
#3 elseif key == 31 – elseif key == 31 then


Thats all i got for now.
ComputerCraftFan11 #3
Posted 05 March 2012 - 04:07 AM
#1 you dont need to have multiple functions of the same thing, you only need one center text function
#2 wait() should be sleep()
#3 elseif key ==31 – elseif key ==31 then


Thats all i got for now.

thanks, i accidentally put wait() from a game called Roblox that has RBLXLua and uses wait instead of sleep