Learning to Program with the Cybiko Handheld Computer Using B2C

Chapter 19 : Role Playing Games

The programs that are the most fun to write are games.  Graphic games (like Super Mario Brothers) are complex programs requiring bitmapped graphics, which is covered in the guide "BitMap.doc".  Other graphic games (like tic-tac-toe) are possible, but text-based games are a good starting point.

B2C is well suited to writing all-text role-playing games (RPGs).  A role-playing game is one where you act as a character in a world created by the computer.  You can interact with objects in that world and move from place to place.  I will present a simple role-playing game using a generic approach.  There are many ways to write RPGs, so you should experiment and find the one that makes most sense to you.

The first step in creating a RPG is deciding what your universe will be like.  Is your setting in outer space?  Or the deep sea?  What is your experience level? Are you an expert spy? Or a novice cowboy?  What is your goal in the adventure?  Are you trying to get to the end of a maze? Or are you attempting to collect all the Dragon Balls?  Regardless of the subject matter, a good RPG tells a story, and the more detailed your descriptions of the adventure, the more interested your user will be and the more exciting your game will be.

Once you have decided on a universe, make a map of the universe.  Make a box for each room and a line with an arrow showing how to go from one room to the next.  Keep it down to 5-10.  Make a description for each room.  Make a list of objects and decide where in universe these objects reside.  Make a description for each object.  Make a list of commands, like "north" to go north and "eat" to eat food.

The next step is a technical one.  How do you get your inputs from the user?  Will you list their options and have them input a number selecting what to do? Or will you have them input text strings and discover what commands work best. 

The TARDIS Adventure

This RPG is located in the TARDIS.  It stands for Time and Relative Dimensions in Space.  The RPG is based on the British TV Series "Dr. Who".  The TARDIS is "dimensionally transcendental" which simply means that it is larger on the inside than on the outside.  While the TARDIS is about the size of a telephone booth, it has many chambers inside.  The goal of our game will be to enter the TARDIS, find the sonic screwdriver, and exit.

In this game each room is a separate Function.  When you enter the function a description of the room is printed. When the function exits, it returns the number of the next room to go to.  This offers our program modularity.  Rather than a linear adventure that runs from the top of the program to the bottom, the user can wander from room to room and even get caught in a trap between rooms.

There are several tricks in this program.  One trick is that the return value from each room is an integer describing the next room to visit.  Using this method you can rewrite the adventure by changing descriptions and the return rooms.  Another trick is "parsing".  I create a variable with the value of the lettername of that variable.  (eg: a=97).  When I parse I check each letter in the "verb" against the variable.  So, for "eat" I check verb[0]=e and verb[1]=a and verb[2]=t as well as verb[3]=0 (null terminator). 

I allow you to carry 2 objects (one in each hand).  And you can drop only 1 object in each room.  The only commands are :

Command

Meaning

Description

n go north  
e go east  
s go south  
w go west  
I Inventory Lists all the objects you are carrying
eat eat something increase your energy
take take something

move an object from right hand to left hand and pick up something in your right hand. do nothing if hands full

drop drop something drop something from your left hand move other object from right hand to left hand. cannot drop if room is full

 

DO THIS

Execute the command "build tardis.bld".  Download the tardis.app file to the cybiko.
print "Welcome to Tardis"
print "Press any key"
print "To Continue"
dim xxx[1] as char
input xxx

font "mini_normal_font"

dim zz as int
dim sd as int
dim ky as int
dim sc as int
dim jb as int
dim st as int

sd=1
ky=2
sc=3
jb=4
st=5

dim iv[2] as int
dim ir as int
dim drp[10] as int
dim vb[10] as char
dim nrg as int
dim cmd as int

dim dr as int
dim eat as int
dim tk as int
dim nh as int
dim sh as int
dim et as int
dim wt as int
dim inventory as int
dr=1
eat=2
tk=3
nh=5
sh=6
et=7
wt=8
inventory=9

dim ou as int
dim co as int
dim ad as int
dim va as int
dim cl as int
dim e1 as int
dim e2 as int
dim so as int

ou=0
co=1
ad=2
va=3
cl=4
e1=5
e2=6
so=7

function parse ( zz as int) as int
parse=0
if vb[0]=101 and vb[1]=97 and vb[2]=116 and vb[3]=0 then
 parse=eat
elseif vb[0]=116 and vb[1]=97 and vb[2]=107 and vb[3]=101 and vb[4]=0 then
 parse=tk
elseif vb[0]=100 and vb[1]=114 and vb[2]=111 and vb[3]=112 and vb[4]=0 then
 parse=dr
elseif vb[0]=105 then
 parse=inventory
elseif vb[0]=104 then
 parse=0'help
elseif vb[0]=110 then
 parse=nh
elseif vb[0]=115 then
 parse=sh
elseif vb[0]=101 then
 parse=et
elseif vb[0]=119 then
 parse=wt
end if
end function

sub descObject(obj as int)
if (obj = 1) then
 print "sonic screwdriver"
elseif (obj = 2) then
 print "golden key"
elseif (obj = 3) then
 print "very long scarf"
elseif (obj = 4) then
 print "delicious jelly babies"
elseif (obj = 5) then
 print "shining star"
else
 print "nothing"
end if
end sub

sub showinventory(zz as int)
print "In your left hand"
descObject(iv[0])
print "In your right hand"
descObject(iv[1])
end sub

sub drItem(zz as int)
if drp[ir]=0 then
 drp[ir]=iv[0]
 iv[0]=iv[1]
 iv[1]=0
 print "You dropped"
 descObject(drp[ir])
else
 print "Cant drop object here"
end if
end sub

sub tkItem(zz as int)
if drp[ir]<>0 then
if iv[1]=0 then
 if iv[0]=0 then
  iv[0]=drp[ir]
  drp[ir]=0
  print "Took . . ."
  descObject(iv[0])
 elseif iv[1]=0 then
  iv[1]=drp[ir]
  drp[ir]=0
  print "Took . . ."
  descObject(iv[1])
 else
  print "Your hands are full"
 end if
end if
else
 print "There is nothing to take"
end if
end sub

sub die(zz as int)
print "you faint on the spot"
print "The light on"
print "top of the police box"
print "flashes and the TARDIS"
print "disappears from view"
print "The End."
beep 4
while 1
wend
end sub

sub eatFood(zz as int)
if iv[0]=jb then
 iv[0] = iv[1]
 iv[1] = 0
 nrg = nrg + 100
elseif iv[1]=jb then
 iv[1]=0
 nrg = nrg + 100
else
print "You have no food"
end if
end sub

function command(zz as int) as int
dim x as int
x=0
while x=0
 nrg = nrg - 1
 if nrg <= 0 then
  die(zz)
 end if
 print "Energy ", nrg
 input "command ", vb
 x=parse(zz)
 if x=0 then
  print "huh?"
 elseif x=dr then
  drItem(zz)
 elseif x=tk then
  tkItem(zz)
 elseif x=inventory then
  showinventory(zz)
 elseif x=eat then
  eatFood(zz)
 end if
wend
command = x
end function

function our(zz as int) as int
print "outside the TARDIS,"
print "a big blue police box"
print "doors e"
print "I see"
descObject(drp[ir])
while cmd=0
 cmd = command(zz)
 if cmd=et then
  our=co
  exit function
 else
  print "nope"
  cmd=0
 end if
wend
end function

function cor(zz as int) as int
print "inside the TARDIS "
print "control room - larger"
print "inside than outside"
print "doors e & w"
print "I see"
descObject(drp[ir])
while cmd=0
 cmd = command(zz)
 if cmd=wt then
  if drp[ir]=ky then
   cor = -1 ' game's over
   exit function
  else
   print "The door is locked"
   cmd=0
  end if
 elseif cmd=et then
  cor = ad
 else
  cmd=0
 end if
wend
end function

function adr(zz as int) as int
print "inside Adric's room."
print "Adric is the doctor's"
print "companion"
print "doors n, e, s, & w"
print "I see"
descObject(drp[ir])
while cmd=0
 cmd = command(zz)
 if cmd=nh then
  adr=cl
 elseif cmd=et then
  adr=e1
 elseif cmd=sh then
  adr=va
 elseif cmd=wt then
  adr=co
 elseif drp[ir]=st then
  drp[ir]=ky
  print "POOF"
  adr=ad
 else
  cmd=0
 end if
wend
end function

function var(zz as int) as int
print "a large room with"
print "vaulted ceilings. There"
print "is a peg on the wall."
print "doors n, e, s"
print "I see"
descObject(drp[ir])
while cmd=0
 cmd = command(zz)
 if cmd=nh then
  var=ad
 elseif cmd=et then
  var=e2
 elseif cmd=sh then
  var=so
 elseif drp[ir]=sc then
  print "PING"
  drp[ir]=st
  var=va
 else
  cmd=0
 end if
wend
end function

function clr(zz as int) as int
print "a huge cloister bell"
print "is here in this room"
print "doors s"
print "I see"
descObject(drp[ir])
while cmd=0
 cmd = command(zz)
 if cmd=sh then
  clr=ad
 else
  print "BONG"
  cmd=0
 end if
wend
end function

function e1r(zz as int) as int
print "there's an echo in here"
print "there's an echo in here"
print "doors n, e, s, & w"
print "I see"
descObject(drp[ir])
while cmd=0
 cmd = command(zz)
 if cmd=nh or cmd=wt then
  e1r=e1
 elseif cmd=sh or cmd=et then
  e1r=e2
 else
  print vb
  print vb
  cmd=0
 end if
wend
end function

function e2r(zz as int) as int
print "there's an echo in here"
print "there's an echo in here"
print "doors n, e, s, & w"
print "I see"
descObject(drp[ir])
while cmd=0
 cmd = command(zz)
 if cmd=sh or cmd=wt or cmd=et then
  e2r=e2
 elseif cmd=nh then
  e2r=ad
 else
  print vb
  print vb
  cmd=0
 end if
wend
end function

function sdr(zz as int) as int
print "this is an empty room."
print "doors n"
print "I see"
descObject(drp[ir])
while cmd=0
 cmd = command(zz)
 if cmd=nh then
  sdr=e1
 elseif cmd=et then
  sdr=ad
 else
  print "hmm..."
 cmd=0
 end if
wend
end function

sub top(zz as int)
ir=0
while ir <> -1
 cmd=0
 if ir=ou then
  ir=our(zz)
 elseif ir=co then
  ir=cor(zz)
 elseif ir=ad then
  ir=adr(zz)
 elseif ir=va then
  ir=var(zz)
 elseif ir=cl then
  ir=clr(zz)
 elseif ir=e1 then
  ir=e1r(zz)
 elseif ir=e2 then
  ir=e2r(zz)
 elseif ir=so then
  ir=sdr(zz)
 end if
wend
cls
if iv[0]=sd or iv[1]=sd then
cls
beep 7
print "Congratulations"
print "The Doctor is here to"
print "shake your hand. He"
print "thanks you for his"
print "Sonic Screwdriver"
wait 50
print "He pokes his head"
print "out. With a gleam in "
print "his eye he asks . . ."
print "Care to go for a ride?"
print "The End."
beep 6
else
cls
beep 4
print "The Doctor is very "
print "disappointed in you."
print "You forgot the Sonic"
print "Screwdriver."
wait 50
print "The Doctor sulks into"
print "the TARDIS and it"
print "disappears"
print "The End."
beep 4
end if
while 1
wend
end sub

nrg=26
drp[cl]=sc
drp[e1]=jb
drp[so]=sd

top(zz)