-1

在将 Python 2.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

我试过的

我知道将 (x) 转换为整数的方法,如 int(x) 或 5 / 3 转换为 5 //3 等。这在过去的所有其他时间都有效,到目前为止我遇到了问题。有类似的问题,但这些答案在这种情况下不起作用TypeError: integer argument expected got float' Python 3.4我尝试了这些方法,因为这也是我的问题。

我正在将 python 3.4 与 pygame 一起使用,这对于本示例可能无关紧要

我正在尝试做的事情:

目标是将教程从 python 2.7 转换为 python 3.4 以使用 python 3.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
4

1 回答 1

1

int([x][y])是一个完全没有意义的陈述。[]是索引运算符,用于获取列表中特定位置的元素。索引稀薄的空气是荒谬的。

要将浮点数转换为缩进索引int,您必须执行类似的操作

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

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

于 2015-08-28T17:36:20.530 回答