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

Creating a map screen

Started by Brod8362, 21 July 2015 - 05:45 AM
Brod8362 #1
Posted 21 July 2015 - 07:45 AM
I know the title isn't very descriptive, I didn't really know what to put there.
I am trying to create a space that the player (&) can move around in. I created some really basic code for it, but the code doesn't work, and I really don't know why, but I think it is something with os.pullEvent("key") that I did wrong. Here is the code:

Spoiler

-- BASE VARS --
charX = 1
charY = 1
-- START CODE -
function changePos()
key = os.pullEvent("key")
if key == keys.w then
charY = charY-1
end
if key == keys.s then
charY = charY+1
end
if key == keys.d then
charX = charX+1
end
if key == keys.a then
charX = charX-1
else
changePos()
end
end

function drawPos()
term.clear()
term.setCursorPos(charX,charY)
print("&")
end

function code()
changePos()
drawPos()
term.setCursorPos(1,1)
print(charX,",", charY)
end

code()
code()
code()
code()
code()
flaghacker #2
Posted 21 July 2015 - 07:58 AM
The first thing returned by os.pullEvent is the event name. Completely useless in your case, as you filter for key events already, but it's useful when you're not filtering. A correct use would look like this:


event, key = os.pullEvent ("key")

if key == keys.w then
  --stuff

To avoid stuff like this in the future, you can try out functions in the lua interactive command line, you start it by typing "lua" in the shell.
Edited on 21 July 2015 - 06:02 AM
Brod8362 #3
Posted 21 July 2015 - 08:08 AM
Thank you, that fixed it.
MKlegoman357 #4
Posted 21 July 2015 - 08:32 AM
Also, instead of calling your functions over and over again you can simply use an infinite loop. You should use 'elseif' if checking one variable against different values. You should also indent your code for better readability. Consider using locals. And use 'term.write()' (not 'write()'!) when drawing your player or other similar objects, instead of using 'print()'. 'print()' and 'write()' will wrap the text, but 'term.write()' will not and thus will work faster. An improved version of your code would look like this:


-- BASE VARS --
local charX = 1
local charY = 1
-- START CODE -
local function changePos()
  local event, key = os.pullEvent("key")

  if key == keys.w then
    charY = charY - 1
  elseif key == keys.s then
    charY = charY + 1
  elseif key == keys.d then
    charX = charX + 1
  elseif key == keys.a then
    charX = charX - 1
  end
end

local function drawPos()
  term.clear()
  term.setCursorPos(charX, charY)
  term.write("&") --# term.write is better than 'print()' because it's faster. Also, 'print()' will try to wrap your text while 'term.write()' will not.
end

local function code()
  while true do --# an infinite loop
    changePos()
    drawPos()
    term.setCursorPos(1,1)
    print(charX,",", charY)
  end
end

code() --# we only need to run this ONCE because the infinite loop will loop the code forever automatically.