简体   繁体   English

我怎么能永远重复这个?

[英]How can I make this repeat forever?

I have this terrain generator that is, in my opinion pretty efficient. 我有这个地形发生器,在我看来非常有效。 I just can't get it to print forever. 我无法让它永远打印出来。 Here's the current code I have for it: 这是我现有的代码:

import random
print(' '.join([random.choice(['#','o','~','*']) for i in range(10000)]))

I tried to do this, but I got a SyntaxError . 我试图这样做,但我得到了一个SyntaxError

import random
print(' '.join([random.choice(['#','o','~','*']) while True]))

How can I get this to repeat forever? 我怎么能让它永远重复? I also want a 0.05 second delay in between the printing of each character. 我还想在每个角色的打印之间延迟0.05秒。 If you can keep this at a max of two lines, that's cool. 如果你能保持最多两行,这很酷。 If you can't, that's okay. 如果你不能,那没关系。 Thanks! 谢谢! Note: This is not about gamedev, I just happened to be using .join for a terrain generator. 注意:不是关于gamedev,我恰好使用.join作为地形生成器。

Well, if you want a delay between the characters, you can't use join() because that will give you a single string instead of a "character generator". 好吧,如果你想在字符之间延迟,你就不能使用join()因为这会给你一个字符串而不是“字符生成器”。

And if you plan on using an "infinite" generator, you can't use join() either for that same reason. 如果您计划使用“无限”生成器,则出于同样的原因,您不能使用join()

How about this: 这个怎么样:

import random
import sys
import time
while True:
    print(random.choice("#o~*"), end="", flush=True) # Python 3.3 and up
    time.sleep(0.05)

You can't have a loop inside your print function, you must put your print function inside a loop, so for example: 你不能在你的打印功能中有一个循环,你必须把你的打印功能放在一个循环中,所以例如:

while True:
    print random.choice(['#','o','~','*']),

You can try this simplified version of you code: 您可以尝试这个代码的简化版本:

import random, sys, time
while True: sys.stdout.write(random.choice('#o~*') + ' '); time.sleep(0.05)

Here's a new approach. 这是一种新方法。

Roll your own 滚动你自己

import random
import time

class GenerateRandomChar(object):
    def __init__(self, charlist):
        self.charlist = charlist
    def __next__(self):
        return random.choice(self.charlist)

terrain_tiles = GenerateRandomChar("#o~*")
while True:
    print(next(terrain_tiles), end='')
    time.sleep(0.05)

Also if you're building a map, you may want to think about something like: 此外,如果您正在构建地图,您可能需要考虑以下内容:

class Map(list):
    def __init__(self, terrain_tiles, WIDTH=12, HEIGHT=12):
        self.WIDTH = WIDTH
        self.HEIGHT = HEIGHT
        self.terrain_tiles = GenerateRandomChar(terrain_tiles)
        for y in range(self.HEIGHT):
            self.append([next(self.terrain_tiles) for x in range(self.WIDTH)])
    def __str__(self):
        return "\n".join([" ".join([tile for tile in row]) for row in self])

>>> map_ = Map("~o#*")
>>> print(map_)
o o o o o o ~ ~ # ~ ~ ~
# # # o o # o # o # * #
~ o # * ~ # o # * ~ # ~
o * o o o * # o # o # *
# * o # # # * o ~ * # #
# ~ o ~ * # # ~ o o ~ ~
~ * # # o ~ o * # # ~ o
# o * o o * o * ~ ~ o *
# # * * ~ ~ ~ * # * # o
o o ~ ~ # * ~ ~ * ~ * o
~ * ~ ~ * # * * ~ # o #
* o ~ # ~ # ~ ~ o o o o

Infinite iterator with random symbols: 带随机符号的无限迭代器:

import itertools
it = (random.choice(['#','o','~','*']) for i in itertools.repeat(""))

As some people have commented, join() won't help you because it generates the whole string before returning. 正如一些人评论的那样, join()不会帮助你,因为它会在返回之前生成整个字符串。 You can try (Python 3): 你可以尝试(Python 3):

[print(char, end="") for char in it] # not such a good idea

or 要么

for char in it: print(char, end="")

You can use generators: 你可以使用发电机:

from itertools import repeat
import random
from time import sleep
import sys

gen = (random.choice(['#','o','~','*']) for _ in repeat(None))
for x in gen:
    print x,
    sys.stdout.flush()
    sleep(0.05)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM