or using types. a bit more complicated. ;)
Global player_money#=0.0
Type shop_item
Field name$
Field price# ; so you can have 1.25
Field stock
End Type
Function createShopItem(name$,price#,stock)
s.shop_item=New shop_item
s\name=name
s\price=price
s\stock=stock
End Function
; this function returns an item given a name
Function getShopItemFromName.shop_item(name$)
name=Lower(name)
For i.shop_item=Each shop_item
If Lower(i\name)=name Then Return i
Next
End Function
; this function "buys" a shop_item
; and deducts the price of it from the player's money
Function buyShopItem(name$)
If player_money=0 Then Return ; you haven't got enough money
item.shop_item=getShopItemFromName(name)
If item=Null
;Print "there is no item called "+name
Return
EndIf
If player_money => item\price
If item\stock>0
; buy it then!
player_money=player_money-item\price
item\stock=item\stock-1
Else
; there is no stock to buy
EndIf
Else
; not enough money
EndIf
End Function
Function countShopItems()
For i.shop_item=Each shop_item
g=g+1
Next
Return g
End Function
Function listShopItems(x,y)
Text x,y,"Name:"
Text x+200,y,"Price:"
Text x+280,y,"Price:"
v=2
For i.shop_item=Each shop_item
Text x,y+(FontHeight()*v),(v-1)+" - "+i\name
Text x+200,y+(FontHeight()*v+1),"£"+i\price
Text x+280,y+(FontHeight()*v+2),i\stock
v=v+1
Next
End Function
;--------------------------------------
; main program
;--------------------------------------
; give the player some money
player_money=5000.0
; create a few items
createShopItem("Fruit",10,100)
createShopItem("Sword",500,3)
createShopItem("Magic Berry Juice",5000,0)
createShopItem("Magic Jam",2500,1)
createShopItem("Normal Jam",1500,6)
createShopItem("Blue Jam",500,16)
; now buy some stuff
While KeyHit(1)=0
Cls
; list the items, their price and stock amount
listShopItems(0,0)
; count how many items there are
numberOfItems=countShopItems()
; draw some stuff
Text 0,(FontHeight()*(numberOfItems+3)),"Press 1-6 to buy something"
Text 0,(FontHeight()*(numberOfItems+4)),"Money left: "+player_money
If KeyHit(2) Then buyShopItem("fruit")
If KeyHit(3) Then buyShopItem("Sword")
If KeyHit(4) Then buyShopItem("Magic Berry Juice")
If KeyHit(5) Then buyShopItem("Magic Jam")
If KeyHit(6) Then buyShopItem("Normal Jam")
If KeyHit(7) Then buyShopItem("Blue Jam")
Flip
Wend
End