列表中出现意外的0,尽管代码似乎阻止将0添加到lis

2024-04-24 03:39:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python开发一个基于tile的游戏。出于某种原因,我的函数在查找指定的磁贴和最近的水磁贴之间的距离(以磁贴空间为单位)时,如果指定的磁贴不是左上角的磁贴或与其相邻的磁贴,则会一直返回0。如果指定的磁贴本身是水磁贴,则函数通常只应返回0。我测试了左上角的瓦片,即使它是水,在这种情况下仍然返回0,就像它应该返回的那样,当然,它可以正确地与周围的瓦片的任何其他编辑一起工作。但是,正如我所说的,任何被测试的图块如果不与左上角的图块相邻,无论在什么情况下都不会工作,它总是返回0。你知道吗

如果我测试一个非水平铺,0不应该出现的原因是,函数将被测试的平铺相对于所有水平铺的距离附加到一个列表中,然后找到该列表的最小值并返回它。由于非水平铺不在水平铺列表中,因此绝对不应存在这样的情况,即is测量指定平铺与自身之间的距离,从而导致0。我做了一些自我测试,确认了water tiles列表确实只包含water tiles的坐标,而且指定的tile当然是在函数中指定的,所以那里不会有错误。你知道吗

下面是我的代码。它最初是用codeskulptor编写的,使用了random模块和simplegui模块,但是由于这个问题不涉及其中任何一个,所以我把它们去掉了。我还删掉了所有与这个问题无关的代码,所以不要担心指出我在做我所做的事情时可能遇到的其他潜在问题。我也为可怕的命名惯例深表歉意。我修改了代码以在Pycharm中工作,以便更多的人能够诊断它。非常感谢。你知道吗

编辑:我注意到我说的好像这个代码产生了物理空间,而这个版本实际上没有。我会给你一些参考点:

-瓷砖[0]将位于左上角

-瓷砖[1]将位于瓷砖[0]的右侧

-tiles[80]将位于tiles[0]的正下方,因为每行是80个tiles

此外,所讨论的函数是靠近顶部的“距水的距离”。你知道吗

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))

Tags: selffalsetruesizelocationelementcolordistance
2条回答

不幸的是,距离不是你唯一的问题。一个非常相似的错误是

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"

你的distance()函数从根本上被破坏了——如果X差是Y差的负数,那么对于任意相距很远的图块,它可以返回一个零距离。表达式应为:

abs(tile2.x - tile1.x) + abs(tile2.y - tile1.y)

而不是:

abs((tile2.x - tile1.x) + (tile2.y - tile1.y))

相关问题 更多 >