-- Shopping Cart Items
items = {
{name="Apple", price=1},
{name="Banana", price=2},
{name="Cherry", price=3}
}
-- Store cart items
cart = {}
-- Selection index and cart operations
selected_index = 1
cart_total = 0
function _init()
end
function _update()
-- Navigate item list
if (btnp(⬆️)) then
selected_index = max(selected_index - 1, 1)
elseif (btnp(⬇️)) then
selected_index = min(selected_index + 1, #items)
end
-- Add item to cart
if (btnp(🅾️)) then
local item = items[selected_index]
add(cart, item)
cart_total += item.price
end
end
function _draw()
cls()
-- Draw item list
print("Shop")
for i, item in ipairs(items) do
local selector = (i == selected_index) and ">" or " "
print(selector .. item.name .. " - $" .. item.price, 8, 8 + i * 8)
end
-- Draw cart
print("Cart", 64, 8)
local y_offset = 16
for i, item in ipairs(cart) do
print(item.name .. " - $" .. item.price, 64, y_offset)
y_offset += 8
end
-- Draw total
print("Total: $" .. cart_total, 64, y_offset + 8)
end
This is a simplistic example. In a real-world use case, you might want to add more features, such as removing items from the cart or checking out.