Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 网格中正方形的对角线_Python_Python 3.x_Grid_Pygame_Grid Layout - Fatal编程技术网

Python 网格中正方形的对角线

Python 网格中正方形的对角线,python,python-3.x,grid,pygame,grid-layout,Python,Python 3.x,Grid,Pygame,Grid Layout,我有一个python网格类,我试图在其中创建一个方法来获取某个tile所属的tile的对角线。我已经成功地做到了这一点,在对角线上从左到右向下,我想知道如何更改它,以便我可以通过更改“方向”参数从右到左。以下是我的方法: def getDiagonal(self, tile, direction = 1): index = self.index(tile) diagonal = [] currentIndex = [i - index[0

我有一个python网格类,我试图在其中创建一个方法来获取某个tile所属的tile的对角线。我已经成功地做到了这一点,在对角线上从左到右向下,我想知道如何更改它,以便我可以通过更改“方向”参数从右到左。以下是我的方法:

    def getDiagonal(self, tile, direction = 1):
        index = self.index(tile)
        diagonal = []

        currentIndex = [i - index[0] for i in index]

        while currentIndex[1] != self.y:
            diagonal.append(self[currentIndex[0]][currentIndex[1]])

            currentIndex = [i + 1 for i in currentIndex]

        return diagonal
下面是包含Grid类的整个模块:

# Grid library for Pygame by Bobby Clarke
# GNU General Public License 3.0

# Version 1.1

import pygame
import math
from fractions import gcd
from functools import reduce

def _isEven(i):
    return i % 2 == 0

def product(_list):
    return reduce(lambda x, y: x * y, _list, 1)

def _range(start, stop, step=1):
    """Range function which can handle floats."""
    while start < stop:
        yield start
        start += step

def _simplify(a, b):
    hcf = gcd(a, b)
    return (a / hcf, b / hcf)

class Tile(pygame.Rect):
    def __init__(self, point, size, colour = None, imgs = [], tags = []):
        self.size = [int(i) for i in size]
        self.point = point
        self.colour = colour

        for img in imgs:
            if isinstance(img, tuple):
                imgs[imgs.index(img)] = pygame.image.fromstring(img[0],
                                                                img[1],
                                                                img[2])
        self.imgs = imgs[:]
        self.tags = tags[:]

        pygame.Rect.__init__(self, self.point, self.size)

    def __lt__(self, other):
        return (self.point[0] < other.point[0] or
                self.point[1] < other.point[1])
    def __gt__(self, other):
        return (self.point[0] > other.point[0] or
                self.point[1] > other.point[1])
    def __le__(self, other):
        return (self.point[0] <= other.point[0] or
                self.point[1] <= other.point[1])
    def __ge__(self, other):
        return (self.point[0] >= other.point[0] or
                self.point[1] >= other.point[1])

    def toData(self, imgFormat = "RGBA"):
        return (self.point, self.size, self.colour,
               [(pygame.image.tostring(img, imgFormat),
                 img.get_size(), imgFormat) for img in self.imgs], self.tags)

    def fromData(data, baseTile = None):
        tile = Tile(*data)

        if baseTile and isinstance(baseTile, Tile):
            baseTile = tile
        else:
            return tile

    def getRect(self):
        return self
    def getColour(self):
        return self.colour
    def setColour(self, colour):
        self.colour = colour
    def getPoint(self):
        return self.point
    def addTag(self, *tags):
        if isinstance(tags[0], list):
            self.tags.extend(tags[0])
        else:
            self.tags.extend(tags)
    def hasTag(self, tag):
        return (tag in self.tags)
    def delTag(self, tag):
        self.tags.remove(tag)
    def clearTags(self):
        self.tags = []
    def addImg(self, img, resize = False):
        if isinstance(img, pygame.Surface):
            if img.get_rect() != self and resize:
                img = pygame.transform.scale(img, (self.size))
            self.imgs.append(img)
        elif img is not None:
            raise TypeError("Images must be pygame.Surface object")
    def delImg(self, img):
        self.imgs.remove(img)
    def clearImgs(self):
        self.imgs = []

    def isClicked(self):
        return self.collidepoint(pygame.mouse.get_pos())

    def draw(self, surface):
        if self.colour is not None:
            surface.fill(self.colour, self)
        for img in self.imgs:
            surface.blit(img, self)

class Grid():
    def __init__(self, surface, num, colour = None, tiles = None, 
                 force_square = False):
        self.WIDTH = surface.get_width()
        self.HEIGHT = surface.get_height()
        self.surface = surface

        aspect_ratio = _simplify(self.WIDTH, self.HEIGHT)

        if isinstance(num, int):
            if aspect_ratio == (1, 1) or force_square:
                self.x = math.sqrt(num)
                self.y = math.sqrt(num)

            else:            
                self.x = aspect_ratio[0] * (num / product(aspect_ratio))
                self.y = aspect_ratio[1] * (num / product(aspect_ratio))
        else:
            try:
                self.x = num[0]
                self.y = num[1]
            except TypeError:
                raise TypeError("2nd argument must be int or subscriptable")

        self.tilesize = (self.WIDTH / self.x,
                         self.HEIGHT / self.y)
        self.num = num
        self.colour = colour

        if tiles:
            if hasattr(tiles, "__getitem__") and isinstance(tiles[0], Tile):
                self.tiles = tiles
            else:
                self.tiles = [[Tile.fromData(tile) for tile in column]
                              for column in tiles]
        else:
            self.tiles = self.maketiles(colour)

    def __getitem__(self, index):
        return self.tiles[index]

    def __setitem__(self, index, new):
        self.tiles[index] = new

    def __len__(self):
        return len(self.tiles)

    def index(self, tile):
        for column in self.tiles:
            if tile in column:
                return self.tiles.index(column), column.index(tile)

    def getTiles(self):
        """Get all tiles. Returns a generator"""
        for column in self.tiles:
            for tile in column:
                yield tile

    def tagSearch(self, tag):
        """Search for tiles by tag. Returns a generator"""

        for tile in self.getTiles():
            if tile.hasTag(tag):
                yield tile

    def pointSearch(self, point):
        """Search for tiles by point. Returns a tile"""

        for tile in self.getTiles():
            if tile.collidepoint(point):
                return tile

    def rectSearch(self, rect):
        """Search for tiles by rect. Returns a generator"""

        for tile in self.getTiles():
            if tile.colliderect(rect):
                yield tile

    def getColumn(self, i):
        return self.tiles[i]
    def getRow(self, i):
        return [column[i] for column in self.tiles]

    def checker(self, colour1, colour2 = None):
        for column in self.tiles:
            for tile in column:
                if _isEven(self.tiles.index(column) + column.index(tile)):
                    tile.setColour(colour1)
                else:
                    if colour2:
                        tile.setColour(colour2)

    def getDiagonal(self, tile, direction = 1):
        index = self.index(tile)
        diagonal = []

        currentIndex = [i - index[0] for i in index]

        while currentIndex[1] != self.y:
            diagonal.append(self[currentIndex[0]][currentIndex[1]])

            currentIndex = [i + 1 for i in currentIndex]

        return diagonal

    def getBetweenTiles(self, tile1, tile2):
        """Inefficient and badly implemented"""

        index1 = self.index(tile1)
        index2 = self.index(tile2)

        if index1[0] != index2[0] and index1[1] != index2[1]:
            raise ValueError("Tiles must be in same row or column")

        for column in self.tiles:
            if tile1 in column and tile2 in column:
                return column[column.index(tile1) : column.index(tile2)]

        for i in range(self.y):
            row = self.getRow(i)
            if tile1 in row and tile2 in row:
                    return row[row.index(tile1) : row.index(tile2)]

    def getSurroundingTiles(self, tile,  adjacent = True, diagonal = True):    
        di = (0, 1, 0, -1, 1, 1, -1, -1)
        dj = (1, 0, -1, 0, 1, -1, 1, -1)
        # indices 0 - 3 are for horizontal, 4 - 7 are for vertical

        index = list(self.getTiles()).index(tile)
        max_x = self.x - 1 # Offset for 0 indexing
        max_y = self.y - 1

        i = int(math.floor(index / self.x))
        j = int(index % self.y)

        surroundingTiles = []

        startat = 0 if adjacent else 4
        stopat = 8 if diagonal else 4

        for k in range(startat, stopat):
            ni = i + di[k]
            nj = j + dj[k]
            if ni >= 0 and nj >= 0 and ni <= max_x and nj <= max_y:
                surroundingTiles.append(self[ni][nj])

        surroundingTiles.reverse()

        return sorted(surroundingTiles)

    def draw(self, drawGrid = False, gridColour = (0, 0, 0), gridSize = 1):
        for tile in self.getTiles():
            tile.draw(self.surface)

            if drawGrid:
                pygame.draw.rect(self.surface, gridColour, tile, gridSize)

    def maketiles(self, colour):
        """Make the tiles for the grid"""

        tiles = []

        width = self.WIDTH / self.x
        height = self.HEIGHT / self.y

        for i in _range(0, self.WIDTH, width):
            column = []

            for j in _range(0, self.HEIGHT, height):
                sq = Tile((i, j), (width, height), colour)

                column.append(sq)

            tiles.append(column)

        return tiles

    def toData(self):
        return (self.num, self.colour,
                [[tile.toData() for tile in column] for column in self.tiles])

    def fromData(data, surface):
        return Grid(*([surface] + list(data)))
Bobby Clarke的Pygame网格库 #GNU通用公共许可证3.0 #版本1.1 导入pygame 输入数学 从分数导入gcd 从functools导入reduce 定义(i): 返回i%2==0 def产品(_列表): 返回减少(λx,y:x*y,_列表,1) def_范围(启动、停止、步骤=1): “”“可以处理浮动的范围函数。”“” 启动<停止时: 产量起点 开始+=步进 定义(a,b): hcf=gcd(a,b) 返回(a/hcf、b/hcf) 类平铺(pygame.Rect): def uuu init uuuuu(自身、点、大小、颜色=无,imgs=[],标记=[]): self.size=[int(i)表示i的大小] self.point=点 颜色 对于img中的img: 如果isinstance(img,元组): imgs[imgs.index(img)]=pygame.image.fromstring(img[0], img[1],, img[2]) self.imgs=imgs[:] self.tags=标记[:] pygame.Rect.\uuuuu init\uuuuu(self,self.point,self.size) 定义(自身、其他): 返回(自身点[0]<其他点[0]或 自点[1]<其他点[1]) 定义(自身、其他): 返回(自身点[0]>其他点[0]或 自点[1]>其他点[1]) 定义(自我、其他): 返回(自身点[0]=其他点[1]) def toData(self,imgFormat=“RGBA”): 返回(自点、自大小、自颜色、, [(pygame.image.tostring(img,imgFormat), img.get_size(),imgFormat),用于self.imgs],self.tags)中的img def fromData(数据,baseTile=None): 平铺=平铺(*数据) 如果baseTile和iInstance(baseTile,Tile): baseTile=瓷砖 其他: 返回磁砖 def getRect(self): 回归自我 def GetColor(自身): 回归自我色彩 def设置颜色(自身,颜色): 颜色 def获取点(自身): 返回自我点 def addTag(自我,*标签): 如果isinstance(标记[0],列表): self.tags.extend(标记[0]) 其他: self.tags.extend(标记) def hasTag(自我,标签): 返回(self.tags中的标记) def delTag(自我,标签): self.tags.remove(标记) def clearTags(自我): self.tags=[] def addImg(self、img、resize=False): 如果isinstance(img,pygame.Surface): 如果img.get_rect()!=自我调整和调整大小: img=pygame.transform.scale(img,(self.size)) self.imgs.append(img) elif img不是无: raise TypeError(“图像必须是pygame.Surface对象”) def delImg(自我,img): 自行移除(img) def清除器(自身): self.imgs=[] def已单击(自): 返回self.collidepoint(pygame.mouse.get_pos()) def绘图(自、表面): 如果self.color不是None: 表面填充(自着色、自着色) 对于self.imgs中的img: 表面。blit(img,自我) 类网格(): 定义初始值(自、表面、数值、颜色=无、瓷砖=无、, force_square=False): self.WIDTH=surface.get_WIDTH() self.HEIGHT=surface.get_HEIGHT() self.surface=曲面 纵横比=_简化(self.WIDTH、self.HEIGHT) 如果isinstance(num,int): 如果纵横比==(1,1)或力平方: self.x=math.sqrt(num) self.y=math.sqrt(num) 其他: self.x=纵横比[0]*(数量/产品(纵横比)) self.y=纵横比[1]*(数量/产品(纵横比)) 其他: 尝试: self.x=num[0] self.y=num[1] 除类型错误外: raise TypeError(“第二个参数必须是int或subscriptable”) self.tilesize=(self.WIDTH/self.x, self.HEIGHT/self.y) self.num=num 颜色 如果瓷砖: 如果hasattr(tiles,“\uuu getitem”和isinstance(tiles[0],Tile)): self.tiles=tiles 其他: self.tiles=[[Tile.fromData(Tile)用于列中的Tile] 用于瓷砖中的柱] 其他: self.tiles=self.maketiles(颜色) 定义uu获取项目uu(自身,索引): 返回self.tiles[索引] 定义设置项(自、索引、新): self.tiles[索引]=新建 定义(自我): 返回透镜(自贴瓷砖) def索引(自、平铺): 对于self.tiles中的列: 如果在列中平铺: 返回self.tiles.index(列),column.index(tile) def getTiles(self): “”“获取所有磁贴。返回生成器”“” 对于self.tiles中的列: 对于列中的瓷砖: 屈服砖 def tagSearch(self,tag): “”“按标记搜索分幅。返回生成器”“” 对于self.getTiles()中的平铺: 如果tile.hasTag(标记): 屈服砖 def pointSearch(自身,点): “”“按点搜索磁贴。返回磁贴”“” 对于self.getTiles()中的平铺: 如果平铺点(点): 返回磁砖 def rectSearch(self,rect): “”“按矩形搜索分幅。返回生成器”“” 对于self.getTiles()中的平铺: 如果tile.collide rect(rect): 屈服砖 def getColumn(自我,i): 返回自我.tiles[i]