![](/img/trans.png)
[英]Is there a way to shorten my code so that I don't have to write “turtle.” every time I use functions from the turtle module?
[英]Is there a way to shorten the following code so I don't have to write it out manually
这是代码本身很奇怪,但这主要是因为对于这个项目,我必须使用一个奇怪的特定 IDE。 但是,它使用相同的 function,只是经常键入略有不同。 无论如何,我目前已经将它设置在创建块的位置,它将使块 position 等于在它之前制作的块的 position。 我将块的 position 存储在一个列表中,并用它来重写 So like snoop the game。 那么有什么方法可以缩短代码,这样我就不必手动编写每条语句了?
for block in snakeBody.children:
#changes position of the first block in snakeBody to be equal to the snake head position that is stored in a list 'snakeList'
snakeBody.children[0].left = snakeList[0]
snakeBody.children[0].top = snakeList[1]
#Makes it so these lines dont run untill the block is actually created preventing an error
if app.snakeCount >= 2:
snakeBody.children[1].left = snakeList[2]
snakeBody.children[1].top = snakeList[3]
if app.snakeCount >= 3:
snakeBody.children[2].left = snakeList[4]
snakeBody.children[2].top = snakeList[5]
每当您发现自己编写相同的代码并增加索引时,它通常应该是一个循环。
for block in snakeBody.children:
for i in range(app.snakeCount):
snakeBody.children[i].left = snakeList[i*2]
snakeBody.children[i].top = snakeList[i*2+1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.