【发布时间】:2015-07-29 02:44:09
【问题描述】:
我正在尝试绘制 Langton 蚂蚁算法的图形表示,但我卡住了。
这是Langton's Ant video description
这是输出的样子:
我的代码:
import pygame
width = 800
height = 600
display = display=pygame.display.set_mode((width,height))
pygame.init()
direction = "up"
pixel_width_height = 2
qg = pixel_width_height
x=400
y=300
display.fill((0,0,0))
pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
try:
a = display.get_at((x, y))
except:
break
if a == (255, 255, 255, 255) or a == (230, 230, 230, 255):
if direction == "right":
direction = "up"
y=y-qg
pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
elif direction == "up":
direction = "left"
x = x-qg
pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
elif direction == "left":
direction = "down"
y=y+qg
pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
elif direction == "down":
direction = "right"
x = x+qg
pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
elif a == (255,0,0,255):
if direction == "right":
direction = "down"
y=y+qg
pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
elif direction == "up":
direction = "right"
x = x+qg
pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
elif direction == "left":
direction = "up"
y = y-qg
pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
elif direction == "down":
direction = "left"
x=x-qg
pygame.draw.rect(display,(255,255,255),(x,y,qg,qg))
pygame.display.update()
print "end"
当它启动时,蚂蚁进入左上角,然后程序停止。 我真的不明白为什么它不起作用..关于如何修复它的任何线索?
【问题讨论】:
标签: python algorithm python-2.7 pygame