- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在用 python 和 pygame 制作一个版本的“Conway's game of life”,但我无法找到相邻的单元格。下面是大部分代码。我删除了不必要的部分,因为问题出在 cell.get_neighbors() 函数上。想看剩下的可以看下here .
def main():
while True:
draw_board()
update_cells()
pygame.display.update()
fpsClock.tick(FPS)
def update_cells():
event_handling()
if clicked == True:
cell = where_clicked(mousex, mousey)
if cell != None:
print (cell.get_neighbors())
class cell:
#The problem is with this function
def get_neighbors(self):
neighbors = []
for y in (-1, 0, 1):
for x in (-1, 0, 1):
neighbor = find_cell(x + self.x, y + self.y)
if neighbor not in (None, self):
neighbors.append(neighbor)
#I added neighbor.switch() so that I could easily see which cells were being returned by the get_neighbors() function.
neighbor.switch()
return neighbors
def switch(self):
if self.alive == False:
self.alive = True
else:
self.alive = False
def find_cell(x, y):
for CELL in board:
if CELL.x == x:
if CELL.y == y:
return CELL
if __name__ == '__main__':
main()
上面写的 get_neighbors() 函数应该将所有 8 个相邻的单元格变成黑色,但它会将 4-10 个看似随机的单元格变成黑色。我做了 this屏幕录像来演示问题。如您所见,如果我单击左上角或顶部,它会正常工作,但当我单击中间时,它会执行如下操作:
http://www.imagesup.net/?di=8140372670715
或者这个:
http://www.imagesup.net/?di=1514037265628
或者这个:
http://www.imagesup.net/?di=1014037266308
我错过了什么吗?因为 cell.get_neighbors() 应该可以正常工作。 find_cell() 工作正常,并且
for y in (-1, 0, 1):
for x in (-1, 0, 1):
...
find_cell(x, y)
应该像这样找到每个相邻的单元格。
(-1, -1), (-1, 0), (-1, 1)
( 0, -1), ( 0, 0), ( 0, 1)
( 1, -1), ( 1, 0), ( 1, 1)
和
if neighbor not in (None, self):
应该防止它返回板外的任何单元格,并防止它返回自己。 (因为一个单元格不能是它自己的邻居)。
感谢任何帮助!
最佳答案
在你发布的代码中你有
neighbor = find_cell(x + self.x, y + self.y)
但是,在您的链接代码中有
for y in (-1, 0, 1):
for x in (-1, 0, 1):
x += self.x
y += self.y
请注意在您的图像中,切换后的单元格似乎是向下移动的。这是因为您在内循环中修改了 y
三次,然后才将其重置为 [-1, 0, 1] 值之一。只运行循环并打印出值,您会看到内循环中的 y
发生了不应该发生的变化。
点击顶部时看起来不错,因为递增的 y 值 self.y
为零。另请注意,您单击的窗口越往下,步进如何变得越夸张。
将您的运行代码更改为您发布的代码,它应该可以正常工作。
关于python - 使用 pygame 在 "Game of life"中查找相邻单元格的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24417658/
我试图通过 DexClassLoader 从 jar 文件加载类: public class AndroidClassLoader implements ClassLoader { priva
我试图了解如何为自己的类型编写trait和impl,以处理一些输入数据。我从一个简单的示例开始,在该示例中,我想使用1, 2, 3, 4处理输入的trait Processor。一个实现将跳过第一个元
生命游戏的算法就不多解释了,百度一下介绍随处可见。 因为网上大多数版本都是基于pygame,matlab等外部库实现的,二维数组大多是用numpy,使用起来学习成本比较高,所以闲暇之余写一个不用外
若凌° Provence - wirepuller 阴谋者 。 交换末日- Brave Hold on Welcome to my life 匪我思存 · L⊙ve/
我在一个网站上工作,用户输入几个名字,它在模板上以漂亮的字体呈现,以便稍后打印和跟踪到其他东西(纸、木头、金属等)。 让我们假设模板是 4"x 3"实物大小。是否可以在所有设备上显示 4"x 3"的图
这是一个有点理论性的问题。在编程作业中,约翰康威告诉我们要实现生命游戏。作为一项附加任务,我们被要求修改程序,以便它可以检测最多四代的模式重复。例如,给定这个特定的游戏“种子”,程序应该像这样: -
我目前正在使用JavaScript编写Conways生活游戏,但是遇到了一些我不知道如何解决的错误。想知道是否有人可以帮助我解决这些问题? 当前在Google Chrome浏览器中显示4个错误,具体如
我和四边形邻居一起写了经典游戏“生活”。当我在调试中运行它时,它说: 连续版本:4.2s 并行版本:1.5s 好的,很好。但是如果我在 release 中运行它,它会说: 连续版本:0.46s 并行版
我正在制作一个 Android 应用程序,它将包含两项服务,每 24 小时持续发送一次有关用户手机使用情况的数据。 用户应该执行该应用程序,切换按钮以启用手机使用记录,然后用户应该能够使用他的手机进行
我有一些可用的 Game of Life 代码。它将每个种群保存为位图。这是输出的样子(裁剪): 在清理代码时,我发现如果我注释掉或以其他方式删除第60行: cout #include #incl
难道后面函数返回的指针是不可访问的吗? char *foo(int rc) { switch (rc) { case 1: return("on
简介 细胞自动机(又称元胞自动机),名字虽然很深奥,但是它的行为却是非常美妙的。所有这些怎样实现的呢?我们可以把计算机中的宇宙想象成是一堆方格子构成的封闭空间,尺寸为N的空间就有NN个格子。而每一
Everything you want comes after you stop looking for it. 当你不再寻觅,你想要的也就来了。 I’m counting the days ti
正如主题所述,我很难想象在现实生活中的示例中何时何地使用可恢复异常,以及我们可以通过使用它们获得哪些有效优势。 我能想象的是,一个子系统已连接,比方说通过 RFC 连接到一个 session ,该 s
什么是“双生命”模块? perldelta for Perl 5.14 中有提到. 最佳答案 Dual Life 模块是存在于两个断开连接的源存储库中的模块,通常意味着在 Perl 核心(与 perl
我想自定义站点管理员角色权限,即我需要启用一项功能来创建相应站点下的站点管理用户。我怎样才能做到这一点?我需要用 Hook 来实现它还是需要创建 EXT?或者还有其他更简单的方法来实现这一目标吗?我需
我在 uminokirin.com 有一个用榆树编写的 Conway 生命游戏的基本实现。 . 来源可见here . 该程序允许用户调整环形网格的大小、单击单元格以更改其状态以及随机化世界。它适用于我
我已经研究这段代码很长时间了,但我似乎无法弄清楚。我的代码的顶部部分用于预填充网格。但由于某种原因,我无法让我的形象按照预期移动和成长。请帮助我!! import java.util.Random;
package edu.bsu.cs121.mamurphy; import java.util.*; import java.io.*; import javax.swing
我想要生命之花,就像这个:https://jsfiddle.net/5ak8P var canvas = document.getElementById('canvas'), context
我是一名优秀的程序员,十分优秀!