我正在用Python开发一个基于平铺的游戏。出于某种原因,如果指定的互动程序不是左上角的互动程序或与其相邻,则我的函数(在互动程序空间中)查找与指定互动程序和最近的water互动程序之间的距离,并始终返回0。如果指定的磁贴本身是水磁贴,则该函数通常只应返回0。我测试了左上角的瓷砖,即使它是水,在这种情况下它仍然返回0,就像它应该返回的一样,当然可以与对其周围瓷砖的任何其他编辑一起正常工作。然而,正如我所说,任何被测试的、不与左上角的图块相邻的图块,无论在什么情况下都不会工作,它总是返回0。
如果我测试一个非水瓷砖,那么0不应该出现的原因是,该函数将被测试瓷砖相对于所有水瓷砖的距离附加到一个列表中,然后找到该列表的最小值并返回它。由于非水磁贴不在水磁贴列表中,因此绝对不应该存在is测量指定磁贴与自身之间的距离的情况,这将导致0。我做了一些自我测试,并确认水砖列表确实只包含水砖的坐标,指定的瓷砖当然是在函数中指定的,所以那里不会有错误。
下面是我的代码。它最初是用codeskulptor编写的,使用了random模块和simplegui模块,但由于这个问题不涉及这两个模块,我把它们去掉了。我还删掉了所有与这个问题无关的代码,所以不要担心指出我在做我所做的事情时可能遇到的其他潜在问题。我也对这些糟糕的命名惯例深表歉意。我将代码修改为在Pycharm中工作,以便更多的人能够对其进行诊断。非常感谢。
编辑:我注意到,我说的好像这段代码产生了物理空间,而实际上在这个版本中没有。我会给你一些参考点:
-瓷砖[80]将位于瓷砖[0]的正下方,因为每行有80个瓷砖
此外,所讨论的函数是顶部附近的“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))