forked from Matthew2000/OLD_Elementals-The-Shadow-Wars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPG.py
153 lines (117 loc) · 4.15 KB
/
RPG.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python3
import locale
import sys
from BaseClasses.NpcClass import *
from BaseClasses.PlayerClass import *
from Functions import Load
from Globals import *
from Maps.Environment import *
locale.setlocale(locale.LC_ALL, '')
code = "utf-8"
# initiates some required variables
save = {"all_NPCs": [], "player": {}, "current_map": "Maps/map1.json"}
map_save = {}
error = open('RPGErrorLog.txt', 'w')
sys.stderr = error
Key = -1
player_name = "Matthew"
QuestClass.load_all_quests()
global current_map_path
def new_game(enemies, npcs):
Load.load_npcs_for_new_game()
Func.set_all_stats(enemies, npcs)
DebugLog.write('NPCs loaded: ' + str(len(npcs)) + "\n\n")
DebugLog.write("############\nGame loaded\n############\n\n")
try:
# hides the cursor
curses.curs_set(0)
curses.noecho()
map_window.border()
map_window.keypad(True)
journal.keypad(True)
# gets the dimensions of the map
screen_dims = screen.getmaxyx()
player1 = create_player("Matthew", "@", Race.Human, [23, 71])
# loads the save if it exits.
# if there is no save it makes a new game
if os.path.exists('save.json'):
with open('save.json', 'r') as f:
save = json.load(f)
f.close()
load_player(player1, save)
player1.inventory = Func.load_inventory(save["player"]["inventory"])
load_player_equipment(player1, save)
Load.load_npcs(save, Character.all_NPCs)
current_map_path = save["current_map"]
DebugLog.write("############\nGame loaded\n############\n\n")
else:
new_game(Character.all_enemies, Character.all_NPCs)
map1.change_map(current_map_path)
map1.show_map()
map1.load_common_npcs()
spawn_character(map_window, player1, player1.location[0], player1.location[1])
screen.refresh()
map_window.refresh()
journal.addstr(1, 1, "game start")
player1.begin_play()
for npc in Character.all_NPCs:
npc.begin_play()
while Key != ord("q"):
Func.update_player_location(player1, map1)
current_map_path = map1.directory
Func.update_npc_locations(Character.all_NPCs, map1)
screen.refresh()
map_window.refresh()
map_window.border()
Func.update_game(player1)
Func.player_dead(player1)
DebugLog.flush()
Key = map_window.getch() # gets the player input
if Key == curses.KEY_RESIZE:
screen_dims = screen.getmaxyx()
screen.erase()
curses.doupdate()
Func.update_player_location(player1, map1)
Func.update_npc_locations(Character.all_NPCs, map1)
player1.update_player_status()
journal.resize(50, 65)
map_window.resize(35, 100)
journal.refresh()
conversation = curses.newwin(10, 20, 38, 84)
player1.make_player_stat_win()
continue
player1.tick(Key, map1)
for npc in Character.all_NPCs:
if Func.on_map(map1, npc.name, npc.id):
npc.tick(Key, player1)
DebugLog.flush()
Func.respawn_enemies(Character.all_NPCs)
Func.update_game(player1)
Func.update_npc_locations(Character.all_NPCs, map1)
screen.refresh()
map_window.refresh()
Func.update_game(player1)
else:
DebugLog.write("\n############\nGame Closed\n############\n\n")
Func.update_npc_locations(Character.all_NPCs, map1)
DebugLog.write("\n############\nSave start\n############\n\n")
save_player(player1, save)
save_npcs(save, Character.all_NPCs)
save["current_map"] = current_map_path
DebugLog.write("\n############\nGame saved\n############")
except:
DebugLog.write(str(sys.exc_info()))
finally:
if os.path.exists('save.json'):
temp = open("save.json", "r")
temp_save = json.load(temp)
if "total_exp" in temp_save["player"]:
os.rename('save.json', 'save.json.bak')
temp.close()
if "total_exp" in save["player"]:
with open('save.json', 'w') as f:
json.dump(save, f, sort_keys=True, indent=2)
f.close()
DebugLog.close()
error.close()
curses.endwin()