İşte bu yapacağını nasıl; açıklama yorumların içinde:
# Print out the start text
print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.")
print("You stand up and decide to take a look around.")
# Use a function to get the direction; saves some repeating later
def get_direction():
answer = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
print(" ")
return answer
# Keep running this block until the condition is False.
# In this case, the condition is True, so it keeps running forever
# Until we tell Python to "break" the loop.
while True:
# I changed "str" to "answer" here because "str" is already a Python
# built-in. It will work for now, but you'll get confused later on.
answer = get_direction()
if answer == "d":
print("You see your combat boots and the grassy ground below your feet. ")
# Stop the loop
break
elif answer == "l":
print("The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...")
break
elif answer == "r":
print("The forest is warm and inviting that way, you think you can hear a distant birds chirp.")
break
elif answer == "u":
print("The blue sky looks gorgeous, a crow flies overhead... that's not a crow...")
print("It's a Nevermore, an aerial Grim. You stand still until it passes.")
break
elif answer == "b":
print("the grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.")
print("Mount Glenn, one of the most Grim-infested places in all of Remnant.")
print("It's a bit unsettling.")
break
else:
print("Try that again")
# NO break here! This means we start over again from the top
Şimdi, bunların hiçbiri birkaç yönden daha eklerseniz çok iyi ölçekler; Gitmene sonra bu döngü içinde yeni döngü yani "doğru" vb, yeni bir soru istediğinizi varsayalım çünkü
# The start text
print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.")
print("You stand up and decide to take a look around.")
# Use a function to get the direction
def get_direction():
answer = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
print(" ")
return answer
# Use a function to store a "location" and the various descriptions that
# apply to it
def location_start():
return {
'down': [
# Function name of the location we go to
'location_foo',
# Description of this
'You see your combat boots and the grassy ground below your feet.'
],
'left': [
'location_bar',
'The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...'
],
'right': [
'location_other',
'The forest is warm and inviting that way, you think you can hear a distant birds chirp.'
],
'up': [
'location_more',
"The blue sky looks gorgeous, a crow flies overhead... that's not a crow...\n" +
"It's a Nevermore, an aerial Grim. You stand still until it passes."
],
'behind': [
'location_and_so_forth',
"The grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.\n" +
"Mount Glenn, one of the most Grim-infested places in all of Remnant.\n" +
"It's a bit unsettling."
],
}
# And another location ... You'll probably add a bunch more...
def location_foo():
return {
'down': [
'location_such_and_such',
'desc...'
],
}
# Store the current location
current_location = location_start
# Keep running this block until the condition is False.
# In this case, the condition is True, so it keeps running forever
# Until we tell Python to "break" the loop.
while True:
# Run the function for our current location
loc = current_location()
answer = get_direction()
if answer == ("d"):
direction = 'down'
elif answer == ("l"):
direction = 'left'
elif answer == ("r"):
direction = 'right'
elif answer == ("u"):
direction = 'up'
elif answer == ("b"):
direction = 'behind'
else:
print("Try that again")
# Continue to the next iteration of the loop. Prevents the code below
# from being run
continue
# print out the key from the dict
print(loc[direction][1])
# Set the new current location. When this loop starts from the top,
# loc = current_location() is now something different!
current_location = globals()[loc[direction][0]]
Şimdi, bu yapıyor sadece biri yoludur; Burada bir dezavantaj oyuncu farklı yönlerden bir yere yaklaşma izin vermek istiyorsanız konumlar için açıklamaları tekrarlamak için gerekir. Bu, macera oyununuza uygulanamaz (, 'u doğru hatırlıyorumsa, buna izin vermez).
Bunu oldukça kolay bir şekilde düzeltebilirsiniz, ancak bunu size bir egzersiz olarak bırakacağım ;-)
Bir ara döngü kullanın, bir kesme koşulu sağlandığında ayrılın. – DhruvPathak
Döngüyü anlamıyorum. Onlar ... sinir bozucu. Bunun için nasıl giderim? –
Neden "str" ("d") 'yi kullanıyorsunuz ve str == d' değil? – ASCIIThenANSI