@pattmax/

Far-Worlds

Python

Far Worlds is an ASCII turn-based exploration Sci-Fi RPG written in Python 3.

fork
loading
Files
  • main.py
  • art
  • art.py
  • enemies.py
  • items.py
  • npcs.py
  • planets.py
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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import enemies, items, planets, npcs
#from sty import fg, bg, ef, rs, RgbFg

#Player vars
player_exp = int(0)
player_lvl = int(1)
player_exp_to_lvl = int(10)

player_hp = int(100)
player_hp_max = int(100)
player_str = int(15)
player_def = int(5)
player_spd = int(10)
player_weight = int(0)
player_weight_max = int(50)

#Inventory array, item arrays are added to this
player_inv = []

#Equip vars, these are changed to "" if empty or an item array
player_head = None
player_torso = None
player_legs = None
player_feet = None
player_weapon = None

player_x = int(0)
player_y = int(0)

#Saves the game
def save():
	outfile = open('Far-Worlds.save','w')
	outfile.write('{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}\n{10}\n{11}\n{12}\n{13}\n{14}'.format(player_exp,player_lvl,player_exp_to_lvl,player_hp,player_hp_max,player_str,player_def,player_spd,player_weight,player_weight_max,player_inv,player_head,player_torso,player_legs,player_feet))

#Loads the game
def load():
	global player_exp,player_lvl,player_exp_to_lvl,player_hp,player_hp_max,player_str,player_def,player_spd,player_weight,player_weight_max,player_inv,player_head,player_torso,player_legs,player_feet
	load_file = open('Far-Worlds.save')
	load_array = load_file.readlines()
	player_exp = load_array[0]
	player_lvl = load_array[1]
	player_exp_to_lvl = load_array[2]
	player_hp = load_array[3]
	player_hp_max = load_array[4]
	player_str = load_array[5]
	player_def = load_array[6]
	player_spd = load_array[7]
	player_weight = load_array[8]
	player_weight_max = load_array[9]
	player_inv = load_array[10]
	player_head = load_array[11]
	player_torso = load_array[12]
	player_legs = load_array[13]
	player_feet = load_array[14]

#A quick way to clear the console since this is used frequently
def clear():
	print("\033[H\033[J")

#A quick way to spit out an incorrect command error
def incorrect():
	print("Please enter a correct command")

#Determines whether or not the player should level up
def level(exp):
	global player_exp,player_lvl,player_exp_to_lvl
	#Determines if any exp is added or if it's just an update
	if int(exp) > 0:
		player_exp = player_exp + exp
	else:
		pass
	#Establishes a loop to continually make calculations until player_exp < player_exp_to_lvl, prevents a 100/50 exp bug
	while player_exp > player_exp_to_lvl:
		player_exp = player_exp - player_exp_to_lvl
		player_lvl = player_lvl + 1

#Handles winning calculations
def win(enemy_xp):
	level(enemy_xp)
	print("Didn't bother to impliment this, you win! yay")

#Handles loading previous save on death
def lose():
	print("Didn't bother to impliment this, should load the last save automatically after printing a death message")

#Battle loop
def battle(enemy):
	global player_hp,player_str,player_def,player_spd,player_weight,player_weight_max,player_inv
	#Takes enemy array and makes all items of the array local variables
	enemy_art = enemy.art
	enemy_name = enemy.name
	enemy_hp = enemy.hp
	enemy_str = enemy.e_str
	enemy_def = enemy.e_def
	enemy_spd = enemy.spd
	enemy_exp = enemy.plus_exp
	#As long as the player and enemy are alive the loop continues
	while player_hp >=1 and enemy_hp >=1:
		clear()
		print(enemy)
		print(enemy_name + " HP: " + str(enemy_hp))
		print("Your" + " HP: " + str(player_hp))
		print(enemy_art)
		action = int(input("1.Attack\n2.Inventory\n>>"))
		#Handles attack and item inputs
		if action == 1:
			if player_spd >= enemy_spd:
				enemy_hp = enemy_hp-(player_str-enemy_def)
				if enemy_hp <=0:
					win(enemy_exp)
				player_hp = player_hp-(enemy_str-player_def)
				if player_hp <=0:
					lose()
			if enemy_spd > player_spd:
				player_hp = player_hp-(enemy_str-player_def)
				if player_hp <=0:
					lose()
				enemy_hp = enemy_hp-(player_str-enemy_def)
				if enemy_hp <=0:
					win(enemy_exp)
		elif action == 2:
			#Opens inventory, enemy attacks after, counts as a turn
			inventory()
			player_hp = player_hp-(enemy_str-player_def)
		else:
			incorrect()

def inventory():
	#Makes loop
	while True:
		clear()
		#Lists all item names in inventory and numbers them
		for item in range(len(player_inv)):
			print(str(item + 1) + ": " + player_inv[item].name)
		#Exits inventory
		action = input("\n0.Back\nV.View Equipped\n>>")
		if action == "0":
			break
		elif action == "V" or action == "v":
			equipped()
		#Handles player entering in an item number
		elif int(action) >= 1:
			clear()
			item_value = int(action) - 1
			category = player_inv[item_value].category
			print(player_inv[item_value].art)
			print("\nName: " + player_inv[item_value].name)
			print("Sell price: " + str(player_inv[item_value].sell_price))
			print("Category: " + str(player_inv[item_value].category))
			#Correctly labels the buff
			if category == "head" or category == "torso" or category == "legs" or category == "feet":
				print("Buff: Def +" + str(player_inv[item_value].plus_def))
			elif category == "weapon":
				print("Buff: Str +" + str(player_inv[item_value].plus_str))
			elif category == "health":
				print("Buff: HP +")
			else:
				print("Error")
			#Handles Back,equip,and drop functions
			action2 = int(input("\n0.Back\n1.Equip\n2.Drop\n3.View Equipped\n>>"))
			if action2 == 0:
				break
			elif action2 == 1:
				equip(item_value)
			elif action2 == 2:
				drop(item_value)
			elif action2 == 3:
				equipped()
			else:
				incorrect()

def equipped():
	#This has a crap ton of if statements, this should probably be fixed eventually :)
	global player_head,player_torso,player_legs,player_feet,player_weapon
	clear()
	head = False
	torso = False
	legs = False
	feet = False
	weapon = False
	while True:
		#Sets old_equip to None every time the loop repeats to avoid bugs
		old_equip = None
		#If a slot is not empty, that slot is printed and a variable is set True
		if player_head != None:
			print("Head: " + player_head.art + player_head.name + "\n")
			head = True
		else:
			pass
		if player_torso != None:
			print("Torso: " + player_torso.art + player_torso.name + "\n")
			torso = True
		else:
			pass
		if player_legs != None:
			print("Legs: " + player_legs.art + player_legs.name + "\n")
			legs = True
		else:
			pass
		if player_feet != None:
			print("Feet: " + player_feet.art + player_feet.name + "\n")
			feet = True
		else:
			pass
		if player_weapon != None:
			print("Weapon: " + player_weapon.art + player_weapon.name + "\n")
			feet = True
		else:
			pass
		#Allows player to unequip an item
		action = int(input("0.Back \nUnequip 1.Head 2.Torso 3.Legs 4.Feet 5.Weapon\n>>"))
		if action == 0:
			break
		elif action == 1:
			#If an item is selected and exists it is copied to old_equip and destroyed
			if head == True:
				old_equip = player_head
				player_head = None
			else:
				print("You don't have anything equipped to this slot")
		elif action == 2:
			if torso == True:
				old_equip = player_torso
				player_torso = None
			else:
				print("You don't have anything equipped to this slot")
		elif action == 3:
			if legs == True:
				old_equip = player_legs
				player_legs = None
			else:
				print("You don't have anything equipped to this slot")
		elif action == 4:
			if feet == True:
				old_equip = player_feet
				player_feet = None
			else:
				print("You don't have anything equipped to this slot")
		elif action == 5:
			if weapon == True:
				old_equip = player_weapon
				player_weapon = None
			else:
				print("You don't have anything equipped to this slot")
		else:
			incorrect()
		#If there was an item to copy over it is appended to player_inv here
		if old_equip != None:
			pickup(old_equip)
		else:
			pass

def pickup(item):
	global player_weight_max,player_weight,player_inv
	#Calculates if item's weight will make player_weight go over player_weight_max
	if player_weight + item.weight <= player_weight_max:
		player_inv.append(item)
		player_weight = player_weight + item.weight
	else:
		print("You're not able to carry this item.")

def equip(item):
	global player_hp,player_str,player_def,player_head,player_torso,player_legs,player_feet,player_weapon,player_inv
	item_equip = player_inv[item]
	category = item_equip.category
	#Checks level requirement of item
	if player_lvl >= item_equip.player_lvl:
		if category == "head":
			#Sets old item to var to add to inventory after destruction
			old_equip = player_head
			#Sets slot to the new item array
			player_head = item_equip
			#Updates player_def after determining if old_equip is empty or not
			if old_equip != None:
				player_def = (player_def - old_equip.plus_def) + item_equip.plus_def
			else:
				player_def = player_def + item_equip.plus_def
		elif category == "torso":
			old_equip = player_torso
			player_torso = item_equip
			if old_equip != None:
				player_def = (player_def - old_equip.plus_def) + item_equip.plus_def
			else:
				player_def = player_def + item_equip.plus_def
		elif category == "legs":
			old_equip = player_legs
			player_legs = item_equip
			if old_equip != None:
				player_def = (player_def - old_equip.plus_def) + item_equip.plus_def
			else:
				player_def = player_def + item_equip.plus_def
		elif category == "feet":
			old_equip = player_feet
			player_feet = item_equip
			if old_equip != None:
				player_def = (player_def - old_equip.plus_def) + item_equip.plus_def
			else:
				player_def = player_def + item_equip.plus_def
		elif category == "weapon":
			old_equip = player_weapon
			player_weapon = item_equip
			if old_equip != None:
				player_str = (player_str - old_equip.plus_str) + item_equip.plus_str
			else:
				player_str = player_str + item_equip.plus_str
		elif category == "health":
			#Adds hp to player's hp
			if player_hp + (player_hp + item_equip.plus_def) <= player_hp_max:
				player_hp = player_hp + item_equip.plus_def
		#As long as old_equip isn't empty, it gets appended to player_inv
		if old_equip != None:
			player_inv.append(old_equip)
		#Removes the item equipped from player_inv to avoid duplicates
		player_inv.remove(item_equip)
	else:
		print("Your level is not high enough.")

def drop(item):
	global player_weight,player_inv
	#Translates item into specific array location as item_remove
	item_remove = player_inv[item]
	#Calculates weight, removes the item, updates inventory length
	player_weight = player_weight - player_inv[item].weight
	player_inv.remove(item_remove)

def npc(npc):
	#Prints NPC speech
	if npc.speech != None:
		print(npc.name + ": " + npc.speech)
	else:
		pass
	for function in npc.functions:
		if function[0] == 'pickup':
			pickup(function[1])
		if function[0] == 'battle':
			battle(function[1])
		else:
			pass

def draw_sprite(sprite):
	for char in sprite:
		print(char)

def draw_map(location):
	for row in location:
		for tile in row:
			print(tile, end=' ')
		print()

#Testing things below this, subject to change often...
if __name__ == "__main__":
	#draw_map(planets.quark_system)
	#print(items.gamma_blaster.art)
	pickup(items.gamma_blaster)
	drop(0)
	pickup(items.gamma_blaster)
	equip(0)
	inventory()