-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc.py
173 lines (149 loc) · 4.78 KB
/
func.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
import os, sys
from random import *
money=0
items=[]
#starting pos
currentLoc=[0,0]
#For moving x distance
getLoc=0
#set game boundaries
min_boundaries=[0,0]
max_boundaries=[2,2]
#Generate the treasure/and other treasure related variables
treasurex=randint(1,2)
treasurey=randint(1,2)
treasureLoc=[treasurex,treasurey]
print("Hint: ",treasureLoc)
#Where the random money prize will be generated
max_earnings=150
generate_money=randint(1,max_earnings)
#For the generation of monsters
monsterx=randint(1,2)
monstery=randint(0,2)
monsterLoc=[monsterx,monstery]
flee_amount=randint(5,50)
availableItems=["flashlight", "crowbar", "sword", "pistol"]
availableItemPrices=["30", "50", "65", "250"]
file="user.txt"
#Function for saving inventory and money
def save(inventory):
#If file does not exist create one
if not os.path.isfile(file):
file_open=open(file, "a")
#Write inventory to file
file_open.write("Inventory: " + ",".join(inventory) + "\n")
#Write money to file
file_open.write("money: " + str(money) + "\n")
file_open.close()
def open_file():
if not os.path.isfile(file):
file_open(file, "a")
with open(file, 'r') as f:
for line in f:
if "money: " in line:
money=line.split("money: ", 1)
money=int(money[1])
print("money: ", money)
#Check if there is enough money
def isEnough(price):
if price <= money:
#Allow changing of global varaible money
global money
money=money-price
return True
else:
return False
def shop():
print("You have %i dollars" % money + "\n")
#Split array into readable text
splitAvailableItems=""
for numVar in range(0, len(availableItems)):
splitAvailableItems+="#%i) "%(numVar+1)+availableItems[numVar] + "\n"
print("There are %i available items you can purchase, Item \n%s"%(len(availableItems), splitAvailableItems))
newItem = raw_input("Which one would you like to purchase?: ")
#If item is already in inventory
x=0
#While length is less than the array
for x in range(0, len(availableItems)):
#If x is equal to the item the user entered
if x == int(newItem):
if availableItems[x-1] in items:
print("You already have that.")
return None
break
if isEnough(availableItemPrices[x-1]):
items.append(availableItems[x-1])
else:
print("Not enough money")
#Exit loop
break
#For user inventory
def inventory(items):
itemsString=",".join(items)
print("\n You have: %s" % itemsString + "\n")
def fight():
print("Battle")
def prize():
print("You have won: %i " % generate_money + "dollars")
return generate_money
#For moves
def move():
#currentLocString=",".join(currentLoc)
print("You are currently ", currentLoc)
print("You can move (right),(left),(up),(down)")
newmove=raw_input("move: ")
if newmove.lower() == "right":
#Get last value of array
getLoc=currentLoc[-1]
getLoc=getLoc+1
if(getLoc>max_boundaries[-1]):
print("Sorry, you've hit a boundary")
return None
#Sets new x location to array
currentLoc[-1]=getLoc
#print("You are now here: %d" % int(getLoc))
print(currentLoc)
elif(newmove.lower() == "down"):
getLoc=currentLoc[0]
getLoc=getLoc+1
if(getLoc<min_boundaries[0]) or (getLoc>max_boundaries[0]):
print("Sorry, you've hit a boundary")
return None
currentLoc[0]=getLoc
#print("You are now here: %d" % int(getLoc))
print(currentLoc)
elif(newmove.lower() == "up"):
getLoc=currentLoc[0]
getLoc=getLoc-1
if(getLoc<min_boundaries[0]):
print("Sorry, you've hit a boundary")
return None
currentLoc[0]=getLoc
print(currentLoc)
elif(newmove.lower() == "left"):
getLoc=currentLoc[-1]
getLoc=getLoc-1
if(getLoc<min_boundaries[0]):
print("Sorry, you've hit a boundary")
return None
currentLoc[-1]=getLoc
print(currentLoc)
return currentLoc
if currentLoc==monsterLoc:
print("A beast has appeared out of nowhere...\n")
print("You can flee(type 'flee' or type 'battle') for %i" % flee_amount + " dollars\n")
decision=raw_input("Descision: ")
if decision == "flee":
if money>=flee_amount:
global money
money-flee_amount
print("You know have %i" % money + " dollars")
return None
else:
print("Not enough. Money")
fight()
else:
fight()
else:
print("Unknown option")
return None