uhh how do I do ths
Blitz3D Forums/Blitz3D Beginners Area/uhh how do I do ths
| ||
Function buy() Print"what do you want to buy" Print "you have " +money#+ "money" item$=Input() If money#=money#-item$+"#" =>0 item$=item$+1 money#=money#-item$+"#" Else Print"you do not have anoth money! EndIf End Function so the problem is item$ will be eqaul to a global with the amount it costs stored in it! but It won't let em do it is there a way to get around it if you understand |
| ||
ohh please help |
| ||
1. this code is gibberish it's doing stuff that's not even remotely possible... 2. You need to work on your written/verbal skills agamer, there's no way we can help you if you post descriptions that could be written by an 11 year old. |
| ||
I am only 12 so haa yes it is possible i know what is possible but this is only one function.I am only goign about it the wrong way I would apreciate if you would not insult my understanding of what it can do maybe you could say what it can't do. Uhh I know I could say something like this if item$ = chair money#=money#-chair# endifbut I have loads of items so I would rather do it deffriently |
| ||
so would some one please like to give some constructive advice |
| ||
I didn't check this but, I think that's a better way than you're trying. A better way than this would be to use types and read from a data file but, this is just off the top of my head. Function buy() Print "what do you want to buy" print "you have" + money# + "money" item$ = input() checkcost(item$) End Function Function Checkcost(item$) if item$ = "sword" then cost = "100" if money# => cost item$ = item$ + 1 mone# = money# - cost Else Print "get outta here you bum!" end if End Function |
| ||
umm could do it this way or I could do iut the other wayb I listed so umm both the same it is just I have about 40 items so it's a very long way round but thanks or the help |
| ||
You could have all your items in custom types, perhaps defined like this:Type Item Field ID% ; Unique identifier for this item Field Name$ ; Name of item Field Desc$ ; Description of item Field Cost# ; Cost of item $ End Type You could then have Data statements that hold all your items. (Alternatively, you could use an external config file too.) Something like this might work: .Items Data 3 ; Number of total items to be initialized Data 1, "Helmet", "Ultra-sturdy dwarven helm", 1.99 Data 2, "Sword", "Saber of Blitz Programming +2", 5.50 Data 3, "Apple", "For when yo uget hungry", .25 Then you could use a For..Next loop and all the Data commands (look them up under Read and Restore) to read in all the data, something like this: ; (psuedocode below) ; restore .items ; for 0 to (Read int) ; create new Item ; item\id = (read int) ; item\name = (read string) ; item\desc = (read string) ; item\cost = (read float) ; next After that you'll have a whole list of items that you didn't have to enter in one by one. Now when the user enters an item by name, just search down the Item type list until it finds the item's name, and then you can get the cost of it. (If you've got a ton of items, you can make the user enter the ID, and have an array of types indexed by that ID to make it so you don't have to search down the list, but you probably don't have to worry about that right now.) Follow? Keep asking questions if this doesn't make sense. |
| ||
ohhh this makes more sense now thanks is it better than this system i just startedFunction buy() Print"what do you want to buy" Print "you have " +money#+ "money" item$=Input() If Lower(item$)= "Desk" If money#=>desk# money#=money#-desk# EndIf If Lower(item$)= "computer" If money#=>computer# money#=money#-computer# EndIf If Lower(item$)= "dvdplayer" If money#=>dvdplayer# money#=money#-dvdplayer# EndIf If Lower(item$)= "dvd" If money#=>dvd# money#=money#-dvd# EndIf If Lower(item$)= "cddrive" If money#=>cddrive# money#=money#-cddrive# EndIf If Lower(item$)= "cdrewriter" If money#=>cdrewriter# money#=money#-cdrewriter# EndIf If Lower(item$)= "scanner" If money#=>scanner# money#=money#-scanner# EndIf If Lower(item$)= "Mic" If money#=>Mic# money#=money#-Mic# EndIf If Lower(item$)= "printer" If money#=>printer# money#=money#-printer# EndIf If Lower(item$)= "netcard" If money#=>netcard# money#=money#-netcard# EndIf If Lower(item$)= "nethub" If money#=>nethub# money#=money#-nethub# EndIf If Lower(item$)= "netcable" If money#=>netcable# money#=money#-netcable# EndIf If Lower(item$)= "modem" If money#=>modem# money#=money#-modem# EndIf If Lower(item$)= "adslmodem" If money#=>adslmodem# money#=money#-adslmodem# EndIf If Lower(item$)= "vr" If money#=>vr# money#=money#-vr# EndIf If Lower(item$)= "cad" If money#=>cad# money#=money#-cad# EndIf If Lower(item$)= "buidings" If money#=>buidings# money#=money#-buidings# EndIf If Lower(item$)= "buildingm" If money#=>buildingm# money#=money#-buildingm# EndIf If Lower(item$)= "buildingm" If money#=>buildingm# money#=money#-buildingL# EndIf If Lower(item$)= "light" If money#=>light# money#=money#-light# EndIf If Lower(item$)= "digicamlow" If money#=>digicamlow# money#=money#-digicamlow# EndIf If Lower(item$)= "digicamhigh" If money#=>digicamhigh# money#=money#-digicamhigh# EndIf If Lower(item$)= "digivid" If money#=>digivid# money#=money#-digivid# EndIf If Lower(item$)= "webcam" If money#=>webcam# money#=money#-webcam# EndIf If Lower(item$)= "laptop" If money#=>laptop# money#=money#-laptop# EndIf If Lower(item$)= "floppy" If money#=>floppy# money#=money#-floppy# EndIf If Lower(item$)= "clock" If money#=>clock# money#=money#-clock# EndIf If Lower(item$)= "chair" If money#=>chair# money#=money#-chair# EndIf If Lower(item$)= "fillingcab" If money#=>fillingcab# money#=money#-fillingcab# EndIf If Lower(item$)= "tvsmall" If money#=>tvsmall# money#=money#-tvsmall# EndIf If Lower(item$)= "tvlarge" If money#=>tvlarge# money#=money#-tvlarge# EndIf If Lower(item$)= "shelves" If money#=>shelves# money#=money#-shelves# EndIf If Lower(item$)= "paper" If money#=>paper# money#=money#-paper# EndIf If Lower(item$)= "pen" If money#=>pen# money#=money#-pen# EndIf If Lower(item$)= "picture" If money#=>picture# money#=money#-picture# EndIf If Lower(item$)= "Displaycase" If money#=>Displaycase# money#=money#-Displaycase# EndIf If Lower(item$)= "dsiaplaycaserotate" If money#=>dsiaplaycaserotate# money#=money#-dsiaplaycaserotate# EndIf If Lower(item$)= "sterob" If money#=>sterob# money#=money#-sterob# EndIf If Lower(item$)= "sterog" If money#=>sterog# money#=money#-sterog# EndIf If Lower(item$)= "cashreg" If money#=>cashreg# money#=money#-cashreg# EndIf If Lower(item$)= "photocopier" If money#=>photocopier# money#=money#-photocopier# EndIf If Lower(item$)= "phone" If money#=>phone# money#=money#-phone# EndIf If Lower(item$)= "tablel" If money#=>tablel# money#=money#-tablel# EndIf If Lower(item$)= "tables" If money#=>tables# money#=money#-tables# EndIf If Lower(item$)= "drinkmachine" If money#=>drinkmachine# money#=money#-drinkmachine# EndIf If Lower(item$)= "changinroom" If money#=>changinroom# money#=money#-changinroom# EndIf If Lower(item$)= "buglaralarm" If money#=>buglaralarm# money#=money#-buglaralarm# EndIf Else Print"you do not have anoth money! EndIf End Function |
| ||
ohhh one question do I need to use all of them |
| ||
plus then I had o have all of htesse ;compnay's money Global money#=0 Global in$ ;list of bought items Global nDesk#=0 Global ncomputer#=0 Global ndvdplayer#=0 Global ndvd=0 Global ncddrive#=0 Global ncdrewriter#=0 Global nscanner#=0 Global nMic#=1 Global nprinter#=0 Global nnetcard#=0 Global nnethub#=0 Global nnetcable#=0 Global nmodem#=0 Global nadslmodem#=0 Global nvr#=0 Global ncad#=0 Global nbuidings#=0 Global nbuildingm#=0 Global nbuildingL#=0 Global nlight#=0 Global ndigicamlow#=0 Global ndigicamhigh#=0 Global ndigivid#=0 Global nwebcam#=0 Global nlaptop#=0 Global nfloppy#=0 Global nclock#=0 Global nchair#=0 Global nfillingcab#=0 Global ntvsmall#=0 Global ntvlarge#=0 Global nshelves#=0 Global npaper#=0 Global npen#=0 Global npicture#=0 Global nDisplaycase#=0 Global ndsiaplaycaserotate#=0 Global nsterob#=0 Global nsterog#=0 Global ncashreg#=0 Global nphotocopier#=0 Global nphone#=0 Global ntablel#=0 Global ntables#=0 Global ndrinkmachine#=0 Global nchanginroom#=0 Global nbuglaralarm#=0 ;end of list ;list of buyble items Const Desk#=200 Const computer#=1000 Const dvdplayer#=50 Const dvd=100 Const cddrive#=10 Const cdrewriter#=30 Const scanner#=50 Const Mic#=10 Const printer#=100 Const netcard#=10 Const nethub#=50 Const netcable#=5 Const modem#=30 Const adslmodem#=50 Const vr#=10000 Const cad#=100 Const buidings#=50000 Const buildingm#=100000 Const buildingL#=200000 Const light#=10 Const digicamlow#=100 Const digicamhigh#=300 Const digivid#=500 Const webcam#=100 Const laptop#=1500 Const floppy#=5 Const clock#=5 Const chair#=100 Const fillingcab#=200 Const tvsmall#=100 Const tvlarge#=400 Const shelves#=50 Const paper#=5 Const pen#=2 Const picture#=30 Const Displaycase#=400 Const dsiaplaycaserotate#=1000 Const sterob#=100 Const sterog#=1000 Const cashreg#=200 Const photocopier#=1000 Const phone#=20 Const tablel#=100 Const tables#=50 Const drinkmachine#=2000 Const changinroom#=5000 Const buglaralarm#=100 ;end of buyable items |
| ||
can data statements be acseessed from within functions |
| ||
If money#=money#-item$+"#" =>0 What the hell does this mean?I have NO IDEA how the compiler resolves that. Why are you adding "#" to item$? Does the compiler add "#" to item$ before or after item$ is subtracted from money#? And would it make a difference? If I'm not mistaken the order that the arguments are handled can be in any order unless strictly specifying the order with parentheses. What I am getting at is that the compiler may add "#" to item$ OR it may add "#" to the result of (money# - item$). Which one do you want? (AND WHY?). There is no guarantee on how the compiler will resolve that unless you specifically add parentheses i.e. money# - (item$ + "#")Comparing (money# - item$ + "#") to whether it's greater than or equal to 0 (zero) will result in a 1 or a 0, SO your initial comparison of (IF money# =) will be comparing it to either 1 or 0 which I doubt is what you want. I don't think you have a clue what you are doing. |
| ||
yep that is true but I have forgot that methord hre is what I have got now I have replaced some of my other functions ype Item Field ID% ; Unique identifier for this item Field Name$ ; Name of item Field Cost# ; Cost of item $ Field Desc$ ; Description of item End Type Function listitems() For T = 1 To 3 Read ID% Read Name$ Read Cost# Next End Function .Items Data 1,computer,1000 Data 2,dvdplayer,50 Data 3,dvd,100 Data 4,cddrive,10 Data 5,cdrewriter,30 Data 6,scanner,50 Data 7,Mic,10 Data 8,printer,100 Data 9,netcard,10 Data 10,nethub,50 Data 11,netcable,5 Data 12,modem,30 Data 13,adslmodem,50 Data 14,vr,10000 Data 15,cad,100 Data 16,buidings,50000 Data 17,buildingm,100000 Data 18,buildingL,200000 Data 19,light,10 Data 20,digicamlow,100 Data 21,digicamhigh,300 Data 22,digivid,500 Data 23,webcam,100 Data 24,laptop,1500 Data 25,floppy,5 Data 26,clock,5 Data 27,chair,100 Data 28,fillingcab,200 Data 29,tvsmall,100 Data 30,tvlarge,400 Data 31,shelves,50 Data 32,paper,5 Data 33,pen,2 Data 34,picture,30 Data 35,Displaycase,400 Data 36,dsiaplaycaserotate,1000 Data 37,sterob,100 Data 38,sterog,1000 Data 39,cashreg,200 Data 40,photocopier,1000 Data 41,phone,20 Data 42,tablel,100 Data 43,tables,50 Data 44,drinkmachine,2000 Data 45,changinroom,5000 Data 46,buglaralarm,100 Data 47,Desk,200 |
| ||
BTW, declaring costants for the prices of your items means that you will never be able to change the prices later if you wanted to. |
| ||
kk well I never would want to but can you change the number in a data stament it's really weird when I run the above cod e(my last post before this) it asks to open a file how weird |
| ||
ahh types are so confusing |
| ||
Here's an implementation that's a bit more elegant, btw. with saying you where doing things that weren't even remotely possible I meanth this: money#=money#-item$+"#" You where substracting a string from a float and then adding another string to that, that is impossible. Sorry if I sounded rude, I wasn't aware you where still so young, welcome to the club in any case, I personaly started coding around age 10-11. This should keep you busy for a while... Copy paste and run, then take a look at the iner workings :) -edit- Updated code in new post! |
| ||
Maybe it just wants to save the source? BTW you should not write Data 1,computer,1000 Data 2,dvdplayer,50 instead write Data 1,"computer",1000 Data 2,"dvdplayer",50 you really shouldn't make this more complicated than it has to be. Did you consider to use simple arrays instead? Here's an example: num=47 dim product$(num) dim price#(num) restore items for i=1 to num read dummy read product$(i) read price#(i) next money#=1000.0 while upper$(in$)<>"EXIT" print "(Enter EXIT to finish)" in$=input$("please enter product name") for i=1 to num if upper$(in$) = upper$(product$(i)) ; product name found? print "Product "+ product$(i)+" costs "+price#(i) if money# >= price#(i) then money#=money#-price#(i) print "you have bought one. Now you still have "+ money# +" Dollars" else print "You haven't got enough money to buy this." endif else print "The product name you entered was not found in the database. Please try again" endif next wend end .Items Data 1,"computer",1000 Data 2,"dvdplayer",50 Data 3,"dvd",100 Data 4,"cddrive",10 Data 5,"cdrewriter",30 Data 6,"scanner",50 Data 7,"Mic",10 Data 8,"printer",100 Data 9,"netcard",10 Data 10,"nethub",50 Data 11,"netcable",5 Data 12,"modem",30 Data 13,"adslmodem",50 Data 14,"vr",10000 Data 15,"cad",100 Data 16,"buidings",50000 Data 17,"buildingm",100000 Data 18,"buildingL",200000 Data 19,"light",10 Data 20,"digicamlow",100 Data 21,"digicamhigh",300 Data 22,"digivid",500 Data 23,"webcam",100 Data 24,"laptop",1500 Data 25,"floppy",5 Data 26,"clock",5 Data 27,"chair",100 Data 28,"fillingcab",200 Data 29,"tvsmall",100 Data 30,"tvlarge",400 Data 31,"shelves",50 Data 32,"paper",5 Data 33,"pen",2 Data 34,"picture",30 Data 35,"Displaycase",400 Data 36,"dsiaplaycaserotate",1000 Data 37,"sterob",100 Data 38,"sterog",1000 Data 39,"cashreg",200 Data 40,"photocopier",1000 Data 41,"phone",20 Data 42,"tablel",100 Data 43,"tables",50 Data 44,"drinkmachine",2000 Data 45,"changinroom",5000 Data 46,"buglaralarm",100 Data 47,"Desk",200 |
| ||
When you finish the shop code you should post it in the code gallery for other people!! |
| ||
yeh this is for a weird text game which will be freeware I am planning to make it multiplayer but it is like tatergy so if you can help it would be nice |
| ||
@RetroBooster I acept your apolgy and i lve your solution but am at a lost woth theesse types and your code would you mind documenting it! |
| ||
I'll comment it some more so you can understand it better :) plus I just saw I did the loader code wrong, silly slip up. New version up soon. |
| ||
kk |
| ||
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 |
| ||
thx will hav a look at it later today thx |
| ||
thx I have now looked at it May I please mod it and use it in my free ware game please! |
| ||
sure, go ahead. |
| ||
thanks so much |