uhh how do I do ths

Blitz3D Forums/Blitz3D Beginners Area/uhh how do I do ths

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's the new version:

Global Money# = 100 ;The players money
Global ItemFile$ = "Items.dat"	;Stores all the items you save
Global GameFile$ = "Game.dat"	;Stores all the items and their current availability in stores
								;as well as how many the player has of them and how much money the player has
Type Item
Field name$		;Name of the item
Field desc$		;Description of the item
Field cost#		;Price of the item
Field ammount	;How many items of this type are available
Field owned		;How many of this item the player posesses
End Type

;DEMO CODE:
Graphics 800,600,0,2

;Make some items
CreateItem("Coffee","Yummy beverage with extra cafein!",10)
CreateItem("Tea","A nice drink from china",5)
CreateItem("Chocolate milk","Hot chocolate with extra milk and sugar",15)
CreateItem("Water","Natural spring water",2)

;Store them for later
SaveItems()

;Offer some items for sale
AddItem("Coffee",3)
AddItem("Tea",10)
AddItem("Chocolate milk",6)
AddItem("Water",100)

;Buy some items
If BuyItem("Coffee",3)
	Print "bought 3 Cups of Coffee!"
Else
	Print "Not enough money!"
EndIf

Print "Money Left: " + Money

If BuyItem("Tea",9)
	Print "bought 9 Cups of Tea!"
Else
	Print "Not enough money!"
EndIf

Print "Money Left: " + Money

If BuyItem("Chocolate milk",1)
	Print "bought 1 Cup of Nice choc!"
Else
	Print "Not enough money!"
EndIf

Print "Money Left: " + Money

If BuyItem("Water",40)
	Print "bought 40 Cups of water!"
Else
	Print "Not enough money!"
EndIf

Print "Money Left: " + Money

;Display all items
PrintItems()

;Save the game!
SaveGame()

;Exit
WaitKey()

;Creates a new item
Function CreateItem(Name$,Desc$,cost#)
	;Make a new item
	I.Item = New Item

	;Set up the stats of the item
	I\name = name
	I\desc = desc
	I\cost = cost
End Function

;Deletes an item
Function FreeItem(Name$)
	;Loop trough all items
	For I.Item = Each Item
		;Check if this is the right one
		If I\name = name
			;Remove it and stop looping trough the items, we're done
			Delete I
			Exit
		EndIf
	Next
End Function

;Saves all items
Function SaveItems()
	;Open ItemFile for writing
	file = WriteFile(ItemFile)

		;Loop trough all items
		For I.Item = Each Item
			;Write the items to the file
			WriteString(file,I\name)
			WriteString(file,I\desc)
			WriteFloat(file,I\cost)
		Next
	;Close the file
	CloseFile(file)
End Function
	
;Loads all items
Function LoadItems()
	;Open ItemFile for reading
	file = ReadFile(ItemFile)

		;Repeat until the file ends
		While Not Eof(file)
			;Create the item stored in the file
			I.Item = New Item
			I\name = ReadString(file)
			I\desc = ReadString(file)
			I\cost = ReadFloat(file)
		Wend
	;Close the file
	CloseFile(file)
End Function

;Adds a certain ammount of an item to the store
Function AddItem(Name$,ammount)
	;Loop trough all items
	For I.Item = Each Item
		;Check if this is the right one
		If I\name = name
			;Add ammount to the amount of this item being offered for sale
			I\Ammount = I\Ammount + ammount
			Exit
		EndIf
	Next
End Function

;Removes a certain ammount of items from the store
Function RemoveItem(Name$,ammount)
	;Loop trough all items
	For I.Item = Each Item
		;Check if this is the right one
		If I\name = name
			;Substract ammount from the amount of this item being offered for sale and make sure it's not under zero
			I\Ammount = I\Ammount - ammount
			If I\Ammount < 0 Then I\Ammount = 0
			Exit
		EndIf
	Next
End Function

;Buys an item
Function BuyItem(name$,ammount = 1)
	;Loop trough all items
	For I.Item = Each Item
		;Check if this is the right one
		If I\name = name
			;Calculate the cost for this purchase
			cost# = ammount * I\Cost

			;Check if we can afford it
			If Money < cost#
				Return False	;you can't pay it silly
			Else
				;Remove the items bought from the store
				I\Ammount = I\Ammount - ammount
				;Add the items bought to the ammount of these the player owns
				I\Owned = Owned + ammount
				;Pay the cost
				Money = Money - cost
				Return True		;All paid, you now own your new stuff
			EndIf
		EndIf
	Next
End Function

;Prints the info on an item
Function PrintItem(name$)
	;Loop trough all items	
	For I.Item = Each Item
		;Check if this is the right one
		If I\name = name
			;Print info on the item
			Print "-----------------------------"
			Print "Item: " + I\Name
			Print "Desc: " + I\Desc
			Print "Cost: " + I\Cost
			If I\Ammount = 0
				Print "This item is out of stock"
			ElseIf I\Ammount = 1
				Print "Only one " + I\Name + " is in store"
			Else
				Print "There are: " + I\Ammount + " of these item in store"
			EndIf
			If I\Owned = 0
				Print "You don't own any of these"
			Else
				Print "You have " + I\owned + " of these"
			EndIf
		EndIf
	Next
End Function

;Prints the info on an all items
Function PrintItems()
	;Loop trough all items
	For I.Item = Each Item
		;Print info on the item
		Print "-----------------------------"
		Print "Item: " + I\Name
		Print "Desc: " + I\Desc
		Print "Cost: " + I\Cost
		If I\Ammount = 0
			Print "This item is out of stock"
		ElseIf I\Ammount = 1
			Print "Only one " + I\Name + " is in store"
		Else
			Print "There are: " + I\Ammount + " of these item in store"
		EndIf
		If I\Owned = 0
			Print "You don't own any of these"
		Else
			Print "You have " + I\owned + " of these"
		EndIf
	Next
End Function

;Saves all game info
Function SaveGame()
	;Open GameFile for writing
	file = WriteFile(GameFile)
		;Write the amount of money we have
		WriteInt(file,Money)
		;Loop trough all items
		For I.Item = Each Item
			;Write the items to the file as well as the amount the store and player own!
			WriteString(file,I\name)
			WriteString(file,I\desc)
			WriteFloat(file,I\cost)
			WriteInt(file,I\Ammount)
			WriteInt(file,I\Owned)
		Next
	;Close the file	
	CloseFile(file)
End Function
	
;Loads all game info
Function LoadGame()
	;Open GameFile for reading
	file = ReadFile(GameFile)
	
		Money = ReadInt(file)
		;Repeat until the file ends
		While Not Eof(file)
			;Restore the item stored in the file
			I.Item = New Item
			I\name		= ReadString(file)
			I\desc		= ReadString(file)
			I\cost		= ReadFloat(file)
			I\Ammount	= ReadInt(file)
			I\Owned		= ReadInt(file)
		Wend

	;Close the file
	CloseFile(file)
End Function