Quantcast
Channel: Small Basic
Viewing all articles
Browse latest Browse all 669

Small Basic Game Programming - Text adventure

$
0
0

Once I have answered a question in Small Basic Forum by Jeffrey SWHS about Text Adventure Game.  At that time, I made a sample text adventure program.  That program (FCD758-0) is not completed.  But I will explain about the program this time.  And until the next time I'd like to refine and upgrade this text adventure.

Opening

This is the game opening.  To game control, many Goto statements are used.  And for choosing an alternative, subroutine Choose is called.  This makes the code simple.

 1.stage_0:
 2.TextWindow.Writeline("You're at a fork in the road.")
 3.TextWindow.Writeline("Which way do you go? ")
 4.choices="LEFT,RIGHT,STAY"
 5.Choose()
 6.If id = 1 Then
 7.  Goto stage_1_1
 8.ElseIf id = 2 Then
 9.  Goto stage_1_2
10.ElseIf id = 3 Then
11.  Goto stage_1_3
12.Else
13.  TextWindow.WriteLine("Invalid choise.")
14.  Goto stage_0
15.EndIf

First Stage

The first stage branches three by the selection.  There are no code for second and third selection, so these two goes to the end.

17.stage_1_1:
18.TextWindow.Writeline("Good choice, you find some money. :)")
19.TextWindow.Writeline("Have a nice day.")
20.Goto end
21.stage_1_2:
22. 
23.stage_1_3:

End of Game

This is the end of game, only writing a new line at this time.

25.end:
26.TextWindow.Writeline("")
27.' end of program

Subroutine to Choose Alternative

This subroutine returns a number (id) selected from given choices that is described as comma separated value.

29.Sub Choose
30.  ' param choices - e.g. "A,B,C"
31.  ' return id - e.g. 1 for A
32.  ' work a,c,choice,i,len,n,p,u - will be broken
33. 
34.  ' Make array of choice 
35.  len = Text.GetLength(choices)
36.  p = 1
37.  i = 0
38.  While p <= len
39.    c = Text.GetIndexOf(Text.GetSubTextToEnd(choices,p), ",")
40.    If c = 0 Then
41.      c = len + 1
42.    Else
43.      c = c + p - 1
44.    EndIf
45.    i = i + 1
46.    choice[i] = Text.GetSubText(choices, p, c - p)
47.    p = c + 1
48.  EndWhile
49.  ' Dispaly choices 
50.  n = i
51.  For i = 1 To n
52.    TextWindow.Write(choice[i])
53.    If i < n - 1 Then
54.      TextWindow.Write(" ")
55.    ElseIf i = n - 1 Then
56.      TextWindow.Write(" or ")
57.    EndIf
58.  EndFor
59.  TextWindow.WriteLine("")
60.  ' Input
61.  a = TextWindow.Read()
62.  ' Convert to upper case
63.  u = Text.ConvertToUpperCase(a)
64.  ' Search id of choces
65.  id = n
66.  While choice[id] <> u And 0 < id
67.    id = id - 1
68.  EndWhile
69.EndSub

Most significant point of this sample is using a subroutine Choice.  So main routine becomes simple.  But, there are many Goto statements.  And if we complete this game with this manner, there will be many TextWindow.WriteLine() operations and Goto statements in the code.  So, the next week I will show simpler sample.


Viewing all articles
Browse latest Browse all 669

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>