Jump to content




Table display help


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

#1 unlimited

  • Members
  • 11 posts

Posted 26 December 2013 - 09:32 AM

Hey i'm trying to build a touchscreen display that lists items in each floor for my shop.
But i'm stuck at the part when i'm trying to list the table, and need to alternate between black and gray in each line,

I need help here:
s = 4
for i = 1, #f1 do
  m.setBackgroundColor(colors.black)
  m.setCursorPos(1, s+1)
  m.write(f1[i])
  m.setBackgroundColor(colors.gray)
  m.setCursorPos(1, s+2)
  m.write(f1[i+1])
  s = s+2
end

It's listing like i want, with alternate background colors but it shows each table key twice.

Here's the full code ive done so far
local m = peripheral.wrap("top")
term.redirect(m)
m.setTextScale(0.5)
m.setTextColor(colors.white)
x, y = m.getSize()
f1 = {"Builder's Wand","Diamond Chest","Electrical Engine","ME 64k Storage","MFSU","Power Drill","Power Saw","Prototype OmniWrench","Quarry","Upgrade Core"}
f2 = {"Acacia Sapling","Acacia Wood","Balsa Sapling","Balsa Wood","Beech Sapling","Beech Wood","Ebony Sapling","Ebony Wood","Kapok Sapling","Kapok Wood","Larch Sapling","Larch Wood","Lime Sapling","Lime Wood","Teak Sapling","Teak Wood"}
paintutils.drawLine(1, 1, x, 1, colors.blue)
paintutils.drawLine(1, 2, x, 2, colors.blue)
paintutils.drawLine(1, 3, x, 3, colors.blue)
paintutils.drawLine(1, 4, x, 4, colors.blue)
paintutils.drawLine(1, y-1, x, y-1, colors.blue)
paintutils.drawLine(1, y, x, y, colors.blue)
m.setCursorPos(x/2-7, 2)
m.write("Ubuy Item Finder")
m.setCursorPos(x/2-13, 3)
m.write("Tap each floor for item list")
m.setCursorPos(x/2-15, y)
m.write("Tap anywhere to get back to menu")
function menu()
s = 8
for i = 1, 4 do
  paintutils.drawLine(1, s+1, x, s+1, colors.gray)
  paintutils.drawLine(1, s+2, x, s+2, colors.gray)
  paintutils.drawLine(1, s+3, x, s+3, colors.gray)
  paintutils.drawLine(1, s+4, x, s+4, colors.gray)
  s = s+8
end
m.setCursorPos(2, 6)
m.setBackgroundColor(colors.black)
m.setTextColor(colors.orange)
m.write("1st Floor")
m.setCursorPos(1,7)
m.setTextColor(colors.white)
m.write("Starting, Essential Items")
m.setCursorPos(2,10)
m.setBackgroundColor(colors.gray)
m.setTextColor(colors.orange)
m.write("2nd Floor")
m.setCursorPos(1,11)
m.setTextColor(colors.white)
m.write("Tree Saplings, Wood Logs")
m.setCursorPos(2, 14)
m.setBackgroundColor(colors.black)
m.setTextColor(colors.orange)
m.write("3rd Floor")
m.setCursorPos(1,15)
m.setTextColor(colors.white)
m.write("Power Armor and Upgrades")
m.setCursorPos(2,18)
m.setBackgroundColor(colors.gray)
m.setTextColor(colors.orange)
m.write("4th Floor")
m.setCursorPos(1,19)
m.setTextColor(colors.white)
m.write("ME System and Storage")
m.setCursorPos(2, 22)
m.setBackgroundColor(colors.black)
m.setTextColor(colors.orange)
m.write("5th Floor")
m.setCursorPos(1,23)
m.setTextColor(colors.white)
m.write("5th")
m.setCursorPos(2,26)
m.setBackgroundColor(colors.gray)
m.setTextColor(colors.orange)
m.write("6th Floor")
m.setCursorPos(1,27)
m.setTextColor(colors.white)
m.write("6th")
m.setCursorPos(2, 30)
m.setBackgroundColor(colors.black)
m.setTextColor(colors.orange)
m.write("7th Floor")
m.setCursorPos(1,31)
m.setTextColor(colors.white)
m.write("7th")
m.setCursorPos(2,34)
m.setBackgroundColor(colors.gray)
m.setTextColor(colors.orange)
m.write("8th Floor")
m.setCursorPos(1,35)
m.setTextColor(colors.white)
m.write("8th")
end
function list()
s = 5
for i = 1, 16 do
  paintutils.drawLine(1, s+1, x, s+1, colors.gray)
  s = s+2
end
end
function first()
list()
m.setBackgroundColor(colors.blue)
m.setCursorPos(x/2-5, 3)
term.clearLine()
m.write("First Floor")
s = 4
for i = 1, #f1 do
  m.setBackgroundColor(colors.black)
  m.setCursorPos(1, s+1)
  m.write(f1[i])
  m.setBackgroundColor(colors.gray)
  m.setCursorPos(1, s+2)
  m.write(f1[i+1])
  s = s+2
end
end
first()
term.restore()

Any other tips to simplify the code are welcome

Thank You

Edited by unlimited, 26 December 2013 - 09:34 AM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 26 December 2013 - 10:43 AM

Set the color based on whether the entry index is even or odd. When you do two entries per iteration and iterate over every entry, of course you'll get most of them twice.

s = 3
for i = 1, #f1 do
  --# check if i is even. Use black if it is and grey if it isn't.
  m.setBackgroundColor(i % 2 == 0 and colors.black or colors.gray)
  m.setCursorPos(1, i + s) --# use s as an offset.
  m.write(f1[i])
end


#3 unlimited

  • Members
  • 11 posts

Posted 26 December 2013 - 12:03 PM

It works, thank you.
I was trying to do something like print ipairs in gray and pairs in black but this is alot better.
I will try to use it on the menu part to shorten the code.
I'm playing on a server without pastebin, don't feel like typing alot xD





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users