Shop menu where you can buy or sell things
Blitz3D Forums/Blitz3D Beginners Area/Shop menu where you can buy or sell things
| ||
Ok Just got Blitz last friday and im trying to write a small menu that acts like a shop, I can get my program to let you buy things and it deducts money from the player but I cant work out how to get my program to check if the player has enough money to buy a certain item. (hope that makes sense) cheers |
| ||
if playermoney >= itemprice then items=items+1 playermoney=playermoney-itemprice endif It should be rather simple... Andy |
| ||
hehe Hmmm ive got a lot to learn :) Didnt realise it could be that simple grrrr Cheers Dan |
| ||
hmmm how do i declere 4 seperate item prices for it to check? sorry for the mega newbie questions. cheers Dan |
| ||
; LIL DEMO ; Press 1 to 4 to deduct price Graphics 320,200,0,2 Global item1# = 1.15 Global item2# = 1.30 Global item3# = 4.99 Global item4# = 6.40 totalmoney# = 100.00 While Not KeyHit(1) Cls If KeyHit(2) Then totalmoney# = totalmoney#-item1# If KeyHit(3) Then totalmoney# = totalmoney#-item2# If KeyHit(4) Then totalmoney# = totalmoney#-item3# If KeyHit(5) Then totalmoney# = totalmoney#-item4# Text 50,50,totalmoney# Flip Wend |
| ||
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 |
| ||
WoW!! Thanks for your time and help everyone youve really helped me out here!! Are there any Books for Blitz Basic that goes more indepth then the manual? Cheers Dan |
| ||
Swift should be writting one soon for all of us I hope :) Swift what do you say? |
| ||
There is a book (blitz2d) that helps you understand the basics. here: http://www.blitzcoder.com/bookinfo2d.html You'll pick it up from examples pretty quick. |
| ||
Cheers just bought the book cant wait for it to get here. Rims i typed in your code lastnight and it doesent seem to accept user input it just sits in the menu screen :) but probly something ive done wrong somewhere cheers Dan |
| ||
Rims i typed in your code lastnight and it doesent seem to accept user input it just sits in the menu screen I hope you actually copied and pasted it. Retyping would be pointless on a multitasking computer. There would be less chance for errors as well. |
| ||
its more fun retyping! just like in the old days when you bought computer magazines that featured games as DATA lines, and you spent several days with a ruler typing in all that data ... only to get the infamous "error check", and never got to see the game. LOL :-D |
| ||
I cant copy and paste it because i am using an Army leaerning Centre pc to read the boards and using my laptop back at camp to write in blitz, so i have to print things out and then re-type them in. |