Computer Science Canada

File save/load and menus

Author:  McCool [ Sat May 06, 2006 2:04 pm ]
Post subject:  File save/load and menus

Hey guys, I've been away from programming for about 3 years now and I am trying to put this game I had into some kind of working order and I've hit a bump.

I need to know what do you guys think is the best way to program the 'buy' menu.

Eg. player has 100 bucks,
Item 1 cost 25 dmg +1
Item 2 cost 75 dmg +2
Item 3 cost 100 dmg +3

how do I program this so when the player buys the item their money goes down accordingly and the damage goes up accordingly. Is there a way to avoid doing this:

code:
put "What do you want to buy?"
put "1) Shiv cost 25 dmg+1"
put "2) Shank cost 75 dmg+2"
put "3) Laser Rifle cost 100 dmg+3"
get itemselection

if itemselection = 1 then
    cost := 25
    dmgincrease := 1
    elsif itemselection = 2 then
    cost := 75
    dmgincrease := 2
    elsif itemselection = 3 then
    cost := 100
    dmgincrease := 3
    else
    put "that is not a selection"
    buymenu
    %name of the procedure^
    end if
   
    money := money - cost
    dmg := dmg + dmgincrease


also I am having no luck saving player stats or loading them for that matter. How do I save information to a separate file and open it at a later date?


thanks a lot guys

Author:  Cervantes [ Sat May 06, 2006 3:17 pm ]
Post subject: 

There certainly is a nice way to avoid that. The trick is to use arrays and records.

code:

type item :
    record
        name, cost, damage_mod : int
    end record
   
var items : array 1 .. 3 of item
items (1).name := "Shiv"
items (1).cost := 25
items (1).damage_mod := +1
items (2).name := "Shank"
items (2).cost := 75
items (2).damage_mod := +2
items (3).name := "Laser Rifle"
items (3).cost := 100
items (3).damage_mod := +3

put "What do you want to buy?"
for i : 1 .. upper (items)
    put i, ") ", items (i).name, " costs ", items (i).cost, ": dmg ", items (i).damage_mod
end for
get item_selection
money -= items (item_selection).cost        % Same as money := money + items (item_selection)
dmg += items (item_selection).damage_mod


: