尽管代码似乎阻止将零添加到列表中,但意外的 0 出现在列表中
Unexpected 0 appearing in a list despite the code seemingly preventing a zero from being added to the list
我正在 Python 开发一款基于图块的游戏。出于某种原因,如果指定的图块不是左上角的图块或毗邻它。如果指定的瓷砖本身是水瓷砖,该功能通常应该只 return 0。我测试了左上角的瓷砖,即使它是水,在这种情况下它仍然是 returns 0,就像它应该的那样,当然它可以与周围瓷砖的任何其他编辑一起正常工作。然而,就像我说的那样,任何不与左上图相邻的被测试的图块无论在什么情况下都不会工作,它总是 returns 0.
如果我测试一个非水瓷砖,0 永远不会出现的原因是该函数将被测试的瓷砖相对于所有水瓷砖的距离附加到一个列表中,然后找到该列表的最小值return就这样了。由于非水瓷砖不会出现在水瓷砖列表中,因此永远不会出现 is 测量指定瓷砖与其自身之间的距离而导致 0 的情况。我做了一些自我测试并确认water tiles list确实只包含water tiles的坐标,指定的tile当然是在函数中指定的,所以不会有错误。
下面是我的代码。它最初是用 codeskulptor 编写的,并使用了 random 模块和 simplegui 模块,但是由于这个问题不涉及其中任何一个,所以我将它们剥离了。我还删除了所有与这个问题无关的代码,所以不要担心试图指出我可能 运行 尝试做我所做的其他潜在问题。我也为糟糕的命名约定深表歉意。我调整了代码以在 Pycharm 中工作,以便更多人能够诊断它。非常感谢。
编辑:我注意到我说的好像是此代码生成的物理 space,而实际上此版本中没有。我给你一些参考点:
-tiles[0] 会在左上角
-tiles[1] 会在 tiles[0]
的右边
-tiles[80] 会在 tiles[0] 下面,因为每行有 80 个 tiles
此外,有问题的函数 "distance_from_water" 靠近顶部。
def distance(tile1, tile2):
# returns the distance of two tiles
return abs((tile2.x - tile1.x) + (tile2.y - tile1.y))
def distance_from_water(tile, index):
# cycles through every water tile and adds the distances
# of them relative to a specified tile to a list, then returns
# the lowest distance
water_tiles = []
water_tile_proximities = []
for element in range(0, len(index)):
if index[element].iswater == True:
water_tiles.append(element)
for element in water_tiles:
water_tile_proximities.append(distance(index[tile], index[element]))
lowest_distance = min(water_tile_proximities)
return lowest_distance
canvaswidth = 1280
canvasheight = 800
tile_size = 16
tile_slots = int((canvaswidth / tile_size) * (canvasheight / tile_size))
tile_slot_locations = [[(tile_size / 2), (tile_size / 2)]]
for element in range(0, (tile_slots - 1)):
# finds how many discrete locations for tiles there are based on the canvas size
if tile_slot_locations[element][0] > (canvaswidth - (tile_size / 2)):
tile_slot_locations[element][0] = (tile_size / 2)
tile_slot_locations[element][1] += tile_size
tile_slot_locations.append([((tile_slot_locations[element][0]) + tile_size), tile_slot_locations[element][1]])
tiles = []
colors = ["blue", "green", "darkgreen", "grey", "khaki", "brown"]
class Tile(object):
list_of_all_tiles = []
def __init__(self, location, color):
self.location = location
self.color = color
self.dimensions = ((location[0] + (tile_size / 2), location[1] + (tile_size / 2)),
(location[0] + (tile_size / 2), location[1] - (tile_size / 2)),
(location[0] - (tile_size / 2), location[1] - (tile_size / 2)),
(location[0] - (tile_size / 2), location[1] + (tile_size / 2)),
(location[0] + (tile_size / 2), location[1] + (tile_size / 2)))
self.x = (location[0] - (tile_size / 2)) / tile_size
self.y = (location[1] - (tile_size / 2)) / tile_size
Tile.list_of_all_tiles.append(self)
# determine the type
if color == "blue":
self.iswater = True
self.island = False
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = False
if color == "green":
self.iswater = False
self.island = True
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = False
if color == "darkgreen":
self.iswater = False
self.island = False
self.isforest = True
self.ismountain = False
self.issand = False
self.isinn = False
if color == "grey":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = True
self.issand = False
self.isinn = False
if color == "khaki":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = False
self.issand = True
self.isinn = False
if color == "brown":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = True
for element in range(0, len(tile_slot_locations)):
# cycles through and assigns the Tile class
# using every tile slot location and saves in "tiles" list
tile = Tile(tile_slot_locations[element], colors[0])
tiles.append(tile)
tiles[120].island = True
tiles[120].iswater = False
tiles[1].island = True
tiles[1].iswater = False
tiles[80].island = True
tiles[80].iswater = False
tiles[81].island = True
tiles[81].iswater = False
tiles[3].island = True
tiles[3].iswater = False
print(distance_from_water(3, tiles))
您的 distance()
函数从根本上被破坏了 - 如果它们的 X 差是它们的 Y 差的负数,它完全有能力为任意远距离的图块返回零距离。表达式应为:
abs(tile2.x - tile1.x) + abs(tile2.y - tile1.y)
而不是:
abs((tile2.x - tile1.x) + (tile2.y - tile1.y))
不幸的是,距离不是您唯一的问题。一个非常相似的错误是
tile_slots = int((canvaswidth / tile_size) * (canvasheight / tile_size))
您最可能想要的地方
tile_slots = int(canvaswidth / tile_size) * int(canvasheight / tile_size)
你在python3中写
tile_slots = (canvaswidth // tile_size) * (canvasheight // tile_size)
更多提示
虽然我认为您的图块表示不理想,但您可以大幅缩短确定类型块
# determine the type
self.iswater = color == "blue"
self.island = color == "green"
self.isforest = color == "darkgreen"
self.ismountain = color == "grey"
self.issand = color == "khaki"
self.isinn = color == "brown"
我正在 Python 开发一款基于图块的游戏。出于某种原因,如果指定的图块不是左上角的图块或毗邻它。如果指定的瓷砖本身是水瓷砖,该功能通常应该只 return 0。我测试了左上角的瓷砖,即使它是水,在这种情况下它仍然是 returns 0,就像它应该的那样,当然它可以与周围瓷砖的任何其他编辑一起正常工作。然而,就像我说的那样,任何不与左上图相邻的被测试的图块无论在什么情况下都不会工作,它总是 returns 0.
如果我测试一个非水瓷砖,0 永远不会出现的原因是该函数将被测试的瓷砖相对于所有水瓷砖的距离附加到一个列表中,然后找到该列表的最小值return就这样了。由于非水瓷砖不会出现在水瓷砖列表中,因此永远不会出现 is 测量指定瓷砖与其自身之间的距离而导致 0 的情况。我做了一些自我测试并确认water tiles list确实只包含water tiles的坐标,指定的tile当然是在函数中指定的,所以不会有错误。
下面是我的代码。它最初是用 codeskulptor 编写的,并使用了 random 模块和 simplegui 模块,但是由于这个问题不涉及其中任何一个,所以我将它们剥离了。我还删除了所有与这个问题无关的代码,所以不要担心试图指出我可能 运行 尝试做我所做的其他潜在问题。我也为糟糕的命名约定深表歉意。我调整了代码以在 Pycharm 中工作,以便更多人能够诊断它。非常感谢。
编辑:我注意到我说的好像是此代码生成的物理 space,而实际上此版本中没有。我给你一些参考点:
-tiles[0] 会在左上角
-tiles[1] 会在 tiles[0]
的右边-tiles[80] 会在 tiles[0] 下面,因为每行有 80 个 tiles
此外,有问题的函数 "distance_from_water" 靠近顶部。
def distance(tile1, tile2):
# returns the distance of two tiles
return abs((tile2.x - tile1.x) + (tile2.y - tile1.y))
def distance_from_water(tile, index):
# cycles through every water tile and adds the distances
# of them relative to a specified tile to a list, then returns
# the lowest distance
water_tiles = []
water_tile_proximities = []
for element in range(0, len(index)):
if index[element].iswater == True:
water_tiles.append(element)
for element in water_tiles:
water_tile_proximities.append(distance(index[tile], index[element]))
lowest_distance = min(water_tile_proximities)
return lowest_distance
canvaswidth = 1280
canvasheight = 800
tile_size = 16
tile_slots = int((canvaswidth / tile_size) * (canvasheight / tile_size))
tile_slot_locations = [[(tile_size / 2), (tile_size / 2)]]
for element in range(0, (tile_slots - 1)):
# finds how many discrete locations for tiles there are based on the canvas size
if tile_slot_locations[element][0] > (canvaswidth - (tile_size / 2)):
tile_slot_locations[element][0] = (tile_size / 2)
tile_slot_locations[element][1] += tile_size
tile_slot_locations.append([((tile_slot_locations[element][0]) + tile_size), tile_slot_locations[element][1]])
tiles = []
colors = ["blue", "green", "darkgreen", "grey", "khaki", "brown"]
class Tile(object):
list_of_all_tiles = []
def __init__(self, location, color):
self.location = location
self.color = color
self.dimensions = ((location[0] + (tile_size / 2), location[1] + (tile_size / 2)),
(location[0] + (tile_size / 2), location[1] - (tile_size / 2)),
(location[0] - (tile_size / 2), location[1] - (tile_size / 2)),
(location[0] - (tile_size / 2), location[1] + (tile_size / 2)),
(location[0] + (tile_size / 2), location[1] + (tile_size / 2)))
self.x = (location[0] - (tile_size / 2)) / tile_size
self.y = (location[1] - (tile_size / 2)) / tile_size
Tile.list_of_all_tiles.append(self)
# determine the type
if color == "blue":
self.iswater = True
self.island = False
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = False
if color == "green":
self.iswater = False
self.island = True
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = False
if color == "darkgreen":
self.iswater = False
self.island = False
self.isforest = True
self.ismountain = False
self.issand = False
self.isinn = False
if color == "grey":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = True
self.issand = False
self.isinn = False
if color == "khaki":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = False
self.issand = True
self.isinn = False
if color == "brown":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = True
for element in range(0, len(tile_slot_locations)):
# cycles through and assigns the Tile class
# using every tile slot location and saves in "tiles" list
tile = Tile(tile_slot_locations[element], colors[0])
tiles.append(tile)
tiles[120].island = True
tiles[120].iswater = False
tiles[1].island = True
tiles[1].iswater = False
tiles[80].island = True
tiles[80].iswater = False
tiles[81].island = True
tiles[81].iswater = False
tiles[3].island = True
tiles[3].iswater = False
print(distance_from_water(3, tiles))
您的 distance()
函数从根本上被破坏了 - 如果它们的 X 差是它们的 Y 差的负数,它完全有能力为任意远距离的图块返回零距离。表达式应为:
abs(tile2.x - tile1.x) + abs(tile2.y - tile1.y)
而不是:
abs((tile2.x - tile1.x) + (tile2.y - tile1.y))
不幸的是,距离不是您唯一的问题。一个非常相似的错误是
tile_slots = int((canvaswidth / tile_size) * (canvasheight / tile_size))
您最可能想要的地方
tile_slots = int(canvaswidth / tile_size) * int(canvasheight / tile_size)
你在python3中写
tile_slots = (canvaswidth // tile_size) * (canvasheight // tile_size)
更多提示
虽然我认为您的图块表示不理想,但您可以大幅缩短确定类型块
# determine the type
self.iswater = color == "blue"
self.island = color == "green"
self.isforest = color == "darkgreen"
self.ismountain = color == "grey"
self.issand = color == "khaki"
self.isinn = color == "brown"