[Tutor] adventure games [transforming Basic BASIC to Basic Python

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue May 6 01:49:02 2003


On Mon, 5 May 2003, reavey wrote:

> thanks, great site.
>
> > http://www.atariarchives.org/adventure
> >
> rewrite the code in Python.
>
> I'm in. Sounds like like great fun with a capital F, Please, suggest as
> an opener.


Hi Reavey,

This one should be fun: how about the "monster name" generator in the
appendix?

    http://www.atariarchives.org/adventure/appendices.php

It's short, it's sweet, and it's sort of an instant gratification thing:
as soon as we have it ported, we can start playing with it, independent of
the rest of the system.

###
10 REM CHARACTER NAME GENERATOR
20 RANDOMIZE VAL(RIGHT$(TIME$,2))
30 DIM 2(5)
40 FOR T= 1 TO 5:READ Z(T):NEXT
50 FOR H = 1 TO 4
60 FOR T = 1 TO 4 + INT(RND(1)*4)
70 B = 0
80 A = 66 + INT(RND(1)*25)
90 IF RND(1)>.7 AND (A=69 OR A = 73 OR A = 79 OR A = 85) THEN 80
100 IF (T = 2 OR T = 5 OR T=7) THEN B= Z(INT(RND(1)*5))
110 IF B<>0 THEN LPRINT CHR$(B);:GOTO 130
120 LPRINT CHR$(A);
130 NEXT T
140 LPRINT "    ";
150 NEXT H
160 LPRINT
170 GOTO 50
180 DATA 65,69,73,79,85
###

This deathly code produces the names of hideous monsters like "MEZPO" or
"GEDIE" or "MEDSA"!  These names strike mortal error in any true
adventurer's heart.  But it, too, pales in comparison to the code itself.


Just kidding.  Actually, this doesn't look so bad.  Part of this code
appears to assume that the user knows that the mysterious numbers are
ASCII character codes.  In Python, we have the functions 'ord()' and
'chr()' to let us go back and forth between the numeric codes and the
actual characters:

###
>>> ord('A')
65
>>> ord('B')
66
>>> ord('Z')
90
>>> chr(66)
'B'
###


so if you're wondering what in the world the BASIC lines:

###
40 FOR T= 1 TO 5:READ Z(T):NEXT
...
100 IF (T = 2 OR T = 5 OR T=7) THEN B= Z(INT(RND(1)*5))
...
180 DATA 65,69,73,79,85
###

are doing, try doing a chr() across the list [65, 69, 73, 79, 85], and you
should immediately see what it's doing.  *grin*


Do you want to give it a shot first?


Good luck!