repl.it
@jdelarios/

main.py

Pygame

No description

fork
loading
Files
  • main.py
  • nohup.out
main.py
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
import re

from constants import Directions, Commands
from cave import Map

class Game(object):
    def __init__(self):
        self.map = Map
        self.location = 'start'
        self.inventory = []

    def follow(self, instruction):
      if re.search(Directions.NORTH, instruction):
        try:
            self.location = self.map[self.location]['exits']['North']
            print(self.map[self.location]['description'])
        except KeyError:
            print('You can\'t go that way.')
      elif re.search(Directions.SOUTH, instruction):
        try:
            self.location = self.map[self.location]['exits']['South']
            print(self.map[self.location]['description'])
        except KeyError:
            print('You can\'t go that way.')
      elif re.search(Directions.EAST, instruction):
        try:
            self.location = self.map[self.location]['exits']['East']
            print(self.map[self.location]['description'])
        except KeyError:
            print('You can\'t go that way.')
      elif re.search(Directions.WEST, instruction):
        try:
            self.location = self.map[self.location]['exits']['West']
            print(self.map[self.location]['description'])
        except KeyError:
            print('You can\'t go that way.')

    if not self.map[self.location]['exits']:
            print('You win!')

    def start(self):
      print(self.map[self.location]['description'])

    while True:
        instruction = input('> ')

        try:
            self.follow(instruction)
        except RuntimeError as err:
            print(err)


if __name__ == '__main__':
    game = Game()
    game.start()