Inventory in a simple text adventure.

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
JBender23
Newbie
Posts: 2
Joined: Tue Jan 19, 2010 1:34 pm

Inventory in a simple text adventure.

Post by JBender23 » Thu Jan 21, 2010 10:24 pm

So I'm a beginner to QBASIC, and coding in general. After completing the first tutorial here on Pete's site, I played around with it, making some aesthetic modifications and adding different difficulties. Then I made a simple program which generated lottery numbers randomly. My third program is my first "real" project. It's a text adventure following a day in the life of a freshman student at my high school. It is not graphical in any way...I'm a story writer, so the idea was to make an interactive book. I will provide the code for the first scene so you can get an idea of the style.
'\\\\UNTITLED TEXT ADVENTURE////
'This will be some sort of text adventure involving a day in the life of a
'Fountain-Fort Carson High School Student

PRINT
PRINT
PRINT " ____________
PRINT " |____ ____| _______ __ __ ___ _______ ______
PRINT " | | \ | || \ | || \ | || \ 's
PRINT " _ | | B || ____|| \ || D || ____|| RR |
PRINT " | | | | / | |_ | |\ || D || |_ | __/
PRINT " | |__| | \ | _} |__| |__||____/ | _} | \ \
PRINT " |_______| B || |_____________________| |____ | | \ \
PRINT " (c)2009|_____ / |________________________________||__| \__\
PRINT " _
PRINT " / \
PRINT " / /_\ \
PRINT " / _____ \
PRINT " /___/ \___\
PRINT " _________ _________
PRINT " |___ ___| |___ ___|
PRINT " | | | |
PRINT " | | | |
PRINT " |_| ROJAN |_| ALE
PRINT
PRINT
PRINT " FRESHMAN YEAR



PRINT
PRINT
INPUT " Press any button to play. " , a$
IF a$ = "" THEN GOTO 10

10 CLS
PRINT
PRINT
COLOR 15
PRINT
PRINT " It still seems as though you were just getting out of school"
PRINT " yesterday, but summer has already faded away. You feel as "
PRINT " though you wasted the time that was given to you. Now you are"
PRINT " starting at a new school: Fountain-Fort Carson High. "
PRINT
PRINT " As the car approaches the school, you feel the sensation of"
PRINT " encroaching doom. What could you possibly do to avoid starting"
PRINT " high school so soon? Maybe you could jump off a cliff and break"
PRINT " your leg. No, you're parents would still make you go. Perhaps"
PRINT " you could gouge out your own eyeballs?"
PRINT
PRINT " It's the day of freshman registration, and your mom sits next"
PRINT " to you, filling out some sort of medical form. She says you must"
PRINT " sign your name at the bottom."
COLOR 7
PRINT
PRINT
INPUT " (NAME): " , nm$
IF nm$ = "" THEN GOTO 10

15 CLS
COLOR 15
PRINT
PRINT
PRINT " After finishing the forms, you have your picture taken for your"
PRINT " School I.D. You talk to a few friends from the middle school, but "
PRINT " everyone seems too nervous to hold much of a conversation. You"
PRINT " get your I.D., and then you have to wait in the library to get your"
PRINT " laptop. After everything is finally taken care of, you return home"
PRINT " for your final week of vacation.
PRINT
COLOR 11
PRINT " ONE WEEK LATER..."
PRINT
PRINT
PRINT
COLOR 12
PRINT " BUZZ! BUZZ! BUZZ! BUZZ! BUZZ! BUZZ! BUZZ!
PRINT
PRINT
PRINT
COLOR 7
PRINT
INPUT " INPUT: " , c$
PRINT
PRINT " For help, type "HELP"

IF c$ = "HELP" THEN GOTO 16
IF c$ = "TURN OFF ALARM" THEN GOTO 20
16 PRINT
PRINT " Try turning off the alarm "type commands in ALL CAPS"
PRINT



20 CLS
PRINT


Anyway, as you can see, I'm working with minimal knowledge. I'm not looking for too much....honestly I could complete this if I could just get the inventory figured out.

I want to implement a very simple inventory system. By typing "BACKPACK" at any point in the game, I want the user to be taken to a simple list of the items he/she has obtained. The user will be able to obtain these items (which will be necessary to complete other parts of the game) by "PICK UP"ing them.

For example, the user could find a spoon early in the game, then later on an NPC will need a spoon so that other events can be triggered.

So I already know how to make a screen with a list, but how could I have the computer automatically add certain words to the list when the user "PICK UP"s them?

Then, how could I have the computer check to see whether or not there is a specific item in the list so that the user can "GIVE TO" this item to trigger another event?

I was brainstorming, and I was thinking you could have it assing the string variable a certain numeric value, then check for that numeric value when it is needed.

But I don't know. Any suggestions?

EDIT: The message board got rid of all the spaces that spaced out the ASCII art and all of the paragraph formatting.
Last edited by JBender23 on Fri Jan 22, 2010 9:48 am, edited 1 time in total.

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Fri Jan 22, 2010 12:55 am

i programmed some Text-Adventures (some month ago), btw. here is
the inventory formula i use:

[code]
dim Inventory$(100) ' declare an array of 100 elements
[/code]

arrays are a long-list of variables, the dollar sign means this is a
String Array. now:

[code]
Items = 0
[/code]

this one sets the Variable items to zero, since the main character
didnt get anything. now lets imagine your character wants to take
a camera by typing "TAKE CAMERA". the following code while add
that item to this inventory:

[code]
if c$ = "TAKE CAMERA" then
Items = Items + 1 ' add one item
Inventory$(Items) = "Camera" ' remove the camera
end if
[/code]

now, whenever you type TAKE CAMERA. you get a camera, to
avoid this. type:

[code]
if c$ = "TAKE CAMERA" then
GotIt = 0
for i = 1 to Items
if Inventory$(i) = "Camera" then GotIt = 1: exit for
next i
if GotIt = 0 then Items = Items + 1: Inventory$(Items) = "Camera"
if GotIt = 1 then print " You already have it!"
end if
[/code]

you already know the first command, the third command starts
going until it reaches a NEXT command and it starts at the third
line until the condition in the FOR command becomes true.
the fourth line, checks if you have the Camera. if you have, then
it sets the Variable GotIt to one and EXITs the FOR loop. the
sixth line checks if you dont have the Camera, if you dont. then
it picks it up. if you have, it prints you already do have it. and
now to show the Inventory when your player types BackPack:

[code]
if c$ = "BACKPACK" then
for i = 1 to items
print Inventory$(i)
next i
end if
[/code]

phew. and thats it! if you have any questions, then tell me.
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

User avatar
burger2227
Veteran
Posts: 2396
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Fri Jan 22, 2010 1:23 am

You can press keys all day using INPUT until they press enter. Try this for most any keypress:

DO
LOOP UNTIL INKEY$ <> ""
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

JBender23
Newbie
Posts: 2
Joined: Tue Jan 19, 2010 1:34 pm

Post by JBender23 » Fri Jan 22, 2010 10:22 am

God damn this message board. I just wrote out a super-long and detailed reply, and the board logged me out automatically while I was in the middle of writing it! All that writing, just Gone!!!

So, I'll try to remember everything I said.

First of all, you explained this to me very well. I found a tut last night that makes use of a similar concept, but the guy didn't take the time to explain why it works, which is something that is absolutely necessary for me. I CRAVE to know why and how things work.

So, here's the piece of code from the other guys tut:
10 Rem *** Main Loop ***
print LocationDescription$(PlayerLocation)
Print : Print "Enter Command: ";
Input PlayerInput$
If (PlayerInput$ = "i") or (PlayerInput$ = "I") then gosub 1000 'Take Inventory
Goto 10

1000 Rem *** Take Inventory ***
Print "You are carrying the following items:"
ItemsCarried = 0
for i = 1 to MaxItemNumber
if ItemLocation(i) = 0 then ItemsCarried = ItemsCarried + 1: Print ItemName$(i)
Next i
If ItemsCarried = 0 then print "Nothing."
Return
As you can see, he makes use of a subroutine. This is so that the user can type "BACKPACK" at any point in the game, and he will be directed to the BACKPACK sub, which will return him to the point in the game where he was at when he exits the BACKPACK. I attempted to use this code, but it didn't exactly work as planned. Here's a piece of code from my game:
INPUT " INPUT: ", l$
IF l$ = "TAKE A SHOWER" THEN GOTO 21
IF l$ = "HELP" THEN GOTO 22
IF l$ = "TURN ON LIGHT" THEN GOTO 24
IF l$ = "BACKPACK" THEN GOSUB Backpack
IF l$ <> "TURN ON LIGHT" OR l$ <> "BACKPACK" OR l$ <> "TAKE A SHOWER" OR l$ <> "HELP" THEN GOTO 23
END
So, if the user types one of the three available commands (TAKE A SHOWER, HELP, and TURN ON LIGHT) then he is taken to their corresponding pages. If he typed anything other than those three commands, then he was taken to a page which said, in so many words, that the command was not one of the available actions. This worked fine until I added the BACKPACK parts. For some reason, when the user types BACKPACK, it refuses to GOSUB: it takes the user to the "Command not available" message. It's not recognizing BACKPACK as one of the available commands. Here is the corresponding SUB.
SUB Backpack
1000 CLS
PRINT " Your Backpack:"
PRINT " _______________________"
ItemsCarried = 0
FOR i = 1 TO MaxItemNumber
IF ItemLocation(i) = 0 THEN ItemsCarried = ItemsCarried + 1: PRINT ItemName$(i)
NEXT i
IF ItemsCarried = 0 THEN PRINT "Nothing."
RETURN

END SUB
So what's the major malfunction here? At first I thought it was because the SUB is all the way at the end of all the code. But, from my understanding of how SUBs work, this shouldn't matter. I'd like to make use of a SUB because I don't want to have to write a new inventory code for every scene, but I'd also like to make use of your inventory code, as it's much cleaner than this guy's inventory code.

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Fri Jan 22, 2010 3:13 pm

sure, its very easy. but lets make something clear, you know goto?
it works like this:

[code]
goto Label:

Label:
print "Label"
[/code]

gosub works like this:

[code]
gosub Label:
print "Back"

Label:
print "Label"
return
[/code]

run the second code, GOSUB doesnt mean to head into
a SUB command. while it goes into a label, and does everything
descriped in that label until it reaches the RETURN command.
on which it returns back to where the GOSUB was last time used.
okay, now for the SUB command. look at the following program:

[code]
declare sub ShowBackPack
if c$ = "BACKPACK" then call ShowBackPack()

sub ShowBackPack
for i = 1 to items
print Inventory$(i)
next i
end sub
[/code]

put this after the TAKE CAMERA program. it prints nothing!
to make it print something, replace the existing ITEMS and
INVENTORY$ with:

[code]
dim shared Inventory$(100)
dim shared items
[/code]

run your program and it prints "Camera" if you got the camera.
hope it helps. if it doesnt, ask me any question!
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Fri Jan 22, 2010 3:18 pm

an example will make all this stuff more clear. ask Any questions!:

Code: Select all

 declare sub ShowBackpack
 declare sub TakeItem(NameOfItem$)

 dim shared items
 dim shared Inventory$(100)
 20
  input c$
  c$ = ucase$(c$)             ' make the command case-insensitive
  if c$ = "TAKE CAMERA" then
   TakeItem("Camera")
  end if
  if c$ = "BACKPACK" then
   ShowBackpack
  end if
 goto 20

 sub ShowBackpack
  for i = 1 to items
   print Inventory$(i)
  next i
 end sub

 sub TakeItem(NameOfItem$)
  for i = 1 to Items
  if NameOfItem$ = Inventory$(i) then GotIt = 1: exit for
  next i
  if GotIt = 1 then print "You already have it!"
 end sub
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Fri Jan 22, 2010 3:21 pm

by the way, to avoid your posts going lost. there are two ways.

1) type your text, copy it, and then post the message. if the
message is deleted, just edit your existing message or make
a new one with the text you copied

2) type your text in notepad or any other text editor, copy the text
and paste it then post your message.
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

User avatar
burger2227
Veteran
Posts: 2396
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Fri Jan 22, 2010 3:34 pm

I hate GOTO! It's for rookies!

Do you see where the 20 is. Type DO there instead.

Code: Select all

Type LOOP UNTIL INKEY$ = CHR$(27) ' where the GOTO line is
To exit press the [Esc] key

Shame on you KING. :( A forever loop that requires Ctrl-Brk?

Don't show people how to code worser! Next you'll be telling him to use LET........mercy
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Sat Jan 23, 2010 12:30 am

every beginner starts with GOTO, JBender too, i always try to start
from scratch and build up every little details you may have skipped.
by the way, whats your problem cheese-burger?
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

User avatar
burger2227
Veteran
Posts: 2396
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Sat Jan 23, 2010 1:01 am

Not EVERY beginner, but as an experienced user, I recommend NOT using it as much as possible! A lot of beginners do it the "easy"(LOL) way until they get tired of trying to figure out what they did later. GOTO code is totally troublesome to fix!

When you have been programming a lot more, perhaps we can discuss optimization of code. Until then, do what YOU want. Don't force it down other peoples throats!

I spend a LOT of time helping people here and I DON'T need THAT kind of help!!!!
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Sat Jan 23, 2010 3:52 am

i hope these four things make something clear:

first off, you are right we are not equal. since my ten years experience
is NOTHING against your WhatSoEver experience of programming.

second off, i never force anybody to do anything.

third off, leave everybody in this board in peace. if you really want
to start arguing about something which has no sense at all. then do
that in the General Discussion one.

fourth off, instead of bothering other people's suggestions. make your
own suggestion.
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

User avatar
burger2227
Veteran
Posts: 2396
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Sat Jan 23, 2010 5:59 am

I already did smart ass.

If you program like your examples here, you didn't learn much!
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Sat Jan 23, 2010 7:41 am

nobody said im an expert in QBASIC, but im an expert in C++.
by the way, i should start ignoring you!
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

User avatar
burger2227
Veteran
Posts: 2396
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Sat Jan 23, 2010 7:35 pm

I HAVE been ignoring you BIG BAD KING OF NOTHING! But you seem to get into every thread for some reason.

GOTO a C forum if that's what you know! Your a BIG MOUTH in my books!

Do it right or be quiet please.......
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Sun Jan 24, 2010 12:21 am

well. have fun. im leaving.
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

Post Reply