repl.it
Python

Text adventure game.

fork
loading
Files
  • main.py
  • movement.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
import time, random, movement, re

def prints(text,endLine=True,min=0.025,max=0.08):
	for x in text:
		print(end=x,flush=True)
		time.sleep(random.uniform(min,max))
	if endLine:
		print()

prints("Adventre! v1.1",max=0.11)
prints("By SirNapkin1334",max=0.11)
prints("Created for the CodeJam4 challenge.",max=0.11)

prints("What is your name? ",False)
name = input(">")
prints(f"Yes, your name is {name}, correct? [y/n] ",False)
yn = input(">")
if yn.lower() == "y" or yn.lower() == "ye" or yn.lower() == "yes":
	prints(f"Alright, {name}, let's get you started on your adventure!")
else:
	prints("Alright, you'd better be right this time; what is your name? ",False)
	name = input(">")
del yn
#initialize game vars
game = True
inventory = []
location_desc = "wild"
location = "wild1"
closet_locked = True

def unlock_closet():
	global closet_locked
	closet_locked = False
	
	movement.bedroom[1].append("odd_key")
def win():
	global game
	game = False
prints("Not sure what to do? Type `help`!")
while game: #main loop
	prints(str(eval("movement."+location+"[0]")).replace("[","").replace("]","").replace("'",""),True,0.02,0.05) #prints location info
	prints("You can go:")
	for a in eval("movement."+location+"[-1]"):
		prints(a[0][0].upper()+a[0][1:])
	if location == "bedroom":
		if closet_locked == True:
			prints("You see a locked closet.")
	if location == "bonus":
		prints("You see an odd cabinet, likely opened by an equally odd key.")
	if eval("movement."+location+"[1]") != []:
		if len(eval("movement."+location+"[1]")) == 1:
			prints("You see a "+eval("movement."+location+"[1][0]"))
		else:
			prints("You see a "+str(eval("movement."+location+"[1][0]")).replace("'","").replace("_"," ")+" and "+str(eval("movement."+location+"[1][-1]")).replace("'","").replace("_"," ")+".")
	action = input(">>>") #main input
	if action.lower() == "help": #help menu
		prints("HELP MENU")
		prints("help: displays this menu (how else would you have gotten here?)")
		prints("go [direction]: goes in said direction")
		prints("use [item]: uses said item")
		prints("take [item]: takes the item")
		prints("inv: prints inventory")
		prints("notes: prints version notes")
	elif action.lower() == "notes":
		prints("Adventre v1.1 Notes:")
		prints("-Added NOTES command")
		prints("-Updated code to use f-strings instead of %-formatting.")
		prints("-Updated the super secret debug features!")
	elif action.lower() == "exit": #in case you get tired
		yn = input("You wish to stop playing? (progress will be lost! (probably)) [y/n] >")
		if yn.lower() == "y" or yn.lower() == "ye" or yn.lower() == "yes":
			del yn
			break
		else:
			prints("I'll take that as a no.")
			del yn
	elif re.match("go *",action):
		if action == "go" or action == "go ":
			prints("Well, where should I go then?")
		else:
			goto = action.split(" ",1)[-1]
			yn = False
			if goto.lower() not in eval("movement."+location+"[-1][0][0]") and goto.lower() not in eval("movement."+location+"[-1][-1][0]"): #check that you can go there
			#for y in eval("movement."+location+"[-1][0]"):
			#	if goto.lower() in y:
			#		yn = True
			#if yn == False:
				prints(f"I can't go {goto}.")
			elif goto.lower() in eval("movement."+location+"[-1][0][0]"):
				location = eval("movement."+location+"[-1][0][-1]") #grab location info, go there
				location_desc = eval("movement."+location+"+[0]") #update location description
			elif goto.lower() in eval("movement."+location+"[-1][-1][0]"):
				location = eval("movement."+location+"[-1][-1][-1]") #same as above but in case it's in second slit
				location_desc = eval("movement."+location+"[0]") #^^^
	elif re.match("use *",action): #using item
		if action == "use" or action == "use ":
			prints("Well, what should I use?")
		else:
			if action.split(" ",1)[-1] not in inventory:
				if action.split(" ",1)[-1][0].lower() == "a" or action.split(" ",1)[-1][0].lower() == "e" or action.split(" ",1)[-1][0].lower()== "i" or action.split(" ",1)[-1][0] == "o" or action.split(" ",1)[-1][0] == "u":
					prints(f"I don't have an {action.split(' ',1)[-1]}.") #ocd /\
				else:
					prints(f"I don't have a {action.split(' ',1)[-1]}.")
			else:
				#if action.split(" ",1)[-1] not in eval("movement."+location+"[2][0]"):
				yn = False
				for x in eval("movement."+location+"[2]"):
					if action.split(" ",1)[-1] in x:
						yn = True
				if yn == False:
					if action.split(" ",1)[-1][0].lower() == "a" or action.split(" ",1)[-1][0].lower() == "e" or action.split(" ",1)[-1][0].lower()== "i" or action.split(" ",1)[-1][0] == "o" or action.split(" ",1)[-1][0] == "u":
						 prints(f"I can't use an {action.split(' ',1)[-1]} here!")
					else:
						prints(f"I can't use a {action.split(' ',1)[-1]} here!")
				else:
					inventory.remove(action.split(" ",1)[-1])
					#toRun = eval("movement."+location+"[2]").index(action.split(" ",1)[-1]) #THIS IS AN INT
					for each in eval("movement."+location+"[2]"):
						if action.split(" ",1)[-1] in each:
							toRun = each.index(action.split(" ",1)[-1])
					cmd = eval("movement."+location+"[2]["+str(toRun)+"][-1]")
					eval(cmd)
	elif re.match("take *",action): #to take an item
		if action == "take" or action == "take ":
			prints("Well, what should I take?")
		else:
			if action.split(" ",1)[-1].lower() not in eval("movement."+location+"[1]"): #check if the item is there
				prints(f"I don't see a {action.split(' ',1)[-1]}.")
			else:
				inventory.append(action.split(" ",1)[-1].lower()) #append said item to inventory
				eval("movement."+location+"[1]").remove(action.split(" ",1)[-1].lower()) #delete item from holding list (movement.{location}[0])
				if action.split(" ",1)[-1][0].lower() == "a" or action.split(" ",1)[-1][0].lower() == "e" or action.split(" ",1)[-1][0].lower()== "i" or action.split(" ",1)[-1][0] == "o" or action.split(" ",1)[-1][0] == "u":
					prints(f"You took an {action.split(' ',1)[-1].lower()}.")
				else:
					prints(f"You took a {action.split(' ',1)[-1].lower()}.")
	elif re.match("leave *", action):
		if action == "leave" or action == "leave ":
			prints("What should I leave?")
		else:
			if action.split(" ",1)[-1].lower() not in inventory: #check if you have item:
				if action.split(" ",1)[-1][0].lower() == "a" or action.split(" ",1)[-1][0].lower() == "e" or action.split(" ",1)[-1][0].lower()== "i" or action.split(" ",1)[-1][0] == "o" or action.split(" ",1)[-1][0] == "u":
					prints(f"I don't have an {action.split(' ',1)[-1]}.")
				else:
					prints(f"I don't have a {action.split(' ',1)[-1]}.")
			else:
				eval("movement."+location+"[1]").append(action.split(" ",1)[-1].lower()) # add item to holding list
				inventory.remove(action.split(" ",1)[-1].lower())
	elif action.lower() == "inv": #prints inventory
		if inventory == []:
			prints("You don't have anything!")# don't want it to look weird
		else:
			prints(f"""Your inventory consists of: {str(inventory)[1:-1].replace("'","")}.""") #make it look nicer
	elif re.match("eval *",action): #for debugging
		if action == "eval" or action == "eval ":
			prints("Invalid Syntax.")
		else:
			try: #so glad I know about this
				eval(action.split(" ",1)[-1])
			except:
				try:
					exec(action.split(" ",1)[-1])
				except:
					prints("Error!")
	else:
		prints("I don't know how to %s." % action)
else:
	prints("Well done, %s, you won! GG :)" % name)
	prints("Check out my other projects:")
	prints(str(["",None]).replace("[","").replace("]","").replace("'",""))
?