我正在使用Python中的graphics.py创建一个非常简单的Langton's Ant程序。我的问题是蚂蚁需要超过2分30秒才能“高速公路”。如果可能的话,我希望这能减少到几秒钟
我已经尝试过“优化”我的代码(从约90行减少到约45行,主要是压缩一堆if语句)。这缩短了大约10秒,所以我猜问题在于实际的渲染。我真的很想看到蚂蚁在屏幕上追踪它的路径,但我担心问题是把每个像素都画成黑色或白色
以下是我当前代码的主要部分:
#Import graphics.py and math, initialize variables
grid = [[0 for x in range(xSize)] for y in range(ySize)] # 0 is white, 1 is black
while key != 'x':
key = win.checkKey()
if grid[antX][antY] == 0: #Light square
if antDir < 4: #Turn ant right
antDir += 1
else:
antDir = 1
grid[antX][antY] = 1
win.plotPixel(antX, antY, "black")
elif grid[antX][antY] == 1: #Dark square
if antDir > 1: #Turn ant left
antDir -= 1
else:
antDir = 4
grid[antX][antY] = 0
win.plotPixel(antX, antY, "white")
if antDir == 1 and antY != 0:
antY -= 1
elif antDir == 2 and antX != xSize-1:
antX += 1
elif antDir == 3 and antY != ySize-1:
antY += 1
elif antDir == 4 and antX != 0:
antX -= 1
else:
break #Break if touching wall
key = win.getKey()
你们觉得怎么样?我应该使用不同的图书馆吗?我的代码里有什么东西在浪费时间吗?或者我可以不在合理的时间段内反复在屏幕上画画吗
我使用的是Acer Aspire E5-576G,带有i5处理器和8 Gig RAM
目前没有回答
相关问题 更多 >
编程相关推荐