【发布时间】:2018-02-08 15:09:09
【问题描述】:
我正在使用 Python 开发基于图块的游戏。出于某种原因,如果指定的瓷砖不是左上角的瓷砖或与其相邻,我的函数会找到指定瓷砖和最近的水瓷砖之间的距离(以瓷砖空间为单位),它会继续返回 0。如果指定的瓷砖本身是水瓷砖,该函数通常应该只返回 0。我测试了左上角的瓷砖,即使它是水,在这种情况下它仍然返回 0,就像它应该的那样,当然,它可以与周围瓷砖的任何其他编辑一起正常工作。然而,就像我说的那样,任何与左上角瓷砖不相邻的瓷砖无论在什么情况下都不会起作用,它总是返回 0。
如果我测试非水瓷砖,0 不应该出现的原因是该函数将正在测试的瓷砖相对于所有水瓷砖的距离附加到一个列表中,然后找到该列表的最小值并返回它。由于非水瓷砖不会出现在水瓷砖列表中,因此绝不应该测量指定瓷砖与其自身之间的距离,这将导致 0。我做了一些自我测试并确认水瓦列表确实只包含水瓦的坐标,指定的瓦当然是在函数中指定的,所以不会有错误。
下面是我的代码。它最初是用 codeskulptor 编写的,并使用了 random 模块和 simplegui 模块,但是由于这个问题不涉及其中任何一个,所以我将它们剥离了。我还删除了所有与这个问题无关的代码,所以不要担心试图指出我在尝试做我所做的事情时可能遇到的其他潜在问题。我也为糟糕的命名约定深表歉意。我修改了代码以在 Pycharm 中工作,以便更多人能够诊断它。非常感谢。
编辑:我注意到我说的好像这段代码产生了物理空间,而这个版本实际上没有。我给你一些参考点:
-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))
【问题讨论】:
-
您确实需要缩小范围。有很多代码需要我们仔细阅读。你在说什么功能?第二个距离一?
-
根据答案,这正是调试器很容易发现的问题。看看this tutorial 和this talk。
标签: python list function class min