[Tutor] my newbie program
Magnus Lycka
magnus@thinkware.se
Mon Nov 18 09:35:01 2002
At 08:12 2002-11-18 -0600, root wrote:
>hello everyone! i am trying again to learn python
>and i have broken my script here. right now i am
>just trying to make rooms and move through them.
>any comments or direction would be much appreciated
This seems like a reasonable beginning... There's a lot
left to do of course :) but I don't think you are headed
in the wrong direction...
>class room:
It's common to begin ClassNames with Capitals. See
http://www.python.org/peps/pep-0008.html
Thus "class Room:" would be better.
> def __init__(self):
> self.desc=""
> self.exits=[]
>
>
>room1=room()
>room2=room()
>room1.exits=[room2,0,0,0]
>
>room1.desc="a big room"
>room2.desc="an even bigger room"
>
>class player:
> def __init__(self):
> self.location=room1
> def act(self):
> cmd = raw_input('>')
> if cmd == 'look':
> print self.location.desc
> if cmd == 'n':
> if self.location.exits[0] != 0:
You can write this as "if self.location.exists[0]:"
Being not equal to 0 is in this case the same as
being I assume. (The meaning will be different if
self.location.exits[0] could contain [] or "" etc, but
not if it only contains numbers. Zero is "false".
You could have something like
directions = {'n':0, 'e':1, 's':2, 'w':3}
and then:
if cmd in directions.keys():
if self.location.exits[directions[cmd]]:
...
> self.location=self.location.exits[0]
> else:
> print "you can't go that way"
>
>me=player()
Good luck!
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se