编辑:这是通过将我的打印语句更改为print(r“”“”“”)来解决的,“ r”将打印原始文本并忽略诸如“ /”之类的符号
我正在为我的高中AP Comp Sci课程使用ASCII图形制作基于文本的游戏。我遇到的问题是由于某种原因,我的图形(仅是三引号字符串)在打印时不像在我的IDE中那样。它们始终偏离一行或将行推得比应往的远。这是一个我的代码摘录,将产生上述错误:
def draw(str):
#This function allows the game to draw graphics
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.0001**100)
print('')
def clear():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def gLine():
#Function Draws lines
draw("*****************************************************************************************************************************")
def gChurch():
clear()
gLine()
draw(""" +
|
,|,
|||
/ | \
| | |
| | |
/ | \
| | |
| | \
/ \ |
| | |
| | \
/ | |
8 | | |
''8'' / | \
8 / \ ,\
,d8888888888888|========|='' |
,d" "88888888888| ,aa, | a |
,d" "888888888| 8 8 | 8 |
,d8888888b, "8888888| 8aa8 | 8,|
,d" "8888888b, "88888|========|='''|
,d" "8888888b, "888| a a | a |
,d" ,aa, "8888888b, "8| 8 8 | 8,|
/| d' 'b |''''''| |========|='''|
| 8 8 | | | ,aa, | a |
| 8aaaa8 | | | 8 8 | 8 |
| | | | "''" | ,,=|
|aaaaaaaaaaaaaa|======''''''''''''''''''''' """)
gLine()
def gGameOver():
clear()
draw("""
_____ ____
/ ____| / __ \
| | __ __ _ _ __ ___ ___ | | | |_ _____ _ __
| | |_ |/ _` | '_ ` _ \ / _ \ | | | \ \ / / _ \ '__|
| |__| | (_| | | | | | | __/ | |__| |\ V / __/ |
\_____|\__,_|_| |_| |_|\___| \____/ \_/ \___|_|
""")
def event():
if Zalgo.happiness < 60:
gChurch()
ps("Zalgo is dissatisfied with your level of faithfullness. A thunderstorm is heard overhead...")
chance = random.randint(1, 4)
ps("Rolling dice...")
if chance == 1:
ps("You have been smitten!")
print("You have died")
time.sleep(3)
gameOver()
else:
ps("Lightning cracks overhead, but does not strike you. Zalgo has forgiven you... You live another day")
此代码本身无缘无故地导致教堂中的一行人被鞭打。我查看了至少40次代码,但仍然没有找到执行此操作的理由。如果您想复制并粘贴它,并亲自查看错误,请参见以下游戏代码:
#Made by Nick Pope -------------------------------------------------#
import sys
import os
import time
import random
#Screen-------------------------------------------------------------#
os.system("mode con cols=125")
#Width of Screen
os.system("mode con lines=50")
#Length of Screen
#Commands-----------------------------------------------------------#
commands = ["//options, //Help"]
#Lists--------------------------------------------------------------#
activity = ["Example Activity 1.", "Example activity 2"]
resource = ["wealth","food","land"]
#Functions----------------------------------------------------------#
def save():
print("This is supposed to save, but it doesn't work yet")
def load():
print("This is supposed to load, but it doesn't work yet")
def clear():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def back():
titleScreen()
def wait():
wait = input("Press Enter to continue: ")
def pI(options):
#PI stands for Player Input
playerInput = (input("==> "))
gSpace()
while playerInput not in options and playerInput not in commands:
print(playerInput + " is not a possible option")
print("Do /options for a list of acceptable options")
playerInput = (input("==> "))
if playerInput == "/commands":
print(commands)
elif playerInput == "//options":
print("Acceptable inouts are" + options)
elif playerInput == "//help":
gHelp()
return(playerInput)
def pC():
#"pC" stands for "playerContinue" This function is essentially a fancy "Wait" function
playerInput = input("Enter anything to continue ==> ")
def name ():
playerInput = input("==> ")
for i in playerInput:
while " " in playerInput:
print("You may not uses spaces in your name, try again")
playerInput = input("==> ")
return (playerInput)
#Text Functions-----------------------------------------------------#
def ps(str):
#This function allows the game to type slowly with textwrap
#PS is short for "Print Statement"
typing_speed = len(str) * 5
count = 0
space = False
#This next aspect of the function works with the autotext wrap
#To make sure that it only wraps after full words, not
#midway through
for letter in str:
count += 1
if letter == " " or letter == "":
space = True
else:
space = False
if count >= 100 and len(str) < 200 and space == True:
print('\n')
count = 0
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(random.random()*10.0/typing_speed)
print('')
def draw(str):
#This function allows the game to draw. I made this so that when
#displays load faster than normal text
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.0001**100)
print('')
#Graphics-----------------------------------------------------------#
#All Graphic functions start with "g" so that i don't take any
#names I might need later
def gSpace():
print(" ")
def gLine():
#Function Draws lines
draw("*****************************************************************************************************************************")
def gtextBox():
draw("##########################")
def gNextWeek():
gLine()
draw("""
_______ __ __ __ __
\ \ ____ ___ ____/ |_ / \ / \ ____ ____ | | __
/ | \_/ __ \\ \/ /\ __\ \ \/\/ // __ \_/ __ \| |/ /
/ | \ ___/ > < | | \ /\ ___/\ ___/| <
\____|__ /\___ >__/\_ \ |__| \__/\ / \___ >\___ >__|_ \
\/ \/ \/ \/ \/ \/ \/
""")
gLine()
def gtitleScreen():
clear()
gLine()
draw("""
___________.__ __ __ __ .__ _____ __________ .__
\__ ___/| |__ ____ / \ / \___________ _/ |_| |__ _____/ ____\ \____ /____ | | ____ ____
| | | | \_/ __ \ \ \/\/ |_ __ \__ \\ __\ | \ / _ \ __\ / /\__ \ | | / ___\ / _ \
| | | Y \ ___/ \ / | | \// __ \| | | Y \ ( <_> ) | / /_ / __ \| |_/ /_/ > <_> )
|____| |___| /\___ > \__/\ / |__| (____ /__| |___| / \____/|__| /_______ (____ /____|___ / \____/
\/ \/ \/ \/ \/ \/ \/ /_____/
""")
gLine()
def gExit():
clear()
#save()
gLine()
draw("""
___________ .__ __ .__
\_ _____/__ __|__|/ |_|__| ____ ____
| __)_\ \/ / \ __\ |/ \ / ___\
| \> <| || | | | | \/ /_/ >
/_______ /__/\_ \__||__| |__|___| /\___ / /\ /\ /\ /\ /\ /\ /\
\/ \/ \//_____/ \/ \/ \/ \/ \/ \/ \/""")
gLine()
time.sleep(3)
sys.exit
def gHelp():
clear()
gLine()
draw("""
___ ___ .__ _____
/ | \ ____ | | ______ / \ ____ ____ __ __
/ ~ \/ __ \| | \____ \ / \ / \_/ __ \ / \| | \
\ Y | ___/| |_| |_> > / Y \ ___/| | \ | /
\___|_ / \___ >____/ __/ \____|__ /\___ >___| /____/
\/ \/ |__| \/ \/ \/
""")
gLine()
print("Welcome to the help menu! This game was made by nick pope")
print("1. Go Back")
options = ["1"]
playerInput = pI(options)
if pI == "1":
(back())
def gGameOver():
clear()
draw("""
_____ ____
/ ____| / __ \
| | __ __ _ _ __ ___ ___ | | | |_ _____ _ __
| | |_ |/ _` | '_ ` _ \ / _ \ | | | \ \ / / _ \ '__|
| |__| | (_| | | | | | | __/ | |__| |\ V / __/ |
\_____|\__,_|_| |_| |_|\___| \____/ \_/ \___|_|
""")
def gNorthernLights():
gLine()
draw(""" ` : | | | |: || : ` : | |+|: | : : :| . `
` : | :| || |: : ` | | :| : | : |: | . :
.' ': || |: | ' ` || | : | |: : | . ` . :.
`' || | ' | * ` : | | :| |*| : : :|
* * ` | : : | . ` ' :| | :| . : : * :.||
.` | | | : .:| ` | || | : |: | | ||
' . + ` | : .: . '| | : :| : . |:| ||
. . ` *| || : ` | | :| | : |:| |
. . . || |.: * | || : : :|||
. . . * . . ` |||. + + '| ||| . ||`
. * . +:`|! . |||| :.||`
+ . ..!|* . | :`||+ |||`
. + : |||` .| :| | | |.| ||` .
* + ' + :|| |` :.+. || || | |:`|| `
. .||` . ..|| | |: '` `| | |` +
. +++ || !|!: ` :| |
+ . . | . `|||.: .|| . . `
' `|. . `:||| + ||' `
__ + * `' `'|. `:
"' `---''''----....____,..^---`^``----.,.___ `. `. . ____,.,-
___,--''''`---'' ^ ^ ^ ^ """'---,..___ __,..---'''
"' ^ ``--..,__ """)
gLine()
def gChurch():
clear()
gLine()
draw(""" +
|
,|,
|||
/ | \
| | |
| | |
/ | \
| | |
| | \
/ \ |
| | |
| | \
/ | |
8 | | |
''8'' / | \
8 / \ ,\
,d8888888888888|========|='' |
,d" "88888888888| ,aa, | a |
,d" "888888888| 8 8 | 8 |
,d8888888b, "8888888| 8aa8 | 8,|
,d" "8888888b, "88888|========|='''|
,d" "8888888b, "888| a a | a |
,d" ,aa, "8888888b, "8| 8 8 | 8,|
/| d' 'b |''''''| |========|='''|
| 8 8 | | | ,aa, | a |
| 8aaaa8 | | | 8 8 | 8 |
| | | | "''" | ,,=|
|aaaaaaaaaaaaaa|======''''''''''''''''''''' """)
gLine()
#Factions Zalgo, King, Clergy, Peasant------------------------------#
class faction():
def __init__(self, name, happiness, population, wealth, food, land):
self.name = name
self.happiness = happiness
self.population = population
self.wealth = wealth
self.food = food
self.land = land
Zalgo = faction(
"Zalgo",
0.100,
1,
50,
25,
0)
church = faction(
"church",
0.100,
16,
50,
25,
5)
king = faction(
"king",
0.100,
1,
50,
100,
50)
peasants = faction(
"peasants",
0.100,
1000,
20,
25,
100)
factions = [Zalgo, church, king, peasants]
#Mechanics----------------------------------------------------------#
def weekEnd():
week += 1
calculateStats(factions)
gNextWeek()
def zalgo():
print("Zalgo mechanics are not done yet")
def calculateStats(factions):
for i in factions:
#Faction Food Falculation
i.food += i.land*0.7 + i.population*0.3
#Faction Wealth Calculation
i.wealth += i.food*0.4 + i.land*0.6
#Faction Happiness Calculation
i.happiness = i.wealth*0.5 + i.food*0.3 + i.land*0.2
if i.happiness > 100:
i.happiness = 100
def gameOver():
gGameOver()
ps("Would you like to play again?")
gtextBox()
print("1. Yes")
print("2. No")
gtextBox()
options = ["1","2"]
playerInput = pI(options)
if playerInput == "1":
titleScreen()
elif playerInput == "2":
titleScreen()
#Events-------------------------------------------------------------#
def event():
if Zalgo.happiness < 60:
gChurch()
ps("Zalgo is dissatisfied with your level of faithfullness. A thunderstorm is heard overhead...")
chance = random.randint(1, 4)
ps("Rolling dice...")
if chance == 1:
ps("You have been smitten!")
print("You have died")
time.sleep(3)
gameOver()
else:
ps("Lightning cracks overhead, but does not strike you. Zalgo has forgiven you... You live another day")
elif king.happiness < 60:
gChurch()
ps("The King summons you to his chamber..")
chance = random.randint(1, 4)
ps("Rolling dice...")
if chance == 1:
ps("The king ambushes you and takes your head!")
print("You have died")
time.sleep(3)
gameOver()
else:
ps("The king forgives you, you live to see another day")
elif church.happiness < 60:
gChurch()
ps("The church is disastisfied by your leadership! They staged a coup!")
chance = random.randint(1, 4)
ps("Rolling dice...")
if chance == 1:
ps("You have been smitten zalgo!")
print("You have died")
time.sleep(3)
gameOver()
else:
ps("The disscuss better leadership techniques with you.. you live to see another day")
elif peasants.happiness < 60:
gChurch()
ps("The peasants revolt and enter the church.")
chance = random.randint(1, 4)
ps("Rolling dice...")
if chance == 1:
ps("The peasants stab you 23 times..you resemble a salad")
print("You have died")
time.sleep(3)
gameOver()
else:
ps("You manage to convince the peasants to go home, or face the wrath of Zalgo. You live another day")
time.sleep(3)
#Game Loop----------------------------------------------------------#
def titleScreen():
clear()
pCS = titleScreen
gtitleScreen()
ps("1. Start")
ps("2. Load")
ps("3. Exit")
ps("4. Help")
options = ["1","2","3","4"]
playerInput = pI(options)
if playerInput == "1":
start0()
pCS = start0()
if playerInput == "2":
gload()
if playerInput == "3":
gExit()
if playerInput == "4":
gHelp()
def gameMenu():
clear()
count = 0
#When this count reaches 1, A faction has been interacted with, and another interaction may not occur
event()
zalgo()
ps("What would you like to do?")
gSpace()
gtextBox()
print("1. Interact with Faction")
print("2. End Turn")
gtextBox()
options = ["1", "2"]
playerInput = pI(options)
if playerInput == "1":
while count < 1:
ps("Which faction do you want to interact with?")
gtextBox()
print("1. Church")
print("2. King")
print("3. Peasants")
gtextBox()
options = ["1", "2", "3"]
playerInput = pI(options)
if playerInput == "1":
ps("How would you like to interact with church?")
gSpace()
gtextBox()
print("1. Treat to activity")
print("2. Ask for resource")
print("3. Give money")
gtextBox()
options = ["1","2","3"]
playerInput = pI(options)
if playerInput == "1":
ps("You treat the clergy of the church to " + str(random.choice(activity)))
value = random.randint(1, 5)*0.001
church.happiness += value
calculateStats(factions)
ps("The church's happiness is now " + str(church.happiness))
count += 1
pC()
elif playerInput == "2":
ps("you ask the clergy of the church for " + str(random.choice(resource)) + "! But since you and the clergy are the church, nothing is gained")
count += 1
pC()
elif playerInput == "3":
value = random.uniform(0.005, church.wealth)
ps("You give the clergy of your church " + str(value) + " gold! Their happiness increases by" + str(value))
church.wealth -= value
count += 1
pC()
elif playerInput == "2":
ps("How would you like to interact with the King?")
gSpace()
gtextBox()
print("1. Treat to activity")
print("2. Ask for resource")
print("3. Give money")
gtextBox()
options = ["1", "2", "3"]
playerInput = pI(options)
if playerInput == "1":
ps("You treat the King to " + str(random.choice(activity)))
value = random.randint(1, 5)*0.001
king.happiness += value
calculateStats(factions)
ps("The King's happiness is now " + str(king.happiness))
count += 1
pC()
elif playerInput == "2":
randomresource = random.choice(resource)
value = randint(1, 20)
ps("you ask the King for " + str(randomresource) + "!")
if king.happiness > 0.70:
ps("The king is in good spirits! He grants you " + str(value) + " " + str(randomresource))
ps("You now have " + str(church.randomresource) + " " + str(randomresource))
king.randomresource -= value
church.randomresource += value
count += 1
pC()
else:
ps("The king does not seem interested in loaning you any " + str(randomresource))
count += 1
pC()
elif playerInput == "3":
value = random.uniform(0.001, church.wealth/3)
church.wealth -= value
king.wealth += value
calculateStats(factions)
count += 1
ps("You give the King " + str(value) + " gold! Their happiness increases by " + str(value*0.001) + " and their happiness is now " + str(king.happiness))
pC()
elif playerInput == "3":
ps("How would you like to interact with the peasants?")
gSpace()
gtextBox()
print("1. Treat noteable figures to activity")
print("2. Ask for resource")
print("3. Give money")
gtextBox()
options = ["1", "2", "3"]
playerInput = pI(options)
if playerInput == "1":
ps("You treat" + random.choice(peasantnames) + " to " + str(random.choice(activity)))
value = random.randint(1, 5)*0.001
peasants.happiness += value
calculateStats(factions)
ps("The peasants happiness is now " + str(peasant.happiness))
count += 1
pC()
elif playerInput == "2":
randomresource = random.choice(resource)
value = random.randint(1, 20)
ps("you humbly ask the peasants for" + str(randomresource) + "!")
if peasants.happiness > 0.60 and peasants.randomresource > value:
ps("The peasants fill your order! They grant you " + str(value) + " " + str(randomresource))
peasant.randomresource -= value
church.randomresource += value
count += 1
pC()
else:
ps("The peasants do not seem interested in taking any orders for " + str(randomresource))
count += 1
pC()
elif playerInput == "3":
value = random.uniform(0.001, church.wealth/3)
ps("You donate to the peasants " + str(value) + " gold! Their happiness increases by " + str(value/100))
church.wealth -= value
peasant.wealth += value
count += 1
pC()
if playerInput == "1" and count == 1:
print("You have already interacted with a faction this week!")
gameMenu()
elif playerInput == "2":
weekEnd()
count = 0
def start0():
gChurch()
time.sleep(3)
gameMenu()
#Player Data--------------------------------------------------------#
pName = "If this name is printed something is wrong"
#Start Location-----------------------------------------------------#
calculateStats(factions)
#titleScreen()
#start0()
gameMenu()
#gameOver()
我对此有零偏见,因此,任何见识都将受到赞赏。谢谢!
第三段:
def gtitleScreen():
clear()
gLine()
draw("""
___________.__ __ __ __ .__ _____ __________ .__
\__ ___/| |__ ____ / \ / \___________ _/ |_| |__ _____/ ____\ \____ /____ | | ____ ____
| | | | \_/ __ \ \ \/\/ |_ __ \__ \\ __\ | \ / _ \ __\ / /\__ \ | | / ___\ / _ \
| | | Y \ ___/ \ / | | \// __ \| | | Y \ ( <_> ) | / /_ / __ \| |_/ /_/ > <_> )
|____| |___| /\___ > \__/\ / |__| (____ /__| |___| / \____/|__| /_______ (____ /____|___ / \____/
\/ \/ \/ \/ \/ \/ \/ /_____/
""")
gLine()
答案 0 :(得分:3)
某些\
被转义,使用\\
正确显示单个\
。
请尝试以下代码:
def gChurch():
clear()
gLine()
draw(""" +
|
,|,
|||
/ | \\
| | |
| | |
/ | \\
| | |
| | \\
/ \ |
| | |
| | \\
/ | |
8 | | |
''8'' / | \\
8 / \ ,\\
,d8888888888888|========|='' |
,d" "88888888888| ,aa, | a |
,d" "888888888| 8 8 | 8 |
,d8888888b, "8888888| 8aa8 | 8,|
,d" "8888888b, "88888|========|='''|
,d" "8888888b, "888| a a | a |
,d" ,aa, "8888888b, "8| 8 8 | 8,|
/| d' 'b |''''''| |========|='''|
| 8 8 | | | ,aa, | a |
| 8aaaa8 | | | 8 8 | 8 |
| | | | "''" | ,,=|
|aaaaaaaaaaaaaa|======''''''''''''''''''''' """)