Python 3.x 在将Python 2.7转换为3.4之后,将[x][y]浮点转换回整数

Python 3.x 在将Python 2.7转换为3.4之后,将[x][y]浮点转换回整数,python-3.x,pygame,python-2to3,Python 3.x,Pygame,Python 2to3,在将Python2.7转换为3.4之后,如何将[x][y]浮点转换回整数。该格式为浮点 例如,self.table[x][y]转换为整数,以获得x和y值 我得到的错误 return self.table[x][y].physics_module TypeError: list indices must be integers, not float 我尝试的 import pygame from .Tile import * class TileSet: def __

在将Python2.7转换为3.4之后,如何将[x][y]浮点转换回整数。该格式为浮点

例如,self.table[x][y]转换为整数,以获得x和y值

我得到的错误

return self.table[x][y].physics_module
TypeError: list indices must be integers, not float
我尝试的

import pygame
   from .Tile import *


    class TileSet:

    def __init__(self, width, height):
        cols = list(range(width))
        for i in range(width):
            cols[i] = list(range(height))
            for j in range(height):
                cols[i][j] = Tile(i, j, False)
        self.table = cols
        self.platforms = []
        self.background_color = (0, 0, 0)
        self.pixel_origin = (0, 0)
        self.background = None

    def setBackground(self, imageFile):
        self.background = imageFile

    def makeGround(self, xa, ya, xb, yb):
        if (xa == xb):
            for y in range(ya, yb + 1):
                self.table[xa][y].makeGround()
        elif (ya == yb):
            for x in range(xa, xb + 1):
                self.table[x][ya].makeGround()

    def applyTileData(self, x, y, imageSequenceStack, passable, physics):
        self.table[x][y].images = imageSequenceStack
        self.table[x][y].passable = passable
        self.table[x][y].physics_module = physics

    def addPlatform(self, x, y, width):
        self.platforms.append((x, y, width))

    def drawBackground(self, screen, topleft, frameNum):
        screen.fill(self.background_color)
        bg = self.background.getImageForFrame(frameNum)
        width = len(self.table) * 32
        height = len(self.table[0]) * 32
        pic_width = bg.get_width() - 640
        pic_height = bg.get_height() - 480
        left = 0
        top = 0
        if pic_width > 0 and width > 0:
            left = -1 * pic_width * topleft[0] / width
        if pic_height > 0 and height > 0:
            top = -1 * pic_height * topleft[1] / height

        screen.blit(bg, (left, top))


    def drawBottom(self, screen, camera, frameNum):
        top_left = self.fromPixelToTile(camera[0], camera[1])
        bottom_right = self.fromPixelToTile(camera[0] + 640, camera[1] + 480)
        left = min(len(self.table) - 1, max(0, top_left[0]))
        top = min(len(self.table[0]) - 1, max(0, top_left[1]))
        right = min(len(self.table) - 1, max(0, bottom_right[0]))
        bottom = min(len(self.table[0]) - 1, max(0, bottom_right[1]))

        for x in range(left, right + 1):
            for y in range(top, bottom + 1):
                topleft = self.fromTileToPixel(x, y)
                for imgSeq in self.table[x][y].images:
                    screen.blit(imgSeq.getImageForFrame(frameNum), (x * 32 - camera[0], y * 32 - camera[1]))


    def fromTileToPixel(self, tile_x, tile_y):
        x = 32 * (tile_x - self.pixel_origin[0])
        y = 32 * (tile_y - self.pixel_origin[1])
        return (x, y)

    def fromPixelToTile(self, pixel_x, pixel_y):
        x = (pixel_x / 32) + self.pixel_origin[0]
        y = (pixel_y / 32) + self.pixel_origin[1]
        return (x, y)

    def nearbyTiles(self, coordinate, radius = 1, tileFilter = None):
        tiles = []
        left = max(0, coordinate[0] - radius)
        right = min(coordinate[0] + radius, len(self.table) - 1)
        top = max(0, coordinate[1] - radius)
        bottom = min(coordinate[1] + radius, len(self.table[0]) - 1)
        for x in range(left, right + 1):
            for y in range(top, bottom + 1):
                tiles.append(self.table[x][y])

        if tileFilter:
            return list(filter(tileFilter, tiles))
        return tiles

    def getPhysicsModule(self, xy):
        x = xy[0]
        y = xy[1]
        x = max(0, x)
        x = min(x, len(self.table) - 1)
        y = max(0, y)
        y = min(y, len(self.table[0]) - 1)
        return self.table[x][y].physics_module

    def isTilePassable(self, xy):
        x = xy[0]
        y = xy[1]
        x = max(0, x)
        x = min(x, len(self.table) - 1)
        y = max(0, y)
        y = min(y, len(self.table[0]) - 1)
        return self.table[x][y].passable
我知道将(x)转换成整数的方法,比如int(x)或将5/3转换成5//3等。这在过去的所有其他时间都起到了作用。到目前为止,我遇到了这个问题。也有类似的问题,但这些答案在这种情况下不起作用,我尝试了这些方法,因为这也是我的问题

我将Python3.4与pygame一起使用,这对于本例可能并不重要

我想做什么:

目标是将教程从Python2.7转换为Python3.4,以便使用Python3.4理解它。我运行了2to3.py,看看这是否也有效

问题所在的代码部分

  def makeGround(self, xa, ya, xb, yb):
        if (xa == xb):
            for y in range(ya, yb + 1):
                self.table[xa][y].makeGround()
        elif (ya == yb):
            for x in range(xa, xb + 1):
                self.table[x][ya].makeGround()

    def applyTileData(self, x, y, imageSequenceStack, passable, physics):
        self.table[x][y].images = imageSequenceStack
        self.table[x][y].passable = passable
        self.table[x][y].physics_module = physics

    def addPlatform(self, x, y, width):
        self.platforms.append((x, y, width))
更大的部分可容纳所需的部分

import pygame
   from .Tile import *


    class TileSet:

    def __init__(self, width, height):
        cols = list(range(width))
        for i in range(width):
            cols[i] = list(range(height))
            for j in range(height):
                cols[i][j] = Tile(i, j, False)
        self.table = cols
        self.platforms = []
        self.background_color = (0, 0, 0)
        self.pixel_origin = (0, 0)
        self.background = None

    def setBackground(self, imageFile):
        self.background = imageFile

    def makeGround(self, xa, ya, xb, yb):
        if (xa == xb):
            for y in range(ya, yb + 1):
                self.table[xa][y].makeGround()
        elif (ya == yb):
            for x in range(xa, xb + 1):
                self.table[x][ya].makeGround()

    def applyTileData(self, x, y, imageSequenceStack, passable, physics):
        self.table[x][y].images = imageSequenceStack
        self.table[x][y].passable = passable
        self.table[x][y].physics_module = physics

    def addPlatform(self, x, y, width):
        self.platforms.append((x, y, width))

    def drawBackground(self, screen, topleft, frameNum):
        screen.fill(self.background_color)
        bg = self.background.getImageForFrame(frameNum)
        width = len(self.table) * 32
        height = len(self.table[0]) * 32
        pic_width = bg.get_width() - 640
        pic_height = bg.get_height() - 480
        left = 0
        top = 0
        if pic_width > 0 and width > 0:
            left = -1 * pic_width * topleft[0] / width
        if pic_height > 0 and height > 0:
            top = -1 * pic_height * topleft[1] / height

        screen.blit(bg, (left, top))


    def drawBottom(self, screen, camera, frameNum):
        top_left = self.fromPixelToTile(camera[0], camera[1])
        bottom_right = self.fromPixelToTile(camera[0] + 640, camera[1] + 480)
        left = min(len(self.table) - 1, max(0, top_left[0]))
        top = min(len(self.table[0]) - 1, max(0, top_left[1]))
        right = min(len(self.table) - 1, max(0, bottom_right[0]))
        bottom = min(len(self.table[0]) - 1, max(0, bottom_right[1]))

        for x in range(left, right + 1):
            for y in range(top, bottom + 1):
                topleft = self.fromTileToPixel(x, y)
                for imgSeq in self.table[x][y].images:
                    screen.blit(imgSeq.getImageForFrame(frameNum), (x * 32 - camera[0], y * 32 - camera[1]))


    def fromTileToPixel(self, tile_x, tile_y):
        x = 32 * (tile_x - self.pixel_origin[0])
        y = 32 * (tile_y - self.pixel_origin[1])
        return (x, y)

    def fromPixelToTile(self, pixel_x, pixel_y):
        x = (pixel_x / 32) + self.pixel_origin[0]
        y = (pixel_y / 32) + self.pixel_origin[1]
        return (x, y)

    def nearbyTiles(self, coordinate, radius = 1, tileFilter = None):
        tiles = []
        left = max(0, coordinate[0] - radius)
        right = min(coordinate[0] + radius, len(self.table) - 1)
        top = max(0, coordinate[1] - radius)
        bottom = min(coordinate[1] + radius, len(self.table[0]) - 1)
        for x in range(left, right + 1):
            for y in range(top, bottom + 1):
                tiles.append(self.table[x][y])

        if tileFilter:
            return list(filter(tileFilter, tiles))
        return tiles

    def getPhysicsModule(self, xy):
        x = xy[0]
        y = xy[1]
        x = max(0, x)
        x = min(x, len(self.table) - 1)
        y = max(0, y)
        y = min(y, len(self.table[0]) - 1)
        return self.table[x][y].physics_module

    def isTilePassable(self, xy):
        x = xy[0]
        y = xy[1]
        x = max(0, x)
        x = min(x, len(self.table) - 1)
        y = max(0, y)
        y = min(y, len(self.table[0]) - 1)
        return self.table[x][y].passable
int([x][y])
是一个完全没有意义的语句
[]
是索引运算符,用于获取列表中特定位置的元素。稀薄的空气是没有意义的

要使用
int
将浮点数转换为缩进索引,必须执行以下操作

self.table[int(x)][int(y)].images

或者你想要的任何代码。您可以将每个浮点数分别转换为整数。请注意,这是截断而不是舍入:2.99将变为2。

很难知道您在问什么,因为您使用了一些令人困惑的未定义术语(“表”、“表浮点”),但听起来您只是想将浮点转换为整数。你认为这会怎样?您想要舍入还是截断?为什么
int(x)
对于某些浮点
x
来说不够好?我正在将一个用python 2.7编写的教程转换为3.4。我还没有完全理解代码。但我知道问题在于Python3.4中的[x]&[y]被转换成浮点。我尝试了self.table(int([x][y])images等,但这不起作用或看起来不正确。谢谢,我知道它看起来是错的。但这很容易记住。在学习python的几周后。我修复了这些内容,并在包含的其他4个文档中发现了一些不相关的错误,但我会解决这些问题。