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

Help with monitor touch screen shop interface

Started by TannerReed, 03 April 2015 - 07:21 PM
TannerReed #1
Posted 03 April 2015 - 09:21 PM
Hi everyone!

First let me explain my rough idea for the interface. The player walks up to the monitor and it has text detailing how the shop works. After reading the initial page it prompts the user to select the "armour shop" to begin making purchases. Once the user selects the shop they are given a $100 balance in the top right corner and given three options to spend their $$$. One option is "Diamond armour $75", another is "Iron Armour $50" and lastly "Leather Armour $25". Lets say the user chose to purchase the diamond armour, the monitor would now send the player to the next shop and display $25 as the balance. In this shop the user has 4 options, "Normal bow $25", "power bow $30", "knock back bow $30", and "Here". The here button allows the player to opt out of purchasing a bow and to move to the next shop. This is where I need help. If the player chose not to make a purchase here how can I make the computer record what option was purchased in the armour menu and display the correct balance.

Any help is appreciated and I am open to any criticism for I want to learn how to code!


Here is the code I have written:
(Note this is my first ever attempt at coding I'm sure it shows!)


local mon = peripheral.wrap("right")
local bal = 100


function touchScreen()
  event, side, xPos, yPos = os.pullEvent("monitor_touch")
  print(event .. " => Side: " .. tostring(side) .. ", " ..
	"X: " .. tostring(xPos) .. ", " ..
	"Y: " .. tostring(yPos))
end
function compVersion()
  mon.clear()
  mon.setCursorPos(2,1)
  mon.setTextColor(colors.yellow)
  mon.write("Prep Computer v1.43")
end
function balance()
mon.setCursorPos(54,3)
mon.setTextColor(colors.green)
end
function bowMenu()
mon.setCursorPos(25,6)
mon.setTextColor(colors.red)
mon.write("Marksman Shop")
mon.setTextColor(colors.blue)
mon.setCursorPos(22,8)
mon.write("Select your bow")
mon.setCursorPos(15,9)
mon.write("Your bow will come with 16 arrows")
mon.setCursorPos(15,10)
mon.write("Not intrested in a bow? Click")
mon.setBackgroundColor(colors.green)
mon.setCursorPos(44,10)
mon.write("[Here]")
mon.setBackgroundColor(colors.black)
mon.setCursorPos(3,18)
mon.setBackgroundColor(colors.green)
mon.write("[Normal Bow $25]")
mon.setBackgroundColor(colors.black)
mon.setCursorPos(20,18)
mon.setBackgroundColor(colors.green)
mon.write("[Power 1 Bow $30]")
mon.setBackgroundColor(colors.black)
mon.setCursorPos(38,18)
mon.setBackgroundColor(colors.green)
mon.write("[Knock Back Bow $30]")
mon.setBackgroundColor(colors.black)
end
function bowButtons()
  while true do
  touchScreen()
	--Here Button
	if (xPos > 43 and xPos < 51) and (yPos > 9 and yPos < 11) then
	  compVersion()
	  balance()
	  mon.write("$" .. tostring(bal))
	--Normal Bow Button
	elseif (xPos > 2 and xPos < 19) and (yPos > 17 and yPos < 19) then
	  compVersion()
	--Power Bow Button
	elseif (xPos > 20 and xPos < 37) and (yPos > 17 and yPos < 19) then
	  compVersion()
	--Knock Back Bow Button
	elseif (xPos > 37 and xPos < 51) and (yPos > 17 and yPos < 19) then
	  compVersion()
	end
  end
end
--I have no idea what im doing but its WORKING KINDA
--Main Program
compVersion()
mon.setCursorPos(21,6)
mon.setTextColor(colors.red)
mon.write("Welcome to the shop!")
mon.setCursorPos(13,8)
mon.setTextColor(colors.blue)
mon.write("You're given $100 dollars use it wisely")
mon.setCursorPos(13,9)
mon.write("Your balance is in the top right corner")
mon.setCursorPos(13,11)
mon.write("Items you purchase will fall from the sky")
balance()
mon.write("$" .. tostring(bal))
mon.setTextColor(colors.blue)
mon.setCursorPos(18,12)
mon.write("Let's start with some armour!")
mon.setCursorPos(18,10)
mon.write("Note items are non refundable")
mon.setCursorPos(28,18)
mon.setBackgroundColor(colors.green)
mon.write("[Armour]")
mon.setBackgroundColor(colors.black)
while true do
touchScreen()
  if (xPos > 27 and xPos < 37) and (yPos > 17 and yPos < 19) then
	compVersion()
	balance()
	mon.write("$" .. tostring(bal))
	mon.setTextColor(colors.red)
	mon.setCursorPos(27,6)
	mon.write("Armour Shop")
	mon.setTextColor(colors.blue)
	mon.setCursorPos(24,9)
	mon.write("Select your armour:")
	mon.setCursorPos(2,18)
	mon.setBackgroundColor(colors.green)
	mon.write("[Diamond Armour $75]")
	mon.setBackgroundColor(colors.black)
	mon.setBackgroundColor(colors.green)
	mon.setCursorPos(23,18)
	mon.write("[Iron Armour $50]")
	mon.setBackgroundColor(colors.black)
	mon.setBackgroundColor(colors.green)
	mon.setCursorPos(41,18)
	mon.write("[Leather Armour $25]")
	mon.setBackgroundColor(colors.black)
	while true do
	  touchScreen()
	  if (xPos > 2 and xPos < 22) and (yPos > 17 and yPos < 19) then
		compVersion()
		balance()
		mon.write("$" .. tostring(bal-75))
		bowMenu()
		bowButtons()
	  elseif (xPos > 23 and xPos < 40) and (yPos > 17 and yPos <19) then
		 compVersion()
		 balance()
		 mon.write("$" .. tostring(bal-50))
		 bowMenu()
		 bowButtons()
	  elseif (xPos > 41 and xPos < 61) and (yPos > 17 and yPos < 19) then
		compVersion()
		balance()
		mon.write("$" .. tostring(bal-25))
		bowMenu()
		bowButtons()
	  end
	end
  end
end
HPWebcamAble #2
Posted 04 April 2015 - 12:56 AM
Personally, I'd use my Screen API

I'll assume you want to go with your current system though.


So if I understand your question, you want to have the color of the buttons change, depending on whether or not the user can afford the item

It's not too hard
Here's an example for the 'Normal Bow'

term.setCursorPos(3,18)

if bal >= 25 then --# If the user can afford it
  term.setBackgroundColor(colors.green)
else --# The user can't afford it
  term.setBackgroundColor(colors.lightGray)
end

term.write("[Normal Bow $25]")
TannerReed #3
Posted 04 April 2015 - 01:47 AM
Sorry that's not what I was looking for more of a way to carry the balance from one page to the next
HPWebcamAble #4
Posted 04 April 2015 - 03:43 AM
I was looking for more of a way to carry the balance from one page to the next

That was my second guess ;)/>

The sort term answer is to do this:
Spoiler

--#Instead of this:
mon.write("$" .. tostring(bal-75))

--#Do this:
bal = bal - 75
mon.write("$"..tostring(bal))
That way, you are actually changing the value of 'bal', instead of just displaying the result of some math


Long term solution:
SpoilerNow, this isn't bad for your first program, but it could use some adjustments to make it easier to add more things (for example, a page for swords)

balance = 100 --#Starting value

function selectArmor()

  --# Draw the options

  while true do
	--# Wait for the user to choose one, update their balance accordingly
  end

end

function selectBow()
  --# Same format as selectArmor
end

function selectOther()
  --# Another thing
end

while true do

  --# Draw the introduction

  selectArmor()
  selectBow()
  --# Ect
end
When drawing the options, make sure to use the if-statement from my first post to reflect the choices the user has with their current balance
Edited on 04 April 2015 - 01:44 AM
TannerReed #5
Posted 04 April 2015 - 05:14 AM
Sounds good I will give this a try!