- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试制作 Langton Ant 算法的图形表示,但我被卡住了。
这是一个 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"
当它启动时, Ant 进入左上角,然后程序停止。我真的不明白为什么它不起作用..关于如何修复它的任何线索?
最佳答案
需要先绘制,再更新坐标。
您的代码首先更新坐标然后绘制(这意味着下一步移动将不取决于您移动到的颜色)。
区别很重要,因为正确完成下一步行动取决于 Ant 着陆的颜色和 Ant 来自的方向;在您的版本中,除了第一步之外,从未使用过棋盘的颜色,因为您正在绘制正方形,然后在其内容上制作 if
(这是您刚刚使用的颜色)。
您需要更改代码
y=y-qg
pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
交换两条线得到
pygame.draw.rect(display,(255,0,0),(x,y,qg,qg))
y=y-qg
在所有地方您都在更新坐标。
另一个错误是在模拟开始时屏幕应该充满白色(255,255,255)
(当前代码使用(0, 0, 0)
代替) .
通过这些更改,模拟就像在视频中一样工作。
请注意,您可以使用一种不同的方法来更好地适应更复杂的情况(超过两种颜色)。这个想法是保留一个directions
数组和一个指向当前direction
的索引;通过这种方式向左或向右转,您只需要增加或减少direction
(模数 4)即可。
import pygame
width, height = 800, 600
display = pygame.display.set_mode((width,height))
pygame.init()
qg = 2
x, y = 400, 300
display.fill((255,255,255))
directions = ((0, -1), (-1, 0), (0, 1), (1, 0))
direction = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# update position
dx, dy = directions[direction]
x += dx * qg
y += dy * qg
try:
a = display.get_at((x, y))
except:
break
if a == (255, 255, 255, 255):
# White square
pygame.draw.rect(display,(255,0,0),(x,y,qg,qg)) # paint red
direction = (direction + 1) % 4 # turn left
else:
# Red square
pygame.draw.rect(display,(255,255,255),(x,y,qg,qg)) # paint white
direction = (direction + 3) % 4 # turn right
pygame.display.update()
print "end"
关于python: langton's ant pygame 第一步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288910/
我正在编写 Langton 的 Ant sim(用于规则字符串 RLR)并试图优化它以提高速度。这是目前的相关代码: #define AREA_X 65536 #define AREA_Y 65536
我正在尝试制作 Langton Ant 算法的图形表示,但我被卡住了。 这是一个 Langton's Ant video description 输出应该是这样的: 我的代码: import pyga
我用 C++(控制台)编写了一个简单的 Langtons Ant。但是(真的不知道为什么)我每次运行我的程序时都会得到一个核心转储: #include #include #include usi
我是一名优秀的程序员,十分优秀!