/
game.py
68 lines (54 loc) · 1.98 KB
/
game.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
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
import functions.commands as command
import functions.database as db
if db.testConnection():
player = db.getPlayer()
f = open('ASCII/StartScreen_UTF-8.asc', 'r')
print(f.read())
f.close()
roomState = db.getRoomState(player.roomID)
if roomState is not None:
print("--\n{}".format(roomState))
else:
roomDescription = db.getRoomDescription(player.roomID)
print("--\n{}".format(roomDescription))
while True:
'''
You can end loop by selecting 5 in main context or write
"quit" in game context.
'''
context = command.doMenu()
while context == "main":
# validate to player that we are in correct context
prompt = "(main) >>> "
try:
# in main menu context we catch only integers as commands
c = int(input(prompt))
context = command.doMenu(c)
except ValueError as e:
print(e)
while context == "game":
# validate to player that we are in correct context
prompt = "(game) >>> "
hp=db.gethp()
points=db.getPointsFromPlayer()
# update player object and if doesn't exist yet create it
player = db.updatePlayer(player)
print("\nYour HP: {}".format(hp) )
print("Your points: {}".format(points))
# get directions player can go
directions = db.getDirections(player.roomID)
# get items in room player can take
items = db.getItemsInRoom(player.roomID)
# get NPCs in room player can action (talk or fight)
npcs = db.getNPCsInRoom(player.roomID)
# ask a command from player
c = input(prompt).lower().split()
# pass a command, possible directions, items in room and the player
# object to command parser
if (command.isValid(c)):
context = command.execute(c, directions, items, npcs, player)
else:
print('Invalid command. '
'Write "help" to get list of available commands.'
)