Python 有没有一种方法可以打印一个字符串并防止它在随机字符串生成器中重印?
我已经制作了一个程序,可以为一个类似地下城爬虫的游戏打印出一行随机的字符,我想知道我是否可以让@symbol打印一次,而且只能打印一次 我已经试着让它检查以前打印的字符串列表,并确保如果已经打印,它不会再次打印,但这似乎不起作用,我对这有点陌生。有什么建议吗Python 有没有一种方法可以打印一个字符串并防止它在随机字符串生成器中重印?,python,Python,我已经制作了一个程序,可以为一个类似地下城爬虫的游戏打印出一行随机的字符,我想知道我是否可以让@symbol打印一次,而且只能打印一次 我已经试着让它检查以前打印的字符串列表,并确保如果已经打印,它不会再次打印,但这似乎不起作用,我对这有点陌生。有什么建议吗 import random import time randomnumber = random.randint(6,16) block = [] printed = [] health = 10 blocks = "\x1b[1;37;48
import random
import time
randomnumber = random.randint(6,16)
block = []
printed = []
health = 10
blocks = "\x1b[1;37;48m#"
x_axis = 1
y_axis = 1
randumplace = random.randint(0, len(block))
multiplier = random.randrange(6,35)
def build(blocks):
for i in range (multiplier):
block.append(blocks)
i = random.randrange(1,75)
if i == 4:
blocks = "\x1b[1;31;48mM"
elif i == 15:
blocks = "\x1b[1;36;48m~"
elif i == 25:
blocks = "\x1b[1;36;48m~"
elif i == 22:
blocks = "\x1b[1;33;48m$"
elif i == 1:
blocks = "\x1b[1;37;48m#"
elif i == 10 and "@" not in printed:
blocks = "@"
else:
blocks = "\x1b[1;37;48m."
fip = build(blocks)
counter = 1
print("# "+"# # # # # #" + " #" * (multiplier - 6))
while counter != randomnumber:
printed.append(block)
del block[:]
build(blocks)
print (*block)
counter += 1
print ("" + "# " * (multiplier +1))
它应该打印出5-15行字符串,如“#….M….$@”
但它会打印多个@符号,如
“#…@M…@…”
我还想确保其他行不打印@符号,这就是“打印”列表的原因。一种更简单的方法是在内存中创建一个矩阵,例如
maze = [[' '] * random_width for i in range(random_height)]
然后,您可以随心所欲地填充迷宫,直到迷宫完成后才打印出来。首先,将您选择的块折叠到一个简单的查找表中:
rand_limit = 75
block = ["\x1b[1;37;48m."] * rand_limit
block[01] = "\x1b[1;37;48m#"
block[04] = "\x1b[1;31;48mM"
block[15] = "\x1b[1;36;48m~"
block[22] = "\x1b[1;33;48m$"
block[25] = "\x1b[1;36;48m~"
现在,您可以将整个迷宫作为嵌套列表进行理解,将块符号替换为随机数:
maze_rows = random.randrange(6, 35)
maze_cols = random.randrange(5, 16)
maze = [ [block[random.randrange(1, rand_limit)]
for col in range(maze_cols)]
for row in range(maze_rows) ]
最后,为您的唯一符号随机选取一行和一列:
maze[random.randrange(maze_rows][random.randrange(maze_cols]] = '@'
如果不想覆盖现有符号,则循环直到找到一个空白点。我刚刚将
printed.append()
函数向上移动到build函数中
试试这个:
import random
import time
randomnumber = random.randint(6,16)
block = []
printed = []
health = 10
blocks = "\x1b[1;37;48m#"
x_axis = 1
y_axis = 1
randumplace = random.randint(0, len(block))
multiplier = random.randrange(6,35)
def build(blocks):
for i in range (multiplier):
block.append(blocks)
i = random.randrange(1,75)
if i == 4:
blocks = "\x1b[1;31;48mM"
elif i == 15:
blocks = "\x1b[1;36;48m~"
elif i == 25:
blocks = "\x1b[1;36;48m~"
elif i == 22:
blocks = "\x1b[1;33;48m$"
elif i == 1:
blocks = "\x1b[1;37;48m#"
elif i == 10 and "@" not in printed:
blocks = "@"
printed.append("@")
else:
blocks = "\x1b[1;37;48m."
fip = build(blocks)
counter = 1
print("# "+"# # # # # #" + " #" * (multiplier - 6))
while counter != randomnumber:
del block[:]
build(blocks)
print (*block)
counter += 1
print ("" + "# " * (multiplier +1))
为什么不创建一个不包含
@
字符的14个字符的随机字符串,然后选择一个随机索引并在所述索引处插入@
字符?或者创建15的随机字符串,在某个随机索引处用@
替换一些字符。我已经尝试过了,当它打印@What isfip
时,它会出错并在下一行打印所有内容,为什么要将build(blocks)
分配给fip
,但build()
函数不返回值,变量fip
再也不用了?fip是我留下的一个意外,因为我在使用它,但我不记得是什么原因了。我会尝试一下,这可能是一种更简单的方法。再一次,我对学习python还不熟悉,所以我正在尝试找到不同的方法来缩短buffah,这确实有效。谢谢,我现在明白了,但是有没有一种方法可以为特定的角色添加一个位置,比如一个xaxis和一个yaxis。你的意思是什么,你能举个例子吗@UnwantedTelemarketing如果我要做这一步,我需要它来产生,然后在某个地方进行特定的输入移动。如果我要替换某个块,我需要让它有一个特定的变量或其他东西,这样我就可以用@替换一个点。TL;博士,我需要让它动起来,我有一些想法