summaryrefslogtreecommitdiff
path: root/src/game.py
blob: 2e247635d7f5389408501a9246b57bb00f2812bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import cmd
from room import get_room
import textwrap
import shutil
import tempfile

class Game(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
        
        self.dbfile = tempfile.mktemp()
        shutil.copyfile("game.db", self.dbfile)
        
        self.loc = get_room(1, self.dbfile)
        self.look()
        
    def move(self, dir):
        newroom = self.loc._neighbor(dir)
        if newroom is None:
            print("you can't go that way")
        else:
            self.loc = get_room(newroom, self.dbfile)
            self.look()
            
        if newroom==13:
            exit()
        
    def look(self):
        print(self.loc.name)
        print("")
        for line in textwrap.wrap(self.loc.description, 72):
            print(line)
    
    def do_up(self, args):
        """Go up"""
        self.move('up')
    
    def do_down(self, args):
        """Go down"""
        self.move('down')
    
    
    def do_n(self, args):
        """Go north"""
        self.move('n')
    
    def do_s(self, args):
        """Go south"""
        self.move('s')
        
    def do_e(self, args):
        """Go east"""
        self.move('e')
    
    def do_w(self, args):
        """Go west"""
        self.move('w')
    
    def do_quit(self, args):
        """Leaves the game"""
        print("Thank you for playing")
        return True

    def do_save(self, args):
        """Saves the game"""
        shutil.copyfile(self.dbfile, args)
        print("The game was saved to {0}".format(args))
        
    def do_xyzzy(self, args):
        print('''A hollow voice says, "This isnt Fortran..."''')
        
if __name__ == "__main__":
    g = Game()
    g.cmdloop()